Automation Testing · Professional Practice

Performance Testing with k6

Performance tests answer: how does the system behave with many users at once? k6 lets you write them in JavaScript.

Advanced 5 min Lesson 22 of 24
Before you start:API Test Automation
0/24 in Automation Testing
On this page · 5 min · Advanced

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#

JavaScriptload-test.js Illustrative
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
Expected output
✓ 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#

  1. Stages ramp to 50 users, hold, then ramp down.
  2. Each virtual user requests the products page every second.
  3. Checks validate each response.
  4. 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#

Testing from a laptop over Wi-Fi

Your machine or network becomes the bottleneck. Use a nearby cloud machine or k6 Cloud.

Load-testing systems you do not own

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
JavaScript Illustrative
stages: [
  { duration: '10s', target: 300 },
  { duration: '1m', target: 300 },
  { duration: '10s', target: 0 },
],

Mini quiz#

Performance Testing with k6 quizMultiple choice Question 1 of 1

What does p(95)<500 mean?

Score so far: 0/0

Want more? Practise all Automation Testing MCQs · Automation Testing interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.