Measure latency distributions and throughput
Measure latency distributions and throughput
Latency is elapsed time for one request; throughput is completed operations divided by elapsed observation time. A mean can hide slow requests. A percentile describes the distribution: using nearest rank, p95 is the value at position ceil(0.95*N) in sorted samples (one-based). State the percentile convention and sample count; do not average percentiles across separate workers.
Create the files shown below in a fresh directory, then run go test -count=1 -v ./... there. The separator lines are file labels, not source code. Go 1.23 is used for these local examples.
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "math" "slices" "testing" "time")
func percentile(samples []time.Duration, p float64) (time.Duration, bool) { if len(samples) == 0 || !(p > 0 && p <= 1) { return 0, false } x := slices.Clone(samples) slices.Sort(x) return x[int(math.Ceil(p*float64(len(x))))-1], true}func TestLesson(t *testing.T) { x := []time.Duration{100, 1, 2, 3, 4} v, ok := percentile(x, .95) if !ok || v != 100 || x[0] != 100 { t.Fatal(v, ok) } if _, ok := percentile(nil, .95); ok { t.Fatal("empty") } if _, ok := percentile(x, math.NaN()); ok { t.Fatal("NaN") }}The synthetic durations are in nanoseconds. With only five samples, nearest-rank p95 equals the maximum; it is not a robust estimate of a production tail. Clone avoids changing the original samples. For long-running systems use bounded histograms, explicit units and sufficient observations. Merge compatible bucket counts before estimating aggregate percentiles.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonCompute throughput over a shared window
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "testing" "time")
func rate(completed int, elapsed time.Duration) (float64, bool) { if completed < 0 || elapsed <= 0 { return 0, false } return float64(completed) / elapsed.Seconds(), true}func TestLesson(t *testing.T) { r, ok := rate(1500, 3*time.Second) if !ok || r != 500 { t.Fatal(r) } if _, ok := rate(1, 0); ok { t.Fatal("zero interval") }}This fixture uses supplied durations to test arithmetic, not simulated load results. For an actual load experiment use time.Now and time.Since, record completions and errors separately, and include queueing from scheduled arrival to completion. A closed-loop client waits before issuing another request and can hide queueing during stalls (coordinated omission). Report offered rate, concurrency, error rate and measurement window alongside tail latency.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonTry it locally
Use nearest rank for p50 and p99 on 100 synthetic durations 1..100 ms; verify empty input is rejected. Use a fresh module with the same go.mod shown above; run go test -count=1 -v ./...
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Performance engineering 07: Measure latency distributions and throughput
Check the lesson objective before moving on.
What denominator belongs in completed throughput?
How should compatible worker histograms be combined?