My objective is to create HAR files using jmeter and Upload created HAR files to HAR Storage during Jmeter Load Test.
JMeter response time is latency plus server processing time. JMeter won't include client side performance means JMeter response time doesn't include java script execution time , rendering time and ..etc. But client side performance place major role in overall response time.
From above diagram, In high level JMeter Response time: Latency + Firewall Rule processing time + Server processing time + DB query processing time
But this response time doesn't include Client response time ( java script execution time , rendering time and ..etc).
To overcome JMeter limitation ,We are coming up with below strategy to solve this limitation.
Strategy : We are pushing load to "AUT" with 200 users (Assume) that time we are capturing total response time (Front end + latency + processing time) for every 2 seconds using JMeter Selenium script for one user. Below is the diagram.
To impliment this stratogy follow the below steps
Browser mob proxy sniffs the traffic between "JMeter (Selenium
driver) and the server and creates "HAR" file.We are uploading this
"HAR" file to "har storage" using JMeter automatically. This is single statement but
internally we are doing following steps.
Download this Project from GitHub
Pre requiremnets:
3. HAR storage , please find har storage configuration steps here
Step 1: Add "Thread Group" to "Test Plan",Add the "jp@gc - Firefox Driver Config" as a child to "Thread Group" and
Add "Simple Controller" as child to "Thread Group" and name the "Simpler Controller" to Home Page.
jp@gc - Firefox Driver Config: Will work as "Forefox profiler".
If you want to use Chrome Driver opt Chrome Driver and add chrome drivers to it.If you want to use Android Driver opt Android Driver and add Android drivers to it.
Step 2: Browser mob proxy is creating HAR file, this HAR file contains "pageref" parameter,which describes page reference name by default "pageref" value is "page1" in "HAR" file. If my application contain three different requests (Home Page,Search and Login). By default for all these three pages the reference name is "page1". So it is very hard to distinguish which har file belongs to which request.
To eliminate this confusion we need to change the "pageref" name for each request. To do this, we need to use browser mob " initialPageRef " put method. Please see more detail in browser mob documentation
Browser mob "initialPageRef " request format is:
Host Name:localhost
Port:9090 (default)
Path: /proxy/[port]/har/?initialPageRef=<pageName>
Example Path: /proxy/9099/har/?initialPageRef=HomePage (proxy port default port (9099)
Method:PUT
Note: The host name for this PUT method is "localhost" because Browser Mob server is running in this machine only (this is mandatory to run JMeter and Browser Mob proxy in a same machine , otherwise browser mob won't sniff the network traffic between JMeter and server).
Add HTTP request sampler to "Home Page" simple controller and fill the above Browser mob "initialPageRef " request details and rename the sampler name to "initialPageRef -har"
Step 3: Open required page to sniff the network elements between JMeter and Server by adding "jp@gc - Web Driver Sampler" from Sampler as a child to Home Page Simple controller.
For demo purpose , I am using google.com. So Browser Mob sniffs the network elements between JMeter and Google.com and the attached code to " jp@gc - Web Driver Sampler"
Code Snippet is:
var pkg = JavaImporter(org.openqa.selenium, org.openqa.selenium.support.ui, org.openqa.selenium.remote,java.util.Arrays, org.openqa.selenium.chrome)
var hostname = WDS.args[0]
var url = 'http://www.google.com'
//Start sample time
WDS.sampleResult.sampleStart()
//Get URL. If unsucessful time-out after 10 s
//WDS.log.info (WDS.name + ' navigating to ' + url)
WDS.browser.get(url)
WDS.browser.manage().timeouts().pageLoadTimeout(10, java.util.concurrent.TimeUnit.SECONDS)
//End sample time
WDS.sampleResult.sampleEnd()
Step 4: Add "View results Tree and "Aggregate Report" from "Listener" as a child to "Thread Group".Save (CTRL+s) and Run (CTRL+r) the Jmeter and Verify the Results.
Step 5: Get the har file from browser mob by calling GET /proxy/9099/har. Please see more detail in browser mob documentation
Host Name:localhost
Port:9090 (default)
Path: /proxy/[port]/har/
Example Path: /proxy/9099/har (proxy port default port (9099)
Method:GET
Add "HTTP Request" sampler to Home Page simple controller as a child and add the browser mob Get Har file details (shown above) to this sampler and rename to "Get-Har"
Write the har file response to Local file system by using "Bean Shell Post Processor" and add this post processor as a child to "Get-Har" sampler
Please find the code sniff below
import org.apache.jmeter.services.FileServer;
import java.io.File;
if (data !=null && prev.getResponseCode().equalsIgnoreCase("200"))
{
String response = new String(data);
log.info (response);
abc= File.separator;
workingDir = FileServer.getFileServer().getBaseDir();
har_directory = workingDir + File.separator + "har-files";
finalfile = har_directory + File.separator + "HomePage.har";
try {
log.info("Final filepath " + finalfile);
dir = new File(har_directory).mkdirs();
File file = new File(finalfile);
FileWriter writer = new FileWriter(file);
writer.write(response);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
} //end of if statement
else {
prev.setSuccessful(false);
log.error("An error occured. Either the sampler didn't received data or the response code wasn't 200");
}
Step 6: In step 3 , JMeter opens google page in Firefox browser, but in Step 5, we are collecting HAR details using Browser mob proxy on 9099. So to couple Step 3 and Step 5 we need to specify the Browser mob proxy details in "jp@gc - Firefox Driver Config".
Below is the Configuration in "jp@gc - Firefox Driver Config" to couple Step 3 and Step 5
Step 7: Upload created har file (in step 6) to Har storage. Below are the har storage details
IP:xx.xx.xx.xx
Port: 5000
Path:/results/upload
Method: Post
Send files with the request:
File Path:c:\report\ HomePage.har
Parameter Name: file
MIME Type: application/json
Add "HTTP Request" sampler to Home Page simple controller as a child and
add the har storage to sampler and rename to "Upload-Har"
Step 8: Start Browser Mob proxy and run Step 9
Linux: Path to BrowserMob\bin# sh browsermob-proxy -port 9090
Windows : Path to browsermob\bin>browsermob-proxy -port 9090
Step 9: Save the "Test plan" and Run the JMeter CTRL+R. See the View results tree for verification