Getting separate summary for different scenarios in k6 script

Hello All,
I did a Google search looking for how I might do exactly this.
I believe I have achieved what is needed.

Try the following

const testConfig = JSON.parse(open('./config/test.json'));
export const options = testConfig;

const myFirstApiCallTrend = new Trend('http_req_duration of first api call', true);
const mySecondApiCallTrend = new Trend('http_req_duration of second api call', true);

function bothMyApiCalls() {
    let response1 = http.post('http://url.to/firstapicall', body, params);
    myFirstApiCallTrend.add(response1.timings.duration);
    let response2 = http.post('http://url.to/secondapicall', body, params);
    mySecondApiCallTrend.add(response2.timings.duration);
}

The output should have a http_req_duration for each api call as below:

http_req_duration........... .............: avg=39.01ms    min=12.11ms med=26.9ms   max=354.61ms p(90)=78.71ms  p(95)=106.88ms p(99)=131.59ms
       { expected_response:true }.........: avg=39.01ms    min=12.11ms med=26.9ms   max=354.61ms p(90)=78.71ms  p(95)=106.88ms p(99)=131.59ms
http_req_duration of first api call.......: avg=42.43ms    min=20.2ms  med=28.08ms  max=138.91ms p(90)=103.05ms p(95)=109.63ms p(99)=127.86ms
http_req_duration of second api call......: avg=35.59ms    min=12.11ms med=23.15ms  max=354.61ms p(90)=65.96ms  p(95)=75.66ms  p(99)=174.72ms

My solution above is a simplified version of what I eventually implemented.
Hopefully this will put you on the right track. Comments and discussion are welcome.

This is similar, or if not the same as this request: How to track request duration for each endpoint? asked back in Nov 2020.

2 Likes