Introduction
Load testing is essential to evaluate how a system behaves under expected and peak loads. Traditionally, we rely on metrics like requests per second (RPS), response time, and error rates. However, an insightful approach called Average Load Testing has been discussed recently. This blog explores that concept in detail, providing practical examples to help you apply it effectively.
Understanding Average Load Testing
Average Load Testing focuses on simulating real-world load patterns rather than traditional peak load tests. Instead of sending a fixed number of requests per second, this approach
- Generates requests based on the average concurrency over time.
- More accurately reflects real-world traffic patterns.
- Helps identify performance bottlenecks in a realistic manner.
Setting Up Load Testing with K6
K6 is an excellent tool for implementing Average Load Testing. Let’s go through practical examples of setting up such tests.
Install K6
brew install k6 # macOS
sudo apt install k6 # Ubuntu/Debian
docker pull grafana/k6 # Using Docker
Example 1: Basic K6 Script for Average Load Testing
import http from 'k6/http';
import { sleep } from 'k6';
export let options = {
scenarios: {
avg_load: {
executor: 'constant-arrival-rate',
rate: 10, // 10 requests per second
timeUnit: '1s',
duration: '2m',
preAllocatedVUs: 20,
maxVUs: 50,
},
},
};
export default function () {
let res = http.get('https://test-api.example.com');
console.log(`Response time: ${res.timings.duration}ms`);
sleep(1);
}
Explanation
- The
constant-arrival-rateexecutor ensures a steady request rate. rate: 10sends 10 requests per second.duration: '2m'runs the test for 2 minutes.preAllocatedVUs: 20andmaxVUs: 50define virtual users needed to sustain the load.- The script logs response times to the console.
Example 2: Testing with Varying Load
To better reflect real-world scenarios, we can use ramping arrival rate to simulate gradual increases in traffic
import http from 'k6/http';
import { sleep } from 'k6';
export let options = {
scenarios: {
ramping_load: {
executor: 'ramping-arrival-rate',
startRate: 5, // Start with 5 requests/sec
timeUnit: '1s',
preAllocatedVUs: 50,
maxVUs: 100,
stages: [
{ duration: '1m', target: 20 },
{ duration: '2m', target: 50 },
{ duration: '3m', target: 100 },
],
},
},
};
export default function () {
let res = http.get('https://test-api.example.com');
console.log(`Response time: ${res.timings.duration}ms`);
sleep(1);
}
Explanation
- The
ramping-arrival-rategradually increases requests per second over time. - The
stagesarray defines a progression from 5 to 100 requests/sec over 6 minutes. - Logs response times to help analyze system performance.
Example 3: Load Testing with Multiple Endpoints
In real applications, multiple endpoints are often tested simultaneously. Here’s how to test different API routes
import http from 'k6/http';
import { check, sleep } from 'k6';
export let options = {
scenarios: {
multiple_endpoints: {
executor: 'constant-arrival-rate',
rate: 15, // 15 requests per second
timeUnit: '1s',
duration: '2m',
preAllocatedVUs: 30,
maxVUs: 60,
},
},
};
export default function () {
let urls = [
'https://test-api.example.com/users',
'https://test-api.example.com/orders',
'https://test-api.example.com/products'
];
let res = http.get(urls[Math.floor(Math.random() * urls.length)]);
check(res, {
'is status 200': (r) => r.status === 200,
});
console.log(`Response time: ${res.timings.duration}ms`);
sleep(1);
}
Explanation
- The script randomly selects an API endpoint to test different routes.
- Uses
checkto ensure status codes are 200. - Logs response times for deeper insights.
Analyzing Results
To analyze test results, you can store logs or metrics in a database or monitoring tool and visualize trends over time. Some popular options include
- Prometheus for time-series data storage.
- InfluxDB for handling large-scale performance metrics.
- ELK Stack (Elasticsearch, Logstash, Kibana) for log-based analysis.
Average Load Testing provides a more realistic way to measure system performance. By leveraging K6, you can create flexible, real-world simulations to optimize your applications effectively.
