Performance, Load & Stress Testing
Test system performance under various load conditions - measure response times, identify bottlenecks, and determine breaking points.
0 of 5 concepts completed (0%)
What You'll Learn
Tutorial Contents
1.Load Testing with k6
// k6 load test full user journey
import http from "k6/http";
import { check, group, sleep } from "k6";const errorRate = new Rate("errors"); const checkoutDuration = new Trend("checkout_duration");
export const options = { stages: [ { duration: "1m", target: 10 }, // ramp up { duration: "5m", target: 50 }, // sustained load { duration: "2m", target: 100 }, // peak load { duration: "1m", target: 0 }, // ramp down ], thresholds: { http_req_duration: ["p(95)<500", "p(99)<1000"], http_req_failed: ["rate<0.02"], // < 2% errors errors: ["rate<0.05"], checkout_duration: ["p(95)<2000"], }, };
const BASE_URL = "https://staging.example.com";
export default function () {
// 1. Browse products
group("Browse", () => {
const res = http.get(${BASE_URL}/api/products?page=1);
check(res, { "products loaded": (r) => r.status === 200 });
errorRate.add(res.status !== 200);
sleep(1);
});
// 2. Add to cart
group("Add to Cart", () => {
const res = http.post(
${BASE_URL}/api/cart,
JSON.stringify({ productId: "prod-123", quantity: 1 }),
{ headers: { "Content-Type": "application/json" } }
);
check(res, { "added to cart": (r) => r.status === 200 });
sleep(0.5);
});
// 3. Checkout
group("Checkout", () => {
const start = Date.now();
const res = http.post(${BASE_URL}/api/checkout, JSON.stringify({
paymentMethod: "test_card",
}), { headers: { "Content-Type": "application/json" } });
checkoutDuration.add(Date.now() - start);
check(res, { "checkout succeeded": (r) => r.status === 201 });
});
sleep(2); } ```
2.Interpreting Results & Bottleneck Analysis
Key metrics and what they mean: ``` http_req_duration: avg=245ms med=180ms p(90)=380ms p(95)=510ms p(99)=1200ms ↑ breached threshold!
Issues: - p95 of 510ms is above 500ms SLA - p99 of 1200ms = 1% of users waiting 1.2 seconds - Gap between p50 and p99 suggests some requests dramatically slower
Possible causes: - Database slow queries (check pg_stat_statements) - Cache misses on certain product IDs - GC pauses in JVM-based services - Specific API path that's unoptimized
Investigation steps: 1. Check APM traces during the test window 2. Look for specific slow endpoints in Nginx/access logs 3. Check DB connection pool exhaustion (connections waiting) 4. Profile memory usage GC pressure causes latency spikes ```
Stress vs load vs spike vs soak: ``` Load test: Simulates expected traffic (100 users/min) → Validates system meets performance SLAs
Stress test: Gradually increase until system breaks → Finds the breaking point and failure mode
Spike test: Sudden 10x traffic (product goes viral) → Does the system degrade gracefully or crash?
Soak test: Sustained moderate load for 12-24 hours → Finds memory leaks, connection pool exhaustion → Most production bugs only show after sustained load ```
Code Example
// Performance test example (k6)
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '30s', target: 20 }, // Ramp up to 20 users
{ duration: '1m', target: 50 }, // Ramp up to 50 users
{ duration: '30s', target: 0 }, // Ramp down to 0
],
thresholds: {
http_req_duration: ['p(95)<500'], // 95% of requests under 500ms
http_req_failed: ['<0.1'], // Less than 10% failure rate
},
};
export default function() {
// Simulate user browsing product catalog
let res = http.get('https://api.example.com/products');
check(res, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
});
sleep(1);
// Add product to cart
res = http.post('https://api.example.com/cart/add', {
productId: 123,
quantity: 1
});
check(res, {
'add to cart succeeds': (r) => r.status === 200,
});
sleep(2);
}Pro Tips
- 1Always run performance tests against a staging environment that mirrors production testing against production risks actual downtime.
- 2Establish a baseline before any optimization. You can't know if you improved something if you don't know where you started.
- 3p95 and p99 latency matter more than averages. An average of 200ms is fine; if 5% of requests take 8 seconds, users are suffering.
- 4Gradual ramp-up mimics real traffic. A sudden spike of 500 users at second 0 tests something different from the gradual traffic increase that happens in reality.
Topics Covered
Continue Learning
Explore the official documentation and deepen your knowledge with real-world practice.
Explore the Full Portfolio
This is the complete portfolio of Sufi Khan Sulaiman, a technology leader specialising in B2B commerce and digital automation. Start from the Home page for the overview, then move through two decades of career experience across FLIR Systems, Lorex Technology, and 1c Platform, and the full catalogue of project case studies spanning headless commerce migrations, AI recommendation engines, and multi-channel fulfilment systems.
The skills and certifications page maps the technical and leadership capabilities behind the work, while the articles and the knowledge base break down the thinking into actionable frameworks. For hands-on learning, the tutorials and applications sections cover practical builds from front-end fundamentals to full-stack web apps.
For consulting engagement, the expertise page outlines service offerings, the ecommerce hub covers platform architecture and automation strategy, and the ecommerce guide (PDF) is a downloadable 55-page field manual. When you are ready to talk, the contact page is the direct line.