Benchmarking with wrk or oha

Learn how to measure an Axum server with wrk, how to read requests per second and latency, how oha differs, and how to avoid fooling yourself with the numbers

Benchmarking means measuring how a server behaves under load: how many requests it answers per second, and how long each request takes. wrk is a small C program that opens many connections at once and hammers a URL for a set time, then reports the totals. oha is a Rust program that does the same job with a friendlier output, including latency percentiles. This page runs wrk against a real Axum server, reads the numbers it prints, and explains what they do and do not mean. It also says plainly where oha could not be run.

Where This Fits

A server that feels fast with one user can fall over with a hundred. The only way to know is to send a hundred requests at once and watch. A benchmark is that watch, done repeatably: same server, same route, same load, same duration, and the numbers written down. Without it, every performance decision is a guess, and the guess is usually wrong in the direction of the thing that was most recently changed.

Think of a road test for a car. One drive down an empty road says nothing about rush hour. A benchmark is rush hour on demand: many cars, same road, timed. The numbers describe that road under that traffic. They say nothing about a different road, a different car, or a different time of day, and a benchmark that pretends otherwise is lying with numbers.

The Smallest Working Version

Benchmarking needs a server to hit. The books API from the earlier topics is enough: one GET route returning a small JSON list. Build it in release mode, because a debug build is unoptimised and its numbers describe the debug build, not the server that would actually run.

cargo build --release
./target/release/books-api

Install wrk from the system packages:

apt-get install -y wrk
wrk --version

The version used to check this page was wrk 4.1.0. Then run the smallest benchmark: two threads, ten connections, five seconds, against the books route.

wrk -t2 -c10 -d5s http://127.0.0.1:3000/books

On the machine used to check this page, the output was:

Running 5s test @ http://127.0.0.1:3000/books
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    15.26ms   20.45ms  106.84ms   80.64%
    Req/Sec     0.99k   441.38     2.47k    71.43%
  9802 requests in 5.02s, 1.56MB read
Requests/sec:   1952.47
Transfer/sec:    318.42KB

Read it line by line. 2 threads and 10 connections is the load that was applied. Latency is the time from sending a request to receiving the full response: 15.26 milliseconds on average, with a worst case of 106.84 milliseconds in this run. Req/Sec is the throughput each thread sustained. 9802 requests in 5.02s is the total work done. Requests/sec: 1952.47 is the headline number: the server answered about 1950 requests per second under this load. Transfer/sec is the bytes read back, which follows from the response size and the request count.

Working Setup:

If wrk prints a Requests/sec line and a non-zero request count, the benchmark ran and the server answered. If it prints connection errors or zero requests, the server is not listening on that address, or the URL is wrong. Check the server first, then the URL.

The Flags, One at a Time

The command has three flags, and each one changes the load in a different way.

-t: threads

wrk -t2 -c10 -d5s http://127.0.0.1:3000/books

-t2 starts two worker threads inside wrk. Each thread runs its own event loop and its own share of the connections. More threads let wrk generate more load, up to the number of CPU cores on the benchmarking machine. Beyond that, extra threads compete with each other and the numbers get worse, not better. Two threads on a four core machine is a reasonable start.

-c: connections

-c10 opens ten connections to the server and keeps them open, sending the next request on a connection as soon as the previous answer arrives. Ten connections is light load. A hundred is heavy. A thousand is a stress test that also measures how the operating system handles many open sockets. The connection count is the single biggest lever on the headline number, and it is the number most often left out when results are quoted, which makes the quote meaningless.

-d: duration

-d5s runs the test for five seconds. Five seconds is short, and short runs are noisy, as the next section shows. Thirty seconds is a better default for a number that will be quoted or compared. The first second or so of any run includes warm-up: the server's caches are cold, the CPU governor is waking up, and the numbers are worse than the steady state. A longer run dilutes the warm-up.

Two more flags are worth knowing. --latency adds a percentile table to the output, showing the 50th, 75th, 90th, and 99th percentile latencies, which say far more about the user experience than the average does. --timeout sets how long wrk waits for a response before counting it as an error, so a hung server shows up as timeouts instead of silence.

The Same Command Twice Gives Different Numbers

This is the most important section on the page, because it is the one that stops a beginner from believing a single number. The exact same command, run twice in a row against the same server on the same machine, gave these two results while checking this page:

Run 1:  9802 requests in 5.02s, Requests/sec: 1952.47, Latency avg 15.26ms
Run 2: 21435 requests in 5.01s, Requests/sec: 4274.60, Latency avg 13.33ms

The second run answered more than twice as many requests as the first. Nothing about the server changed. The server was a debug build running on a shared virtual machine, where other work competes for the same CPUs, and the first run included warm-up. Both numbers are real measurements of the same command, and neither is the speed of the server. The speed of the server is a range, and a single five second run does not establish it.

Three habits follow from this. Run the benchmark more than once and report the range, not the best run. Use a release build, because a debug build is slower and noisier. And run the benchmark on a quiet machine, or at least say what else the machine was doing. A number without its conditions is a story, not a measurement.

