Sleep() function and it's accuraccy

Hello,

I was thinking about sleep function, to my understanding once first requests sent, they finish the http req iteration, and sleep for 1 sec, and if iteration is ongoing for 20 seconds, only after it is done the sleep() function will be called ?

So part 2 of question, if I am sending 100 vus, and 30 of them get stuck and take longer to the extent of 10 seconds, the other 70 keep on going and new 30 are being added ? Or does the same 100 vus/virtual machines stay for the whole test if maximum is predetermined to be 100

Hi @sillymoomoo,

I believe to answer properly I need the code examples, but generally sleep will just suspend VU execution at the place where it stays.

For example, in the code below, a request to https://test.k6.io/news.php will be executed after getting a response of the https://test.k6.io plus 5 seconds.

import { sleep } from 'k6';
import http from 'k6/http';

export default function () {
  http.get('https://test.k6.io');
  sleep(5);
  http.get('https://test.k6.io/news.php');
}

So part 2 of question, if I am sending 100 vus, and 30 of them get stuck and take longer to the extent of 10 seconds, the other 70 keep on going and new 30 are being added ? Or does the same 100 vus/virtual machines stay for the whole test if maximum is predetermined to be 100

The actual behavior depends on the type of the executor. But even if you limit the maximum of the virtual users and the value isn’t enough to satisfy the scenario k6 should warn you, like:

import http from 'k6/http';
import { sleep } from 'k6';

export const options = {
  discardResponseBodies: true,
  scenarios: {
    contacts: {
      executor: 'constant-arrival-rate',
      duration: '30s',
      rate: 30,
      timeUnit: '1s',
      preAllocatedVUs: 2,
      maxVUs: 5,
    },
  },
};

export default function () {
  http.get('https://test.k6.io/contacts.php');
  sleep(2);
}

Let me know if that answers,
Cheers!

1 Like