The goal of API Performance Tests are to conduct load tests that will run broadly across all endpoints of an API and help us understand the distribution of throughput in requests per second – average, peak, etc.
It is important to record response times and resource utilization at average and peak loads. This will allow us to determine system response times, network latency, etc. We should also be able to determine the concurrency and processing overhead of the API. We should measure performance when concurrent instances are instantiated with instructions to run load testing scripts.
Tooling
Vegeta
Vegeta is an easy to use command line tool for API load testing.
https://github.com/tsenart/vegeta
Testing can be done in 3 simple steps:
- Install
$ brew update && brew install vegeta
- Run a list of APIs can be listed in a file called targets.txt
vegeta -cpus 4 attack -targets targets.txt -rate 50 -duration 30s | tee results.bin | vegeta report
- Plot
cat results.bin | vegeta plot > plot.html
One limitation of vegeta is that cookie session are not supported which shouldn’t be an issue if we follow the JWT stateless model that is scalable and avoid sessions.
K6
k6 is another modern load testing tool that allows us to easily create load test scenarios based on virtual users and simulated traffic configurations
- Install
$brew tap loadimpact/k6 && brew install k6
- Run a es6 Javascript that defines which endpoints to test and what custom metrics and thresholds need to be gathered.
k6 run --vus 100 --duration 5m --out json=outputs/result.json k6/script.js
- Plot
Types of Performance Test
- Stress test: Determine what is the maximum number of concurrent users that the system supports with an acceptable user experience.
- Soak test: Used to find problems that arise when a system is under pressure for extended periods of time. Test is run for longer duration and is used to find long term problems such as memory leaks, resource leaks or corruption and degradation that occurs over time
- Spike test: Spike tests are vital to testing how well your API can perform at peak times. This will ensure your API can handle the amount of users coming in a very short amount of time e.g. if you running a holiday ad campaign and you see a significant rise in traffic.