Created
October 3, 2024 06:12
-
-
Save muhx/d73178ea8c45802f10c29e1bea4db8f0 to your computer and use it in GitHub Desktop.
K6 script for API load test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
1. Install k6 https://grafana.com/docs/k6/latest/set-up/ | |
2. Setup test machine (can handle up to 64510 vus): | |
- Update sysctl to handle more vus | |
echo "net.ipv4.ip_local_port_range = 1024 65535" >> /etc/sysctl.conf | |
echo "net.ipv4.tcp_tw_reuse=1" >> /etc/sysctl.conf | |
echo "net.ipv4.tcp_timestamps=1" >> /etc/sysctl.conf | |
echo "kernel.pid_max=999999" >> /etc/sysctl.conf | |
echo "kernel.thread-max=999999" >> /etc/sysctl.conf | |
echo "vm.max_map_count=99999" >> /etc/sysctl.conf | |
echo "fs.file-max=999999" >> /etc/sysctl.conf | |
echo "* - nofile 999999" >> /etc/security/limits.conf | |
echo "* hard fsize -1" >> /etc/security/limits.conf | |
echo "root - nofile 999999" >> /etc/security/limits.conf | |
echo "root hard fsize -1" >> /etc/security/limits.conf | |
sysctl -p /etc/sysctl.conf . | |
3. k6 run k6-load-test.js | |
*/ | |
import http from 'k6/http'; | |
import { check, group, sleep } from 'k6'; | |
import { htmlReport } from 'https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js'; | |
export function handleSummary(data) { | |
return { | |
'public/summary.html': htmlReport(data), | |
'target/summary_distributed.json': JSON.stringify(data), | |
}; | |
} | |
export const options = { | |
scenarios: { | |
Test: { | |
executor: 'per-vu-iterations', | |
vus: 10000, //adjust virtual users can start with 10K, 20K, 30K | |
iterations: 100, | |
maxDuration: '90s', | |
exec: 'load_test', | |
}, | |
}, | |
}; | |
export function load_test() { | |
group('Test 1', () => { | |
const params = { | |
timeout: '60s', | |
}; | |
const response = http.get('https://example.com/index.html', params); //adjust the url | |
check(response, { | |
'Test URL 1 Success': () => response.status.toString() === '200', | |
}); | |
sleep(0.5); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment