Joshua Blewitt

An introduction to k6 - a way of automating performance tests ⌚

###Introduction

Recently at work, I've been (trying) to find time to automate several processes such as regression for an area that I've been working on. Whilst planning out what I needed to do etc, a thought popped into my head; "What about performance?"

Performance testing has been an area that I haven't had much experience with and I wanted to add another notch to my bow. So I decided to do some investigations.

There's JMeter, but that's not great. I was looking for something modern and something that could be run in a pipeline.

I stumbled across something called k6. It seemed to tick the boxes I was looking for:

  • Scripting in JavaScript
  • Open source
  • Integrations to services such as Azure, New Relic and Postman
  • Up to date documentation

I was intrigued!

Since k6 can run via a CLI, it's possible to run this in a pipeline to automate the performance process entirely. Looks good!

But how easy is it to use?

Well, we're going to find out!

Installing k6 is pretty easy so we'll skip over that, the documentation on k6 goes over that. What we're interested in is scripting and using k6.

In terms of writing scripts, tests are written in JavaScript and the k6 documentation goes into detail of how to write a test.

Let's look at an example

import http from 'k6/http';
import { sleep } from 'k6';
export let options = {
  stages: [
    { duration: '30s', target: 20 },
    { duration: '1m30s', target: 10 },
    { duration: '20s', target: 0 },
  ]
};
export default function () {
  http.get('http://test.k6.io');
  sleep(1);
}

This test will go to the test k6 website and perform a get request against it, while ramping up the number of virtual users at different stages for 2 minutes and 20 seconds.

Executing this test with k6 in a CLI shows the following results....

k6 output

...which doesn't easily show how it performed.

So how can we take the data and analyse it?

Thankfully, k6 offers plenty of different ways to output the results of the test. Such as to a CSV or JSON file, New Relic or Data Dog. But for the sake of practice and making a proof of concept, I was looking for something quick and easy to use.

I wouldn't export the results to a CSV or a JSON file unless you really have to, these files can be quite big.

And thankfully, k6 offers a cloud system for that. You can output the results to the k6 cloud (where you login with your k6 cloud account). So you run the test locally on your machine and send the results to k6 cloud for analysis. And that looks like this...

k6 visual output

...which is much better!

###Writing and building tests

This is when things get a little trickier, when it comes to writing and building tests - knowledge of JavaScript (or at least a programming language) is essential. It's a little bit intimidating at first, and although the documentation does help, writing out a script that works can be a challenge.

That's where the k6 test builder comes into play. For those who have used Postman, you should feel at home. The builder make it easy to build tests. You can view the code for a completed test which gives some insight into how it works (which is why I suggest having some programming knowledge when using k6).

k6 visual builder

You can build up a journey, add in the number of seconds the script needs to sleep for, perform checks, create variables and more! This is where k6 begins to shine and starts to become a good alternative to JMeter.

Plus, there is the browser extension that records your activity and builds your script for you. This is incredibly helpful when you are working with APIs. In fact, I got a proof of concept up and running in minutes. And once you have the script in your editor, you start to see how everything works together.

###More to come?

I've been looking at k6 for a few weeks now and it's definitely caught my attention. I'm hoping to use this a bit more and learn more about it.

And since k6 tests can be integrated into services such as Azure pipelines - it'll make automating performance tests much easier!

Thanks for reading! 👏

Tags:


A photo of me!

I'm Joshua Blewitt, I'm passionate about product, a technology advocate, customer champion, curious mind and writer. I've worked for companies such as Rightmove, Domino's Pizza and IQVIA.

Let me know your thoughts!
More about me