DEV Community

Isha Training Solutions
Isha Training Solutions

Posted on Edited on

How to Write Your First k6 Load Test in 10 Minutes

If you have never done performance testing before, k6 is one of the easiest tools to start with. It is free, open source, and you write tests in simple JavaScript. In this short tutorial you will install k6, write your first load test and understand the results.

1. Install k6

  • Windows: winget install k6
  • Mac: brew install k6
  • Linux (Debian/Ubuntu): follow the steps on the official k6 installation page

Check it works:

k6 version
Enter fullscreen mode Exit fullscreen mode

2. Write your first test

Create a file called first-test.js and paste this:

import http from 'k6/http';
import { check, sleep } from 'k6';

export const options = {
  vus: 10,          // 10 virtual users
  duration: '30s',  // run for 30 seconds
};

export default function () {
  const res = http.get('https://test.k6.io');
  check(res, {
    'status is 200': (r) => r.status === 200,
  });
  sleep(1); // wait 1 second, like a real user
}
Enter fullscreen mode Exit fullscreen mode

What this does:

  • vus is the number of virtual users hitting the site at the same time.
  • duration is how long the test runs.
  • check confirms the page returned status 200 (OK).
  • sleep adds a pause between requests so the load behaves like real users.

3. Run the test

k6 run first-test.js
Enter fullscreen mode Exit fullscreen mode

4. Read the results

When the test finishes, k6 prints a summary. The most important lines are:

  • http_req_duration: how long requests took. Look at avg and p(95). The p(95) value means 95% of requests were faster than this.
  • http_req_failed: the percentage of failed requests. Ideally 0%.
  • checks: how many of your checks passed.
  • iterations: how many times the test function ran.

5. Add a pass/fail rule (thresholds)

In real projects you want the test to fail automatically if performance is too slow. Add thresholds to your options:

export const options = {
  vus: 10,
  duration: '30s',
  thresholds: {
    http_req_duration: ['p(95)<500'], // 95% of requests under 500 ms
    http_req_failed: ['rate<0.01'],   // less than 1% errors
  },
};
Enter fullscreen mode Exit fullscreen mode

Now k6 marks the test as failed if these limits are crossed, which is perfect for running in CI/CD pipelines like Jenkins or GitHub Actions.

What to learn next

  • Ramping users up and down with stages
  • Testing APIs with POST requests and authentication tokens
  • Sending results to Grafana dashboards
  • Running k6 tests in CI/CD

Written by the team at Isha Training Solutions, where we teach live online k6 and performance testing courses with real-time projects.

Top comments (0)