Help with Implementing a k6 Scenario

Greetings All,

I have attempted to create the following scenario in k6 for awhile now without any success.

Here are the parameters:

  • v = number of VUs (users)
  • t = total time (duration) of the scenario
  • s = stages where:
    ** Each stage has a percentage of the total number of requests sent by the VUs
    ** Each stage has a duration equal to a portion of the total time
    ** ex, if number of stages == 6 AND time == 1 minute then each stage lasts 10 seconds

Example (with s == 6):

  • 1st stage: 70% of n (70% of VUs)
  • 2nd stage: 80% of n (80% of VUs)
  • 3rd stage: 90% of n (90% of VUs)
  • 4th stage: 100% of n (100% of VUs)
  • 5th stage: 115% of n (115% of VUs)
  • 6th stage: 125% of n (125% of VUs)

Example with values:

  • v, VUs = 100
  • t, time = 1 minute
  • s, stages = 6

So:

  • 1st stage: 70% of 100 for 10 seconds
  • 2nd stage: 80% of 100 for 10 seconds
  • 3rd stage: 90% of 100 for 10 seconds
  • 4th stage: 100% of 100 for 10 seconds
  • 5th stage: 115% of 100 for 10 seconds
  • 6th stage: 125% of 100 for 10 seconds

Calculates to:

  • 1st stage: 70 requests for 10 seconds
  • 2nd stage: 80 requests for 10 seconds
  • 3rd stage: 90 requests for 10 seconds
  • 4th stage: 100 requests for 10 seconds
  • 5th stage: 115 requests for 10 seconds
  • 6th stage: 125 requests for 10 seconds

Or:
A grand total of 580 requests for a total duration of 1 minute

Hi @sqa,

Do you want to create this scenarios programatically?

How do you calculate the percentages (70,80,90,100,115,125) from these values?:

  • v, VUs = 100
  • t, time = 1 minute
  • s, stages = 6

The percentages, for the short-term are fine being hard-coded.

@sqa

How do you like this aproach? You can run it on linux with t=60 v=100 s=6 k6 run script.js

import { sleep } from 'k6';


export const options = getOptions(__ENV["v"],__ENV["t"],__ENV["s"])

export function getOptions(v,t,s){
    const percentages =[0.7,0.8,0.9,1,1.15,1.25]
    const secondsPerScenario = Math.round(t/s)
    const options = {scenarios:{}}
    for (var i = 0; i < s; i++) {
        options["scenarios"]["scenario_"+i]={
            executor: 'constant-vus',
            duration: secondsPerScenario+'s',
            vus: Math.round(percentages[i]*v),
            startTime: i*secondsPerScenario+'s',
        }
    }
    return options
}

export default function () {
    console.log("iter")
    sleep(1)
}


2 Likes

This is great! But not quite what I am looking for.

Using your function:
running (1m00.7s), 000/430 VUs, 5771 complete and 0 interrupted iterations // 5771 requests

Ideally:
running (1m00.7s), 000/430 VUs, 5771 complete and 0 interrupted iterations // 580 requests

Then I don’t really understand what you want to achieve. VU is not the same as iteration. Actually a VU can run as many iterations as possible during the given time (you see only 5771 because of the sleep). Even iteration is not the same as “request” because an iteration can call multiple requests.

1 Like

I see, sorry for the confusion. This is what I’m trying to accomplish (I’ll try to rephrase my question):

I have a value, say 100. I want to send iterations in stages based on percentages of that value (70, 80, 90 etc). So stage one would only have 70 iterations, stage two would have 80 etc. Is that possible?

Yes, it is possible, but you have to use another executor (constant-arrival-rate). With this executor, we dont need, the sleep, because iterations will be executed with a predefined rate.

In this case, the v variable sets the maxVUs, and the i variable will set the “value” which controls the iteration count

so you can run like this: t=60 v=20 s=6 i=100 k6 run script.js

import { sleep } from 'k6';


export const options = getOptions(__ENV["v"],__ENV["t"],__ENV["s"],__ENV["i"])

export function getOptions(vus,time,scenarioCount,iters){
    const percentages =[0.7,0.8,0.9,1,1.15,1.25]
    const secondsPerScenario = Math.round(time/scenarioCount)
    const options = {scenarios:{}}
    for (var i = 0; i < scenarioCount; i++) {
        options["scenarios"]["scenario_"+i]={
            executor: 'constant-arrival-rate',
            duration: secondsPerScenario+'s',
            preAllocatedVUs: 10,
            maxVUs: vus,
            rate: Math.round(percentages[i]*iters),
            timeUnit: secondsPerScenario+'s',
            startTime: i*secondsPerScenario+'s',
            gracefulStop: '3s',
        }
    return options
}

export default function () {
    console.log("iter")
}
2 Likes

Okay this getOption function is more similar to what I’ve been trying to accomplish, thank you. Couldd you help me complete my understanding of the difference between a “request” vs an “iteration”?

Using this script:

export let options = {
  vus: 10,       // 10 virtual users
  duration: '30s' // Test duration is 30 seconds
};

export default function () {
  http.get('https://example.com');
}

request_count: ?
iteration_count: ?

1 iteration is when the test function (e.g. default) runs once. In your example the test function starts only one http request, so in this example the iteration count will be equal to the request count.

3 Likes