The goal of API Performance Tests are to conduct load tests that will run broadly across all endpoints of an API to 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 how the concurrency and processing overhead of the API. We should measure performance when concurrent instances are instantiated with instructions to run load testing scripts.
Vegeta is an easy to use command line tool for API load testing.
Testing can be done in 3 simple steps:
$ 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
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 is another modern load testing tool that allows us to easily create load test scenarios based on virtual users and simulated traffic configurations
$brew tap loadimpact/k6 && brew install k6
k6 run --vus 100 --duration 5m --out json=outputs/result.json k6/script.js
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.