Load Testing Tools
Apache JMeter
<!-- test-plan.jmx -->
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2">
<hashTree>
<TestPlan guiclass="TestPlanGui">
<elementProp name="TestPlan.user_defined_variables"/>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui">
<elementProp name="ThreadGroup.main_controller">
<stringProp name="LoopController.loops">10</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">100</stringProp>
<stringProp name="ThreadGroup.ramp_time">60</stringProp>
</ThreadGroup>
</hashTree>
</hashTree>
</jmeterTestPlan>
k6 Script
// load-test.js
import http from 'k6/http';
import { check, sleep } from 'k6';
export let options = {
stages: [
{ duration: '1m', target: 50 }, // Ramp up
{ duration: '3m', target: 50 }, // Stay at 50
{ duration: '1m', target: 100 }, // Ramp up to 100
{ duration: '3m', target: 100 }, // Stay at 100
{ duration: '1m', target: 0 }, // Ramp down
],
thresholds: {
http_req_duration: ['p(95)<2000'], // 95% under 2s
http_req_failed: ['rate<0.01'], // <1% failures
},
};
export default function () {
let response = http.get('https://magento.example.com/');
check(response, {
'status is 200': (r) => r.status === 200,
'response time < 2s': (r) => r.timings.duration < 2000,
});
sleep(1);
}
Key Points
- JMeter: GUI-based, Java, widely used
- k6: JavaScript-based, modern, lightweight
- Artillery: YAML-based, easy setup
- Choose based on team skillset
Test Scenarios
E-commerce Scenarios
// k6 scenarios
export let options = {
scenarios: {
browse_products: {
executor: 'constant-vus',
vus: 50,
duration: '5m',
},
add_to_cart: {
executor: 'constant-vus',
vus: 20,
duration: '5m',
},
checkout: {
executor: 'constant-vus',
vus: 10,
duration: '5m',
},
},
};
Realistic User Flow
export default function () {
// 1. Homepage
http.get('https://magento.example.com/');
sleep(2);
// 2. Browse category
http.get('https://magento.example.com/category.html');
sleep(3);
// 3. View product
http.get('https://magento.example.com/product.html');
sleep(5);
// 4. Add to cart
http.post('https://magento.example.com/rest/V1/guest-carts', {
product_id: 1,
qty: 1,
});
sleep(1);
// 5. Checkout
http.get('https://magento.example.com/checkout/');
}
Key Points
- Simulate realistic user behavior
- Include think time between actions
- Mix read/write operations
- Test different user roles
Performance Under Load
Key Metrics
// Response time metrics
// - Average response time
// - 95th percentile response time
// - 99th percentile response time
// Throughput metrics
// - Requests per second
// - Transactions per second
// Error metrics
// - Error rate
// - Failed requests
// Resource metrics
// - CPU usage
// - Memory usage
// - Database connections
Analyze Results
// k6 summary
export function handleSummary(data) {
return {
'summary.json': JSON.stringify(data, null, 2),
};
}
Bottleneck Identification
# Monitor during test
# 1. Server CPU/Memory
htop
# 2. Database
mysqladmin processlist
# 3. Web server
tail -f /var/log/nginx/access.log
# 4. PHP-FPM
pm2 monit
Key Points
- Monitor server resources during test
- Identify bottleneck (CPU, memory, I/O, database)
- Compare results against performance budgets
- Document findings for optimization
Capacity Planning
Capacity Formula
// Calculate required capacity
$peakUsers = 1000; // Peak concurrent users
$avgRequests = 10; // Requests per user
$avgResponseTime = 1; // Seconds
$requiredThroughput = $peakUsers * $avgRequests / $avgResponseTime;
// = 10,000 requests/second
Scaling Strategies
// Horizontal scaling
$servers = ceil($requiredThroughput / $singleServerCapacity);
// Vertical scaling
$cpuNeeded = $peakUsers * $cpuPerUser;
$memoryNeeded = $peakUsers * $memoryPerUser;
Infrastructure Planning
# production-config.yml
infrastructure:
web_servers: 4
load_balancer: 1
database_master: 1
database_replicas: 2
redis: 2
varnish: 2
cdn: true
resources_per_server:
cpu: 4 cores
memory: 16GB
storage: 100GB SSD
Key Points
- Plan for peak traffic, not average
- Include growth projections
- Consider failover requirements
- Monitor and adjust based on real traffic
Practice Problems
0 / 1 solved
Load Test Setup
Create a load test for a Magento store homepage and category pages.
Solution
// load-test.js
import http from 'k6/http';
import { check, sleep } from 'k6';
export let options = {
stages: [
{ duration: '2m', target: 100 },
{ duration: '5m', target: 100 },
{ duration: '2m', target: 0 },
],
thresholds: {
http_req_duration: ['p(95)<2000'],
http_req_failed: ['rate<0.01'],
},
};
export default function () {
const rand = Math.random();
if (rand < 0.5) {
// Homepage
http.get('https://magento.example.com/');
} else if (rand < 0.8) {
// Category
http.get('https://magento.example.com/category.html');
} else {
// Product
http.get('https://magento.example.com/product.html');
}
sleep(Math.random() * 3 + 1);
} Quiz
1. What is a good response time target?
2. Why include think time in tests?
3. What is the 95th percentile?
4. How to plan for peak traffic?
Flashcards
Question
Load testing tools?
Click to reveal answer
Answer
JMeter, k6, Artillery
Question
Response time target?
Click to reveal answer
Answer
Under 2000ms for web apps
Question
Think time purpose?
Click to reveal answer
Answer
Simulate realistic user behavior
Question
95th percentile?
Click to reveal answer
Answer
95% of requests complete within this time
Revision Notes
Key Takeaways
- 1. Use JMeter or k6 for load testing
- 2. Simulate realistic user flows with think time
- 3. Monitor server resources during tests
- 4. Plan capacity for peak traffic + growth
Interview Tips
- • Explain load testing scenarios
- • Discuss how to identify bottlenecks
- • Know capacity planning basics
Cheat Sheet
Load Testing
- Tools: JMeter, k6
- Metrics: response time, throughput, errors
- Scenarios: realistic user flows
- Plan: peak traffic + growth