K6 Summary - How to create new json file for every run using handleSummary

Hi all,
I am running Script sin K6 v 0.30.0 and using handleSummary function to send the test summary to Json. However when I try to parametrize Json file name with timestamp in the Json path, the statement is not working. The below statement is creating a json file with name “${Date.now()}_Summary.json”.
Any solution would be appreciated.

export function handleSummary(data) {
  console.log('Preparing the end-of-test summary...');

  return {
      '/usr/bin/temp/${Date.now()}_summary.json': JSON.stringify(data), // and a JSON with all the details...
  }
}

You have to use backticks (“`”) for JS template literals, not single quotes: Template literals (Template strings) - JavaScript | MDN

Moreover, to use a dynamic object property in JS, you have to surround it in square brackets: Object initializer - JavaScript | MDN

So this works:

import http from "k6/http";

export function handleSummary(data) {
    return {
        [`${(new Date).toISOString()}_summary.json`]: JSON.stringify(data, null, 4),
    }
}

export default function () {
    http.get("https://test-api.k6.io/");
}

Though it might be cleaner if you just save it to a variable :man_shrugging:

export function handleSummary(data) {
    let filename = (new Date()).toISOString() + '_summary.json'
    let result = {}
    result[filename] = JSON.stringify(data, null, 4)
    return result
}
4 Likes

Thanks ned!! Was abel to generate different files for each run. Appreciate it!!

Hi @ned,

your solution is working for some level, but when I use it with the stdout it throws an error.


How to overcome this problem.

stdout is not a variable like the other reportNamehtml, etc., so you need to surround it in quotes, like this: result['stdout'] = ....

2 Likes