Measure API latency before optimizing it
In this article (3 sections)
Optimization needs a baseline tied to a user-visible boundary. A single local stopwatch run cannot represent tail latency or production load.
Calculate fixture percentiles
The integration lab applies a stated nearest-rank-style teaching calculation to ten authored values.
from integration_cases import latency_case
result = latency_case()
assert result["count"] == 10
assert result["p50_nearest_rank"] == 60
assert result["p95_nearest_rank"] == 120
assert result["max_ms"] == 120
assert result["optimization_done"] is False
assert result["fixture_timings"] is TrueDefine start/end, route, payload size, concurrency, warm/cold path and environment. Measure end-to-end plus database/dependency spans using a monotonic clock. Report distribution, errors and sample count by meaningful slice.
Profile the slow path before changing code. Compare candidates on the same workload and include resource/correctness effects. An average improvement can still worsen p95 or errors.
The FDE for Freshers course connects measurement to observability and cost.
Exercise
Benchmark a local endpoint with three payload sizes and concurrency levels. Identify the slow span, make one change and rerun the identical workload.
Continue learning
This article is part of the FDE integration and deployment foundations sequence. Use the neighbouring tasks when you need the prerequisite or the next application.
- Review the prerequisite or neighbouring task in Add structured request logs and correlation IDs.
- Continue with Create a backup and restore exercise for project data.
Reference: OpenTelemetry metrics conventions.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our FDE for Freshers programme — 6–7 months. Build your engineering foundations, then take AI from discovery to delivery.
Explore FDE for Freshers