Web

Simulate Latency-Browsermob Proxy: Convert HAR to Locust Scenarios

Image title

I hope you had enjoyed reading the first article of this series Performance Capture I – Export HAR Using Selenium and BrowserMob-Proxy? Let’s see how to simulate the latencies, get DOMContenLoaded time,  convert the HAR file to locust tasks to do performance testing.

Simulate Network Latencies

Browsermob proxy provides an option to limit the bandwidth through the proxy. This helps in identifies the bottlenecks in performance with slow networks. The limit function that takes a dictionary as it allows the users to play with the latencies. To reduce the bandwidth, just add the below lines to the server after starting the server.

from browsermobproxy import Server server = Server("path_to_your_browsermob-proxy", options={'port': 8090})
server.start()
proxy = self.server.create_proxy()
proxy.limits({'upstream_kbps': 50 ,'downstream_kbps': 50})

Add the upload latency to upstream_kbps and download latency to downstream_kbps.

You may also like: How To Use HAR File To Find The Hidden Performance Bottlenecks In Your App.

Get DOMContentLoaded Time

It is not possible to get DOMContentLoaded time (highlighted in red in the below image) as a part of HAR using Browsermob proxy. But in many places, we do consider this time as the page ready time for the users to read the content. Isn’t it?
Viewing DOMContentLoaded time

Viewing DOMContentLoaded time

Ok. Let’s see how to get that without BrowserMob proxy and using selenium. Surprised? Are you surprised to know there is a way in selenium to get this? Yes, there is a way in selenium to do this. Though there is no direct API in Selenium to get that, we can get it using JavaScript calls.

We can achieve this with the below steps:

navigationStart = driver.execute_script("return window.performance.timing.navigationStart")
dom_completion = driver.execute_script("return window.performance.timing.domContentLoadedEventEnd")
DOMContentLoaded_time = dom_completion - navigationStart

Navigation start returns the time when the user agent starts fetching the resource. This is the time immediately before the user agent starts checking any relavant application caches.

The difference between these two will give the domContentLoaded value. (Refer below the image with the time shown on chrome window and the time we got in seconds — highlighted in red.)

Image title

Convert HAR to Locust File

Until now, we have covered 50% by getting the HAR file in an automated way. Now, let’s see if we can parse this HAR to locust fil, so that it can be used to run performance tests. Let’s first see how to generate a locust file. 

Have you heard about har-transformer? No? Not a problem. It is a Python library that converts HAR file to locaust load testing scenarios. 

Oh! wait. I heard your question. You want to know can this be converted in an automated way? Yes, it is possbile to do that programatically. 

HAR-Transformer provides both CLI version as well as a library to achieve it.

Want to know how?

You can do this just by copying the following lines of code.

import transformer with open("locustfile.py", "w") as f: transformer.dump(f, ["my_har_files_directory/"])

Thats it now you have the script ready to run the load tests with locust. More about the library is here.

Happy Reading!!!

Further Reading

Comment here

This site uses Akismet to reduce spam. Learn how your comment data is processed.