Learning center

Why did the load test show lower latency than production?

A load test's p99 can sit below production's at the same request rate because production really is slower, or because the load generator measured wrongly. Production has other data, caches and dependencies. A closed-loop generator waits for each response before it sends the next request, so when the server stalls it stops sending and records one slow request per connection where users would have kept arriving; a saturated generator cannot send the planned load at all. Check whether the generator kept its intended rate through the slow periods, and measure latency from each request's intended send time.

The symptom and three explanations#

The example is fictional. Before launch, the quote-api team ran a two-minute load test at 100 requests a second and recorded a p99 of 105 ms. Two weeks after launch, production served the same 100 requests a second, and the service's own latency histogram showed a p99 of about 850 ms. Three explanations fit:

  • Production is different. Production holds more data, colder caches or a slower dependency, and the service really takes longer there.
  • The generator omitted the slow periods. The test tool kept 8 connections open, each sending its next request only after the previous response arrived. The server pauses for about a second every 20 seconds, and during each pause every connection waited with one request in flight, so the test recorded 8 slow requests per pause where production users, who do not wait for each other, sent about 100.
  • The generator was saturated. The machine running the test could not send 100 requests a second, so the server saw less load than planned, or the tool's own queueing distorted its timing.

The wrk2 documentation calls the second effect coordinated omission: since each connection sends only after a response, high-latency responses make the load generator "coordinate" with the server to avoid measuring during the slow periods. wrk2 on coordinated omission Choose the observation where explanations disagree

What each explanation predicts#

ObservationProduction differsOmitted slow periodsSaturated generator
Load test p99lowlowlow or erratic
Requests sent per second, second by secondsteady at the targetdips during the server's slow periodsbelow the target throughout, generator CPU near its limit
Same test, open loop, timed from intended sendstill lowhigh, near productionstill unreliable until the generator is fixed
Server pauses during the test, in its logs or metricsabsentpresenteither
Server time per dependency, production against testhigher in productionsimilarsimilar

The p99 itself separates nothing. The second row is the cheapest test, because most generators already report what they sent. The third row settles it: an open-loop generator sends at its scheduled times whether or not earlier requests have returned, so slow periods receive their full share of requests. The k6 documentation describes the same failure of the closed model: when the system slows, iterations take longer and the arrival rate of new iterations tapers off. k6 open and closed models

The lab: one server, two generators#

The script simulates two minutes of a server whose requests take about 33 ms at the median, and which stops for one second every 20 seconds. Two generators aim at 100 requests a second. The closed-loop one keeps 8 connections, each on an 80 ms schedule, waits for its previous response, skips the slots it missed, and times each request from when it actually sent it. The open-loop one sends a request every 10 ms regardless, and times each from when it was meant to be sent, as wrk2 does. It runs in under a second with Python 3.8 or later.

import random, statistics, sys

seed = int(sys.argv[1]) if len(sys.argv) > 1 else 2
RATE, SECONDS = 100, 120                    # target: 100 requests a second for two minutes
STALLS = [(s, s + 1.0) for s in range(10, SECONDS, 20)]  # the server stops for 1 s every 20 s
rng = random.Random(seed)
service = [rng.lognormvariate(-3.4, 0.45) for _ in range(RATE * SECONDS * 2)]  # ~33 ms median

def finish(start, n):                       # when a request sent at `start` gets its response
    for a, b in STALLS:
        if a <= start < b:
            start = b                       # waits out the stall
    return start + service[n]

def report(name, rows):                     # rows: (send time, latency)
    lat = sorted(l * 1000 for _, l in rows)
    q = statistics.quantiles(lat, n=1000, method="inclusive")
    in_stalls = sum(any(a <= t < b for a, b in STALLS) for t, _ in rows)
    print(f"{name:<34} sent {len(rows):>5}  in stalls {in_stalls:>4}  "
          f"p50 {q[499]:4.0f}  p99 {q[989]:4.0f}  p99.9 {q[998]:5.0f}  max {lat[-1]:5.0f} ms")

# Closed loop: 8 connections, each sends on an 80 ms schedule but waits for the
# previous response first, skips the slots it missed, and times from its own send.
closed, n = [], 0
for c in range(8):
    t = c * 0.010
    while t < SECONDS:
        done = finish(t, n); n += 1
        closed.append((t, done - t))
        slot = t + 0.080
        while slot < done:
            slot += 0.080
        t = slot
report("closed loop, timed from send", closed)

