# Validating Estimates with Load Tests — Cost & Capacity Estimation

Source: https://www.skillbyai.com/en/capacity-estimation/ce-load-testing

> Design a load test that measures per-server throughput and finds the latency knee.

## Turn assumptions into measurements

An estimate is a hypothesis; a **load test** checks it. The goal is a number you can plug back into the formulas: the **sustainable throughput per instance at an acceptable latency**. Run the real service on production-like hardware and data, then increase load in steps (a **ramp**) while recording throughput, error rate and latency percentiles (p50, p95, p99), plus CPU, memory, connection pool usage and I/O. Plot latency against throughput: it stays flat, then bends sharply upward at the **knee**. Plan capacity somewhat below the knee, not at the maximum throughput reached before errors. Pitfalls include testing with tiny datasets that fit in cache, using one client machine that becomes the bottleneck itself, ignoring think time and realistic request mixes, and running too briefly to see garbage-collection pauses, connection exhaustion or memory leaks.

## Ramp until the curve bends

Start with baseline load, ramp in steps, observe where latency bends sharply at the knee, then plan capacity below that point.

![Four connected stages: baseline load, step ramp, latency knee, plan below knee.](assets/figures/capacity-estimation/section-8-map.svg) — Figure 8.1: baseline load -> step ramp -> latency knee -> plan below knee

## A stepped load test with k6

A k6 script that ramps virtual users in stages and fails the run if p95 latency or the error rate crosses a threshold. Point it at a staging environment, never production without agreement.

```js
import http from 'k6/http';
import { sleep } from 'k6';

export const options = {
  stages: [
    { duration: '2m', target: 50 },
    { duration: '5m', target: 200 },
    { duration: '5m', target: 400 },
    { duration: '2m', target: 0 },
  ],
  thresholds: {
    http_req_duration: ['p(95)<300'],
    http_req_failed: ['rate<0.01'],
  },
};

export default function () {
  http.get('https://staging.example.com/api/menu');
  sleep(1);
}
```

## Find the knee from your own results

Find the highest step where p95 stayed within your target and errors stayed near zero, then plan on maybe 60 to 70% of that. Write the result down as the new per-server assumption.

**Quiz:** Why should you not plan capacity at the maximum throughput a load test reached?

- [ ] Because load tests always overestimate throughput by exactly 2x
- [ ] Because providers forbid running servers at maximum
- [x] Because latency rises sharply near the knee, leaving no headroom for spikes, failures or deploys
- [ ] Because maximum throughput is only reached with spot instances

*Answer:* Because latency rises sharply near the knee, leaving no headroom for spikes, failures or deploys. Near saturation, latency and errors climb rapidly. Planning below the knee keeps latency stable and leaves room for real-world variation.
