Can we access global Collection values in handleSummary() ?, i am adding api response to collection. I want to access those values in handleSummary

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

Hey @sujithmtla,

Welcome to our community, thanks for sharing your question here :tada:

I’m sorry, but I don’t think what you’re trying to do here is possible in k6, because the responseTimes variable you’re defining here, will be defined once for each VU, which means that the variable isn’t really global, but “local” to the VU.

This in practice means that there’s no single responseTimes variable that holds all the values, but multiple instances, distributed and isolated.

Additionally, the VU/JS runtime that runs the handleSummary function isn’t probably the same as the one running the test script (iterations executed during the test run), so that’s why the variable isn’t defined/is empty at that time.

That’s by design, because k6 is designed to operate at scale, to cater to the technical aspects involved in executing JavaScript code efficiently, and the resource management challenges associated with such operations. Also, bear in mind that technically k6 could be executed as a distributed system, and in such case, there’s nothing like a “global variable” in the context of the JS runtime.

So, if you want to store some data globally, you’ll need to use an external storage, shared across all the virtual users (VUs) and/or JS runtimes. For instance, you could use Redis with xk6-redis.

I hope that helps, at least to try to clarify the behavior you’re observing.
Please, let me know you have any additional doubt :bowing_man: