Sub topic 15
Benchmarking throughput and latency
This chapter assumes basic Python, HTTP APIs, GPU memory, and vLLM server operation. You should already know how to start an OpenAI-compatible vLLM server and send a chat request.
A benchmark measures how quickly and how much work a server completes. Latency is the time for one request. Throughput is the amount of work completed per unit of time. For LLMs, report both requests per second and generated tokens per second.
What to measure
Two latency values answer different questions:
- Time to first token (TTFT) measures how long the user waits before generation begins. It is affected by prompt length and prefill work.
- End-to-end latency measures the time from sending the request until the final token arrives. It includes TTFT and decode time.
Measure a distribution, not only an average. Report the median, p95, and p99. The p95 is the value below which 95% of requests finish. Tail latency often increases when the server is near its concurrency limit.
| Metric | Meaning | Useful for |
|---|---|---|
| Requests per second | Completed requests divided by elapsed time | Workload capacity |
| Output tokens per second | Generated tokens divided by elapsed time | Generation throughput |
| p95 latency | 95th-percentile completion time | User experience and service limits |
Build a repeatable benchmark
Use a fixed prompt set, fixed max_tokens, and a known concurrency level. Warm up the server first so model loading and compilation do not distort the results. Repeat each test with several concurrency values, such as 1, 4, 8, and 16.
The following client measures end-to-end latency and output-token throughput:
import math
import os
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from openai import OpenAI
client = OpenAI(
base_url=os.getenv("VLLM_BASE_URL", "http://localhost:8000/v1"),
api_key="EMPTY",
)
model = os.getenv("MODEL", "Qwen/Qwen2.5-1.5B-Instruct")
requests_to_send = 20
concurrency = 4
def one_request():
start = time.perf_counter()
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": "Explain caching in two sentences."}],
temperature=0,
max_tokens=64,
)
elapsed = time.perf_counter() - start
tokens = response.usage.completion_tokens if response.usage else 0
return elapsed, tokens
start = time.perf_counter()
with ThreadPoolExecutor(max_workers=concurrency) as pool:
futures = [pool.submit(one_request) for _ in range(requests_to_send)]
results = [future.result() for future in as_completed(futures)]
wall_time = time.perf_counter() - start
latencies = sorted(item[0] for item in results)
p95 = latencies[max(0, math.ceil(len(latencies) * 0.95) - 1)]
tokens = sum(item[1] for item in results)
print(f"requests/s: {len(results) / wall_time:.2f}")
print(f"output tokens/s: {tokens / wall_time:.2f}")
print(f"median latency: {latencies[len(latencies) // 2]:.3f}s")
print(f"p95 latency: {p95:.3f}s")This script measures completed responses, not TTFT, because the standard non-streaming call returns only after generation finishes. Add streaming timestamps when TTFT matters.
Interpret results
Increasing concurrency usually raises throughput until GPU resources are busy. After that point, queueing increases p95 latency while throughput levels off. Compare configurations only when prompt lengths, output limits, sampling settings, and hardware are identical.
The /metrics endpoint provides server-side measurements:
curl -s http://localhost:8000/metrics | grep '^vllm:'Use these metrics to distinguish client delay from server queueing. Track running and waiting requests, prompt-token throughput, generation-token throughput, and GPU cache usage during each test.
Exercise
Throughput stops increasing when concurrency rises from 8 to 16, but p95 latency doubles. What does this suggest?
Show answer
The server has reached a capacity limit. Additional requests are waiting in the scheduler instead of increasing useful GPU work.