To use an extension that you found on the Extension page or the xk6 GitHub topic, you need to build a binary using Go.
Support for Docker
Not interested in setting up a Go environment? You can use Docker instead.
Before you start
- Set up Go and Git.
- Make sure that your $PATH environment variable is updated so that go version returns the correct version.
Installing xk6
Given the prerequisite Go setup, installing xk6 itself requires only the following command:
To confirm your installation, run which xk6 on Linux and Mac, or where xk6 on Windows. Make sure the command returns a valid path.
If not, check that you've correctly defined the$GOPATH environment variable and that $GOPATH/bin is in your $PATH. See the Go documentation for details.
Building your first extension
Once installed, building a k6 binary with one or more extensions can be done with the xk6 build command as follows:
When creating your bundles, use the interactive builder to avoid typing mistakes!
Once completed, the current directory contains your newly built k6 binary with the xk6-sql and xk6-output-prometheus-remote extensions.
Breaking down the xk6 command
From the xk6 documentation, the command-line usage is as follows:
The use of --replace should be considered an advanced feature to be avoided unless required.
Referring back to our executed command, note that:
- We specify the version as latest, which is also the default if we hadn't supplied a version. latest means that we'll build using the latest source code for k6. Consider using a stable release version as a best practice unless you genuinely want the bleeding edge.
- With each --with, we specified a full GitHub URI for the extension repository. If not specifying a version, the default is latest once again. Check your extension repository for stable release versions, if available, to lock in your version as we've done with xk6-sql@v0.0.1.
- We did not use the --output option; therefore, our new binary is k6 within the current directory. Had we used --output k6-extended, our binary would be named k6-extended within the current directory. If a directory is specified, then the new binary would be k6 within that directory. If a path to a non-existent file, e.g. /tmp/k6-extended, this will be the path and filename for the binary.
Running ./k6 version should show your build is based upon the appropriate version.
Building from a local repository
Suppose now you've cloned the xk6-sql repository and want the local version included in your custom binary? From the cloned directory, we would then use:
Based upon the command usage described in the previous section, this tells xk6 to use the current directory as the replacement for the module.
Running your extended binary
Now that we have our newly built k6 binary, we can run scripts using the functionalities of the bundled extensions.
Be sure to specify the binary just built in the current directory as ./k6, or else Linux/Mac could execute another k6 binary on your system path. Windows shells will first search for the binary in the current directory by default.