Example addon for xk6-g0
The xk6-g0-figure addon is an example xk6-g0 addon that demonstrates how to add third-party go packages. The example includes the go-figure package, which is of course not useful in k6 tests.
exports.go
package figure
import (
_ "github.com/szkiba/xk6-g0"
"github.com/szkiba/xk6-g0/g0"
"github.com/traefik/yaegi/interp"
"go.k6.io/k6/js/modules"
)
var Symbols = interp.Exports{}
//go:generate go run github.com/traefik/yaegi/cmd/yaegi extract -name figure github.com/common-nighthawk/go-figure
func exports(vu modules.VU) interp.Exports {
return Symbols
}
func init() {
g0.RegisterExports(exports)
}
This is a template repository from which a new addon repository can be easily created. Create a repository based on this template, then:
- change the
module
line of thego.mod
file to the name of the new module - change the
package
line of theexports.go
file to the name of the new package - run
go mod tidy
to fix the go.mod file - modify the
//go generate
line in theexports.go
file to use the appropriate third-party package - add the corresponding third-party package using the
go get
command - generate the source code using the
go generate .
command - delete
github_com-common-nighthawk-go-figure.go
file - run
go mod tidy
to fix the go.mod file
After that, the new addon repository is ready to use.
In the rest of the page, of course, the name of the new repository should be used instead of
szkiba/xk6-g0-figure
You can build the k6 binary on various platforms, each with its requirements. The following shows how to build k6 binary with this extension on GNU/Linux distributions.
You must have the latest Go version installed to build the k6 binary. The latest version should match k6 and xk6.
-
Install
xk6
:go install go.k6.io/xk6/cmd/xk6@latest
-
Build the binary:
xk6 build --with github.com/szkiba/xk6-g0-figure@latest
If you want to add a feature or make a fix, clone the project and build it using the following commands. The xk6 will force the build to use the local clone instead of fetching the latest version from the repository. This process enables you to update the code and test it locally.
git clone git@github.com:szkiba/xk6-g0.git && cd xk6-g0-figure
xk6 build --with github.com/szkiba/xk6-g0-figure@latest=.
There is an example in the example directory that show how to use the extension.
command
./k6 run example/script.go
script.go
package main
import "github.com/common-nighthawk/go-figure"
func Default() {
figure.NewFigure("Hello World!", "", true).Print()
}
output
/\ |‾‾| /‾‾/ /‾‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (‾) |
/ __________ \ |__| \__\ \_____/ .io
execution: local
script: -
output: -
scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
* default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)
_ _ _ _ __ __ _ _ _
| | | | ___ | | | | ___ \ \ / / ___ _ __ | | __| | | |
| |_| | / _ \ | | | | / _ \ \ \ /\ / / / _ \ | '__| | | / _` | | |
| _ | | __/ | | | | | (_) | \ V V / | (_) | | | | | | (_| | |_|
|_| |_| \___| |_| |_| \___/ \_/\_/ \___/ |_| |_| \__,_| (_)
data_received........: 0 B 0 B/s
data_sent............: 0 B 0 B/s
iteration_duration...: avg=1.46ms min=1.46ms med=1.46ms max=1.46ms p(90)=1.46ms p(95)=1.46ms
iterations...........: 1 613.200866/s
running (00m00.0s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs 00m00.0s/10m0s 1/1 iters, 1 per VU