Streamlining Performance Testing with K6 and ChatGPT

Monish Correia
5 min readJul 11, 2023

--

Introduction:

In today’s fast-paced software development landscape, efficient performance testing is crucial to ensure optimal application performance. This article explores how to leverage K6, a powerful load testing tool, in combination with ChatGPT, an AI language model, to streamline performance testing processes. We’ll cover setting up K6, generating automated K6 scripts using ChatGPT, interpreting test results with different executors, and exporting results in preferred formats.

  1. Setting up K6 for your testing environment:

Learn how to install and configure K6 for your specific testing environment. Understand the basic structure of a K6 script and how to define test scenarios, virtual users, and thresholds.

Installation:

For other OS, please refer — https://k6.io/docs/get-started/installation/

Sample K6 script as below:

import http from 'k6/http';
import { sleep } from 'k6';

export default function () {
http.get('https://test.k6.io');
sleep(1);
}

Run K6 with following command:

k6 run script.js

Adding Virtual Users:

import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
vus: 10,
duration: '30s',
};
export default function () {
http.get('http://test.k6.io');
sleep(1);
}

2. Using ChatGPT to generate K6 scripts automatically:

Discover how to harness the power of ChatGPT to automate the generation of K6 scripts. Engage in conversational interactions with ChatGPT to quickly create test scenarios, define API endpoints, set load parameters, and customize test configurations.

Probelm 1: I provided below prompt to ChatGPT

The response generated as below:

Problem 2: I have a sample curl request as below and wanted to create a K6 script to performance test this API

curl --location 'https://reqres.in/api/users' \
--header 'Content-Type: application/json' \
--data '{
"name": "morpheus",
"job": "leader"
}'

I gave ChatGPT a prompt to create K6 script using above curl request as below:

The response I got is as below:

Problem 3: Now I gave prompt to modify above script, to include checks and thresholds, let’s see how ChatGPT responds

And ChatGPT gave below response:

3. Understanding and interpreting results for optimized performance:

Gain insights into effectively analyzing K6 test results. Explore different executors offered by K6, such as the default executor, Ramping VUs executor, and Gradual VUs executor, and understand how they impact performance measurements. Learn to interpret key metrics like response time, throughput, and error rates to identify performance bottlenecks.

import http from 'k6/http';
import { check, sleep } from 'k6';

export let options = {
vus: 10,
duration: '1m',
thresholds: {
http_req_duration: ['p(95)<500'], // Response time should be below 500ms for 95% of requests
http_req_failed: ['rate<0.1'], // Failure rate should be below 10%
},
exec: 'ramping-vus', // Executor: Ramping VUs
stages: [
{ duration: '30s', target: 20 }, // Ramping up to 20 VUs over 30 seconds
{ duration: '30s', target: 20 }, // Staying at 20 VUs for 30 seconds
{ duration: '30s', target: 0 }, // Ramping down to 0 VUs over 30 seconds
],
};

export default function () {
// Make a GET request to the API endpoint
let response = http.get('https://api.example.com/users');

// Check the response status code
check(response, {
'Status is 200': (r) => r.status === 200,
});

// Simulate user think time
sleep(1);
}

In this script:

  • The options object defines various parameters:
  • vus specifies the number of virtual users.
  • duration sets the duration of the test.
  • thresholds define performance thresholds for metrics like response time and failure rate.
  • exec determines the executor type. In this example, we use the 'ramping-vus' executor.
  • stages specify the ramping up, maintaining, and ramping down of virtual users.
  • Inside the default function, we make a GET request to the API endpoint as before and check the response status code.
  • The thresholds in the options object allow us to set performance thresholds for the test. In this example, we set a threshold that 95% of the requests should complete within 500 milliseconds and the failure rate should be below 10%.
  • The exec property specifies the 'ramping-vus' executor, which ramps up the number of virtual users, maintains it for a specific duration, and then ramps down.

4. Exporting results in preferred formats:

Discover various methods to export K6 test results in formats suitable for analysis and reporting. Explore exporting options like generating HTML reports, integrating with data visualization tools, and exporting data to formats like CSV or JSON.

I will show below how to get HTML report at the end of performance execution. To generate HTML report, we are going to use Github repository — https://github.com/benc-uk/k6-reporter

To use, add this module to your test code.

Import the htmlReport function from the bundled module hosted remotely on GitHub

import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";

Then outside the test’s default function, wrap it with the handleSummary(data) function which K6 calls at the end of any test, as follows:

export function handleSummary(data) {
return {
"summary.html": htmlReport(data),
};
}

So, my modified script from Step 1 will become like this:

import http from 'k6/http';
import { sleep } from 'k6';
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";

export default function () {
http.get('https://test.k6.io');
sleep(1);
}

export function handleSummary(data) {
return {
"summary.html": htmlReport(data),
};
}

Now, if you run above script, you will get HTML report like below:

Conclusion:

By combining the power of K6 and ChatGPT, you can streamline your performance testing efforts. Automating script generation with ChatGPT saves time and effort, while K6 provides the flexibility and robustness required for load testing. Understanding and interpreting test results empowers you to optimize application performance. Lastly, exporting results in preferred formats allows for effective analysis and reporting.

Enhance your performance testing workflow by leveraging the synergy between K6 and ChatGPT, enabling you to identify and address performance issues early on and deliver high-performing applications to your users.

Monish Correia — QA Lead

If you like this article, please show your support clicking the clap button below and follow for more. Thank you! ❤️

Software Testing Strategy Toolkit: https://monishcactus.gumroad.com/l/xkyic

--

--

Monish Correia

QA Lead | Postman Supernova| K6 Champion| Test Automation | API Testing | Performance Testing | Selenium | K6 | Consultation: https://topmate.io/monish_correia