Translate

PERFORMANCE MONITORING AND SYSTEM MEMORY USAGE WHILE RUNNING SELENIUM TEST SCRIPTS

Here I am trying to monitor system usage and the performance with different browsers which we use in selenium (Firefox,Chrome,Headless Browser). Just wanted to know how it works. Generally we never use this method. Whenever you execute your test cases, there would be load on your system.

Performance factors :
Important influence factors to the performance of any Java program can be separated into two main parts:

1.Memory Consumption of your program

2.Total runtime of your program


Memory in Java :
Java manages the memory for use. New objects created and placed in the heap. Once your application have no reference anymore to an object the Java garbage collector is allowed to delete this object and remove the memory so that your application can use this memory again.

Java Heap :
In the heap the Java Virtual Machine (JVM) stores all objects created by the Java application, e.g. by using the “new” operator. The Java garbage collector (gc) can logically separate the heap into different areas, so that the gc can faster identify objects which can get removed.

The memory for new objects is allocated on the heap at run time. Instance variables live inside the object in which they are declared.

Java Stack :
Stack is where the method invocations and the local variables are stored. If a method is called then its stack frame is put onto the top of the call stack. The stack frame holds the state of the method including which line of code is executing and the values of all local variables. The method at the top of the stack is always the current running method for that stack. Threads have their own call stack.
Here you see an example of selenium test script and you can also monitor the system usage.

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.annotations.Test;

public class PerfomanceMontior_Broswers {
@Test(priority = 1)
public static void performanceMonitor_Firefox() {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get(“https://seleniumbycharan.wordpress.com”);
WebElement caption = driver.findElement(By
.xpath(“.//*[@id=’masthead’]/a/h2”));
System.out
.println(“=====================Firefox Browser=====================”);
System.out.println(“Caption of the blog : ”
+ caption.getText().toUpperCase());
getSystemUsage();
driver.close();
System.out
.println(“=====================*******************=====================”);
}

/*
* @Test(priority=2) public void performanceMonitor_HTMLUnitDriver() {
* HtmlUnitDriver fd= new HtmlUnitDriver(); fd.manage().window().maximize();
* fd.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
* fd.get(“https://seleniumbycharan.wordpress.com”); WebElement caption =
* fd.findElement(By .xpath(“.//*[@id=’masthead’]/a/h2”)); System.out
* .println(“=====================HeadLess Browser=====================”);
* System.out.println(“Caption of the blog : ” +
* caption.getText().toUpperCase()); printUsage(); fd.close(); System.out
* .println
* (“=====================*******************=====================”); }
*
* @Test(priority=3) public void performanceMonitor_ChromeBrowser() {
* System.
* setProperty(“webdriver.chrome.driver”,”F:\\Dollar\\lib\\chromedriver.exe”
* ); WebDriver driver = new ChromeDriver();
* driver.manage().window().maximize();
* driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
* driver.get(“https://seleniumbycharan.wordpress.com”); WebElement caption
* = driver.findElement(By .xpath(“.//*[@id=’masthead’]/a/h2”)); System.out
* .println(“=====================Chrome Browser=====================”);
* System.out.println(“Caption of the blog : ” +
* caption.getText().toUpperCase());
*
* printUsage(); driver.close(); System.out
* .println(“=====================*******************=====================”
* ); }
*/
private static void getSystemUsage() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory
.getOperatingSystemMXBean();
for (Method method : operatingSystemMXBean.getClass()
.getDeclaredMethods()) {

method.setAccessible(true);
if (method.getName().startsWith(“get”)
&& Modifier.isPublic(method.getModifiers())) {
Object value;
try {
value = method.invoke(operatingSystemMXBean);
} catch (Exception e) {
value = e;
}
System.out.println(method.getName() + ” = ” + value);

}
}
}

}

To Monitor System Usage while execution of your test scripts,Run the scripts one after one by using comments.

No comments:

Post a Comment