K6 is a load testing tool by Grafana Labs. It allows for load testing and stress testing to simulate real-world traffic patterns and identify bottlenecks or performance issues. It is particularly favoured for its easy-to-write JavaScript-based test scripts and robust reporting features. At the time of writing this post, there is a K6 charmed operator which can be used for load testing other charms (read this amazing post by @lucabello on the specifics).
From time to time, you may find yourself needing K6’s Go-based extensions to enhance what your load tests can do. Such extensions are powered by XK6, which is an K6 extension toolbox. In essence, XK6 gives you the ability to add new features or integrate with third-party tools that aren’t available in the default k6 distribution. With XK6, you can create a K6 binary that includes the exact set of capabilities you need. Extensions are particularly helpful because while K6 relies on JavaScript for scripting, it’s not using the traditional Node.js environment, but rather a Go-based JavaScript engine (Goja). As a result, if you need a Node.js package, you can’t simply install it using npm
and import it directly into your K6 scripts. In such cases, you need to find an XK6 extension which gives you the functionality that you need. There are plenty of available extensions for K6 which help you go above and beyond. For example, at one point I wanted to push logs to Loki in my load test. The K6 extension XK6-Loki made that easy and possible for me.
Now, let’s walk through how you can install an extension yourself! I’ll use the XK6-Faker extension as an example. Follow along with the steps below!
- Let’s first ensure we have Go installed:
sudo snap install go --classic
- Now, we’ll access and install the XK6 tool from its remote repo:
go install go.k6.io/xk6/cmd/xk6@latest
- Go binaries (like
xk6
) are installed into thebin
directory inside yourGOPATH
folder. Let’s make sure that the system can find and execute Go-installed binaries from any terminal session:export PATH=$PATH:$(go env GOPATH)/bin
- We can now build a K6 executable which includes the Faker extension:
xk6 build --with github.com/grafana/xk6-faker@latest
Okay! We have successfully installed the Faker extension. Now, all we need to do in order to run a load test script is (assuming your load test file is called file.js
: ./k6 run file.js
.
Congrats! You can use the same recipe to install other extensions. Happy load testing!