Performance Testing with k6
Performance tests answer: how does the system behave with many users at once? k6 lets you write them in JavaScript.
On this page · 5 min · Advanced
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Oct 29, 2026
Related tutorials
Learning objectives#
- Distinguish load, stress, spike and soak tests
- Write a k6 script with stages
- Set pass/fail thresholds
- Read k6 results
Explanation#
Load test: expected traffic. Stress test: beyond expected until it breaks. Spike: sudden surge. Soak: long duration to find leaks.
k6 simulates virtual users (VUs) running your script; stages ramp users up and down.
Thresholds turn results into pass/fail: e.g. 95% of requests under 500 ms and under 1% errors.
Always test a production-like environment you own — never someone else’s site.
Code example: Load test for the products API#
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '1m', target: 50 }, // ramp up to 50 users
{ duration: '3m', target: 50 }, // hold
{ duration: '1m', target: 0 }, // ramp down
],
thresholds: {
http_req_duration: ['p(95)<500'], // 95% of requests faster than 500 ms
http_req_failed: ['rate<0.01'], // fewer than 1% errors
},
};
export default function () {
const res = http.get(`${__ENV.BASE_URL}/api/products?page=1`);
check(res, { 'status is 200': (r) => r.status === 200 });
sleep(1);
}
// k6 run -e BASE_URL=https://staging.shop.example load-test.js
✓ status is 200 ✓ http_req_duration..............: avg=182ms p(95)=341ms ✓ http_req_failed................: 0.12% vus_max........................: 50
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Stages ramp to 50 users, hold, then ramp down.
- Each virtual user requests the products page every second.
- Checks validate each response.
- Thresholds decide pass/fail: p95 341 ms and 0.12% errors pass.
Real-world use case#
Before a festival sale, the team load-tests checkout at three times normal traffic and fixes a slow database query.
Common mistakes#
Your machine or network becomes the bottleneck. Use a nearby cloud machine or k6 Cloud.
That can be a denial-of-service attack. Only test your own environments with permission.
Best practices#
- Production-like environment
- Thresholds on p95 and error rate
- Ramp gradually
- Monitor servers while testing
Remember this#
Load · stress · spike · soak.
VUs + stages + thresholds.
p95 latency and error rate decide.
Practice exercise#
Your task: Change the script into a spike test: 0 → 300 users in 10 seconds, hold 1 minute.
Show solution
stages: [
{ duration: '10s', target: 300 },
{ duration: '1m', target: 300 },
{ duration: '10s', target: 0 },
],
Mini quiz#
What does p(95)<500 mean?
Want more? Practise all Automation Testing MCQs · Automation Testing interview questions
My notes#
Saved in this browser.