Does the `sleep()` function in k6 pause execution for all virtual users or just a single user?

When running load tests in k6, many testers wonder how the sleep() function behaves, especially when simulating multiple virtual users.

For instance, if you add a sleep(Math.floor(Math.random() * 4) + 1) between your HTTP requests, you may notice improved application stability and fewer 500 errors during high load.

However, it’s important to understand that the sleep() function only pauses execution for the virtual user (VU) that called it, not for all users.

Each VU in k6 runs its own instance of the test script independently. So, when one VU hits a sleep() command, it pauses only its own execution for the specified duration (in milliseconds or seconds), while other users continue running their scripts normally.

This behavior helps you simulate more realistic traffic patterns where users perform actions at different intervals rather than all at once.

The sleep() function in k6 only pauses execution for the virtual user (VU) that calls it, not all users.

Each VU runs independently, so when one sleeps, others continue executing their scripts without waiting.

Follow this tutorial on K6 and how to use various methods of K6 k6 Testing Tutorial: A Complete Guide To Browser Testing With Grafana k6 | LambdaTest

I agree with @netra.agarwal, in K6, each virtual user runs in its own isolated JavaScript runtime. When you call sleep(), it only delays that specific VU’s execution, not the entire test.

Other VUs continue sending requests or performing actions. This helps simulate realistic user pacing, where not all users act simultaneously.

The sleep() function affects only the individual virtual user thread in k6.

Every VU runs in a separate environment, executing its own copy of the test script.

So, calling sleep(3) pauses that VU’s operations for 3 seconds, while others keep executing in parallel.

This design enables realistic load testing and avoids synchronized traffic spikes that could distort test results.