Creating a Locust file with Har2Locust
As promised, if you have followed the locust.io post: load testing made easy, I mentioned that you could perform all kinds of simulations. I’m going to explain how you can generate a file for Locust with har2locust.
Step by Step:
Install har2locust
After following the previous post on Locust, you just need to do the following in your Python environment previously created for Locust:
pip install har2locust
Simple, right?
Obtain the file .har
In our browser, press F12 to open the dev tools, go to the Network tab, and make sure that the network logging is enabled.
Once recording is activated, we browse the website we want to test and, after finishing browsing, we stop recording and export it to a file with the .har extension by right-clicking on the network panel and selecting Save All as HAR with Content.
Now we have to convert this .har file to a python file that can run Locust. In our case, we downloaded the file to /tmp, so in our Locust directory we run:
har2locust /tmp/test.har > testweb.py
¡Time to try it!
As we saw in the previous post, we now run locust indicating the file to use with locust -f testweb.py
Depending on the website, you may get some kind of error, for example if it uses zstd. In that case, we would need to remove the file generated by zstd. That is, we search the file for all the “accept-encoding” entries and delete zstd, for example:
“accept-encoding”: “gzip, deflate”,
You can use a script or even sed.
sed -i ‘s/”accept-encoding”: “.*zstd.*”/”accept-encoding”: “gzip, deflate”/g’ testweb.py
And that’s it. Have a nice weekend.
TL.
Thanks for reading our posts.
FAQs
har2locust is a tool that allows you to convert HAR (HTTP Archive) files into Locust scripts. HAR files contain detailed information about the HTTP requests made during a browsing session. With har2locust, you can transform this information into a Locust script that simulates user behavior in a performance testing environment.
Yes, once har2locust generates the script, you can modify it to suit your needs. You can add tasks, modify request parameters, or customize user behavior to more closely resemble a real-world flow or adapt it to specific situations in your load tests.
Yes, har2locust can handle large and complex HAR files, but it’s important to note that the generated script will be larger and potentially more difficult to maintain if the HAR file contains many requests or resources. In these cases, it may be helpful to filter requests or reduce the HAR file size before converting it. It’s also a good idea to review and optimize the generated script to ensure proper performance during testing.
If your HAR file contains requests to external resources (such as images, scripts, or fonts), har2locust will include them in the generated script. However, if you want to simulate user load without relying on these external resources, you can modify the script to omit or simulate those requests. You can do this by manually editing the generated script and removing or modifying the tasks associated with those resources.