Using the HAR converter
The har-to-k6 converter is a NodeJS tool that generates a k6 script based on the HTTP requests included in a HAR file. It is an alternative to the Browser recorder.
Note
HAR is a file format used by all major browsers and various other tools to export recorded HTTP requests.
Before you start
Before you start, consider the following:
You’ll need to choose a tool to record your HAR file. Multiple browsers and tools can export HTTP traffic in a HAR format. A few popular ones are:
- Chrome
- Firefox
- Microsoft Edge
- Charles recording proxy(HTTP proxy/recorder)
- Fiddler (HTTP proxy/recorder)
1. Record a HAR file
Here are the basic steps you need to record in Chrome:
- Open a new incognito window in Chrome. (This is optional, but it means you won’t send things like cookies, which your browser might have saved).
- Open up Chrome developer tools (press F12).
- Select the Network tab.
- Check that the recording button (round button) is activated (red color).
- If you want to make a recording of several successive page loads, select the Preserve log checkbox.
- Enter the URL of your site and start doing whatever you want your simulated load-test users to do.
- When done, in Chrome developer tools, right-click the URLs and choose Save as HAR with content.
2. Convert with har-to-k6
The har-to-k6 converter is a NodeJS tool that can convert a HAR file (browser session) into a k6 script.
Make sure that you have installed NodeJS (version >=11.0.0).
Install the converter. You can use
npm
:$ npm install -g har-to-k6
For other installation options, check out the har-to-k6 installation instructions.
Generate a k6 script from a HAR file with the convert command:
$ har-to-k6 myfile.har -o loadtest.js
This command auto-generates a k6 script for you. It reads the HAR file (myfile.har) and converts it into a k6 test (loadtest.js).
3. Modify the auto-generated k6 script
In the previous step, the converter created a k6 script for testing. Now, you should evaluate whether you have to change any part of the k6 script.
Depending on your use case, you might need to:
- Configure the load options
- Remove third-party content
- Correlate dynamic data
Configure the load options
Now, k6 has auto-generated a “functional” test. By default, this test runs with one virtual user and for one iteration.
It’s time for you to configure the load options of your performance tests. k6 lets you configure this in several ways:
As CLI arguments while running the test:
k6 run --vus 10 --duration 30s loadtest.js
As options in the script file.
export const options = { vus: 10, duration: '30s', };
To learn more about how to configure the load options, read the Adding more VUs guide and the Options guide.
Remove third-party content
If you are recording a user session of a website, by default, you’ll record all the HTTP requests that your website uses. This includes requests from the third-party tools that your site uses, e.g. analytics tools, Facebook, Twitter, Support Widgets, CDNs, etc.
You should remove these third party requests:
- They will skew the percentiles of your performance results.
- You may be unable to affect the performance of the third-party service.
- The load test may violate the terms-of-service contract that you have with the provider.
Your k6 script can skip third-party requests in a few ways:
- Edit the auto-generated k6 script, removing the requests one-by-one
- Download a HAR file with only requests to the selected domains.
In Chrome, you can use the DevTools Network Filter to select only particular domains. The Filter input accepts a Regex to match multiple domains.
/loadimpact.com|cloudfront.net/
After filtering your selected domains, you can download the HAR file as described in the first step of this tutorial. The HAR file will include only the requests to the selected domains.
If you don’t know all the domains to filter, it helps to use the query language of the Network Filter.
Just input domain:
in the filter to see all the different domains recorded by the Network Panel.
Correlate dynamic data
In load testing, correlation is when you extract the value from the response of one request and reuse it in a subsequent request. Often, the correlation would be for a token or ID that is needed to run a sequence of steps in a user journey.
The recorded HAR file may include dynamic data used on your site - IDs
, CSRF tokens
, VIEWSTATE
, wpnonce
, and other dynamic values
- that will be converted into the k6 script.
To run your load test correctly, you may need to replace some recorded data with dynamic data that k6 gets from previous requests. For example, tokens expire quickly, and they are one of the most common things that users will correlate from a recorded session.
Here are a few examples using the k6 API to correlate dynamic data.
4. Run the test
Now, you can run your load test with k6. If you have not installed k6 yet, please, follow the k6 installation instructions.
Execute the k6 run
command to run your k6 script:
$ k6 run loadtest.js
To learn about running k6, check out the Running k6 tutorial.