autorenew

K6 Performance Testing Tutorial - Advanced Usage: How to Quickly Write K6 Performance Test Scripts

This blog post provides an in-depth introduction to K6 performance testing, covering multiple shortcuts beyond traditional JavaScript script writing. First, through K6's Test Builder tool, readers can easily and quickly generate performance test scripts, simplifying the script writing process. Second, the post introduces how to use the K6 Recorder to generate scripts by recording operations, eliminating manual writing steps. Finally, readers can learn how to use browser developer tools to obtain HAR files and convert them to K6 scripts using the har-to-k6 tool. Through this article, readers will gain a more comprehensive understanding of K6 performance testing tool's flexibility and diverse script writing methods.

Type: Performance Testing Framework: K6 Stage: Advanced Difficulty: Advanced Duration: 50 min Language: JavaScript Read: 8 min read

How to Quickly Write K6 Performance Test Scripts

Besides writing K6 performance test scripts using JavaScript, K6 provides multiple convenient ways to write performance test scripts:

    1. Use K6’s Test Builder tool to write scripts
    1. Use K6 Recorder to record scripts
    1. Use browser developer tools to obtain HAR files and then use the har-to-k6 tool to convert HAR files to K6 scripts

These three methods will be introduced separately below.

Using K6’s Test Builder Tool to Write Scripts

k6’s Test Builder provides a graphical interface that generates k6 test scripts based on your input. You can then copy the test script and run the test from the CLI.

Test Builder is currently an experimental feature and may change in future versions. You can check the official documentation: https://k6.io/docs/using-k6/test-builder/ for more information

Installing Test Builder

Test Builder doesn’t require installation; it’s a feature provided by Grafana Cloud k6 that can be used in a browser.

You need to register for a Grafana Cloud k6 account and then log in to Grafana Cloud.

How to access the Test Builder interface:

  • Log in to the Grafana Cloud homepage

Grafana Cloud home page before opening k6 performance projects

  • Click on Testing & synthetics--->Performance--->Projects in the left menu
  • Then select the default project or create a new project to enter the project details page Grafana Cloud page for selecting or creating a k6 project
  • Click the Start testing button on the page, then select Test builder at the bottom of the page to enter the Test Builder interface Grafana Cloud k6 Test Builder opened from the project page

Reminder: Since Test Builder is used by logging into Grafana Cloud, Grafana Cloud may store some sensitive data, so it’s recommended not to use Test Builder in production environments.

How to Use Test Builder

    1. In the Test Builder interface, click the Options button under Scenario_1 to enter the configuration page and configure basic information for the test scenario, as shown below: k6 Test Builder options for configuring Scenario_1

You can see that the scenario configuration page provides multiple configuration options, allowing you to configure scenario names, executor types, and different VU configurations according to your needs.

    1. In the Test Builder interface, click the Requests button under Scenario_1 to enter the Requests management page, as shown below: k6 Test Builder Requests page for Scenario_1
    1. Click the Request button at the bottom of the page to enter the add request page, as shown below: k6 Test Builder Add Request page for a new request
    1. On the add request page, enter the request URL, then add request headers, request body, or checkpoints according to actual needs, then click the Create button on the page to complete the performance scenario configuration, as shown below: k6 Test Builder request form for URL, headers, body, and checks

Getting Scripts Generated by Test Builder

In the Test Builder interface, click the Script button on the page, and the page will display the script generated by Test Builder, as shown below:

Generated k6 test script displayed in Test Builder

You can see that the script generated by Test Builder is a complete K6 test script that can be directly copied locally and then run using k6.

Running Scripts Generated by Test Builder

In the Test Builder interface, click the Run Test button on the page, and the page will display the running results of the script generated by Test Builder, as shown below:

Run Test results for a k6 script generated by Test Builder

You can see very detailed test result information from the script generated by Test Builder.

Other Test Builder Features

  • Also supports importing HAR files to generate test scripts
  • Also supports configuration of multiple scenarios and multiple requests in one scenario

For more information about Test Builder, please refer to the official documentation: https://grafana.com/docs/grafana-cloud/k6/author-run/test-builder/

Using K6 Recorder to Record Scripts

K6 Recorder is a browser extension provided by K6 that can record user interactions with web applications in the browser and convert them into k6 test scripts.

Installing K6 Recorder

