Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 36 ยท Checks and Thresholds

Think Time and Pacing

Make k6 load realistic with sleep() think time, random pauses and pacing that keeps a steady iteration rate, and learn why tests without pauses overstate the load.

Real people do not click as fast as a computer can. They read, scroll, type and think. If your virtual users never pause, each VU behaves like a hundred real users and your results describe a situation that will never happen in production (or one that you did not intend). Think time is the deliberate pause between actions that models human behaviour. Pacing is a related technique that controls how long a whole iteration takes so the arrival rate stays steady. Both are about making the load realistic and predictable.

sleep()

sleep(seconds) from the k6 module pauses the current VU. The argument is in seconds, and fractions are fine.

JavaScript
import { sleep } from 'k6';

export default function () {
  http.get(`${BASE}/`);
  sleep(2);                // read the page for 2 s
  http.get(`${BASE}/products`);
  sleep(0.5);
}

Sleeping costs almost nothing: the VU yields while sleeping, and k6 handles thousands of sleeping VUs easily. Sleep time is not included in http_req_duration, only in iteration_duration.

sleep takes seconds, not milliseconds. sleep(500) pauses for more than eight minutes. This is one of the most common beginner bugs.

Random think time

If every VU sleeps exactly 1 second, all of them tend to fire requests at the same moments, producing artificial synchronized bursts. Randomness spreads the load the way real users do:

JavaScript
import { sleep } from 'k6';

function thinkTime(min, max) {
  sleep(min + Math.random() * (max - min));
}

export default function () {
  http.get(`${BASE}/`);
  thinkTime(1, 4);
}

The k6-utils library provides randomIntBetween for the same purpose. Pick the distribution from real analytics when you can: a page that people read for 30 seconds needs a longer pause than a quick confirmation screen.

Why a missing sleep is dangerous

With no sleep, the request rate of a closed-model test equals VUs / response_time. With 100 VUs and 50 ms responses that is 2,000 requests per second, from only "100 users". Real users making one request every few seconds would be hundreds of thousands of users. A test without think time is a stress test whether you meant it or not, and it can make a healthy system look broken or hide capacity problems by saturating the load generator first.

There is a legitimate use: when you want maximum pressure to find the limits of one endpoint, you can leave sleep out. State it explicitly in the script's comments so nobody mistakes the numbers for realistic traffic.

Pacing: keep the iteration length constant

Think time gives each iteration a variable length: a slow response makes the iteration longer. Pacing sets a target length and sleeps only for whatever time is left:

JavaScript
const ITERATION_SECONDS = 5;

export default function () {
  const start = Date.now();

  http.get(`${BASE}/`);
  http.get(`${BASE}/products`);

  const elapsed = (Date.now() - start) / 1000;
  sleep(Math.max(0, ITERATION_SECONDS - elapsed));
}

If the requests took 1.2 s, the VU sleeps 3.8 s, so every iteration starts 5 seconds after the previous one. The rate of iterations per VU is stable at 0.2 per second, regardless of response time, until responses take longer than 5 s. k6 can do this for you at a larger scale with the arrival-rate executors (Arrival-Rate Executors), which are usually the better tool; hand-written pacing is handy in small scripts.

TEXT
Think time (1 s sleeps):
  [req A 1.2 s][sleep 1][req B 3 s][sleep 1]        -> iteration 6.2 s
Pacing (target 5 s):
  [req A 1.2 s][req B 3 s][sleep 0.8 s]             -> iteration 5.0 s

With think time the iteration length changes with the server; with pacing it stays at 5 seconds.

Measuring the effect

The stand-in below is not k6. It simulates 4 VUs for a fixed window with a server that takes about 20 ms, once with no sleep and once with a 40 ms think time, and counts the requests. The exact counts depend on the machine, so the program only prints the comparison, which is always the same: without think time the server receives many times more requests from the same number of users.

Node.js Stand-in: the effect of think time on request volume

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The 40 ms pause roughly triples the length of each iteration (20 ms request plus 40 ms sleep), so the request volume falls to about a third from the same number of VUs.

Guidelines

  • Use think time in all realistic user-journey tests; leave it out only on purpose.
  • Make it random within a range that fits the page.
  • Keep an eye on the units: seconds.
  • Prefer arrival-rate scenarios when you need a target request rate; use VUs plus think time when you want to model concurrent users.
  • Remember sleep adds to iteration_duration; do not compare that metric with request timings.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What unit does k6's sleep() use?

    sleep(1) pauses one second. sleep(500) would pause more than eight minutes.
  2. 2. Why use random think time rather than a fixed pause?

    Fixed pauses make VUs fire at the same moments; randomness spreads the load.
  3. 3. Is sleep time included in http_req_duration?

    http_req_duration measures just the request; sleep adds to the whole iteration time.
  4. 4. What does pacing do?

    Pacing computes how long the iteration took and sleeps for the rest of the target duration.

Summary

  • Think time (sleep) models human pauses; without it each VU is far more aggressive than a person.
  • sleep uses seconds; random ranges avoid synchronized bursts.
  • Pacing keeps the iteration length constant by sleeping for the time left.
  • A test without pauses is a stress test; do it only on purpose.
  • Arrival-rate executors are the scalable alternative for a fixed request rate.

Next: Scenarios Overview