Sub topic 15
Benchmarking throughput and latency
This chapter assumes you can run vllm serve and call it with the OpenAI client, as covered earlier. It also assumes you have read the chapters on memory tuning and monitoring. Here you will measure how fast a server is and learn how to read the numbers.
Why you benchmark
Settings such as gpu_memory_utilization, max_num_seqs, quantization, and tensor parallelism all change speed. Without measurements you cannot tell whether a change helped. A benchmark gives you numbers for one model, one GPU setup, and one traffic shape. Treat it as an experiment: change one setting, rerun the same load, and compare.
Two goals pull against each other:
- Throughput is how much work the server finishes per second. Batch jobs and cost per token depend on it.
- Latency is how long one user waits. Interactive chat depends on it.
Raising concurrency usually raises throughput, because the GPU does more useful work per decode step. It also raises per-request latency, because requests share each step and wait longer in the queue. Benchmarking shows where that trade-off sits for your hardware.
The metrics
Generation has two phases, prefill and decode. Latency is therefore split into several numbers.
| Metric | Meaning | What drives it |
|---|---|---|
| TTFT (time to first token) | Time from sending the request to receiving the first token | Queueing, prompt length, prefill cost |
| TPOT (time per output token) | Average time per token after the first | Decode speed, batch size |
| ITL (inter-token latency) | Gap between consecutive tokens, reported per gap | Same as TPOT, but shows spikes |
| E2E latency | Total time for the whole response | TTFT plus output length times TPOT |
| Request throughput | Completed requests per second | Concurrency, output length |
| Output token throughput | Generated tokens per second across all requests | Batch size, memory, GPU speed |
Report percentiles, not only averages. The median (p50) shows typical behavior. The p95 and p99 show what your slowest users see. A server with a good mean and a bad p99 still feels unreliable.
Choosing a workload
A benchmark is only as good as its traffic. Set these to match production:
- Input and output lengths. Long prompts stress prefill and the KV cache. Long outputs stress decode. A chat service and a summarization service have very different shapes.
- Arrival pattern. Offline jobs submit everything at once. Online services receive requests spread over time.
- Concurrency. The number of requests in flight at once.
- Prefix sharing. If prompts share a start, prefix caching changes the result. Random prompts hide that effect.
Benchmarking with the built-in tool
vLLM ships benchmark commands. Recent versions expose them as vllm bench. Older versions use scripts in the repository's benchmarks/ folder, so check vllm bench --help on your install. Start a server first, then run the serving benchmark against it:
vllm serve Qwen/Qwen2.5-1.5B-Instruct \
--max-model-len 4096 \
--gpu-memory-utilization 0.85 \
--port 8000In a second terminal:
vllm bench serve \
--model Qwen/Qwen2.5-1.5B-Instruct \
--base-url http://localhost:8000 \
--dataset-name random \
--random-input-len 512 \
--random-output-len 128 \
--num-prompts 200 \
--max-concurrency 32 \
--percentile-metrics ttft,tpot,itl,e2el \
--metric-percentiles 50,95,99The command sends 200 synthetic requests with 512 input tokens and 128 output tokens each, never more than 32 at a time. It prints request throughput, output token throughput, and the chosen percentiles for each latency metric.
There are two ways to control load:
--max-concurrency Ncaps requests in flight. This is a closed loop: a new request starts only when an old one finishes.--request-rate Rsends R requests per second on average, whether or not earlier ones are done. This is an open loop and resembles real users. If R is above what the server can handle, the queue grows and TTFT climbs without limit.
For engine-level numbers without HTTP, vllm bench throughput runs the engine directly on a batch of prompts, and vllm bench latency measures the latency of single fixed batches. These skip network and API overhead, so they show the engine's ceiling. Use bench serve for numbers that include the real server path.
Measuring from your own client
The built-in tool is the right default. A small script helps when you want to use your own prompts or check what the tool reports. This one streams each response to time the first token, then runs requests in parallel.
import time
from concurrent.futures import ThreadPoolExecutor
from openai import OpenAI
client = OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY")
MODEL = "Qwen/Qwen2.5-1.5B-Instruct"
PROMPT = "Write a short paragraph about the history of the bicycle."
NUM_REQUESTS = 64
CONCURRENCY = 16
def one_request(_):
start = time.perf_counter()
first = None
tokens = 0
stream = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": PROMPT}],
max_tokens=128,
temperature=0.0,
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if first is None and chunk.choices and chunk.choices[0].delta.content:
first = time.perf_counter()
if chunk.usage:
tokens = chunk.usage.completion_tokens
end = time.perf_counter()
if first is None:
first = end
return first - start, end - start, tokens
def percentile(values, p):
ordered = sorted(values)
return ordered[round(p / 100 * (len(ordered) - 1))]
wall_start = time.perf_counter()
with ThreadPoolExecutor(max_workers=CONCURRENCY) as pool:
results = list(pool.map(one_request, range(NUM_REQUESTS)))
wall = time.perf_counter() - wall_start
ttfts = [r[0] for r in results]
e2es = [r[1] for r in results]
total_tokens = sum(r[2] for r in results)
print(f"requests/s: {NUM_REQUESTS / wall:.2f}")
print(f"output tokens/s: {total_tokens / wall:.1f}")
print(f"TTFT p50/p95: {percentile(ttfts, 50):.3f}s / {percentile(ttfts, 95):.3f}s")
print(f"E2E p50/p95: {percentile(e2es, 50):.3f}s / {percentile(e2es, 95):.3f}s")Throughput is total tokens divided by wall-clock time for the whole run, not an average of per-request speeds. Per-request averages hide the effect of batching.
Running a sweep
One run tells you little. Vary one setting and keep everything else fixed. A typical sweep raises --max-concurrency through 1, 2, 4, 8, 16, 32, 64 and records throughput and p95 latency at each step. Plot the results. You will usually see:
- At low concurrency, throughput rises almost linearly while latency barely moves.
- Near the GPU's capacity, throughput flattens.
- Past that point, latency keeps rising with no throughput gain. In the extreme case the KV cache fills and vLLM preempts requests, which hurts TTFT and tail latency.
The best operating point is the highest concurrency that still meets your latency target. Compare the benchmark with the Prometheus metrics from the monitoring chapter. If vllm:num_requests_waiting grows during the run, requests are queueing, and that queueing shows up as TTFT.
Common errors
- Benchmarking a cold server. The first requests include warm-up work such as CUDA graph capture and caches. Send a few requests first and discard them.
- Too few requests. With 20 requests, p99 is one data point. Use at least a few hundred for stable tail numbers.
- Unrealistic prompts. Identical prompts with prefix caching on give flattering TTFT. Random prompts with caching on understate what real shared prefixes save. Match your traffic.
- Client as the bottleneck. A Python client on a busy or distant machine can limit the load before the server does. Run it close to the server and check client CPU use.
- Mixing up metrics. A high output tokens/s figure can coexist with poor TTFT. Check both before declaring a change a win.
- Changing several things at once. If you alter quantization and
max_num_seqstogether, you cannot attribute the difference.
Exercise
You run vllm bench serve at concurrency 8 and again at concurrency 64. Output tokens/s rises from 900 to 1,050, but p95 TTFT rises from 0.2 s to 3.5 s. Your service has a 1 s p95 TTFT target. What should you conclude, and what would you try next?
Show answer
The server is saturated past some concurrency below 64. Going from 8 to 64 adds about 17% throughput but breaks the latency target, so the extra requests mostly wait in the queue. Run intermediate points such as 16 and 24 to find the highest concurrency that keeps p95 TTFT under 1 s, and cap load there. To raise capacity, change one setting at a time, such as quantization, more KV cache space, or more GPUs, and rerun the same sweep.
Exercise
Why can a script that averages each request's tokens-per-second underestimate server throughput at high concurrency?
Show answer
Each request only gets a share of every decode step, so its individual speed is low. The server produces tokens for many requests in the same step, so total throughput is far higher. Server throughput is total output tokens divided by the wall-clock time of the whole run.