# Write k6 Load Tests for an Axum API

Yes. An agent starts your Axum API with its own Postgres, writes k6 scripts for its main flows, ramps them until errors appear and opens a PR with the results.

[Run this on Rush](https://cloud.getrush.ai/run/write-load-tests/axum/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads how the API starts before it writes a script. In an API shaped like launchbadge/realworld-axum-sqlx, `Cargo.toml` pins `axum` `0.3.4`, `sqlx` `0.5` with the `postgres`, `uuid` and `time` features on the Tokio runtime, `tokio` `1.14`, `clap` for configuration, `argon2` for passwords and `jwt` with `hmac` and `sha2` for tokens. `src/config.rs` reads `DATABASE_URL` and `HMAC_KEY` from the environment or the command line, `.env.sample` shows both, and `src/main.rs` opens a pool with `max_connections(50)`, runs `sqlx::migrate!()` on startup and serves on `0.0.0.0:8080`. `.github/workflows/rust.yml` is the model for setup: a `postgres` service, `cargo install sqlx-cli --features postgres`, `sqlx db setup`, then `cargo run` in the background and the Realworld Postman collection against `http://localhost:8080/api`. So in its sandbox the agent starts Postgres in Docker, sets the two variables, runs `sqlx db setup` and `cargo build --release`, and starts the binary before any k6 runs.

Then it maps the routes to flows. `src/http/users.rs` has `POST /api/users`, `POST /api/users/login` and `GET` and `PUT /api/user`; `src/http/profiles.rs` has `GET /api/profiles/:username` and follow and unfollow; `src/http/articles/mod.rs` has `GET` and `POST /api/articles`, `GET /api/articles/feed`, `GET`, `PUT` and `DELETE /api/articles/:slug`, favorite, `GET /api/tags` and the comment routes from `comments.rs`. `src/http/extractor.rs` expects `Authorization: Token <jwt>`, an HMAC-SHA-384 token with a two-week expiry, so every authenticated flow in k6 starts by logging in and keeping the token in the VU's context. `listing.rs` takes `tag`, `author`, `favorited`, `limit` and `offset`, and `migrations/4_article.sql` stores `tag_list` as `text[]` with a GIN index, which makes the tag-filtered listing worth a script of its own. `2_user.sql` stores an Argon2 hash, so registration and login spend CPU on hashing by design and will be the first flow to slow down.

The agent writes one script per flow under `load/`: `signup-login.js` (register a unique user per iteration from `__VU` and `__ITER`, then log in), `read-articles.js` (list with and without `?tag=`, open one article by slug, read its comments, hit `/api/tags`), and `write-articles.js` (log in, create an article with tags, add a comment, favorite it). Each uses a `ramping-arrival-rate` executor with stages that climb, `check`s on status codes and on the shape of the `user` or `article` object in the body, and thresholds of `http_req_failed` under one percent and a `p(95)` on `http_req_duration` the script records rather than assumes. It runs each script, raises the arrival rate stage by stage, and records the request rate at which the error threshold trips and the `p(95)` just below it. The sandbox is a single machine running both k6 and the API, so the numbers are relative between flows, not a capacity claim, and the `load/README.md` says so.

Nothing in the scripts points at a real deployment: the base URL is an environment variable that defaults to `http://localhost:8080`, and the README shows the Docker, `sqlx db setup` and `cargo run` sequence to reproduce the setup. The agent also leaves the test data where it made it, in the sandbox's throwaway Postgres, and does not add a cleanup route to the API. The pull request adds the three scripts, the README and a table of results, and changes no Rust.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Write k6 load tests for this Axum API and open a pull request. Read Cargo.toml, .env.sample, src/main.rs, src/config.rs, src/http/mod.rs, src/http/extractor.rs, every route module under src/http, the migrations and .github/workflows/rust.yml first. Start Postgres in Docker, set DATABASE_URL and HMAC_KEY, install sqlx-cli, run sqlx db setup, build the API in release mode and start it on its default port. Write one k6 script under load/ for each of the three most important flows, including registration and login, using a ramping-arrival-rate executor, checks on status codes and response shape, and thresholds of http_req_failed below 1% and a p(95) on http_req_duration. Make every user and slug unique per VU and iteration, and read the base URL from an environment variable that defaults to the local address. Run each script and raise the arrival rate until the error threshold trips; record the rate where errors start and the p(95) just below it. Add load/README.md with the setup commands, how to run each script and a results table that says the numbers come from one machine running both k6 and the API. Change no Rust source. Open a pull request with the scripts, the README and the table.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request adds a `load/` directory with three k6 scripts and a README, and nothing under `src/`. Each script declares its executor, stages and thresholds at the top, and the README shows the exact sequence to reproduce the setup: the Postgres container, `sqlx db setup`, `cargo build --release`, the two environment variables and the k6 command with `BASE_URL`.

The results table lists, per flow, the arrival rate where `http_req_failed` crossed one percent and the `p(95)` latency just below it, read from k6's end-of-test summary that the agent pastes into the body. Registration and login are expected to turn over first because of Argon2 hashing; the listing with a tag filter and the write flow give the reviewer two more points of comparison from the same machine.

Before merging, a reviewer runs one script locally with the README's commands to confirm it drives the API and not a stale port, checks that user names and slugs really are unique per iteration (a duplicate `username` is a `422` from `users.rs`, which would show up as errors that are the script's fault), and reads the README's caveat about a single machine so nobody quotes the table as production capacity.

