How Stress Testing Can Make More Attractive Systems ?
Introduction
Stress testing is a critical aspect of performance testing that evaluates how a system performs under extreme loads. Unlike load testing, which simulates expected user traffic, stress testing pushes a system beyond its limits to identify breaking points and measure recovery capabilities.
In this blog, we will explore stress testing using K6, an open-source load testing tool, with detailed explanations and full examples to help you implement stress testing effectively.
Why Stress Testing?
Stress testing helps you
- Identify the maximum capacity of your system.
- Detect potential failures and bottlenecks.
- Measure system stability and recovery under high loads.
- Ensure infrastructure can handle unexpected spikes in traffic.
Setting Up K6 for Stress Testing
Installing K6
# macOS brew install k6 # Ubuntu/Debian sudo apt install k6 # Using Docker docker pull grafana/k6
Understanding Stress Testing Scenarios
K6 provides various executors to simulate different traffic patterns. For stress testing, we mainly use
- ramping-vus β Gradually increases virtual users to a high level.
- constant-vus β Maintains a fixed high number of virtual users.
- spike β Simulates a sudden surge in traffic.
Example 1: Basic Stress Test with Ramping VUs
This script gradually increases the number of virtual users, holds a peak load, and then reduces it.
import http from 'k6/http'; import { sleep } from 'k6'; export let options = { stages: [ { duration: '1m', target: 100 }, // Ramp up to 100 users in 1 min { duration: '3m', target: 100 }, // Stay at 100 users for 3 min { duration: '1m', target: 0 }, // Ramp down to 0 users ], }; export default function () { let res = http.get('https://test-api.example.com'); sleep(1); }
Explanation
- The test starts with 0 users and ramps up to 100 users in 1 minute.
- Holds 100 users for 3 minutes.
- Gradually reduces load to 0 users.
- The
sleep(1)
function helps simulate real user behavior between requests.
Example 2: Constant High Load Test
This test maintains a consistently high number of virtual users.
import http from 'k6/http'; import { sleep } from 'k6'; export let options = { vus: 200, // 200 virtual users duration: '5m', // Run the test for 5 minutes }; export default function () { http.get('https://test-api.example.com'); sleep(1); }
Explanation
- 200 virtual users are constantly hitting the endpoint for 5 minutes.
- Helps evaluate system performance under sustained high traffic.
Example 3: Spike Testing (Sudden Traffic Surge)
This test simulates a sudden spike in traffic, followed by a drop.
import http from 'k6/http'; import { sleep } from 'k6'; export let options = { stages: [ { duration: '10s', target: 10 }, // Start with 10 users { duration: '10s', target: 500 }, // Spike to 500 users { duration: '10s', target: 10 }, // Drop back to 10 users ], }; export default function () { http.get('https://test-api.example.com'); sleep(1); }
Explanation
- Starts with 10 users.
- Spikes suddenly to 500 users in 10 seconds.
- Drops back to 10 users.
- Helps determine how the system handles sudden surges in traffic.
Analyzing Test Results
After running the tests, K6 provides detailed statistics
checks..................: 100.00% β 5000 β 0 http_req_duration......: avg=300ms min=200ms max=2000ms http_reqs..............: 5000 requests vus_max................: 500
Key Metrics to Analyze
- http_req_duration β Measures response time.
- vus_max β Maximum concurrent virtual users.
- http_reqs β Total number of requests.
- errors β Number of failed requests.
Stress testing is vital to ensure application stability and scalability. Using K6, we can simulate different stress scenarios like ramping load, constant high load, and spikes to identify system weaknesses before they affect users.