Never Quote a Single Run:

A single Requests/sec number from one five second run is noise with a decimal point. Run it several times, use a release build, keep the load the same, and report the range. If two runs disagree by a factor of two, the benchmark setup is the problem, not the server.

Changing the Load

The shape of the load matters as much as its size. The same server, hit with four threads and a hundred connections instead of two threads and ten, answered:

wrk -t4 -c100 -d5s http://127.0.0.1:3000/books
  36603 requests in 5.09s, Requests/sec: 7193.09, Latency avg 21.45ms

Throughput went up, from about 4300 to about 7200 requests per second, because more connections keep the server busier. Average latency went up too, from about 13 milliseconds to about 21 milliseconds, because each request now waits behind more requests. That trade is the normal shape of a loaded server: more throughput, worse latency, and the two numbers must be read together. A benchmark that reports throughput without latency is hiding the cost. A benchmark that reports latency without throughput is hiding the scale.

The right load for a benchmark is the load the server will actually see. A public API with a hundred simultaneous users should be tested with a hundred connections. A private service with five callers should be tested with five. Testing with a thousand connections when the real load is ten measures a situation that will never happen, and optimising for it is wasted work.

Reading Latency Properly

The average latency hides the experience of the slowest users. Averages are pulled down by the many fast requests and pulled up by a few slow ones, and the result describes neither group. Percentiles describe the groups. The 50th percentile, written p50, is the middle request: half were faster, half were slower. The 99th percentile, p99, is the request that only one in a hundred was slower than. Users feel p99, because one slow request in a hundred is the one they remember, and a page that makes ten requests will hit a p99 experience on most loads.

wrk prints percentiles with the --latency flag:

wrk -t2 -c10 -d30s --latency http://127.0.0.1:3000/books

The output adds a table of percentile latencies to the usual summary. Read p50 for the typical experience, p99 for the bad experience, and the maximum for the worst thing that happened during the run. If p50 is fine and p99 is terrible, the server is usually fine and occasionally stuck, and the next question is what the stuck requests were doing. That question belongs to profiling, which is the previous topic.

oha, and Why It Was Not Run Here

oha is a load generator written in Rust, and it does the same job as wrk with a different interface. Its basic form is:

oha -n 1000 -c 10 http://127.0.0.1:3000/books

The flags mirror wrk: -n is the total number of requests to send, -c is the number of connections to keep open, and -z runs for a duration instead of a count, as in oha -z 30s -c 10 URL. It prints a summary with requests per second and a latency percentile table by default, including p99, which wrk needs a flag for. It can send other methods with -m POST, attach a body with -d, set headers with -H, and write machine readable output with --json. It also shows a histogram of latencies, which makes a bimodal distribution, fast requests and slow requests mixed together, visible at a glance.

None of that was run while checking this page, and the reason is concrete: oha is not installed on this machine, and installing it failed. cargo install oha attempted to build version 1.17.0 and the build failed to compile. No oha binary exists here, no oha numbers were measured, and no oha output appears on this page. The flags above come from the tool's documented interface, not from a run. On a machine where oha installs cleanly, the same books server and the same load give the same kind of numbers as wrk, and the choice between them is a matter of taste and of which output is easier to read.

Two Tools, One Job:

wrk and oha both generate load and report throughput and latency. wrk is the older C tool, widely installed, with Lua scripting for complex scenarios. oha is the newer Rust tool, with percentiles and a histogram in its default output. Pick one, learn its flags, and use it consistently, because numbers from two different tools under two different loads cannot be compared.

What wrk Can and Cannot Tell You

wrk measures the server as a client sees it: requests per second and latency, under a given load, for a given route. That is the whole job, and it does it well. What it cannot do is say why the numbers are what they are. A slow route could be slow because of the database, the JSON parsing, the handler logic, or the network, and wrk cannot distinguish them. That question belongs to profiling. A benchmark says how fast. A profile says why.

wrk also measures the benchmarking machine as much as the server. If wrk itself runs out of CPU, the numbers describe wrk, not the server. The sign is a benchmark where adding connections stops raising throughput while CPU on the benchmarking machine is saturated. The fix is to run wrk on a different machine from the server, or to lower the load until wrk is no longer the bottleneck. Benchmarking a server from the same machine that runs it, as this page does for convenience, is fine for learning and for rough comparisons, and it is not how production numbers should be taken.

Benchmarking POSTs and Other Methods

A GET benchmark is the simplest case. Real APIs take writes, and writes are usually slower than reads, because they touch the database and cannot be cached. wrk sends a POST with a Lua script, which is a small program that tells wrk what request to build:

-- post.lua
wrk.method = "POST"
wrk.body = '{"title":"Dune","author":"Frank Herbert"}'
wrk.headers["Content-Type"] = "application/json"
wrk -t2 -c10 -d5s -s post.lua http://127.0.0.1:3000/books

