import http from 'k6/http';
import { check } from 'k6';
import { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.1/index.js';
let responseTimes = []; // Global array to store responses
export let options = {
vus: 2, // Number of virtual users
duration: '10s', // Test duration
};
// The default function where each VU will run the test
export default function () {
let url = 'https://jsonplaceholder.typicode.com/posts/1'; // Sample API endpoint
let response = http.get(url); // Make GET request
// Store relevant response data for each VU
responseTimes.push({
vu: __VU,
iter: __ITER,
status: response.status,
body: response.body,
time: response.timings.duration,
});
// Optionally check the response
check(response, {
'status is 200': (r) => r.status === 200,
});
}
// The handleSummary function to process results at the end of the test
export function handleSummary(data) {
// Print all captured responses in the summary
let customSummary = {
'Captured Responses': responseTimes.map((res) => ({
VU: res.vu,
Iteration: res.iter,
Status: res.status,
Duration: `${res.time}ms`,
BodySnippet: res.body.substring(0, 50), // Display the first 50 chars of the body
})),
};
console.log(JSON.stringify(customSummary, null, 2)); // Log the custom response
// Return the summary as usual
return {
stdout: textSummary(data, { indent: ' ', enableColors: true }), // Generate the default summary
};
}
responseTimes is empty in handleSummary