This command line tool and associated Go package makes it easy to make custom builds of k6.
It is used heavily by k6 extension developers as well as anyone who wishes to make custom k6 binaries (with or without extensions).
Stay updated, be aware of changes, and please submit feedback! Thanks!
- Go installed. At least version 1.17 is needed.
You can download binaries that are already compiled for your platform, or build xk6 from source:
$ go install go.k6.io/xk6/cmd/xk6@latestThis will install the xk6 binary in your $GOPATH/bin directory.
If you're getting a command not found error when trying to run xk6, make sure that you precisely follow the Go installation instructions for your platform.
Specifically, ensure that the $GOPATH/bin directory is part of your $PATH. For example, you might want to add this to your shell's initialization file: export PATH=$(go env GOPATH)/bin:$PATH. See this article for more information.
If you're still struggling with this, consider downloading a pre-built binary from our releases page, and placing the binary in a directory already part of your $PATH.
The xk6 command has two primary uses:
- Compile custom
k6binaries - A replacement for
go runwhile developing k6 extensions
The xk6 command will use the latest version of k6 by default. You can customize this for all invocations by setting the K6_VERSION environment variable.
As usual with go command, the xk6 command will pass the GOOS, GOARCH, and GOARM environment variables through for cross-compilation.
Syntax:
$ xk6 build [<k6_version>]
[--output <file>]
[--with <module[@version][=replacement]>...]
[--replace <module=replacement>...]
<k6_version>is the core k6 version to build; defaults toK6_VERSIONenv variable or latest.--outputchanges the output file.--withcan be used multiple times to add extensions by specifying the Go module name and optionally its version, similar togo get. Module name is required, but specific version and/or local replacement are optional. For an up-to-date list of k6 extensions, head to our extensions page.--replacecan be used multiple times to add replacements by specifying the Go module name and the replacement module, similar togo mod edit -replace=. Version of the replacement can be specified with the@versionsuffix in the replacement path.
Examples:
$ xk6 build \
--with github.com/grafana/xk6-browser
$ xk6 build v0.35.0 \
--with github.com/grafana/xk6-browser@v0.1.1
$ xk6 build \
--with github.com/grafana/xk6-browser=../../my-fork
$ xk6 build \
--with github.com/grafana/xk6-browser=.
$ xk6 build \
--with github.com/grafana/xk6-browser@v0.1.1=../../my-fork
# Build using a k6 fork repository. Note that a version is required if
# XK6_K6_REPO is a URI.
$ XK6_K6_REPO=github.com/example/k6 xk6 build master \
--with github.com/grafana/xk6-browser
# Build using a k6 fork repository from a local path. The version must be omitted
# and the path must be absolute.
$ XK6_K6_REPO="$PWD/../../k6" xk6 build \
--with github.com/grafana/xk6-browserIf you run xk6 from within the folder of the k6 extension you're working on without the build subcommand, it will build k6 with your current module and run it, as if you manually plugged it in and invoked go run.
The binary will be built and run from the current directory, then cleaned up.
The current working directory must be inside an initialized Go module.
Also note that because of the way xk6 works, vendored dependencies (the vendor directory created by go mod vendor) will not be taken into account when building a binary, and you don't need to commit them to the extension repository.
Syntax:
$ xk6 <args...>
<args...>are passed through to thek6command.
For example:
$ xk6 version
$ xk6 run -u 10 -d 10s test.jsThe race detector can be enabled by setting the env variable XK6_RACE_DETECTOR=1 or through the XK6_BUILD_FLAGS env variable.
builder := xk6.Builder{
K6Version: "v0.35.0",
Extensions: []xk6.Dependency{
{
PackagePath: "github.com/grafana/xk6-browser",
Version: "v0.1.1",
},
},
}
err := builder.Build(context.Background(), "./k6")Versions can be anything compatible with go get.
Because the subcommands and flags are constrained to benefit rapid extension prototyping, xk6 does read some environment variables to take cues for its behavior and/or configuration when there is no room for flags.
K6_VERSIONsets the version of k6 to build.XK6_BUILD_FLAGSsets any go build flags if needed. Defaults to '-ldflags=-w -s'.XK6_RACE_DETECTOR=1enables the Go race detector in the build.XK6_SKIP_CLEANUP=1causes xk6 to leave build artifacts on disk after exiting.XK6_K6_REPOoptionally sets the path to the main k6 repository. This is useful when building with k6 forks.
This project originally forked from the xcaddy project. Thank you!