## Run it on every pull request that changes a query

Once the scripts exist, a second automation on the Project with a `github` trigger on `pull_request.opened` and `pull_request.synchronize`, and a prompt that says to run the three scripts in `load/` and compare the table with the one in the README, gives every change to `src/http` or `migrations/` a before-and-after on the same sandbox. The trigger appends the pull request to the prompt, so the agent checks out that branch; `read_only` on the automation keeps it to a comment with the comparison rather than a push.

From your own server, the same comparison runs on demand: `POST /o/{org}/sessions` with the prompt and `https://github.com/your-org/your-app/tree/<branch>` in `resources` starts on that branch, and a `request_id` that is a UUID v5 of the branch and commit keeps a retried request from starting a second run.

## Why run it on Rush?

A load test on a laptop measures the laptop: whatever else is running, the database on the same disk, a k6 that competes with the API for cores. In a Rush sandbox the API, its Postgres and k6 run on a machine that does nothing else, the setup is the one the README describes rather than one person's shell history, and every k6 summary is in the transcript next to the command that produced it.

The scripts are the deliverable and the table is the baseline. A later run on a branch gives a second table from the same kind of machine, which is the comparison a reviewer needs when a query changes; absolute capacity is a question for a staging environment, and the README says so.

## Frequently asked questions

### Does it hit my production API?

No. The scripts read `BASE_URL` from the environment and default to the local address, and the agent runs them only against the API it built and started in its sandbox with its own Postgres. Pointing them elsewhere is a decision you make when you run them.

### Why does login slow down before reads do?

`users.rs` hashes passwords with Argon2 on registration and verifies them on login, which is deliberately expensive CPU work; a read of `/api/articles` is a single SQL query. The signup and login script turns over first on a shared machine, and the table makes that visible.

### Will the scripts leave data behind?

In the sandbox, yes, and it is discarded with the sandbox. The scripts create users and articles with unique names rather than deleting them, and the API has no bulk-delete route; against a shared environment you would run them on a database you can reset.

### Can I use another tool instead of k6?

Yes. Say Artillery, Locust or Gatling in the prompt; the flows, the unique data and the thresholds are the same, only the script syntax changes. The results table and the single-machine caveat stay as they are.

## More Axum jobs

- [Write Integration Tests for an Axum API](https://getrush.ai/run/write-rust-integration-tests/axum/)
- [Write Unit Tests for an Axum + SQLx API](https://getrush.ai/run/write-unit-tests/axum/)
- [Add Response Compression to an Axum API](https://getrush.ai/run/add-compression/axum/)

## The same job on other stacks

- [Write k6 Load Tests for an Express API](https://getrush.ai/run/write-load-tests/express/)
- [Write k6 Load Tests for a FastAPI API](https://getrush.ai/run/write-load-tests/fastapi/)
- [Write Load Tests for a Fastify API](https://getrush.ai/run/write-load-tests/fastify/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