K6 Recorder is a browser extension that can be used in Chrome or Firefox. You can install it from the Chrome Web Store or Firefox Add-ons page.

After installation, you can use K6 Recorder in your browser.

How to Use K6 Recorder

    1. Click to open the k6 Recorder extension in your browser.
    1. Select where to save the automatically generated script.
    • To save it on your local computer, select “I don’t want to save tests in the cloud” (I chose this option in the following example).
    • To save it to any Grafana Cloud k6 project, select “Login”. Grafana Cloud k6 Recorder screen for choosing a script save location
    1. After selecting the script save location, enter the test website address in the current browser tab, then click the start recording button to begin recording the current browser tab. k6 Recorder screen for starting a browser recording

Google homepage with 123 entered in the search box during a k6 recording

In the image, I opened Google’s homepage and clicked the search box, entered 123, then clicked the search button.

    1. Click the stop recording button in k6 Recorder to stop recording.
    1. Name and save the recorded file locally (I named it record-demo.har here).
    1. Use the har-to-k6 tool to convert the HAR file to a K6 script. Terminal example converting a HAR file to a k6 script with har-to-k6

har-to-k6 is a command-line tool that can convert HAR files to k6 scripts. You need to first install the har-to-k6 tool via npm install -g har-to-k6, then convert the HAR file to a K6 script via the har-to-k6 record-demo.har -O record-demo.js command.

    1. Partial screenshot of the converted K6 script is shown below: Excerpt of a k6 script converted from a HAR recording
    1. You can modify the converted K6 script according to your needs, then run the script using k6.
    1. Use k6 to run the converted K6 script and view the running results. k6 execution results for a script converted from a HAR recording

For more information about K6 Recorder, please refer to the official documentation: https://grafana.com/docs/k6/latest/using-k6/test-authoring/create-tests-from-recordings/using-the-browser-recorder/

Using Browser Developer Tools and har-to-k6 Tool to Generate K6 Scripts

Besides using K6 Recorder to record scripts, we can also use browser developer tools to obtain HAR files of test requests, then use the har-to-k6 tool to convert HAR files to generate K6 scripts.

Browsers and Tools That Can Obtain HAR Files

We can choose a tool to record HAR files according to actual situations. Many browsers and tools on the market can export HTTP traffic in HAR format. Commonly used ones are:

  • Chrome browser
  • Firefox browser
  • Microsoft Edge browser
  • Charles proxy tool (HTTP proxy/recorder)
  • Fiddler proxy tool (HTTP proxy/recorder)

How to Use Browser Developer Tools to Obtain HAR Files

Here’s an example of using Chrome browser developer tools to obtain test request HAR files:

  • Open a new incognito window in Chrome (can exclude interference from login cookies, etc.).

  • Open Chrome Developer Tools (press F12).

  • Select the Network tab.

  • Check and confirm that the recording button (round button) is activated (red).

  • If you want to record multiple consecutive page loads, check the “Preserve log” checkbox. Chrome DevTools Network panel with Preserve log enabled

  • Enter the test website URL (such as https://www.google.com/), then start executing and subsequent operations simulating user execution (such as entering 123 to search).

  • After completion, in Chrome Developer Tools, right-click the URL and select “Save content as HAR”. Chrome DevTools menu for saving a network request as a HAR file

  • Select the location to save the HAR file and rename it (such as har-demo), then click the save button to complete saving the HAR file.

Using har-to-k6 to Convert HAR Files

    1. Install the har-to-k6 tool
    • Install the har-to-k6 tool via the npm install -g har-to-k6 command.
    1. Use the har-to-k6 tool to convert HAR files to K6 scripts
    • Convert the HAR file to a K6 script via the har-to-k6 har-demo.har -O har-demo.js command. har-to-k6 command converting har-demo.har into har-demo.js
    1. Partial screenshot of the converted K6 script is shown below: Excerpt of the k6 script generated from har-demo.har
    1. You can modify the converted K6 script according to your needs, then run the script using k6.
    1. Use k6 to run the converted K6 script and view the running results. k6 run results for the script generated from har-demo.har

For more information about HAR files, please refer to the official documentation: https://grafana.com/docs/k6/latest/using-k6/test-authoring/create-tests-from-recordings/using-the-har-converter/

Reference Documentation


Welcome to follow the WeChat official account “Software Testing Classmate” for original QA technical articles pushed first.