Configuring sleep for constant-arrival-rate + ramping-vus

I seem to have ran into the issue outlined at Can K6 support send requests at constant throughput? - #8 by ned where using constant-arrival-rate executor with sleep is reducing the desired request rate. But I am combining 2 scenarios where other one is ramping-vus and I want to use sleep there. So how to I ensure only ramping-vus uses the sleep parameter?

  scenarios: {
    // // scenario 1
    // https://k6.io/docs/using-k6/scenarios/executors/constant-arrival-rate
    constant_arrival_rate: {
      executor: 'constant-arrival-rate',
      startTime: '0s',
      rate: `${__ENV.RPS}`,
      timeUnit: '1s',
      duration: `${__ENV.STAGETIME}`,
      preAllocatedVUs: `${__ENV.REQRATE_USERS}`,
      maxVUs: 150000,
      gracefulStop:     '5s',
      tags: { executor: 'constant-arrival-rate' },
    },
    // scenario 4
    // https://k6.io/docs/using-k6/scenarios/executors/ramping-vus
    ramping_vus: {
      executor: 'ramping-vus',
      startTime: `${__ENV.STAGETIME + 18}s`,
      // startTime: '0s',
      startVUs: `${__ENV.USERS}` || 0,
      stages: [
        { duration: `${__ENV.STAGETIME}`, target: `${__ENV.STAGE_VU1}` },
        { duration: `${__ENV.STAGETIME}`, target: `${__ENV.STAGE_VU2}` },
        { duration: `${__ENV.STAGETIME}`, target: `${__ENV.STAGE_VU3}` },
        { duration: `${__ENV.STAGETIME}`, target: `${__ENV.STAGE_VU4}` },
      ],
      gracefulRampDown: '5s',
      gracefulStop:     '5s',
      // tags: { executor: 'ramping-vus' },
    },
  },
export default function () {
  const sleepMin = 1.5;
  const sleepMax = 4.5;

  sleep(randomIntBetween(sleepMin, sleepMax));
}

Hi @eva2000 !

There is a k6/execution module it provides details about the execution so you can use it.

Something like:

import { sleep } from 'k6';
import exec from 'k6/execution';

export let options = {
  scenarios: {
    myscenario1: {
      executor: 'constant-arrival-rate',
      duration: '30s',
      rate: 30,
      timeUnit: '1s',
      preAllocatedVUs: 2,
      maxVUs: 50,
    },
    myscenario2: {
      executor: 'ramping-vus',
      stages: [
         { duration: '20s', target: 5 },
         { duration: '20s', target: 0 },
      ],
    },
  },
};

export default function () {
   console.log(exec.scenario.executor);

   if (exec.scenario.executor == 'ramping-vus') {
      console.log("I like to sleep more");

      sleep(10);
   }
}

Let me know if that answers,
Cheers!

1 Like

Thanks. Can we also use the scenarios’ exec option to define separate functions

export let options = {
  scenarios: {
    myscenario1: {
      executor: 'constant-arrival-rate',
      exec:        'constant',
      duration: '30s',
      rate: 30,
      timeUnit: '1s',
      preAllocatedVUs: 2,
      maxVUs: 50,
    },
    myscenario2: {
      exec:        'default',
      executor: 'ramping-vus',
      stages: [
         { duration: '20s', target: 5 },
         { duration: '20s', target: 0 },
      ],
    },
  },
};

export default function () {
sleep(10);
}

export function constant () {

}

or is your suggested way better?

Yes, it is possible.

or is your suggested way better?

No, for sure you can use your solution :relaxed:

1 Like

Thanks for the clarification - learning more everyday :slight_smile:

1 Like