Load testing and capacity
What you will learn
Capacity testing asks how a service handles a defined arrival rate, concurrency, data mix and duration. A microbenchmark isolates an operation; a service test includes queues, database and network waits. Here we use synthetic counts and a bounded queue, not production load.
- Differentiate open and closed load models.
- Account for offered, started and successful work.
- Use stable averages to reason about resource budgets.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Standalone Java 21 functional examples; measurements are separate. JMH is source-reviewed, not executed.
Account for dropped work
public class Main { record Counts(long offered,long started,long succeeded) { Counts { if(succeeded<0||started<succeeded||offered<started) throw new IllegalArgumentException(); } } public static void main(String[] args) { var s=new Counts(1000,900,850); System.out.println(s.offered()-s.started()); System.out.println(s.started()-s.succeeded()); System.out.println(s.succeeded()/(double)s.offered()); }}Expected output:
100500.85Fictional finished window; no in-flight requests. 100 not started, 50 started without success.
Define arrival and success
Closed workloads start new work after earlier work completes; open workloads schedule arrivals independently. In a closed model a stall can reduce offered traffic and hide waiting that independent arrivals would experience (coordinated omission). Track planned arrivals, actual starts, outcomes, timeouts and drops. A saturated load generator can also distort results.
Queues and pools
A bounded queue limits accumulation but can reject work. Thread pools, connections and downstream limits need separate measurements. Little’s law L = λW relates long-run average occupancy, flow rate and residence time for a stable system with consistent boundaries. It is not a sizing guarantee or tail-latency formula. Low CPU can coexist with saturated database pools.
A responsible workload
Use only an authorized isolated target, fixed dataset, caps on rate and concurrency, explicit deadlines and stop rules. Record latency distribution, successful throughput, failures, resource use and offered load. Change one parameter and repeat. This chapter runs no service load generator or database; actual timing is the companion local micro-workload.
Demonstrate bounded admission
public class Main {
public static void main(String[] args) { var queue=new java.util.concurrent.ArrayBlockingQueue<String>(2); System.out.println(queue.offer("a")); System.out.println(queue.offer("b")); System.out.println(queue.offer("c")); System.out.println(queue.size()); }}Expected output:
truetruefalse2Nonblocking offer exposes rejection. This is not a concurrent load test.
Apply Little’s law
public class Main { static double inFlight(double rate,double millis) { if(!Double.isFinite(rate)||!Double.isFinite(millis)||rate<0||millis<0) throw new IllegalArgumentException(); return rate*millis/1000; } public static void main(String[] args) { System.out.println(inFlight(200,50)); }}Expected output:
10.0Synthetic 200 completions/s and mean residence time 50 ms imply 10 average in flight under steady-state assumptions.
Common mistakes
Do not hide timeouts, assume unlimited queues improve capacity, or extrapolate local timing to production. Never load test without authorization.
Practice
For 500 offered, 480 started and 450 successful, print dropped 20, failed 30 and successful fraction 0.9. Reject inconsistent counts.
Expected output:
20300.9Compare with the manual review criteria.
Further reading: Load · Dropped iterations. Independent editorial review remains pending.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Load testing and capacity — knowledge check
Performance engineering reasoning.
A load test schedules 100 arrivals/s even as earlier requests become slow. Which model is this?
Of 1,000 offered requests, 900 start and 850 succeed. What is the success fraction over offered work?
In a stable system, completed throughput is 200/s and mean residence time is 50 ms. What is average occupancy under Little’s law?
A fixed-capacity queue is full. What happens when nonblocking offer(x) receives an admissible non-null item?
Which service-test plan is suitable for the tutorial?