How to initiate trend for dynamic request names?

I’m trying to use the trends for my tests and I’m unable to when the request name is dynamic. There are ~300 api calls and I don’t execute tests on all at all the time nor can I declare trends for 300 request names. I want to be able to run each request individually and I want to declare trend in the below fashion so that the requests names change and the trends for them are initiated in run time.

import { Trend } from 'k6/metrics';

let requestTrends = {};

export function requestTrend(stepName, response) {
  if (!requestTrends[stepName]) {
    requestTrends[stepName] = new Trend(stepName);
  }
  requestTrends[stepName].add(response.timings.duration);
}

When I import the function in other file and call it I get
GoError: metrics must be declared in the init context

Hi @shivadutt1988,
you should be able to find the answer to your question in this issue’s comment.

I hope it helps, let me know in the case you need more details.

1 Like