Introduction to k6.io

How to use k6 for performance testing

Marie Drake

Quality Engineering Manager, Zoopla

mariedrake.com | @mcruzdrake

agenda

but first..

why performance testing?

We use technology on a daily basis and have been accustomed to instant gratification.

“While customers will forgive and forget the odd bug, crash, or period of site maintenance, they will not tolerate a site that doesn't move at the speed they’ve become accustomed to.”

- Dr. John Bates, CEO, Eggplant from Slow Websites Are Silent Killers for Businesses

Performance Testing

Front End/Client Side

Back End/Server Side

types of performance test

  • Load testing
  • Soak testing
  • Stress testing

What is k6?

k6 is a developer-centric, free and open-source load testing tool built for making performance testing a productive and enjoyable experience - k6 docs

k6 services

k6 open source

k6 cloud

Why was k6 created?

  • Tests written in JavaScript

  • Flexible

  • Test scripting (load testing as code)

  • Test recording

  • Integrates well with CI/CD pipelines

  • Test visualisation with New Relic, Grafana and others!

k6 

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

export default function () {
  http.get('https://test.k6.io');
  sleep(1);
}
  • Virtual Users (VUs)

  • Metrics

  • Checks

  • Thresholds

k6 key concepts

virtual users

export const options = {
  vus: 10,
  duration: '1h',
};
export const options = {
  stages: [
    { duration: '1m', target: 10 },
    { duration: '1m', target: 20 },
    { duration: '1m', target: 0 },
  ]
};

export default function () {
  http.get('http://test.k6.io/');
}

metrics

running (00m03.8s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs  00m03.8s/10m0s  1/1 iters, 1 per VU

     data_received..................: 22 kB 5.7 kB/s
     data_sent......................: 742 B 198 B/s
     http_req_blocked...............: avg=1.05s    min=1.05s    med=1.05s    max=1.05s    p(90)=1.05s    p(95)=1.05s
     http_req_connecting............: avg=334.26ms min=334.26ms med=334.26ms max=334.26ms p(90)=334.26ms p(95)=334.26ms
     http_req_duration..............: avg=2.7s     min=2.7s     med=2.7s     max=2.7s     p(90)=2.7s     p(95)=2.7s
       { expected_response:true }...: avg=2.7s     min=2.7s     med=2.7s     max=2.7s     p(90)=2.7s     p(95)=2.7s
     http_req_failed................: 0.00% ✓ 0        ✗ 1
     http_req_receiving.............: avg=112.41µs min=112.41µs med=112.41µs max=112.41µs p(90)=112.41µs p(95)=112.41µs
     http_req_sending...............: avg=294.48µs min=294.48µs med=294.48µs max=294.48µs p(90)=294.48µs p(95)=294.48µs
     http_req_tls_handshaking.......: avg=700.6ms  min=700.6ms  med=700.6ms  max=700.6ms  p(90)=700.6ms  p(95)=700.6ms
     http_req_waiting...............: avg=2.7s     min=2.7s     med=2.7s     max=2.7s     p(90)=2.7s     p(95)=2.7s
     http_reqs......................: 1     0.266167/s
     iteration_duration.............: avg=3.75s    min=3.75s    med=3.75s    max=3.75s    p(90)=3.75s    p(95)=3.75s
     iterations.....................: 1     0.266167/s
     vus............................: 1     min=1      max=1
     vus_max........................: 1     min=1      max=1

checks

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

export default function () {
  const res = http.get('http://test.k6.io/');
  check(res, {
    'is status 200': (r) => r.status === 200,
  });
}
k6 run script.js

  ...
    ✓ is status 200

  ...
  checks.........................: 100.00% ✓ 1        ✗ 0
  data_received..................: 11 kB   12 kB/s

thresholds

import http from 'k6/http';

export const options = {
  thresholds: {
    http_req_failed: ['rate<0.01'], // http errors should be less than 1%
    http_req_duration: ['p(95)<200'], // 95% of requests should be below 200ms
  },
};

export default function () {
  http.get('https://test-api.k6.io/public/crocodiles/1/');
}
  • Tests written in JavaScript

  • Load generation distribution not included in the open source version

  • Node JS libraries not compatible with k6 

  • TypeScript support requires a lot of boilerplate so might not be ideal if you want something quick

limitations

demonstration

questions?

Introduction to k6.io

How to use k6 for load testing

Marie Drake

Quality Engineering Manager, Zoopla

mariedrake.com | @mcruzdrake

Introduction to k6

By Marie Cruz

Introduction to k6

  • 2,041