23.11 An open-loop load generator
The engine from the tokio server and its metrics from serving metrics and tracing are ready. How much load they take before the SLOs break is not yet known, and finding out is harder than it sounds.
Concepts from other sections (14)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Service Level Objective (SLO): a target for a defined measure of service behavior over a specified window. Read more: Chapter 13: Engineering Acronyms in Software Engineering
- PCG32: a permuted congruential generator with 64-bit state and an odd stream increment, producing 32-bit outputs from the old state. Read more: 23.1 Your first endpoint: a std-only Rust HTTP/1.1 + SSE server
- TTFT (time to first token): time from a request’s arrival to its first generated token. Read more: 23.4 Chunked prefill (mixed prefill + decode batches)
- TPOT (time per output token): time between successive tokens of a running request, about one step. Read more: 23.4 Chunked prefill (mixed prefill + decode batches)
- Server-Sent Events (SSE): a
text/event-streambody ofdata: ...events, each terminated by a blank line. Read more: 23.1 Your first endpoint: a std-only Rust HTTP/1.1 + SSE server - Poisson process: events with independent exponential gaps; the count in a window of length is Poisson with mean . Read more: 13.2 Categorical sampling: inverse CDF, Gumbel-max, alias method in Mathematics
- Cumulative distribution function (CDF): , defined for every random variable, discrete or continuous; it rises from 0 to 1, and for a continuous variable its derivative is the density. Read more: Chapter 13: Probability and Statistics in Mathematics
- goroutine: a function running concurrently, scheduled by the Go runtime. Read more: 1.6 Go: packages, interfaces, goroutines, channels, context, net/http, testing in Programming and Algorithms
- Box-Muller: the transform , that turns two uniforms into two independent standard normals. Read more: 13.9 Probability problem set, part a: axioms, Bayes, random variables, Box-Muller in Mathematics
- SplitMix64: the mixer used to derive child seeds that depend only on the seed and the purpose. Read more: 12.3 Modular arithmetic, hashing, and PCG32 in Python in Mathematics
- Regression gate: a check that fails a head run only when it is worse than the base by more than a budget and the difference is unlikely under noise. Read more: 23.12 Run comparison and the regression gate
- Permutation test: a test that compares the observed statistic difference with its distribution over random relabelings of the pooled samples. Read more: 23.12 Run comparison and the regression gate
- k-means++: a seeding rule that picks each new centroid with probability proportional to its squared distance from the nearest seed. Read more: 27.2 Retrieval: BM25, Flat and IVF Vectors, RRF, and MMR
- rounding error: the error from floating-point rounding, about in a difference quotient. Read more: 7.1 Derivatives and Finite Differences in Mathematics
The earlier Python load script waits for each response before sending the next one. That is a closed loop, and it has a fatal flaw: when the server stalls, the script stops sending and records one slow request instead of the dozens a real user population would have sent. Its percentiles look fine during exactly the outages that matter. This failure has a name, coordinated omission. Every load number from here on (the 64-request burst against the engine, the production SLO run, the incident walkthroughs, and the regression gate in CI) comes from the generator this section builds, so it has to be right: open loop, latencies from the intended start, honest histograms, seeded arrivals.
The generator is written in Go. It calls nothing else from the book, but it re-implements two ideas you have seen before: inverse-CDF sampling from categorical sampling and the PCG32 generator from modular arithmetic, hashing, and PCG32, both in Mathematics. The Go primer in Programming and Algorithms covers the language.
The contracts are the report format formats/loadgen-report.schema.json, the generator spec spec/pcg32.md (conformance parity/rng), the stream being measured in openapi/openai-subset.v1.yaml, and the {loadgen} role in spec/cli-roles.md. The solution is go/ds/rng/pcg32.go (PCG32: New, Uint32, Float64, Normal, Below, Shuffle, Stream), go/loadgen/schedule.go (Poisson, Constant, and Burst arrivals), go/loadgen/histogram.go (a log-linear Histogram with at most 1/128 relative error), go/loadgen/run.go (ReadStream, Measure, the open-loop Run, and BuildReport), and the entry point go/cmd/loadgen. A run looks like this:
loadgen --target http://127.0.0.1:8000 --model smol-135m --mode poisson --rate 4 --duration 60s --seed 7 --out report.jsonLearning objectives
- Explain coordinated omission and why an open-loop generator charging each request from its intended send time avoids it.
- Measure TTFT, ITL, E2E, and TPOT from a streamed SSE response.
- Build a log-linear histogram whose quantiles are within 1/128 of exact, always on the pessimistic side, in fixed memory.
- Port PCG32 to Go so it produces the same bits as the other ports for the same seed.
- Compute error rate and goodput so that failures never flatter the results.
Arrivals
Section titled “Arrivals”Here is the notation for the section.
| Symbol | Meaning | Type |
|---|---|---|
| offered rate (requests per second) | float64 | |
| gap before arrival | time.Duration | |
| the intended send time of request | time.Time | |
| a recorded value in nanoseconds, | int64 | |
| the value’s octave | int | |
| recorded values; a quantile |
A load generator models users, not a client, and real users do not wait for each other. So arrival times must come from the schedule alone.
A Poisson process of rate has independent exponential gaps. By inverse CDF, one uniform gives ; note that is never 0. Constant arrivals use every time. A burst of size every uses gaps .
The runner sends request at in its own goroutine, whether or not earlier requests have finished, and stops scheduling at $s_0 + $ duration. An arrival exactly at the end is not sent.
Coordinated omission
Section titled “Coordinated omission”The second half of being open loop is how latency is charged. Charge a request from , not from when the client got around to sending it, and not from when the server answered the headers. If request is sent late because the client was busy, the user still waited from . If the server stalls for one second at 10 rps, ten requests each see the stall, and all ten latencies record it.
What one request measures
Section titled “What one request measures”Each request reads an SSE stream: data: <json>\n\n events, with [DONE] last. Each event is stamped as it is read, because TTFT and ITL are differences of arrival times, and stamping after parsing a batch of lines would inflate them. From those stamps:
- TTFT is the first content chunk minus .
- ITL is the gaps between consecutive content chunks.
- E2E is
[DONE]minus . - Tokens are
usage.completion_tokenswhen the server sends usage, else the content chunks. - TPOT is for at least 2 tokens.
A failure is not a fast success. A 429 returns in a millisecond, and recording it as a latency would pull p50 down exactly when the system is overloaded. So an error status, an SSE error event, or a stream cut before [DONE] counts in errors and is kept out of every histogram.
The log-linear histogram
Section titled “The log-linear histogram”Percentiles need bounded relative error in fixed memory. Storing every value grows without bound, and fixed linear buckets lose precision at either the low or the high end. The answer is to split every power of two into 128 equal buckets.
Values below get a bucket each. Above that, with and shift , the top 8 bits lie in and select bucket , which covers . A bucket is at most of its lower bound wide.
The q-quantile is the nearest-rank value. The rank is , clamped to ; the small epsilon forgives float rounding, since is in float64. The quantile is reported as the upper bound of the bucket holding the -th smallest value, clamped to the true maximum. Reporting the upper bound means the estimate is never below the exact value: a reported percentile must never flatter the system.
Memory is fixed at 7424 counters for all of int64, and merging two histograms is adding counters.
The generator
Section titled “The generator”Seeded runs must repeat across languages. The Poisson gaps draw from PCG32, so the Go port must produce the same bits as the Python, C, and Rust ports for the same seed.
The port follows spec/pcg32.md exactly. The state update is , and the output is the XSH-RR permutation of the old state. Seeding sets . uniform_f64 takes two draws (53 bits). Normals use Box-Muller with the spare kept. Below(n) rejects draws under . Shuffles are Fisher-Yates from the end. Sub-streams come from SplitMix64.
The report
Section titled “The report”The report follows formats/loadgen-report.schema.json: run id, target, mode, offered rate, duration, requests, errors, p50/p90/p95/p99/mean per metric in milliseconds, error rate, goodput, output tokens per second, and each histogram’s non-empty buckets as [upper_ms, count]. Goodput is the share of all requests, failures included, that met every SLO bound.
Working it by hand
Section titled “Working it by hand”Three examples pin down the histogram, one request, and the generator.
A histogram. Record 1000, 1002, 1005, and 2000 ns. Each lands in a bucket as follows:
| Value | shift | top bits | bucket | |
|---|---|---|---|---|
| 1000 | 9 | 2 | 250 | [1000, 1003] |
| 1002 | 9 | 2 | 250 | [1000, 1003] |
| 1005 | 9 | 2 | 251 | [1004, 1007] |
| 2000 | 10 | 3 | 250 | [2000, 2007] |
The median has rank : the second smallest value is in [1000, 1003], reported as 1003. The 100th percentile is in [2000, 2007] but clamped to the true maximum, 2000. The mean, from the exact sum, is 1251.75.
One request. Intended at ; content at 120, 150, and 190 ms; usage 3 tokens; [DONE] at 200 ms. TTFT is 120 ms, ITL is 30 and 40 ms, E2E is 200 ms, and TPOT is ms.
The generator. Seeding pcg32(0): inc . From state 0, one step gives 109; adding seed 0 and taking one more step gives 0x9AE4F7499BA72696. The first output permutes that state: xs 0x5C9A3E14, rot , output 0x47C28B93.
Designing the interface
Section titled “Designing the interface”The code splits into the generator package and the load generator package.
func New(seed, seq uint64) *PCG32; func Seeded(seed uint64) *PCG32 // pcg32_srandom_r; seq 54func (r *PCG32) Uint32() uint32; func (r *PCG32) Float64() float64; func (r *PCG32) Normal() float64func (r *PCG32) Below(n uint64) uint32; func (r *PCG32) Shuffle(n int, swap func(i, j int))func (r *PCG32) State() (state, inc uint64)func Mix64(z uint64) uint64; func ChildSeed(seed, purpose uint64) uint64; func Stream(seed, purpose uint64) *PCG32
// go/loadgen/schedule.gotype Schedule interface { Next() time.Duration; Name() string; Rate() float64 }func Poisson(rate float64, r *rng.PCG32) Schedule; func Constant(rate float64) Schedule; func Burst(size int, every time.Duration) Schedule
// go/loadgen/histogram.gofunc NewHistogram() *Histogramfunc (h *Histogram) Record(ns int64); func (h *Histogram) Quantile(q float64) int64; func (h *Histogram) Merge(o *Histogram)func (h *Histogram) Count() uint64; func (h *Histogram) Mean() float64; func (h *Histogram) Min() int64; func (h *Histogram) Max() int64func (h *Histogram) Buckets() []Bucket // Bucket{Lower, Upper int64; Count uint64}
// go/loadgen/run.gotype Clock interface { Now() time.Time; After(d time.Duration) <-chan time.Time } // a fake clock satisfies it in teststype Config struct { Target, Model string; Prompts []string; MaxTokens int; APIKey string; Schedule Schedule Duration time.Duration; MaxRequests int; SLO map[string]float64; RunID string; Seed uint64; Clock Clock; Client *http.Client }type Event struct { At time.Time; Kind EventKind; Tokens int } // Content | Usage | Donetype Sample struct { Start time.Time; TTFT, E2E time.Duration; ITL []time.Duration; Tokens int; Err error } // TPOT()func ReadStream(r io.Reader, now func() time.Time) ([]Event, error)func Measure(start time.Time, events []Event) Samplefunc Run(ctx context.Context, cfg Config) (*Report, error)func BuildReport(cfg Config, samples []Sample, elapsed time.Duration) *Report // Report has the schema's json tagsThe Clock interface is the one design decision worth calling out: it lets the same runner loop run on a fake clock in tests, so every latency the tests check is an exact number.
Building it step by step
Section titled “Building it step by step”Reasoning from first principles
Section titled “Reasoning from first principles”Seven observations, each already motivated above, fix the design.
- The generator models users, not a client. Arrival times come from the schedule alone. That forces an open loop: the runner computes from gaps, sleeps until , and starts request in its own goroutine.
- Latency is charged from the intended time. Every
SamplecarriesStart = s_i, and TTFT and E2E subtract it. - Each event’s timestamp must be taken when it is read. So
ReadStreamcallsnow()once per data line, before parsing it. - A failure is not a fast success. Failures are counted in
errorsand kept out of every histogram, and goodput divides by all requests. - Percentiles need bounded relative error in fixed memory. Splitting every power of two into 128 equal buckets bounds the error at of the value, everywhere, in 7424 counters.
- A reported percentile must never flatter the system. Reporting a bucket’s upper bound (clamped to the true max) makes the estimate the exact value; the lower bound would make an SLO look met when it is not.
- Seeded runs must repeat across languages. The Go PCG32 must produce the same bits as the Python, C, and Rust ports.
The generator
Section titled “The generator”Start at the bottom, with pcg32.go:
// New is pcg32_srandom_r(seed, seq): inc = (seq << 1) | 1, state = 0, one// step, state += seed, one step.func New(seed, seq uint64) *PCG32 { r := &PCG32{inc: seq<<1 | 1} // odd increment: the LCG has full period 2^64 r.Uint32() r.state += seed r.Uint32() return r}
// Uint32 is one step; the output is a permutation of the OLD state.func (r *PCG32) Uint32() uint32 { old := r.state r.state = old*mult + r.inc // uint64 arithmetic wraps mod 2^64, as the spec needs xs := uint32(((old >> 18) ^ old) >> 27) // XSH: xorshift high bits down rot := uint32(old >> 59) // RR: the top 5 bits pick the rotation return xs>>rot | xs<<((-rot)&31)}
// Float64 is uniform_f64: 27 bits from the first draw, 26 from the second, 53 in all.func (r *PCG32) Float64() float64 { a := uint64(r.Uint32() >> 5) b := uint64(r.Uint32() >> 6) return float64(a<<26+b) * (1.0 / 9007199254740992.0) // 2^-53: exactly representable, [0, 1)}The hand-worked example is New(0, 54): inc = 109, one step from state 0 gives 109, plus seed 0, one more step gives 0x9AE4F7499BA72696, and the first output permutes that state.
Bounded draws reject the biased zone so that every value of is equally likely:
func (r *PCG32) Below(n uint64) uint32 { // ... 1 <= n <= 2^32 t := ((1 << 32) - n) % n // the 2^32 mod n smallest draws would favor small results for { x := uint64(r.Uint32()) if x >= t { return uint32(x % n) } }}Shuffle is Fisher-Yates from the end (for i := n - 1; i >= 1; i--, j := Below(i + 1)), and Stream(seed, purpose) is New(ChildSeed(seed, purpose), purpose) with SplitMix64’s finalizer, so each purpose gets an independent stream from one seed.
Arrivals
Section titled “Arrivals”The schedules in schedule.go are each a few lines:
func (p *poisson) Next() time.Duration { u := p.r.Float64() sec := -math.Log(1.0-u) / p.rate // inverse CDF of Exp(rate); 1 - u is in (0, 1], so the log is finite return time.Duration(sec * float64(time.Second))}
func (b *burst) Next() time.Duration { i := b.i b.i++ if i > 0 && i%b.size == 0 { // the first burst starts at once return b.every } return 0}and have the same distribution, but only the first matches the other ports draw for draw, which is what makes seeded runs repeat.
Reading the stream
Section titled “Reading the stream”On the response side, run.go reads SSE line by line and stamps each data line before parsing it:
func ReadStream(r io.Reader, now func() time.Time) ([]Event, error) { br := bufio.NewReader(r) var out []Event errEvent := false for { line, err := br.ReadString('\n') if len(line) > 0 { line = strings.TrimRight(line, "\r\n") switch { case line == "" || strings.HasPrefix(line, ":"): // event separators and ": ping" case strings.HasPrefix(line, "event:"): errEvent = strings.TrimSpace(line[len("event:"):]) == "error" case strings.HasPrefix(line, "data:"): at := now() // stamped before parsing: parse time is not latency data := strings.TrimSpace(line[len("data:"):]) if data == "[DONE]" { return append(out, Event{At: at, Kind: Done}), nil } // ... an error event or an "error" member is an error; // parseChunk classifies Content, Usage, or nothing (the role chunk) } } if err != nil { if errors.Is(err, io.EOF) { return out, errors.New("stream ended before [DONE]") // a cut stream is a failure } return out, err } }}Measure turns the events into one sample, relative to the intended start:
func Measure(start time.Time, events []Event) Sample { s := Sample{Start: start} var prev time.Time usage := -1 for _, ev := range events { switch ev.Kind { case Content: if s.Tokens == 0 { s.TTFT = ev.At.Sub(start) // from the intended send time } else { s.ITL = append(s.ITL, ev.At.Sub(prev)) } prev = ev.At s.Tokens++ case Usage: usage = ev.Tokens case Done: s.E2E = ev.At.Sub(start) // [DONE], not the last token } } if s.Tokens == 0 { s.Err = errors.New("no content before [DONE]") return s } if usage >= 0 { s.Tokens = usage // a chunk can carry several tokens: the server's count wins } return s}On the hand-worked request, content at 120, 150, and 190 ms gives TTFT 120 and ITL [30, 40]; [DONE] at 200 gives E2E 200; usage 3 gives TPOT ms (Sample.TPOT needs at least 2 tokens).
The open-loop runner
Section titled “The open-loop runner”The runner is the heart of the section, and it is short:
next := startfor i := 0; cfg.MaxRequests <= 0 || i < cfg.MaxRequests; i++ { // The intended time comes from the schedule alone, never from when // the previous request finished. next = next.Add(cfg.Schedule.Next()) if cfg.Duration > 0 && !next.Before(start.Add(cfg.Duration)) { break // an arrival exactly at the end is not sent } if wait := next.Sub(clk.Now()); wait > 0 { select { case <-clk.After(wait): case <-ctx.Done(): } } if ctx.Err() != nil { break } wg.Add(1) go func(i int, at time.Time) { // one goroutine per request: a stalled one blocks nothing defer wg.Done() s := one(ctx, &cfg, client, clk, i, at) // `at` is the intended time, passed through to Measure mu.Lock() samples = append(samples, s) mu.Unlock() }(i, next)}wg.Wait()next accumulates gaps from start, not from clk.Now(). If the loop wakes late, the next arrival is still at its scheduled time, and wait is negative, so it is sent at once. one sends the request with temperature: 0, stream: true, and include_usage, and treats a non-200 status as a failed sample.
The histogram
Section titled “The histogram”Samples are recorded into histograms from histogram.go:
// bucketOf maps v >= 0 to its bucket index: v itself below 128; else, with// e = floor(log2 v) and shift = e - 7, the top 8 bits sub = v >> shift (in// [128, 256)) select index (shift + 1) * 128 + (sub - 128).func bucketOf(v int64) int { if v < subCount { return int(v) // exact below 128 } e := bits.Len64(uint64(v)) - 1 shift := e - SubBits sub := v >> uint(shift) return (shift+1)*subCount + int(sub-subCount)}
func (h *Histogram) Quantile(q float64) int64 { // ... empty -> 0; q clamped to [0, 1] // ceil(q * n), forgiving the rounding of q * n: 0.55 * 100 is // 55.00000000000001 in float64, and its rank is 55, not 56. rank := uint64(math.Ceil(q*float64(h.n) - 1e-9)) // ... clamp rank to [1, n] var cum uint64 for i, c := range h.counts { cum += c if cum >= rank { _, hi := bounds(i) if hi > h.max { hi = h.max // the upper bound, but never past the true maximum } return hi } } return h.max}Check it against the hand-worked values. 1000 has bits.Len64 = 10, so , shift 2, sub , bucket . The median of four values has rank 2, found in that bucket, reported as 1003; p100 is in and clamped to the max, 2000. Mean uses the exact running sum, not bucket midpoints. Merge adds counters and combines count, sum, min, and max, so merged per-worker histograms equal one histogram that saw everything.
The report
Section titled “The report”Finally, BuildReport folds the samples into the report:
for _, s := range samples { if s.Err != nil { r.Errors++ continue // never in a latency histogram } hs["ttft_ms"].Record(int64(s.TTFT)) hs["e2e_ms"].Record(int64(s.E2E)) if t, ok := s.TPOT(); ok { hs["tpot_ms"].Record(int64(t)) } for _, d := range s.ITL { hs["itl_ms"].Record(int64(d)) } tokens += s.Tokens if good(s, cfg.SLO) { nGood++ }}if r.Requests > 0 { r.ErrorRate = float64(r.Errors) / float64(r.Requests) r.Goodput = float64(nGood) / float64(r.Requests) // over ALL requests, failures included}good checks every SLO key it recognizes (ttft_ms_p95 bounds ttft_ms, and so on); for ITL it uses the request’s worst gap. Each histogram’s non-empty buckets go into the report as [upper_ms, count] rows, which the regression gate reads back as samples.
The entry point go/cmd/loadgen parses --target --model --prompt --max-tokens --rate --duration --mode poisson|constant|burst --burst --seed --out (and the goodput bounds --slo-ttft-ms, default 500, and --slo-tpot-ms, default 60). It builds the schedule (Poisson(rate, rng.Stream(seed, rng.PurposeSample)) for poisson), calls loadgen.Run, writes the report to --out, and prints it as the final stdout line.
The full solution is pcg32.go, schedule.go, histogram.go, and run.go.
Why this design
Section titled “Why this design”The cost is small. Record is : a bit length, a shift, an increment. Quantile scans at most 7424 counters, about 58 KiB of memory per histogram regardless of how many values it holds. One goroutine per request is cheap in Go at the rates a single engine sustains.
Why not store all values and sort? That is exact, but memory grows with the run, and per-worker results cannot be merged without shipping every value. The log-linear histogram is within (0.78%) of exact, always on the pessimistic side, and merges by addition.
Why not a closed loop with N workers? It is simpler and is what many tools do, but its send rate drops when the server stalls, so the stall is recorded once instead of in every request it delayed. The open loop measures what users would see at the offered rate.
One tradeoff is accepted: goroutines are unbounded. If the server stops answering at a high rate, in-flight requests pile up in the client. That is the honest behaviour of an open loop; the run’s ctx and the duration bound it.
What can go wrong
Section titled “What can go wrong”The tests run on a fake clock: time is moved by hand and the runner’s timers are awaited, so every latency is an exact number. The generator is checked against the shared PCG32 golden vectors of parity/rng. A correct solution handles each of these cases:
| Case | Checks | Why it matters downstream |
|---|---|---|
| The hand histogram | the worked buckets, quantiles, extremes, mean | the worked example |
| Quantiles within 1/128 of exact | log-uniform values over six decades: every reported percentile in | the report’s 1% promise |
| Rank rounding | p7 and p55 of 1..100 are 7 and 55 | float rounding in the rank |
| Edge values | empty, negative, MaxInt64 | no panic on odd input |
| Merge equals recording everything | merged buckets, count, extremes, mean equal one histogram | per-worker histograms merge exactly |
| The hand PCG32 | the worked state and output; O’Neill’s demo line | the generator is the spec’s |
| Golden vectors | 1024 u32, 64 uniforms bit for bit, 64 normals within 4 ulp, three seeds | parity/rng across four languages |
Below rejects the biased zone | bounded draws for n = 10, 3·2^30, 2^32, 1 from the golden stream | unbiased permutations in the regression gate |
| Fisher-Yates from the end | the spec’s swap order | the same permutation in every language |
| Child seeds and streams | SplitMix64 sub-streams | independent purposes |
| Poisson gaps by inverse CDF | 32 gaps equal for the golden uniforms | seeded runs repeat |
| Poisson rate and shape | 20000 gaps: mean within 2%, 63.2% below the mean | the offered rate is the reported rate |
| Constant and burst gaps | constant and burst gaps, names, rates | the other two modes |
| Stamping each event | one now() per data event; role chunk, ping, usage, [DONE] | TTFT and ITL timestamps |
| Broken streams | cut stream, SSE error event, error object | outages show in error_rate |
| The hand request | the worked request | the worked example |
| Usage over chunks, empty rejected | usage over chunks; no content is an error; TPOT needs 2 tokens | multi-token chunks |
| No coordinated omission | five requests sent on schedule into a stalled engine, each charged from its intended start | the reason for open loop |
| Errors counted, only successes recorded | 429s counted as errors, never as latencies; tokens per second | fast errors do not flatter p50 |
| Duration ends arrivals | the arrival at exactly the end is not sent | back-to-back runs |
| Goodput and report shape | goodput over all requests; the schema’s keys exactly; histogram rows | the regression gate and every system-level run read it |
Warning: The measurement pitfalls are the ones that matter most, because they make a broken system look healthy. Waiting for each request (a closed loop) makes the send rate collapse during a stall. Charging from the response headers makes a stalled server look fast. Failed requests in the histograms let fast 429s pull p50 down during overload. Goodput over successes only lets an engine that fails half its requests report 100% goodput. The solution guards these with one goroutine per request and arrivals from the schedule,
Measuresubtracting the intended start passed fromRun,BuildReportskipping samples withErr, and goodput dividing byRequests.
The histogram has two quieter pitfalls. Reporting a bucket’s lower bound gives quantiles below the true value, so the SLO looks met when it is not; Quantile returns the bucket’s upper bound, clamped to the max. A plain makes p55 of 100 values the 56th; the rank is ceil(q * n - 1e-9).
The generator’s pitfalls break cross-language repeatability. An even increment gives a short-period stream, different from every other port; New sets inc = seq<<1 | 1. Taking 27 bits from the second draw makes uniforms differ from Python’s; Float64 takes >> 5 (27 bits) then >> 6 (26 bits). x mod n without rejection favors small values in permutations; Below redraws below (2^32 - n) mod n. Fisher-Yates from the front gives a different permutation than the other ports; Shuffle runs i from n - 1 down to 1. Using instead of gives the same distribution but different seeded runs; the Poisson gap is -math.Log(1.0-u) / rate.
The stream reader and schedule have the rest. If the first burst waits, a burst run starts late and sends fewer requests; burst.Next returns every only for i > 0. Stamping events late inflates TTFT and ITL by parsing time; ReadStream calls now() before parsing each data line. Counting a cut stream as a success makes outages vanish from error_rate; EOF before [DONE] is an error. And taking E2E from the last token instead of [DONE] shaves a few milliseconds off E2E; E2E is taken at the Done event.
Where this leads
Section titled “Where this leads”The optional next section, run comparison and the regression gate, compares two reports: the histograms become samples for its permutation test, and rng.Stream shuffles them. In AI Engineering, the Go PCG32 (rng.Seeded, rng.Stream, Below, Float64) is reused for k-means++ seeding in retrieval, the bootstrap of the eval runner, and the permutation test of paired A/B experiments.
The {loadgen} entry point is the load behind every system-level run: a burst of 64 against the engine, the gateway run, the production SLO run on kind, and the incident drills a decode worker dies mid-stream and noisy neighbor in Systems Engineering. The server-side histograms from serving metrics and tracing are the other side of the same measurement.
Production tools extend each piece. wrk2 and k6’s constant-arrival-rate executors provide corrected latency at fixed rates with many workers. HdrHistogram adds configurable precision, coordinated-omission correction, and logs. The vLLM and SGLang benchmark scripts and LLMPerf add dataset-driven prompts, request-rate sweeps, and goodput curves on top of TTFT, TPOT, and ITL. And multi-host load runs distributed generators that merge their histograms, for which the exact merge here is the building block.
Further reading
Section titled “Further reading”- Gil Tene, How NOT to Measure Latency (free talk).
- HdrHistogram (free) and HdrHistogram Go.
- O’Neill, PCG (free).
- Schroeder, Wierman, Harchol-Balter, Open Versus Closed (free).
- wrk2 and k6 arrival-rate executors.
- vLLM benchmarks and LLMPerf.
Key terms
Section titled “Key terms”- Open loop: a load model in which arrivals follow a schedule regardless of how the server responds.
- Coordinated omission: the measurement error of a closed-loop client that stops sending during a stall and so records it once instead of in every request it delayed.
- Intended send time: , the time from which request ‘s latency is charged.
- ITL: inter-token latency, the gap between consecutive content chunks of a stream.
- Log-linear histogram: a histogram with 128 equal buckets per power of two, giving at most 1/128 relative error in fixed memory.
- Nearest-rank quantile: the -th smallest value, here with a allowance for float rounding.
- Goodput: the share of all requests, failures included, that met every SLO bound.
Summary
Section titled “Summary”- Open loop: arrivals follow the schedule whatever the server does, and each request is charged from its intended send time, so a stall shows up in every request it delayed.
- A log-linear histogram keeps 128 buckets per power of two: every quantile is within 1/128 of the exact sorted value, never below it, in fixed memory, and two histograms merge exactly.
- The nearest-rank quantile is the -th smallest value, with an allowance for float rounding: is .
- The Go PCG32 gives the same stream as Python, C, and Rust for the same seed, bit for bit.
- A failed request (an error status, an SSE error event, a stream cut before
[DONE]) counts inerrors, never in a latency histogram; goodput is the share of all requests that met every SLO bound.
Review questions
Section titled “Review questions”-
The engine stalls for 1 s at 10 rps. What does a closed-loop client record, and what does this runner record?
Answer
A closed loop with one worker is stuck on one request for the second and records one slow latency; it then resumes, and the requests users would have sent during the stall never existed. This runner keeps sending at regardless, so about ten requests are in flight during the stall, and each is charged from its own intended start: ten latencies show the stall, with the earliest seeing nearly the full second.
-
Why does
Quantilesubtract1e-9beforemath.Ceil?Answer
in float64 is , so a plain ceiling gives rank 56 and p55 of 1..100 would be 56. The nearest-rank definition is . Subtracting a tiny epsilon forgives the rounding error without changing any rank whose product is genuinely above an integer.
-
Why is a quantile reported as its bucket’s upper bound, and why clamped to the max?
Answer
The upper bound is never below any value in the bucket, so the estimate is never below the exact quantile and at most above it: the error is always pessimistic, so an SLO can only look worse than it is, never better. The clamp keeps p100 (and any quantile in the top bucket) from exceeding the largest value actually seen.
-
Why does
nextaccumulate fromstartinstead of fromclk.Now()after each send?Answer
Adding gaps to the current time would push every later arrival back by however long the loop was late, so a slow client would silently lower the offered rate. Accumulating from
startkeeps exactly; if the loop wakes late,waitis negative and the request goes at once, still charged from its intended . -
An engine fails half of its requests with fast 429s and serves the rest within SLO. What are
error_rateandgoodput?Answer
Both 0.5. The 429s count in
requestsanderrors, are kept out of every histogram (so they do not pull p50 down), and are in goodput’s denominator. Dividing by successes only would report 100% goodput for an engine that turned away half its users. -
Which bucket does the value 1005 ns fall in, and what are its bounds?
Answer
bits.Len64(1005) = 10, so and shift . The top bits are , which selects the bucket . Its width, 4, is of its lower bound, as the design promises.