Measuring Performance with BrowserMob proxy

13 April, 2014
BrowserMob proxy is a free tool that supports monitoring and manipulating the network traffic from web applications. It can capture the performance data from a web application in an HTML Archive (HAR) format, as well as manipulate the browser behavior and traffic, such as whitelisting and blacklisting content, simulating network traffic and latency, and rewriting HTTP requests and responses.

BrowserMob proxy helps in automating client-side performance data collection for a web application using the Selenium WebDriver.In this example, we will set up a BrowserMob proxy for the Selenium WebDriver test to collect the performance data in an HAR format.

Download BrowserMob proxy
Download the BrowserMob proxy from here.

Reference the BrowserMob proxy JARs from the lib folder in the BrowserMob proxy folder to your existing Selenium WebDriver test project.

Below is a example test that uses BrowserMob for performance monitoring:


import java.io.File;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.browsermob.proxy.ProxyServer;
import org.browsermob.core.har.Har;

public class PerformanceTest {
	public static void main(String[] args) throws Exception {
		
		// Start the BrowserMob Proxy
		ProxyServer server = new ProxyServer(9090);
		server.start();
		
		// Get the Selenium proxy object
		Proxy proxy = server.seleniumProxy();
		
		// Configure Desired capability for using Proxy Server
		DesiredCapabilities capabilities = new DesiredCapabilities();
		capabilities.setCapability(CapabilityType.PROXY, proxy);

		// Start the Browser up
		WebDriver driver = new FirefoxDriver(capabilities);

		// Create a new HAR with the label "StockMarketData"
		server.newHar("StockMarketData");

		// Open the Application
		driver.get("http://money.rediff.com/");

		WebElement searchTextBox = driver.findElement(By.name("srchword"));
		searchTextBox.sendKeys("Page Industries Ltd.");
		searchTextBox.sendKeys(Keys.ENTER);
		
		Thread.sleep(5000);
		
		// Collect the performance data from the BrowserMob proxy server
		// Get the HAR data 
		Har har = server.getHar();

		// Write the HAR Data in a File
		File harFile = new File("C:\\tmp\\rediffmoney.har");
		har.writeTo(harFile);
		
		// Stop the BrowserMob Proxy Server
		server.stop();
		
		// Close the browser
		driver.quit();
				
	}
}	

The BrowserMob proxy server works like a normal proxy server between the application and the browser. However, it can monitor the traffic and collect the performance data that can be used for analysis. We can also manipulate the BrowserMob proxy to create a low-latency network to observe the performance of the application.

The BrowserMob proxy collects the performance data in the HAR format and we can access this data by using the getHar() method. We can save this data in an HAR file, using the writeTo() method of the Har class available in the BrowserMob proxy API. We can open the HAR file for analysis using a HAR viewer. The following screenshot is a HAR representation using an online HAR viewer available at http://www.softwareishard.com/har/viewer/.







No comments:

Post a Comment