How to create endpoint wise summary for each requests in k6 script?

Monish Correia
3 min readFeb 29, 2024

In performance testing, having detailed insights into the performance of each endpoint is crucial for identifying bottlenecks and optimizing system performance.

While tools like JMeter offer endpoint-wise summaries by default, achieving the same in k6 requires a bit of customization. In this guide, I’ll walk you through how to enhance endpoint-wise reporting in k6 scripts, enabling you to gain valuable insights into the performance of individual endpoints.

Introduction

Performance testing plays a critical role in ensuring the reliability and scalability of applications.

With the increasing complexity of modern web applications, it’s essential to have robust tools that provide comprehensive insights into performance metrics. While k6 is a powerful load testing tool, it doesn’t offer endpoint-wise summaries out of the box.

However, with some customization, we can leverage k6 to generate detailed reports for each endpoint, similar to what JMeter provides.

Customizing k6 Scripts for Endpoint-Wise Reporting

To achieve endpoint-wise reporting in k6, we’ll utilize custom trends to track the performance of each request individually. By creating separate trends for each endpoint, we can capture metrics such as response times and status codes for analysis. Let’s dive into the implementation steps:

  1. Defining Custom Trends: We’ll start by defining custom trends for each endpoint in our k6 script. These trends will track the performance metrics specific to each request, allowing us to generate endpoint-wise summaries.
  2. Sending Requests in Batch: Next, we’ll use the http.batch() function to send requests to multiple endpoints concurrently. This approach ensures efficient utilization of resources and enables us to gather performance data for multiple endpoints in a single test run.
  3. Processing Responses: As the responses are received, we’ll iterate through them and update the corresponding trend with the relevant performance metrics. This includes capturing response times, status codes, and any other metrics of interest.
  4. Generating Reports: Finally, we’ll configure k6 to generate comprehensive reports that include endpoint-wise summaries. This allows us to visualize the performance of each endpoint and identify any outliers or areas for optimization.

For understanding this in a step-by-step manner, let’s consider below 5 endpoints in our scope of testing:

  "https://jsonplaceholder.typicode.com/todos/1",
"https://jsonplaceholder.typicode.com/users",
"https://test.k6.io/",
"https://test.k6.io/contacts.php",
"https://test.k6.io/news.php"

Let’s create separate trends for these 5 API endpoints namely API1 to API5 as below:

let requestTrends = [
new Trend('API1'),
new Trend('API2'),
new Trend('API3'),
new Trend('API4'),
new Trend('API5')
];

My overall script looks like below:

import { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.1/index.js';
import http from 'k6/http';
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
import { Trend } from 'k6/metrics';

export const options = {
"duration": "10s",
"vus": 1
};

let requestTrends = [
new Trend('API1'),
new Trend('API2'),
new Trend('API3'),
new Trend('API4'),
new Trend('API5')
];

const urls = [
"https://jsonplaceholder.typicode.com/todos/1",
"https://jsonplaceholder.typicode.com/users",
"https://test.k6.io/",
"https://test.k6.io/contacts.php",
"https://test.k6.io/news.php"
];

export default function () {
let responses = http.batch(urls.map(url => ({ url: url })));

for (let i = 0; i < responses.length; i++) {
requestTrends[i].add(responses[i].timings.duration);
}
}

export const handleSummary = function (data) {
return {
'stdout': textSummary(data, { indent: ' ', enableColors: true }), // Show the text summary to stdout...
"results.html": htmlReport(data, { title: new Date().toLocaleString() }),
'summary.json': JSON.stringify(data), // and a JSON with all the details...
}
}

When I run this script, I get below HTML output as “results.html”

Conclusion

While k6 doesn’t offer endpoint-wise reporting by default, it’s possible to achieve this functionality through customization.

By leveraging custom trends and batch requests, we can track the performance of each endpoint and generate comprehensive reports for analysis.

Implementing endpoint-wise reporting in k6 empowers us to identify performance bottlenecks, optimize system performance, and deliver a better user experience.

Happy testing!

Monish Correia — QA Lead

https://www.linkedin.com/in/monishcorreia/

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

https://topmate.io/monish_correia

--

--

Monish Correia

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