Skip to content

API performance testing: scenarios, data and thresholds

B2Bnetwork team · · 6 min

System performance

In brief

An API performance test checks whether the interface can handle partner and app traffic with the response times and error rates it promises. A good test starts from API gateway logs, not from the list of endpoints in the documentation: logs give you the mix, response sizes and peak hours. Then you need unique data, tokens and idempotency keys, and thresholds set per operation, because search and writes have different response times and matter differently to the client.

From gateway logs to a list of operations

OpenAPI documentation tells you which operations exist. It does not tell you which are used, how often and with what parameters. The API gateway or server logs know that: calls per minute for each operation, the daily distribution, response size, error rate and the most common parameters.

Such an analysis usually shows that a few operations generate most of the traffic, while a few rare ones generate most of the database load. Both groups go into the test:

GroupExampleWhy it is in the test
Frequent and lightfetching a dictionary, order statusthey form background traffic and load thread pools
Frequent and heavyfiltered search, price calculationthey decide response times at peak
Rare and heavyexport, report, a list with a large page sizethey can lock the database for seconds
Writingplacing an order, a payment, an updatelocks, transactions, downstream calls

It is also worth checking who calls the API. A mobile app, a price comparison site and a partner system have different patterns: the comparison site sends bursts of quote requests, the mobile app refreshes data every time it opens. How to build an API layer that can take this is covered in The bank's API layer.

Scenarios: single calls and chains

A test of a single endpoint in a loop is easy to write but rarely reflects reality. An API client usually runs a chain: log in or get a token, search, fetch details, write. Each step uses data from the previous one. A chain shows effects a single endpoint will not: a cache that helps with details after search, or a lock that a write takes on a record that was read a moment earlier.

In practice a test combines both: a few chains that reproduce the most important client journeys and background traffic from frequent single calls. For a public API an open model works better, where you set the request rate per second, because partners do not wait for a response before sending the next request. A sketch in k6, with thresholds per operation:

export const options = {
  scenarios: {
    kalkulacja: { executor: 'constant-arrival-rate', rate: 120, timeUnit: '1s', duration: '20m', preAllocatedVUs: 200 },
  },
  thresholds: {
    'http_req_duration{operacja:kalkulacja}': ['p(95)<400', 'p(99)<900'],
    'http_req_duration{operacja:polisa}': ['p(95)<1200'],
    'http_req_failed': ['rate<0.005'],
  },
};

Test data, tokens and idempotency

Most false results in API tests come from data. Three traps repeat across many projects:

  1. The same data over and over. A thousand calls for the same customer ID hit the cache and produce great response times. The test needs a pool of IDs with a distribution similar to production, including customers with short and long histories.
  2. A token for every request, or one for the whole test. Fetching a token on every call overloads the authorization server, while one token shared by all virtual clients bypasses limits and sessions. The closest to reality is one token per virtual client, refreshed the way the real app does it.
  3. Writes without cleanup. A test that creates orders changes the database state. After a few runs tables grow and later results get worse. You need a way to restore data before each run.

When the API accepts an idempotency key (a header that makes it safe to repeat a write), the test should send a unique key for each write and check repeats separately. A repeated request must return quickly and must not create a second order. Contract consistency before a performance test is covered by the tests described in API contract testing.

Limits, 429 errors and client behavior

Most APIs limit requests per client. Exceeding the limit usually ends with status 429 (Too Many Requests), defined in RFC 6585, often with a Retry-After header from RFC 9110 that says when to try again. In a performance test, treat limits deliberately:

  • if the test measures system capacity, spread traffic across many test clients so you do not bounce off a single partner's limit;
  • if the test checks the limits themselves, what matters is whether the gateway returns 429 quickly, before the request loads the system behind it;
  • count 429 responses separately from 5xx errors, because they mean different things: the limit works, or the system is failing.

Check the API clients too. A client that retries immediately after an error, with no delay, can turn a brief slowdown into an avalanche. A test with a forced slowdown of one dependency shows whether clients and the gateway behave sensibly.

Thresholds and reading the results

API thresholds are set per operation, in percentiles, together with error rate and throughput. A single threshold for the whole API hides a slow operation among many fast ones. Good sources for thresholds are partner agreements, SLO targets and the times users see in the app. If the agreement with a comparison site says one second, the p99 threshold for quotes has to leave room for the network.

When analyzing results, look at response size and serialization. A list with hundreds of items or deeply nested objects can cost more than the database query itself. Pagination with a sensible limit and returning only the fields needed both help.

An example of such goals, with limits per partner, is the case study An API platform for comparison sites and multi-agencies. At B2Bnetwork, API performance testing is built on gateway logs, and we leave scripts ready to run in CI. The functional side of the same API is covered by API test automation.

Questions and answers

Does an API performance test replace testing the whole application?

Not fully. An API test shows backend capacity and is cheaper to maintain, but it skips what happens in the browser or mobile app: rendering, third-party scripts, the number of calls per screen. The usual approach is a large test at API level and a small test with a few users in a real browser.

How do you test an API that calls external systems?

Dependencies that do not allow test traffic are replaced with stubs that have realistic latency and error rates. That way the test checks your API, not someone else's system. Separately, check behavior when a dependency slows down: timeouts, retries and the circuit breaker.

What response time threshold should an API have?

There is no single number. The threshold depends on who is waiting: a person in an app, a partner system with a contractual timeout or a batch process. Set it per operation, at p95 and p99, and leave headroom for the network and the layers in front of the API.

Should we test the API in production?

Reads sometimes, in a low-traffic window and with tagged test traffic. Writes almost never, because they create real effects at partners and in the books. It is safer to test in an environment with the same configuration and to watch the same metrics in production at peak.

What it looks like in a project

Your system slows down under load? We'll find where it loses time.

Let's talk

Read more: System performance

All in System performance →