-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DevSecOps : Writing Load Tests #17006
Comments
Understanding the Basics of Load TestingGoals of Load Testing
Key Metrics to Capture
Test Scenarios to Simulate
|
Designing Load TestsStep 1: Define Test Objectives
Step 2: Define Workload Patterns
Step 3: Define Success Criteria
|
Writing Load Testing ScriptsA. Writing API Load TestsAPI load tests simulate traffic to one or more endpoints. Example: K6 Script for API Load Testingimport http from 'k6/http';
import { sleep, check } from 'k6';
export let options = {
stages: [
{ duration: '1m', target: 10 }, // Ramp up to 10 users
{ duration: '5m', target: 50 }, // Hold 50 users
{ duration: '1m', target: 0 }, // Ramp down
],
thresholds: {
http_req_duration: ['p(95)<500'], // 95% of requests must complete under 500ms
http_req_failed: ['rate<0.01'], // Error rate <1%
},
};
export default function () {
const res = http.get('https://your-api-endpoint.com/data');
check(res, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
});
sleep(1);
} |
B. Writing Batch Processing Load TestsBatch processing tests simulate the submission of varying batch sizes and track performance. Example: K6 Script for Batch Processingimport http from 'k6/http';
import { sleep } from 'k6';
export let options = {
stages: [
{ duration: '1m', target: 20 }, // Ramp up to 20 users
{ duration: '5m', target: 100 }, // Hold 100 users
{ duration: '1m', target: 0 }, // Ramp down
],
};
export default function () {
const payload = JSON.stringify({
batchId: `batch-${__VU}-${__ITER}`, // Unique batch ID
data: Array.from({ length: 2500 }, (_, i) => i + 1), // Batch of 2.5k items
});
const params = {
headers: {
'Content-Type': 'application/json',
},
};
const res = http.post('https://your-api-endpoint.com/process-batch', payload, params);
sleep(1);
} |
C. Writing Concurrency TestsConcurrency tests simulate multiple users or processes interacting with the system simultaneously. Example: K6 Script for Concurrency Testingimport http from 'k6/http';
import { sleep } from 'k6';
export let options = {
vus: 50, // Simulate 50 concurrent users
duration: '10m', // Run the test for 10 minutes
};
export default function () {
const payload = JSON.stringify({
batchId: `batch-${__VU}-${__ITER}`,
data: Array.from({ length: 1000 }, (_, i) => i + 1), // Batch of 1k items
});
const params = {
headers: {
'Content-Type': 'application/json',
},
};
http.post('https://your-api-endpoint.com/process-batch', payload, params);
sleep(Math.random() * 2); // Random interval between requests
} |
D. Writing Complex Traffic SimulationsSimulate a combination of GET and POST requests with randomized payloads and intervals. Example: Mixed Traffic Simulationimport http from 'k6/http';
import { sleep, check } from 'k6';
export let options = {
scenarios: {
steady: {
executor: 'constant-vus',
vus: 50,
duration: '10m',
},
spike: {
executor: 'ramping-vus',
startVUs: 10,
stages: [
{ duration: '2m', target: 100 }, // Ramp up to 100 users
{ duration: '5m', target: 100 }, // Hold at 100 users
{ duration: '1m', target: 0 }, // Ramp down
],
},
},
};
export default function () {
const urls = [
'https://your-api-endpoint.com/get-data',
'https://your-api-endpoint.com/process-data',
];
const payload = JSON.stringify({ key: 'value' });
const res = Math.random() > 0.5
? http.get(urls[0])
: http.post(urls[1], payload, { headers: { 'Content-Type': 'application/json' } });
check(res, { 'status was 200': (r) => r.status === 200 });
sleep(Math.random() * 2);
} |
Automating Load TestingGitHub Actions WorkflowAutomate the execution of load tests after deployments. name: Load Testing Workflow
on:
push:
branches:
- main
jobs:
load-test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install K6
run: sudo apt-get update && sudo apt-get install -y k6
- name: Run Load Test
run: k6 run --vus 100 --duration 5m tests/load_test.js
- name: Upload Results
uses: actions/upload-artifact@v3
with:
name: load-test-results
path: ./k6-results.json |
Reporting Load Test ResultsAzure Monitor IntegrationLog custom metrics (e.g., batch latency, error rate) to Azure Monitor for centralized analysis. export default function () {
const res = http.post('https://your-api-endpoint.com/process-batch', payload);
// Log custom metrics
const latency = res.timings.duration;
const success = res.status === 200;
// Log data to Azure (use SDK or custom logging integration)
logToAzureMonitor({
metricName: 'batch_processing_latency',
value: latency,
dimensions: { success },
});
sleep(1);
} Power BI Reporting
|
Load testing involves simulating user behavior or workload patterns to evaluate the performance and scalability of a system. Below is a detailed framework on how to design, write, and execute effective load tests.
The text was updated successfully, but these errors were encountered: