Skip to content

Commit

Permalink
feat: add native (go) build support
Browse files Browse the repository at this point in the history
  • Loading branch information
szkiba committed Sep 26, 2023
1 parent 633a15e commit c7d840d
Show file tree
Hide file tree
Showing 14 changed files with 692 additions and 316 deletions.
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ SPDX-License-Identifier: AGPL-3.0-only

**Run k6 with extensions as easy as possible**

k6x is a [k6](https://k6.io) launcher that automatically provides k6 with the [extensions](https://k6.io/docs/extensions/) used by the test. To do this, k6x analyzes the test script and creates a list of required extensions (it also parses the command line to detect output extensions). Based on this list, k6x builds (and caches) the k6 binary and runs it. The build step uses a Docker Engine (even a [remote one](#remote-docker)), so no other local tools (go, git, docker cli, etc.) are needed, just k6x.
k6x is a [k6](https://k6.io) launcher that automatically provides k6 with the [extensions](https://k6.io/docs/extensions/) used by the test. To do this, k6x analyzes the test script and creates a list of required extensions (it also parses the command line to detect output extensions). Based on this list, k6x builds (and caches) the k6 binary and runs it.

The build step uses a [Docker Engine](https://docs.docker.com/engine/) (even a [remote one](#remote-docker)), so no other local tools (go, git, docker cli, etc.) are needed, just k6x. If [Go language toolkit is installed](https://go.dev/doc/install), the build step uses it instead of Docker Engine. In this case, Docker Engine is not needed and build will be faster.

**asciicast**

[![asciicast](https://asciinema.org/a/j6Lr3jKreYGlTt8FVyDPcRxBn.svg)](https://asciinema.org/a/j6Lr3jKreYGlTt8FVyDPcRxBn)
[![asciicast](https://asciinema.org/a/9WR2NOXd9b1kg2TfldyJXLtKJ.svg)](https://asciinema.org/a/9WR2NOXd9b1kg2TfldyJXLtKJ)

## Prerequisites

- properly configured Docker Engine environment (e.g. `DOCKER_HOST` environment variable)
- Either a [Go language toolkit](https://go.dev/doc/install) or a properly configured [Docker Engine](https://docs.docker.com/engine/install/) environment (e.g. `DOCKER_HOST` environment variable)

## Usage

Expand Down Expand Up @@ -87,10 +89,17 @@ The k6 subcommands are extended with some global command line options related to
- `--clean` the cached k6 binary will be deleted and a new binary will be built
- `--dry` only the cached k6 binary will be updated if necessary, the k6 command will not be executed
- `--bin-dir path` the directory specified here will be used to cache the k6 binary (it will overwrite the value of `K6X_BIN_DIR`)
```
k6x run --bin-dir ./custom-k6 script.js
```

```
k6x run --bin-dir ./custom-k6 script.js
```
- `--builder list` a comma-separated list of builders (default: `native,docker`), available builders:
- `native` this builder uses the installed go compiler if available, otherwise the next builder is used without error
- `docker` this builder uses Docker Engine, which can be local or remote (specified in `DOCKER_HOST` environment variable)

```
k6x run --buider docker script.js
```

### Subcommands

Expand All @@ -104,6 +113,7 @@ Some new subcommands will also appear, which are related to building the k6 bina
Flags:
-o, --out name output extension name
--bin-dir path folder for custom k6 binary (default: .)
--builder list comma separated list of builders (default: native,docker)
-h, --help display this help
```

Expand Down Expand Up @@ -144,7 +154,7 @@ The git repository for each extension is determined based on the [k6 extension r

Taking into account the optional version constraints, the appropriate extension version is selected from the git tags of the extension's git repository. Currently, only GitHub repositories are supported, if required, additional repository managers can be supported (eg GitLab).

The custom k6 binary is created using the [xk6 custom k6 builder docker image](https://hub.docker.com/r/grafana/xk6/). The Docker Engine API is accessed using the [docker go client](https://pkg.go.dev/github.com/docker/docker/client), so there is no need for a docker cli command and even a remote Docker Engine can be used.
If the Go compiler is installed, the k6 binary is created using it. Otherwise the custom k6 binary is created using the [xk6 custom k6 builder docker image](https://hub.docker.com/r/grafana/xk6/). The Docker Engine API is accessed using the [docker go client](https://pkg.go.dev/github.com/docker/docker/client), so there is no need for a docker cli command and even a remote Docker Engine can be used.

The compiled k6 binary is stored in the cache. This binary will be used as long as the extensions included in it meet the current requirements, taking into account the optional version constraints. In order to increase the efficiency of the cache, the newly built k6 binary will also include the previously used extensions (if they are still included in the registry).

Expand Down Expand Up @@ -229,6 +239,4 @@ major change is API breaking. For example,

## Future

One possible future development idea is to support native k6 build without Docker Engine. If the user has a go compiler and a git version manager, the k6 build can be done natively, without using Docker Engine. This can already reduce the first cache loading time, and it can significantly speed up subsequent builds.

Another possible future development idea is to make it possible to use pre-built k6 binaries. Instead of building, the k6 binary could be downloaded from properly prepared custom k6 GitHub releases. This will allow you to use k6x even without Docker Engine. In addition, the use of pre-built k6 binaries will significantly reduce the cache loading time (from 45-50 seconds to a few seconds).
One possible future development idea is to make it possible to use pre-built k6 binaries. Instead of building, the k6 binary could be downloaded from properly prepared custom k6 GitHub releases. This will allow you to use k6x even without Docker Engine. In addition, the use of pre-built k6 binaries will significantly reduce the cache loading time (from 45-50 seconds to a few seconds).
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/spf13/afero v1.9.5
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
go.k6.io/xk6 v0.9.2
golang.org/x/sys v0.11.0
)

Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
Expand Down Expand Up @@ -221,6 +222,8 @@ github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.k6.io/xk6 v0.9.2 h1:OhuFZSSrz7KdslLO8NKX7dp0B4/so3/ejNrmlE0kw+E=
go.k6.io/xk6 v0.9.2/go.mod h1:4+BwjriwZ52roJ9RHHQhTBb9Q6ZctEUtsCO10H8rJIo=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
Expand Down
Loading

0 comments on commit c7d840d

Please sign in to comment.