The script sets the method, the body, and the header for every request wrk sends. oha does the same without a script, with -m POST -d '{"title":"Dune"}' -H "Content-Type: application/json". Either way, the important thing is to benchmark the method the clients actually use. A GET benchmark says nothing about POST performance, and a server that is fast at reads and slow at writes will look fast until the first real write arrives.

Do Not Benchmark Against a Real Database:

A load test that creates data will create a lot of it. A thousand POSTs a second for thirty seconds is thirty thousand rows, and a DELETE benchmark deletes real rows. Run load tests against a throwaway database or a test server, never against production data. The numbers are the same, and the data survives.

What the Flow Looks Like

wrk starts 2 threads, opens 10 connections
        |
        v
Each connection sends a request, waits for the answer
        |
        v
Server routes the request, handler runs, answers
        |
        v
wrk records the latency, sends the next request
        |
        v
After 5 seconds wrk stops and totals the counts
        |
        v
Requests/sec, latency average and max, transfer rate

The loop is the measurement. Everything wrk reports is a summary of that loop, and the loop is only as representative as the route and the load chosen for it.

Testing It

A benchmark is not a unit test, and it should not live in the test suite. What belongs in the test suite is a test that the route being benchmarked still works, so a benchmark run is never measuring a broken route by accident.

tests/app_test.rs
use axum::{
    body::Body,
    http::{Request, StatusCode},
};
use books_api::app;
use tower::ServiceExt;

#[tokio::test]
async fn books_route_answers_ok() {
    let response = app()
        .oneshot(
            Request::builder()
                .uri("/books")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
}

The test needs tower as a dev dependency:

Cargo.toml
[dev-dependencies]
tower = { version = "0.5", features = ["util"] }

The benchmark itself belongs in a script or a document, not in cargo test, because it takes seconds to run, needs a running server, and produces numbers that change from run to run. A test that asserts a benchmark number would fail on a busy machine and pass on a quiet one, which is the opposite of what a test should do. Keep the benchmark as a command to run by hand, with its conditions written next to its numbers.

Common Beginner Mistakes

Benchmarking the debug build

A debug build can be several times slower than a release build, and its numbers describe unoptimised code. The fix is one flag: cargo build --release, and run the release binary. Every number on this page that came from a debug build is labelled as such, because the build profile is part of the measurement.

Quoting the best run

The fastest run is the luckiest run. Reporting it as the speed of the server is a selection bias: the number was chosen because it was high, not because it was representative. Report the range across several runs, or the median, and say how many runs were done. The range on this page, about 1950 to about 4300 requests per second for the same command, is the honest answer for that setup.

Benchmarking with one connection

One connection measures the latency of a single request, not the throughput of the server. A server that answers one request in 15 milliseconds can still answer thousands per second, because it handles requests in parallel. Throughput needs parallel connections. Latency needs one connection and many repetitions. The two measurements answer different questions, and one connection cannot answer both.

Forgetting the warm-up

The first seconds of a run include cold caches and a cold CPU. A five second run is mostly warm-up. A thirty second run dilutes it. A benchmark that starts measuring from the first request measures the warm-up, and the numbers are worse than the steady state. Longer runs and repeated runs are the cure.

Final Complete Example

Here is everything from the page put together: the commands to build, run, and benchmark the server, and the numbers measured while checking this page.

cargo build --release
./target/release/books-api
wrk -t2 -c10 -d5s http://127.0.0.1:3000/books
wrk -t4 -c100 -d5s http://127.0.0.1:3000/books
wrk -t2 -c10 -d30s --latency http://127.0.0.1:3000/books

Measured while checking this page, against a debug build on a shared virtual machine, so the numbers are noisy and the range matters more than any single value:

2 threads, 10 connections, 5s, run 1:  9802 requests,  1952.47 req/sec, latency avg 15.26ms
2 threads, 10 connections, 5s, run 2: 21435 requests, 4274.60 req/sec, latency avg 13.33ms
4 threads, 100 connections, 5s:       36603 requests, 7193.09 req/sec, latency avg 21.45ms

Throughput rose with more connections, and average latency rose with it. oha was not run, because it is not installed here and its build failed on this machine, so no oha numbers appear on this page. The oha command for the same load, for a machine where it is installed, is oha -n 1000 -c 10 http://127.0.0.1:3000/books.

Summary

The single most important insight is that a benchmark number is a measurement of a setup, not a property of a server. The setup is the build profile, the machine, the load, the duration, and the route, and changing any of them changes the number. The numbers on this page prove it: the same command measured 1952 and 4274 requests per second on consecutive runs. That does not make benchmarking useless. It makes the conditions part of the result.

The decision framework follows from that. Benchmark with wrk or oha, whichever is installed, against a release build, with the load the server will actually see, for long enough to pass warm-up, several times, and report the range with the latency percentiles beside it. Use the numbers to find regressions and to compare two versions of the same server under the same conditions. Use profiling, not benchmarking, to find out why a number is what it is.