Get total duration for specific scenarios to calculate TPS

Hi,

I am attempting to calculate TPS from my test run, using the durations.

Our scenarios look like this:

{ 
    "createCustomers": { 
        "duration": "2m",
        "executor": "constant-vus", 
        "exec": "create_customers",
        "vus": "2", 
        "startTime": "0s"
    },
    "test1": { 
        "executor": "ramping-arrival-rate", 
        "exec": "test1",
        "preAllocatedVUs": 2,
        "stages": [
            {"target": 2, "duration": "1m"},
            {"target": 4, "duration": "1m"},
            {"target": 4, "duration": "1m"},
            {"target": 2, "duration": "1m"}
        ],
        "startTime": "2m1s"
    },
    "test2": { 
        "executor": "ramping-arrival-rate", 
        "exec": "test2",
        "preAllocatedVUs": 10,
        "stages": [
            {"target": 10, "duration": "1m"},
            {"target": 20, "duration": "1m"},
            {"target": 20, "duration": "1m"},
            {"target": 10, "duration": "1m"}
        ],
        "startTime": "2m1s"
    }
}

What we did was use k6 Counters and extract the TPS information from there, but this doesn’t work for our situation, as we do not want to count the duration that “createCustomers” runs, so what we are doing is this:

taking the scenarios, extracting the “stages” from test1 and test2, then summing it together and converting it to seconds:

let test1_stages = configJSON.test1.stages;
let test2_stages = configJSON.test2.stages;

function durationToSeconds(duration) {
    const match = duration.match(/(\d+)m/);
    if (match) {
        const minutes = parseInt(match[1], 10);
        return minutes * 60;
    }
    return 0;
}

let test1_sumSeconds = 0;
for (const stage of test1_stages){
  test1_sumSeconds += durationToSeconds(stage.duration);
}
let test2_sumSeconds = 0;
for (const stage of test2_stages){
  test2_sumSeconds += durationToSeconds(stage.duration);
}

// test1_iterations and test2_iterations come from a counter that increments every time they are called
test1_tps = test1_iterations/test1_sumSeconds;
test2_tps = test2_iterations/test2_sumSeconds;

total_tps = test1_tps + test2_tps;

is there a simpler way to do this, where we can get the total duration for only the time test1/test2 ran? especially since here they will always run the same amount of duration.

getting the total duration of the tests from using k6 Counters was giving us a low TPS, since it adds the initial “createCustomers” duration too, which is not required.

thank you.

@basilzuberi

Maybe you could use a Gauge.

import { Counter, Gauge } from 'k6/metrics';
import exec from 'k6/execution';

const test1Duration = new Gauge('test1_duration');
const test2Duration = new Gauge('test2_duration');
...


export function test1(){
  ...
  test1Duration.add(Date.now() - exec.scenario.startTime);
}

export function test2(){
  ...
  test2Duration.add(Date.now() - exec.scenario.startTime);
}

export function handleSummary(data){
   let test1_sumSeconds = data.metrics.test1_duration.values.value/1000;
   let test2_sumSeconds = data.metrics.test2_duration.values.value/1000;
   ...
}