How to find the current "stage" during the test

Hello All,

I have defined the following stages in my script.

  stages: [
    { duration: '5s', target: 20 },
    { duration: '10s', target: 20 },
    { duration: '5s', target: 0 },
  ],

How to find in the script, when it reached the stage 2 ( { duration: ā€˜10sā€™, target: 20 }) ?

I want to add all the response times of stage 2 to custom metrics as shown below.

  const res = http.get(`http://localhost:9000/page/1`, { tags: { page: 'localhost' } });

  if (exec.test.stages=2) { //How to implement this IF ?
    page1.add(res.timings.duration, { page: 'localhost' });
  }

Following is the complete example.

import http from 'k6/http'
import { check, sleep, group } from 'k6';
import { tagWithCurrentStageIndex } from 'https://jslib.k6.io/k6-utils/1.3.0/index.js';
import exec from 'k6/execution';
import { Trend } from 'k6/metrics';
const page1 = new Trend('page1');
export const options = {
  stages: [
    { duration: '5s', target: 20 },
    { duration: '10s', target: 20 },
    { duration: '5s', target: 0 },
  ],
  tags: {
    testName: '6',
  },
};

export default function () {

  tagWithCurrentStageIndex();

  const res = http.get(`http://localhost:9000/page/1`, { tags: { page: 'localhost' } });

  if (exec.test.stages=2) { //How to implement this IF?
    page1.add(res.timings.duration, { page: 'localhost' });
  }

  sleep(1);

}

1 Like

I believe you are looking for getCurrentStageIndex which is also available in our JSLib utils.

1 Like