Getting the number of requests started per second

Hi there,

I’m hoping to be able to record, as live output, the number of iterations started per second by k6. This GitHub issue comment indicates that the iteration counter metric is emitted at the end of the corresponding iteration, meaning it cannot serve as a reliable indicator of the actual request rate (instead it acts as a response rate).

This is within the context of the use of a ramping-arrival-rate executor, where it is desirable to compare a changing request rate to the various performance metrics of the SUT. I am not interested (in this case) in the number of requests that have received responses per second (which is what the http_req/iteration counters can get you), but simply the number started per second regardless of whether a response has been received.

One way I figure I could infer this information would be to calculate something akin to response.timings.duration + response.timings.connecting, and then get the approx. request start time by subtracting this value from the current time, and then get my requests-started-per-second metric from there. Is there something more straightforward than this I can do?

Hi
It sounds like custom metrics can support this. Also, as request is not important, we could rely on js internals

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

// Define custom metrics
let iterationCounter = new Counter('iterations_started');
let iterationStartGauge = new Gauge('iteration_start_time');

export let options = {
  scenarios: {
    default: {
      executor: 'ramping-arrival-rate',
      startRate: 50,
      timeUnit: '1s',
      preAllocatedVUs: 50,
      maxVUs: 100, 
      stages: [
        {
            duration: '5s',
            target: 50,
        }
      ]
    },
  },
};

export default function () {
  // Record the start time of the iteration
  let startTime = Date.now();
  iterationCounter.add(1);
  iterationStartGauge.add(startTime);
}```

```./k6 run test.js

          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  (‾)  | 
  / __________ \  |__| \__\ \_____/ .io

     execution: local
        script: test.js
        output: -

     scenarios: (100.00%) 1 scenario, 100 max VUs, 35s max duration (incl. graceful stop):
              * default: Up to 50.00 iterations/s for 5s over 1 stages (maxVUs: 50-100, gracefulStop: 30s)


     data_received..........: 0 B           0 B/s
     data_sent..............: 0 B           0 B/s
     iteration_duration.....: avg=54.5µs min=26.48µs med=41.39µs max=423.45µs p(90)=92.09µs p(95)=113.52µs
     iteration_start_time...: 1722413370172 min=1722413365192 max=1722413370172
     iterations.............: 250           49.990752/s
     iterations_started.....: 250           49.990752/s
     vus....................: 0             min=0             max=0            
     vus_max................: 50            min=50            max=50           


running (05.0s), 000/050 VUs, 250 complete and 0 interrupted iterations
default ✓ [======================================] 000/050 VUs  5s  50.00 iters/s```

The iterationCounter metric you have defined will result in the same response rate calculation that can be obtained with the default iteration counter metric. However, the startTime you defined would indeed be a fairly simple way of getting the info I need.

1 Like