Sub topic 7
Benchmarking with Warmup and Percentile Latencies
Assume you know basic Python and NumPy, have an ONNX model, and can create an ONNX Runtime InferenceSession. This chapter measures model execution after the session has been created.
Latency and throughput measure different goals
Latency is the time for one request to complete. It matters for interactive applications, where each user waits for a result.
Throughput is the number of items processed per unit of time. It matters for batch jobs and services that process many requests.
A larger batch often improves throughput because the GPU does more work in parallel. It can also increase the latency of each request. Choose the measure that matches your service requirement.
| Metric | Meaning | Useful for |
|---|---|---|
| p50 latency | The median request time | Typical user experience |
| p95 latency | 95% of requests are this fast or faster | Service-level targets |
| p99 latency | 99% of requests are this fast or faster | Tail behavior |
| Throughput | Items processed per second | Batch processing |
Warm up before timing
The first few inferences can be slower. ONNX Runtime may allocate memory, initialize execution providers, select kernels, or build cached GPU state. Measuring these calls together with steady-state calls gives a misleading result.
Run warmup iterations first, then record many timed iterations. Use percentiles instead of one average. A single slow request can disappear inside an average even though it affects users.
import time
import numpy as np
import onnxruntime as ort
sess = ort.InferenceSession(
"resnet50.onnx",
providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
)
input_name = sess.get_inputs()[0].name
x = np.random.rand(8, 3, 224, 224).astype(np.float32)
def benchmark(session, input_name, x, warmup=20, iterations=200):
feed = {input_name: x}
for _ in range(warmup):
session.run(None, feed)
durations_ms = []
for _ in range(iterations):
start = time.perf_counter()
session.run(None, feed)
durations_ms.append((time.perf_counter() - start) * 1000)
p50, p95, p99 = np.percentile(durations_ms, [50, 95, 99])
mean_ms = np.mean(durations_ms)
throughput = x.shape[0] / (mean_ms / 1000)
return p50, p95, p99, throughput
results = benchmark(sess, input_name, x)
print("p50/p95/p99 latency (ms):", results[:3])
print("throughput (items/s):", results[3])This benchmark measures the time around session.run. That boundary includes work needed to return the output to the host. If you use GPU-resident inputs and outputs with I/O binding, benchmark that path separately because it has different copy behavior.
Read the results
A result such as p50 = 4 ms and p99 = 18 ms means the usual request is fast, but some requests take much longer. Investigate memory pressure, CPU scheduling, provider fallbacks, input transfers, and concurrent workloads before accepting the result.
Exercise
Why should the first inference usually be excluded from a steady-state latency measurement?
Show answer
It may include one-time initialization such as memory allocation, kernel selection, or execution-provider setup, so it does not represent normal request latency.