Performance Profiling and Load Testing
Find real bottlenecks with profilers and load tests instead of guessing, then fix the part that matters.
Advanced · about 5 hours · difficulty 3/5
Learn first: Metrics, Dashboards and Alerting, Indexes and Query Performance
Performance work without measurement is guesswork. The discipline is: measure, find the bottleneck, fix it, measure again.
Load testing
Tools like k6, Locust, Gatling, JMeter or autocannon simulate many users hitting your API.
- Define a realistic scenario (login, browse, checkout) and targets (for example p95 under 300 ms at 200 requests per second).
- Ramp load up gradually to find the knee, where latency starts climbing sharply.
- Run against a production-like environment with realistic data volumes; an empty database lies.
- Watch server metrics during the test to see which resource saturates first: CPU, DB connections, memory or an external API.
Profiling
A profiler shows where CPU time or memory goes inside your process:
- CPU profiles and flame graphs: the widest bars are where time is spent. Node has
--cpu-profand Clinic.js, Python has cProfile and py-spy, Go has pprof, Java has JFR and async-profiler. - Heap snapshots and allocation profiles find memory leaks: objects that keep growing between snapshots.
- Continuous profiling in production (Pyroscope, Parca, cloud profilers) catches issues that only appear under real traffic.
The usual suspects
In most backends the bottleneck is not your language but I/O: N+1 queries, missing indexes, chatty calls to other services, large JSON serialization, synchronous CPU work blocking an event loop, or no caching. Fix these before micro-optimizing loops.
Pitfalls
- Optimizing code that accounts for 1 percent of the time.
- Load testing from a laptop that becomes the bottleneck itself.
You are ready to move on when you can load test an endpoint, find its bottleneck with a flame graph or query plan, fix it and show the before and after numbers.
Key concepts
- Load testing with k6 or Locust
- Finding the saturation point
- CPU profiles and flame graphs
- Heap snapshots and memory leaks
- Event loop blocking: Critical for Node.js
- Continuous profiling
- Benchmarking methodology
Mini project
Write a k6 script for your three busiest endpoints, find the first bottleneck, fix it using a profile or query plan, and publish a short before/after report with p95 and throughput.
Check yourself
Your p95 latency doubles under load while CPU stays at 30 percent. What are the likely bottlenecks?
Free resources
- Grafana k6 documentation (Grafana Labs)
- Flame Graphs (Brendan Gregg)
- Profiling Node.js applications (Node.js)