Are you delivering what you promised?
The only time you shouldn’t run Performance tests is if you don’t care how your API works in production.
The idea is to understand how your API will perform under real-world conditions. How your API handles actual traffic and your user’s behavior.
These tests help you to identify and address potential performance bottlenecks.
4 types of performance tests and what they tell you.
Load Tests
This test checks the API under an expected load, which is the usual number of users or transactions it needs to handle.
You will learn how many users or transactions it can support without degradation.
Load tests help you understand everyday operational performance.
Stress Tests
This test determines the limits of your API by increasing the load until it becomes unstable.
This helps you understand how much your application can handle before it fails. But also, how your API recovers from such failures.
Stress tests help you prepare for extreme conditions.
Spike Tests
This test examines how well the API handles sudden traffic increases.
If you are running any kind of promotion, you should be running these tests.
Spike tests ensure your API can handle sudden bursts of traffic and can scale without problems.
Soak Tests
The idea is to check the API's performance under an average load extended over a long period.
It will show you issues that might not appear in shorter tests, like:
memory leaks
slowdowns that develop over time
resource depletion
Soak tests uncover long-term stability and performance issues.
Done with the theory, let’s jump into the practice.
A simple way to implement this is by using k6.
You can install K6 by following the instructions here.
Here are the script examples to run:
Load Tests
import http from 'k6/http';
import { sleep } from 'k6';
export let options = {
stages: [
{ duration: '2m', target: 50 }, // ramp up to 50 users over 2 minutes
{ duration: '3h56m', target: 50 }, // stay at 50 users for 3 hours 56 minutes
{ duration: '2m', target: 0 }, // ramp down to 0 users over 2 minutes
],
};
export default function () {
http.get('https://api.yourservice.com/endpoint');
sleep(1);
}
Stress Tests
import http from 'k6/http';
import { sleep } from 'k6';
export let options = {
stages: [
{ duration: '2m', target: 100 }, // ramp up to 100 users over 2 minutes
{ duration: '5m', target: 100 }, // stay at 100 users for 5 minutes
{ duration: '2m', target: 200 }, // ramp up to 200 users over 2 minutes
{ duration: '5m', target: 200 }, // stay at 200 users for 5 minutes
{ duration: '2m', target: 300 }, // ramp up to 300 users over 2 minutes
{ duration: '5m', target: 300 }, // stay at 300 users for 5 minutes
{ duration: '10m', target: 0 }, // ramp down to 0 users over 10 minutes
],
};
export default function () {
http.get('https://api.yourservice.com/endpoint');
sleep(1);
}
Spike Tests
import http from 'k6/http';
import { sleep } from 'k6';
export let options = {
stages: [
{ duration: '10s', target: 0 }, // baseline
{ duration: '10s', target: 500 }, // spike to 500 users
{ duration: '1m', target: 500 }, // stay at 500 users for 1 minute
{ duration: '10s', target: 0 }, // ramp down to 0 users
],
};
export default function () {
http.get('https://api.yourservice.com/endpoint');
sleep(1);
}
Soak Tests
import http from 'k6/http';
import { sleep } from 'k6';
export let options = {
stages: [
{ duration: '2m', target: 100 }, // ramp up to 100 users over 2 minutes
{ duration: '8h', target: 100 }, // stay at 100 users for 4 hours
{ duration: '2m', target: 0 }, // ramp down to 0 users over 2 minutes
],
};
export default function () {
http.get('https://api.yourservice.com/endpoint');
sleep(1);
}
Key Takeaways
Load Tests ensure your API can handle normal traffic levels.
Stress Tests reveal the API's maximum capacity and recovery behavior.
Spike Tests verify the API's ability to manage sudden traffic surges.
Soak Tests uncover long-term stability and performance issues.
Is your API delivering what your users expect?
Articles I enjoyed this week
How YouTube Was Able to Support 2.49 Billion Users With MySQL by
Code Reviews by
nice little article on testing, thanks for writing this.
Here is my favourite tool to run stress tests, written in Go - https://github.com/tsenart/vegeta