# Open loop: a request every 10 ms whatever happened to the previous ones,
# timed from the moment it was meant to be sent.
opened = [(i / RATE, finish(i / RATE, i) - i / RATE) for i in range(RATE * SECONDS)]
report("open loop, timed from intended send", opened)

Save it as omission.py. The server is the same for both generators, so production cannot differ here. Before running it, write down each generator's p99 and how many requests each will send during the six one-second stalls. Python 3.11 printed:

closed loop, timed from send       sent 11155  in stalls   48  p50   33  p99  105  p99.9  1008  max  1044 ms
open loop, timed from intended send sent 12000  in stalls  600  p50   34  p99  838  p99.9  1020  max  1068 ms

The two generators nearly agreed on the median and on the maximum. The closed loop sent 48 requests during the stalls, one per connection per stall, so stalled requests were 0.4% of its samples and only reached its p99.9. The open loop sent 600, 5% of its samples, and its p99 was 838 ms. The closed loop also fell short of the target, 11,155 requests instead of 12,000, and most of the shortfall sits in the stall windows: the second row of the table, visible in the tool's own output.

The prediction fails if the closed loop sends about 100 requests per stall, or if the open loop's p99 stays near the closed loop's. Run python3 omission.py 7 for other service times. Then give the closed loop 200 connections on a 2-second schedule by changing range(8) to range(200) and both 0.080 values to 2.0. With every connection's interval longer than the stall, no request waits behind another; that run printed a closed-loop p99 of 842 ms against the open loop's 838 ms.

Read it on a real system#

Start with the test's own output. Plot requests sent per second over the run and mark the seconds where the server was slow; a closed-loop tool's rate dips exactly there. Read the generator host's CPU for the same run.

Rerun against the test environment with an open-loop generator. This command sends load: point it only at an environment you are allowed to load. wrk2 holds a constant rate with -R and reports percentiles with --latency, measuring from when each request should have been sent; --u_latency also prints the uncorrected histogram for comparison. wrk2 README

wrk -t2 -c100 -d120s -R100 --latency --u_latency https://quote-api.test.example/quote

In k6, the constant-arrival-rate executor starts iterations at a fixed rate as long as virtual users are available, and counts the iterations it could not start in dropped_iterations. A nonzero count means the generator, or the server's slowness, prevented the planned load, so read it before the latency. constant-arrival-rate ยท Dropped iterations

export const options = {
  scenarios: {
    quote: {
      executor: 'constant-arrival-rate',
      rate: 100, timeUnit: '1s', duration: '2m',
      preAllocatedVUs: 50, maxVUs: 200,
    },
  },
};

If only closed-loop results exist, HdrHistogram can correct them when the expected interval between requests is known: recordValueWithExpectedInterval adds the samples a waiting client would have taken. HdrHistogram AbstractHistogram

To test "production differs", compare the service's own latency histogram, which counts every request it served, in production and during an open-loop test at the same rate, split by dependency call if the service records it. Is the dependency slow, or the client?

Limits#

A constant rate is a model too. Users arrive in bursts, and some systems really are closed, such as a worker pool with fixed concurrency. Schroeder, Wierman and Harchol-Balter showed that open and closed models behave very differently under the same load and offered guidance for choosing between them; match the generator to how requests arrive in production. Open versus closed: a cautionary tale

An open-loop generator is only as accurate as its clock and its capacity. If it falls behind its own schedule, latency measured from intended send time includes its own delay, so a generator near its CPU limit makes both columns unreliable.

The lab's server stalls the same way in test and in production. A production stall the test never triggers, such as a garbage collection pause that grows with a larger heap, shows up in neither generator; that case belongs to "production differs", and finding the missing condition is its own question. The bug will not reproduce in staging

Questions#

What is coordinated omission?

It is the measurement error of a load generator that waits for each response before sending the next request. While the server is slow, the generator sends almost nothing, so the slow period contributes a handful of samples instead of the many requests real clients would have sent, and the reported percentiles come out too low.

Do more connections fix a closed-loop test?

They reduce the error. In the lab, 200 connections each sending every 2 seconds came within 4 ms of the open loop's p99, because no connection had a second request due during a 1-second stall. That works only when you already know the longest stall; measuring from intended send times works without knowing it.

Can I use the load test's p99 as evidence for a latency objective?

Only if the generator achieved the planned rate second by second, including during slow periods, and measured latency from intended send times. Report the achieved rate and the dropped requests beside the percentile so a reader can check both.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading