Metrics per stage in k6

Posted on Fri 25 November 2022 in k6 • Tagged with k6

If you want to have custom metrics per each stage in an executor, you have to tag the stages (with tagWithCurrentStageIndex()) and add bogus thresholds for each metric that you want with the tag stage:i (being i the number of each stage). The bogus threshold has to be different for gauges (max>0) and rates (rate>=0).

This example file shows how to do it:

import http from 'k6/http';
import { tagWithCurrentStageIndex } from 'https://jslib.k6.io/k6-utils/1.3.0/index.js';

const stages = [
    { target: 1, duration: '10s' },
    { target: 5, duration: '10s' },
];

export const options = {
  scenarios: {
    contacts: {
      executor: 'ramping-arrival-rate',
      timeUnit: '1s',
      preAllocatedVUs: 10,
      maxVUs: 200,
      stages: stages,
    },
  },
  // Uncomment the next line if you want the count statistic
  // summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'],
  thresholds: {
    // Intentionally empty. We'll programatically define our bogus
    // thresholds (to generate the sub-metrics) below. In your real-world
    // load test, you can add any real threshoulds you want here.
  }
}

function addThreshold(thresholdName, threshold) {
    if (!options.thresholds[thresholdName]) {
        options.thresholds[thresholdName] = [];
    }

    // 'max>=0' is a …

Continue reading

Reading traces from a file in k6

Posted on Wed 24 November 2021 in k6 • Tagged with k6, kubernetes

I wanted to read a trace of requests per second I have in a file and use it as the injection pattern in k6. I could do it by reading the values into an array, which is then used as the stages in a ramping arrival rate excutor, like this:

import http from 'k6/http';
import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js';
import { SharedArray } from 'k6/data';

const trace_file = 'PATH_TO_THE_TRACE_FILE';
const trace = new SharedArray('another data name', function () {
  return papaparse.parse(open(trace_file)).data;
});

var stages = []
for (var i of trace) {
  stages.push({ target: trace[i][0], duration: "1s" })
}

export const options = {
  discardResponseBodies: true,
  scenarios: {
    contacts: {
      executor: 'ramping-arrival-rate',
      startRate: 1,
      timeUnit: '1s',
      preAllocatedVUs: 50,
      maxVUs: 1000,
      stages: stages
    },
  },
};

export default function () {
  const url = "http://localhost:8555";
  http.get(url);
}