Performance & load testing (k6, Gatling, profiling)
Load vs stress vs soak vs spike tests, writing a k6 script with thresholds, percentiles over averages, finding bottlenecks with JFR and async-profiler, and capacity planning.
Test types
| Type | Question | Shape |
|---|---|---|
| Load | Does it meet SLOs at expected peak? | Ramp to target, hold |
| Stress | Where does it break, and how? | Keep ramping past capacity |
| Spike | Can it survive a sudden 10× burst (admission day)? | Jump up instantly |
| Soak | Does it degrade over hours (leaks, bloat, pool exhaustion)? | Moderate load for 4–24 h |
A k6 script
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '2m', target: 100 }, // ramp to 100 virtual users
{ duration: '5m', target: 100 }, // hold
{ duration: '1m', target: 0 },
],
thresholds: {
http_req_duration: ['p(95)<300', 'p(99)<800'], // fail the run if SLO breached
http_req_failed: ['rate<0.01'],
},
};
export default function () {
const res = http.get(`${__ENV.BASE}/api/hostels?near=6.67,-1.57&radius=2000`);
check(res, { 'status 200': (r) => r.status === 200 });
sleep(1); // think time
}
Run it with k6 run -e BASE=https://staging.api script.js, and never against production without a plan.
Read percentiles, not averages
An average of 120 ms can hide a p99 of 4 s, and 1% of users wait four seconds. Track p50, p95, p99 and max, plus throughput and error rate. Watch the knee of the curve, where latency suddenly climbs as you approach saturation.
Finding the bottleneck
- Metrics during the test. CPU, GC pauses, Hikari active and pending, Tomcat threads, DB CPU, slow queries.
- Profilers. Java Flight Recorder (
-XX:StartFlightRecording, view it in JDK Mission Control) and async-profiler flame graphs show where CPU and allocations go. - DB.
pg_stat_statementsduring the run. - The usual culprits:
- N+1 queries and missing indexes.
- A small connection pool, or a big one that overwhelms the DB.
- Synchronous external calls.
- Excessive JSON or logging.
- Lock contention on hot rows.
ReportBenchmarkITA performance test inside the test suite: report generation over a large ledger must stay within a budget. It catches regressions on every build, which is cheaper than finding them in production.
Tools
k6 (JS, developer-friendly), Gatling (Scala/Java DSL), JMeter (GUI, older), Locust (Python), wrk/hey (quick HTTP benchmarks), and JMH (micro-benchmarks for Java methods).