Code Monkey home page Code Monkey logo

xk6's Introduction

xk6 - Custom k6 Builder

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).

Docker

The easiest way to use xk6 is via our Docker image. This avoids having to setup a local Go environment, and install xk6 manually.

For example, to build a k6 v0.43.1 binary on Linux with the xk6-kafka and xk6-output-influxdb extensions, you would run:

docker run --rm -it -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" grafana/xk6 build v0.43.1 \
  --with github.com/mostafa/[email protected] \
  --with github.com/grafana/[email protected]

This would create a k6 binary in the current working directory.

Note the use of the -u (user) option to specify the user and group IDs of the account on the host machine. This is important for the k6 file to have the same file permissions as the host user.

The -v (volume) option is also required to mount the current working directory inside the container, so that the k6 binary can be written to it.

Note that if you're using SELinux, you might need to add :z to the --volume option to avoid permission errors. E.g. -v "${PWD}:/xk6:z".

If you prefer to setup Go and use xk6 without Docker, see the "Local Installation" section below.

macOS

On macOS you will need to set the GOOS=darwin environment variable to build a macOS binary.

You can do this with the --env or -e argument to docker run:

docker run --rm -it -e GOOS=darwin -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" \
  grafana/xk6 build v0.43.1 \
  --with github.com/mostafa/[email protected] \
  --with github.com/grafana/[email protected]

Windows

On Windows you can either build a native Windows binary, or, if you're using WSL2, a Linux binary you can use in WSL2.

For the native Windows binary if you're using PowerShell:

docker run --rm -it -e GOOS=windows -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" `
  grafana/xk6 build v0.43.1 --output k6.exe `
  --with github.com/mostafa/xk6-kafka@v0.17.0 `
  --with github.com/grafana/xk6-output-influxdb@v0.3.0

For the native Windows binary if you're using cmd.exe:

docker run --rm -it -e GOOS=windows -v "%cd%:/xk6" ^
  grafana/xk6 build v0.43.1 --output k6.exe ^
  --with github.com/mostafa/xk6-kafka@v0.17.0 ^
  --with github.com/grafana/xk6-output-influxdb@v0.3.0

For the Linux binary on WSL2, you can use the same command as for Linux.

Local Installation

Requirements

Install xk6

go install go.k6.io/xk6/cmd/xk6@latest

This 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.

Command usage

The xk6 command has two primary uses:

  1. Compile custom k6 binaries
  2. A replacement for go run while 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.

Custom builds

Syntax:

xk6 build [<k6_version>]
    [--output <file>]
    [--with <module[@version][=replacement]>...]
    [--replace <module=replacement>...]
  • <k6_version> is the core k6 version to build; defaults to K6_VERSION env variable or whatever is the latest version needed by all extensions. For example, if extension A requires k6 v0.41.0 and extension B requires k6 v0.43.0, the final k6 version used in the binary will be v0.43.0. Note that depending on the differences in these versions, this behavior might cause the build to fail. This is something enforced by the Go build system, and we have no way of fixing it.
  • --output changes the output file.
  • --with can be used multiple times to add extensions by specifying the Go module name and optionally its version, similar to go 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.
  • --replace can be used multiple times to add replacements by specifying the Go module name and the replacement module, similar to go mod edit -replace=. Version of the replacement can be specified with the @version suffix in the replacement path.

Examples:

xk6 build \
    --with github.com/grafana/xk6-browser

xk6 build v0.35.0 \
    --with github.com/grafana/[email protected]

xk6 build \
    --with github.com/grafana/xk6-browser=../../my-fork

xk6 build \
    --with github.com/grafana/xk6-browser=.

xk6 build \
    --with github.com/grafana/[email protected]=../../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-browser

For extension development

If 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 the k6 command.

For example:

xk6 version
xk6 run -u 10 -d 10s test.js

The race detector can be enabled by setting the env variable XK6_RACE_DETECTOR=1 or through the XK6_BUILD_FLAGS env variable.

Library usage

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.

Environment variables

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_VERSION sets the version of k6 to build.
  • XK6_BUILD_FLAGS sets any go build flags if needed. Defaults to '-ldflags=-w -s'.
  • XK6_RACE_DETECTOR=1 enables the Go race detector in the build.
  • XK6_SKIP_CLEANUP=1 causes xk6 to leave build artifacts on disk after exiting.
  • XK6_K6_REPO optionally sets the path to the main k6 repository. This is useful when building with k6 forks.

Keeping dependencies in sync

We recommend extension maintainers to keep dependencies in common with k6 core in the same version k6 core uses. This guarantees binary compatibility of the JS runtime, and ensures uses will not have to face unforeseen build-time errors when compiling several extensions together with xk6.

The go-depsync tool can check for this automatically and produce a go get command that syncs common dependencies:

/your/extension$ go-depsync --parent go.k6.io/k6

This project originally forked from the xcaddy project. Thank you!

xk6's People

Contributors

andrewslotin avatar ansrivas avatar chaudum avatar francislavoie avatar gauravgpta93 avatar javaducky avatar jaysonsantos avatar jpughcs avatar kempsterc avatar mholt avatar mohammed90 avatar mstoykov avatar na-- avatar olegbespalov avatar oleiade avatar pablochacin avatar roobre avatar ryoshindo avatar szkiba avatar thgruiz avatar wolfogre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xk6's Issues

Request to maintain xk6 extensions as a part of monorepo

Currently, there are many different xk6 repos for each extension and thus making the extensions easy to develop. However, it seems that the extension's dependency is getting out of sync a lot and many versions of the same artifact are available in different extensions, including k6 itself (0.28...0...0.40.0). In fact go versions are also ranging out from 1.15 -> 1.18 within extensions.

Being said that it's very common for us to use different extensions together, like load test SQL and send metrics to Prometheus.

I sincerely request that k6 maintainers consider providing a monorepo for all the k6 extensions. This also lowers the barrier to submitting new k6 extensions to a common pool, auto-generate docs, enforcing better code reviews etc...

We also created an xk6 extension for Cassandra/scylladb https://github.com/rverma-nsl/xk6-cassandra which we would love to contribute.

Note: We can merge these repos without loosing commits from initial contributors using the steps https://stackoverflow.com/questions/1425892/how-do-you-merge-two-git-repositories.

If grafana creates an empty repo with xk6-extensions with a readme, I can contribute with merged repo for the extensions we are using.

'GoError: unexpected EOF' when running behind corporate proxy

Hi,

I've just made my first xk6 extension which was very fun! Basically it allows you to talk to NATS from k6 scripts.
Everything is working fine on localhost (as always), however I see an error when running in our staging environment, where every outgoing HTTP request must go through our corporate proxy.

Extension code:

package nats

import (
	"context"

	"time"

	"github.com/loadimpact/k6/js/common"
	"github.com/nats-io/nats.go"

	"github.com/loadimpact/k6/js/modules"
)

func init() {
	modules.Register("k6/x/nats", new(Nats))
}

type Nats struct{}

type Client struct {
	client         *nats.Conn
	defaultTimeout time.Duration
}

func (r *Nats) XClient(ctxPtr *context.Context, natsServer string, timeoutMS int) interface{} {
	rt := common.GetRuntime(*ctxPtr)

	conn, err := nats.Connect(natsServer)
	if err != nil {
		panic(err)
	}
	var timeout time.Duration
	if timeoutMS == 0 {
		timeout = time.Millisecond * 5 // 5 second default timeout
	} else {
		timeout = time.Millisecond * time.Duration(timeoutMS)
	}
	return common.Bind(rt, &Client{client: conn, defaultTimeout: timeout}, ctxPtr)
}

func (c *Client) Request(subject string, payload string) (string, error) {
	msg, err := c.client.Request(subject, []byte(payload), c.defaultTimeout)
	if err != nil {
		return "", err
	}
	return string(msg.Data), err
}

func (c *Client) Publish(subject string, payload string) error {
	return c.client.Publish(subject, []byte(payload))
}

Which gets built into a binary:

$ xk6 build v0.31.1 --with github.com/username/xk6-nats-bridge="/home/username/go/src/github.com/username/xk6-nats-bridge"

Used in a script:

import { check } from 'k6';
import nats from 'k6/x/nats';
import * as faker from 'faker/locale/en_US';

export let options = {
  vus: 10,
  duration: '30s',
};

const natsTimeoutMS = 2000;
const natsServer = 'nats:4222';
const natsClient = new nats.Client(natsServer, natsTimeoutMS);

export default function () {
  const want = 'sample data'
  const payload = faker.name.findName()
  const subject = 'testSubject';
  var got = natsClient.request(subject, payload)
  check(got, {
    'Correct data received from NATS': (data) => data === want
  })

  const reportingSubject = 'reports.loadTest';
  natsClient.publish(reportingSubject, 'Tested OK!');
}

The script get bundled with webpack to include

When running the custom binary the proxy settings are set as env vars:

$ export http_proxy=http://proxy.domain.com
$ export HTTP_PROXY=http://proxy.domain.com
$ export https_proxy=http://proxy.domain.com
$ export HTTPS_PROXY=http://proxy.domain.com
$ k6 run main.js

The code works fine when running on my local kubernetes cluster (no proxy), but fails on another cluster that uses the proxy.

It fails with the following error:

time="2021-03-23T16:21:43Z" level=error msg="GoError: unexpected EOF\n\tat github.com/loadimpact/k6/js/common.Bind.func1 (native)\n\tat file:///main.js:1:3972(90)\n" executor=constant-vus scenario=default source=stacktrace

I know that xk6 is bleeding edge and experimental, but any pointers on what goes wrong, or how to increase logging will be much appreciated.

Cannot install xk6 - 403 AuthorizedOnly Error

Hi,

I want to install xk6 using the following command
go install go.k6.io/xk6/cmd/xk6@latest
But it fails with below error:

go: go.k6.io/xk6/cmd/xk6@latest: module go.k6.io/xk6/cmd/xk6: reading https://proxy.golang.org/go.k6.io/xk6/cmd/xk6/@v/list: 403 AuthorizedOnly

I'm behind corporate proxy and I set the HTTPS_PROXY / HTTP_PROXY environnement variables but it does not helo.

Where I can find the direct link for the xk6 executable to download it directly?

Thanks.

go 1.16 compatibitity

TLDR

using go 1.16 :

2021/03/01 18:02:48 [INFO] exec (timeout=0s): /usr/local/go/bin/go build -o /home/pierrot/Downloads/k6 -ldflags -w -s -trimpath 
go: k6: package github.com/loadimpact/k6/cmd imported from implicitly required module; to add missing requirements, run:
	go get github.com/loadimpact/k6/[email protected]

workarround (Working fine)

Artificially add the k6 repo as golang 1.16 does not implicitly adds it to go mod.

./xk6 build  --with github.com/pmalhaire/xk6-mqtt --with github.com/loadimpact/k6/cmd

details

I may have done something wrong but compiling my extension does not seam to work using golang 1.16

with go 1.16

go version go1.16 linux/amd64
xk6 build  --with github.com/pmalhaire/xk6-mqtt 
2021/03/01 18:02:47 [INFO] Temporary folder: /tmp/buildenv_2021-03-01-1802.161811804
2021/03/01 18:02:47 [INFO] Writing main module: /tmp/buildenv_2021-03-01-1802.161811804/main.go
2021/03/01 18:02:47 [INFO] Initializing Go module
2021/03/01 18:02:47 [INFO] exec (timeout=10s): /usr/local/go/bin/go mod init k6 
go: creating new go.mod: module k6
go: to add module requirements and sums:
	go mod tidy
2021/03/01 18:02:47 [INFO] Pinning versions
2021/03/01 18:02:47 [INFO] exec (timeout=0s): /usr/local/go/bin/go get -d -v github.com/loadimpact/k6 
go get: added github.com/loadimpact/k6 v0.30.0
2021/03/01 18:02:48 [INFO] exec (timeout=0s): /usr/local/go/bin/go get -d -v github.com/pmalhaire/xk6-mqtt 
go get: upgraded github.com/loadimpact/k6 v0.30.0 => v0.30.1-0.20210208205855-a379c3787372
go get: added github.com/pmalhaire/xk6-mqtt v0.0.0-20210301160808-4ee3400d52b1
2021/03/01 18:02:48 [INFO] Build environment ready
2021/03/01 18:02:48 [INFO] Building k6
2021/03/01 18:02:48 [INFO] exec (timeout=0s): /usr/local/go/bin/go build -o /home/pierrot/Downloads/k6 -ldflags -w -s -trimpath 
go: k6: package github.com/loadimpact/k6/cmd imported from implicitly required module; to add missing requirements, run:
	go get github.com/loadimpact/k6/[email protected]
2021/03/01 18:02:48 [INFO] Cleaning up temporary folder: /tmp/buildenv_2021-03-01-1802.161811804
2021/03/01 18:02:48 [FATAL] exit status 1

with go 1.15 all fine

xk6 build  --with github.com/pmalhaire/xk6-mqtt 
2021/03/01 17:59:40 [INFO] Temporary folder: /tmp/buildenv_2021-03-01-1759.483292408
2021/03/01 17:59:40 [INFO] Writing main module: /tmp/buildenv_2021-03-01-1759.483292408/main.go
2021/03/01 17:59:40 [INFO] Initializing Go module
2021/03/01 17:59:40 [INFO] exec (timeout=10s): /usr/local/go/bin/go mod init k6 
go: creating new go.mod: module k6
2021/03/01 17:59:40 [INFO] Pinning versions
2021/03/01 17:59:40 [INFO] exec (timeout=0s): /usr/local/go/bin/go get -d -v github.com/loadimpact/k6 
go: github.com/loadimpact/k6 upgrade => v0.30.0
2021/03/01 17:59:40 [INFO] exec (timeout=0s): /usr/local/go/bin/go get -d -v github.com/pmalhaire/xk6-mqtt 
go: github.com/pmalhaire/xk6-mqtt upgrade => v0.0.0-20210301160808-4ee3400d52b1
2021/03/01 17:59:41 [INFO] Build environment ready
2021/03/01 17:59:41 [INFO] Building k6
2021/03/01 17:59:41 [INFO] exec (timeout=0s): /usr/local/go/bin/go build -o /home/pierrot/stereolabs/xk6-mqtt/k6 -ldflags -w -s -trimpath 
2021/03/01 17:59:50 [INFO] Build complete: ./k6
2021/03/01 17:59:50 [INFO] Cleaning up temporary folder: /tmp/buildenv_2021-03-01-1759.483292408

Remove goreleaser

As explained in #59 (comment), we decided to remove goreleaser and stop publishing pre-built binaries on each new release. We would still create releases as usual, it's just that the uploaded artifacts would only include the packaged source code.

Unable to run xk6 from ubuntu-latest and Mac using homebrew

Following the steps below I get an error. Both on Mac where k6 is installed via homebrew, and on ubuntu-latest image where its installed using apt-get.

cd k6_tests
go install github.com/k6io/xk6/cmd/xk6@latest (succeeds)
xk6 build master --with github.com/imiric/xk6-sql (fails with: command not found: xk6)

I feel like I am missing something super basic.

k6 run test.js reports:

ERRO[0000] GoError: unknown module: k6/x/sql
at reflect.methodValueCall (native)
at file:///Users/myusername/Documents/Repositories/k6_tests/test.js:25:22(26) hint="script exception"

Migrate from deprecated features in Github Actions

What?

We should update the GitHub Action pipelines since several features that we're using are deprecated and will be removed:

  • The set-output and save-state commands are deprecated and will be disabled soon. We should use Environment Files. (sunset date 31st May 2023).
  • Node.js 12 actions are deprecated (sunset ~September 2023).

Why?

xk6 cannot run k6 if there is a k6 directory

xk6 cannot run the k6 command if there is a k6 directory (or a non-executable file, I guess) in the extension folder: fork/exec ./k6: permission denied.

Reproduce:

  • git clone https://github.com/grafana/xk6-browser (or any extension)
  • cd xk6-browser
  • mkdir k6
  • xk6 run something.js

Output:

2022/05/20 15:01:55 [INFO] Temporary folder: /Users/inanc/grafana/xk6-browser/buildenv_2022-05-20-1501.2156919550
2022/05/20 15:01:55 [INFO] Writing main module: /Users/inanc/grafana/xk6-browser/buildenv_2022-05-20-1501.2156919550/main.go
2022/05/20 15:01:55 [INFO] Initializing Go module
2022/05/20 15:01:55 [INFO] exec (timeout=10s): /usr/local/go/bin/go mod init k6 
go: creating new go.mod: module k6
go: to add module requirements and sums:
        go mod tidy
2022/05/20 15:01:55 [INFO] Replace github.com/grafana/xk6-browser => /Users/inanc/grafana/xk6-browser
2022/05/20 15:01:55 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod edit -replace github.com/grafana/xk6-browser=/Users/inanc/grafana/xk6-browser 
2022/05/20 15:01:55 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17 
go: finding module for package go.k6.io/k6/cmd
go: found go.k6.io/k6/cmd in go.k6.io/k6 v0.38.3
go: finding module for package github.com/nxadm/tail
go: found github.com/nxadm/tail in github.com/nxadm/tail v1.4.8
2022/05/20 15:01:56 [INFO] Pinning versions
2022/05/20 15:01:56 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod edit -require go.k6.io/k6@latest 
2022/05/20 15:01:56 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17 
2022/05/20 15:01:56 [INFO] Build environment ready
2022/05/20 15:01:56 [INFO] Building k6
2022/05/20 15:01:56 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17 
go: found github.com/grafana/xk6-browser in github.com/grafana/xk6-browser v0.0.0-00010101000000-000000000000
2022/05/20 15:01:56 [INFO] exec (timeout=0s): /usr/local/go/bin/go build -o /Users/inanc/grafana/xk6-browser/k6 -ldflags -w -s -trimpath 
2022/05/20 15:01:57 [INFO] Build complete: ./k6
2022/05/20 15:01:57 [INFO] Cleaning up temporary folder: /Users/inanc/grafana/xk6-browser/buildenv_2022-05-20-1501.2156919550
2022/05/20 15:01:57 [INFO] Running [./k6 run -q examples/browser_args.js]

2022/05/20 15:01:57 [ERROR] fork/exec ./k6: permission denied

Consumer waits 10 minutes when no message are coming

Hello,
I run a script where I produce a message to a topic A then waits for another message on a topic B.
Sometimes, message is not posted on topic B, and I would like the consumer to wait a few seconds, no more. Currently, it waits around 10 minutes.
I don't really understand why as I see that the MaxWait is configured to 200ms in the go consumer class.

provide help for build command

If I just type xk6, I'd get an obscure error like this:

go list -m: not using modules
2020/12/02 11:33:35 [ERROR] exec [go list -m]: exit status 1:

And xk6 -h or xk6 --help or xk6 help don't seem to do anything. This is not urgent, but something we shouldn't forget to polish eventually, hopefully without repeating the same mistakes with global as in k6. xk6 version should probably also be a thing, eventually.

Add option for synchronizing dependencies with k6

When developing an extension, it is convenient to keep dependencies shared with k6 synchronized at the same version to prevent conflicts.

In the context of the xk6-disruptor extension, @roobre developed a handy tool for checking the differences between the dependencies of an extension and k6

The tool looks for overlapping dependencies with k6 which do not match the version used in the extension. Those dependencies are logged to stderr, and go get commands are generated to stdout to download the version k6 core is using.

An example of running in the main branch of xk6-disruptor (at the time of writing this issue)

go run hack/depsync/depsync.go 
2023/09/14 16:17:56 detected k6 core version v0.46.0

2023/09/14 16:17:56 Mismatched versions for github.com/spf13/afero:
Ours: v1.2.2
Core: v1.1.2
go get github.com/spf13/[email protected]
2023/09/14 16:17:56 Mismatched versions for google.golang.org/grpc:
Ours: v1.57.0-dev
Core: v1.56.1
go get google.golang.org/[email protected]
2023/09/14 16:17:56 Mismatched versions for golang.org/x/sys:
Ours: v0.11.0
Core: v0.9.0
go get golang.org/x/[email protected]
2023/09/14 16:17:56 Mismatched versions for github.com/spf13/cobra:
Ours: v1.5.0
Core: v1.4.0
go get github.com/spf13/[email protected]

This functionality could be added to xk6 as a help for developers, either as a subcommand or as an option that automatically updates the version before building the binary.

go Mod not updated for transition

Trying to build a docker image:

# Build the k6 binary with the extension
FROM golang:1.16.4-buster as builder

RUN go install github.com/grafana/xk6/cmd/xk6@latest
RUN xk6 build --output /k6 --with github.com/szkiba/xk6-prometheus@latest

# Use the operator's base image and override the k6 binary
FROM loadimpact/k6:latest as runner
COPY --from=builder /k6 /usr/bin/k6

Results in:

 > [builder 2/3] RUN go install github.com/grafana/xk6/cmd/xk6@latest:                                                                                                                                                                                                                      
#9 1.856 go: downloading github.com/grafana/xk6 v0.4.2                                                                                                                                                                                                                                      
#9 1.979 go install github.com/grafana/xk6/cmd/xk6@latest: github.com/grafana/xk6@none updating to                                                                                                                                                                                          
#9 1.979        github.com/grafana/[email protected]: parsing go.mod:                                                                                                                                                                                                                              
#9 1.979        module declares its path as: github.com/k6io/xk6
#9 1.979                but was required as: github.com/grafana/xk6

Docker image does not support CGO_ENABLED on Mac

Some extensions depend on libraries written in C, requiring the CGO_ENABLED=1 environment setting. An example is xk6-sql to support using SQLite3 databases. The preference, of course, would be for extensions to utilize pure-Go libraries strictly, but this will not always be the case.

We need the xk6 builder image to allow for the CGO runtime.

Warn when old k6 version is used

After #44, xk6 will not always try to use the latest k6 version to build the binary, it will instead use the one specified in the extensions' go.mod file(s). However, as this forum thread has shown, this can cause problems for unmaintained extensions, when users expect to be able to access newer k6 built-in APIs.

So, it would be nice if xk6 emits a warning when we are going to use an older version of k6. It seems like we can do it by running go list -m -json -u -mod=readonly go.k6.io/k6 for the repo, it will produce something like this:

{
	"Path": "go.k6.io/k6",
	"Version": "v0.33.1-0.20210825161650-c932a28ff940",
	"Time": "2021-08-25T16:16:50Z",
	"Update": {
		"Path": "go.k6.io/k6",
		"Version": "v0.40.0",
		"Time": "2022-09-08T07:54:34Z"
	},
	"Dir": "/go/pkg/mod/go.k6.io/[email protected]",
	"GoMod": "/go/pkg/mod/cache/download/go.k6.io/k6/@v/v0.33.1-0.20210825161650-c932a28ff940.mod",
	"GoVersion": "1.16"
}

I guess we can compare the Time or semver-compare the Version attributed between the main object and Update 🤔 Or we can figure out some other way to detect newer k6 versions.

Can I build the custom extension on windows machine

I am able to build the k6 extension for excel in mac and linux environment. Can i also build it for windows?

I also observer that the k6 extension i create on Mac works only on mac and not on linux. Is my observation correct or there is a way i can build one extension for any operating system.

xk6 binary works fine locally, but fails in CI

I was using k6 v0.32.0, and after updating to a higher version (v0.45.1), the CI runs started failing during the build stage:

xk6 build v0.45.1 \
  --with github.com/tmieulet/xk6-cognito \
  --with github.com/MyPackage/xk6-email="${{ github.workspace }}/k6-tests/extensions/xk6-email"

Despite fixing the build issue, running k6 tests in the CI pipeline now fails, even though the same tests and build work fine locally. The error is as follows:

Copy code
          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  (‾)  | 
  / __________ \  |__| \__\ \_____/ .io

time="24-01-17T09:28:56Z" level=error msg="TypeError: could not convert function call parameter 0: could not convert *** to context.Context\n\tat reflect.methodValueCall (native)\n\tat webpack://k6/./src/auth/AuthProvider.ts:24:41(75)\n\tat webpack://k6/./src/account/AccountManager.ts:20:24(31)\n\tat webpack://k6/./src/index.ts:12:23(286)\n\tat file:///home/k6/main.js:764:12(3)\n" hint="script exception"

No code changes were made. The CI runs on Ubuntu 22.04 LTS (amd64), while locally it works on Darwin Kernel, macOS (x86_64).

Also, it fails if using a locally built docker image with the same error:

docker run --rm -v $PWD/build-output/:/home/k6 lightleo/xk6-ext:v0.0.1 run /home/k6/main.js

Pass `go build` arguments from env var

Currently changing go build arguments is very limited, and only -race is exposed via XK6_RACE_DETECTOR.

It would be useful to pass a generic env var like XK6_BUILD_ARGS to go build instead, so that, for example, debug symbols can be optionally included if desired, as we recently discussed in Slack. We should get rid of XK6_RACE_DETECTOR in that case, since it could be passed via XK6_BUILD_ARGS.

Problem parsing go.mod since k6 v0.32.0 update

Since the k6 update v0.32.0 went live (earlier today), the extension I was working on stopped working. The error I get is:

go: github.com/k6io/[email protected]: parsing go.mod:
	module declares its path as: go.k6.io/k6
	        but was required as: github.com/k6io/k6

I started having this error even without updating anything, and trying to build with v0.31.1 or v0.32.0. k6 mentions a breaking change for the xk6 extensions, but I do not know how to fix this.

The expected outcome is that the xk6 build command works, either with v0.31.1 or v0.32.0.

cant build custom extensions with k6 flag --profiling-enabled

k6 have flag --profiling-enabled

Usage:
  k6 [command]
Flags:
      --profiling-enabled   enable profiling (pprof) endpoints, k6's REST API should be enabled as well

but after build flag does not exist

CGO_ENABLED=1 xk6 build --output k6_extensions --with k6_extensions=. 
 ./k6_extensions  run 1_test.js --profiling-enabled

ERRO[0000] unknown flag: --profiling-enabled 

Using `.` for replace should work

Running
xk6 build --with something/else=.

inside the directory of an extension should work instead you will get

go mod: -replace=github.com/mstoykov/xk6-counter=.: invalid new path: malformed import path ".": invalid path element "."

Separate xk6 library from xk6 and k6 binaries

Organize the code of xk6 into two main repositories, one for the functionalities that could be used by multiple binaries (k6, xk6, build service) and the other for the xk6 command. This would facilitate the versioning of the library independently of the xk6 command versioning (which we expect to have more changes initially

Tasks

Can't build custom extension for k6

Brief summary

Im try build custom extension for k6.
But I have an error while building.
Xk6 does not see types in package that I am importing in the extension.

k6 version

v0.42.0

OS

macOS 11

Docker version and image (if applicable)

No response

Steps to reproduce the problem

xk6 build --output xk6Custom --with github.com/ChipArtem/bugxk6

Expected behaviour

Package xk6Custom created

Actual behaviour

Error:
../../../../go/pkg/mod/github.com/ibm-messaging/[email protected]/jms20subset/MQOptions.go:5:32: undefined: ibmmq.MQCNO
(I checked, this type is imported correctly in mq-golang-jms20)

Full info:
2023/01/11 11:09:39 [INFO] Temporary folder: /Users/chip/git_repo/bugxk6/buildenv_2023-01-11-1109.559430917
2023/01/11 11:09:39 [INFO] Initializing Go module
2023/01/11 11:09:39 [INFO] exec (timeout=10s): /usr/local/go/bin/go mod init k6
go: creating new go.mod: module k6
2023/01/11 11:09:39 [INFO] Pinning versions
2023/01/11 11:09:39 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod edit -require github.com/ChipArtem/bugxk6@latest
2023/01/11 11:09:39 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17
go: downloading github.com/ChipArtem/bugxk6 v0.0.0-20230111080905-ebbb48a21070
go: finding module for package github.com/nxadm/tail
go: finding module for package github.com/google/go-cmp/cmp
go: found github.com/google/go-cmp/cmp in github.com/google/go-cmp v0.5.9
go: found github.com/nxadm/tail in github.com/nxadm/tail v1.4.8
2023/01/11 11:09:55 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17
2023/01/11 11:09:55 [INFO] Writing main module: /Users/chip/git_repo/bugxk6/buildenv_2023-01-11-1109.559430917/main.go
2023/01/11 11:09:55 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17
2023/01/11 11:09:55 [INFO] Build environment ready
2023/01/11 11:09:55 [INFO] Building k6
2023/01/11 11:09:55 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17
2023/01/11 11:09:55 [INFO] exec (timeout=0s): /usr/local/go/bin/go build -o /Users/chip/git_repo/bugxk6/xk6 -ldflags=-w -s -trimpath
#github.com/ibm-messaging/mq-golang-jms20/jms20subset
../../../../go/pkg/mod/github.com/ibm-messaging/[email protected]/jms20subset/MQOptions.go:5:32: undefined: ibmmq.MQCNO
../../../../go/pkg/mod/github.com/ibm-messaging/[email protected]/jms20subset/MQOptions.go:8:25: undefined: ibmmq.MQCNO
2023/01/11 11:09:56 [INFO] Cleaning up temporary folder: /Users/chip/git_repo/bugxk6/buildenv_2023-01-11-1109.559430917
2023/01/11 11:09:56 [FATAL] exit status 2

xk6 failing to build due to missing 3rd party dependencies

I dont know if you manage these in grafana or k6, but this is happening since yesterday

❯ ./xk6 build
2022/09/28 09:39:40 [INFO] Temporary folder: /tmp/buildenv_2022-09-28-0939.48910491
2022/09/28 09:39:40 [INFO] Initializing Go module
2022/09/28 09:39:40 [INFO] exec (timeout=10s): /usr/bin/go mod init k6
go: creating new go.mod: module k6
2022/09/28 09:39:40 [INFO] Pinning versions
2022/09/28 09:39:40 [INFO] exec (timeout=0s): /usr/bin/go mod tidy -compat=1.17
go: warning: "all" matched no packages
2022/09/28 09:39:40 [INFO] Writing main module: /tmp/buildenv_2022-09-28-0939.48910491/main.go
2022/09/28 09:39:40 [INFO] exec (timeout=0s): /usr/bin/go mod tidy -compat=1.17
go: finding module for package go.k6.io/k6/cmd
go: found go.k6.io/k6/cmd in go.k6.io/k6 v0.40.0
go: finding module for package github.com/spf13/afero
go: finding module for package github.com/mailru/easyjson
go: finding module for package github.com/Soontao/goHttpDigestClient
go: finding module for package github.com/spf13/pflag
go: finding module for package github.com/davecgh/go-spew/spew
go: finding module for package github.com/oxtoacart/bpool
go: finding module for package github.com/go-sourcemap/sourcemap
go: finding module for package github.com/mailru/easyjson/jlexer
go: finding module for package gopkg.in/guregu/null.v3
go: finding module for package github.com/mailru/easyjson/jwriter
go: found github.com/spf13/afero in github.com/spf13/afero v1.9.2
go: found gopkg.in/guregu/null.v3 in gopkg.in/guregu/null.v3 v3.5.0
go: finding module for package github.com/spf13/pflag
go: finding module for package github.com/mailru/easyjson/jwriter
go: finding module for package github.com/mailru/easyjson/jlexer
go: finding module for package github.com/davecgh/go-spew/spew
go: finding module for package github.com/mailru/easyjson
go: finding module for package github.com/go-sourcemap/sourcemap
go: finding module for package github.com/oxtoacart/bpool
go: finding module for package github.com/Soontao/goHttpDigestClient
k6 imports
        go.k6.io/k6/cmd imports
        github.com/spf13/pflag: module github.com/spf13/pflag@latest found (v1.0.5), but does not contain package github.com/spf13/pflag
k6 imports
        go.k6.io/k6/cmd imports
        go.k6.io/k6/cloudapi imports
        github.com/mailru/easyjson: module github.com/mailru/easyjson@latest found (v0.7.7), but does not contain package github.com/mailru/easyjson
k6 imports
        go.k6.io/k6/cmd imports
        go.k6.io/k6/cloudapi imports
        github.com/mailru/easyjson/jlexer: module github.com/mailru/easyjson@latest found (v0.7.7), but does not contain package github.com/mailru/easyjson/jlexer
k6 imports
        go.k6.io/k6/cmd imports
        go.k6.io/k6/cloudapi imports
        github.com/mailru/easyjson/jwriter: module github.com/mailru/easyjson@latest found (v0.7.7), but does not contain package github.com/mailru/easyjson/jwriter
k6 imports
        go.k6.io/k6/cmd imports
        go.k6.io/k6/js imports
        github.com/oxtoacart/bpool: module github.com/oxtoacart/bpool@latest found (v0.0.0-20190530202638-03653db5a59c), but does not contain package github.com/oxtoacart/bpool
k6 imports
        go.k6.io/k6/cmd imports
        go.k6.io/k6/js imports
        go.k6.io/k6/js/compiler imports
        github.com/go-sourcemap/sourcemap: package github.com/go-sourcemap/sourcemap provided by github.com/go-sourcemap/sourcemap at latest version v2.1.3+incompatible but not at required version v2.1.4-0.20211119122758-180fcef48034+incompatible
k6 imports
        go.k6.io/k6/cmd imports
        go.k6.io/k6/output/cloud imports
        go.k6.io/k6/lib/netext/httpext imports
        github.com/Soontao/goHttpDigestClient: module github.com/Soontao/goHttpDigestClient@latest found (v0.0.0-20170320082612-6d28bb1415c5), but does not contain package github.com/Soontao/goHttpDigestClient
k6 imports
        go.k6.io/k6/cmd tested by
        go.k6.io/k6/cmd.test imports
        github.com/stretchr/testify/assert imports
        github.com/davecgh/go-spew/spew: module github.com/davecgh/go-spew@latest found (v1.1.1), but does not contain package github.com/davecgh/go-spew/spew
2022/09/28 09:39:42 [INFO] Cleaning up temporary folder: /tmp/buildenv_2022-09-28-0939.48910491
2022/09/28 09:39:42 [FATAL] exit status 1

Improve the end-of-build message

xk6 currently makes assumptions that users are familiar with go build system and understand what $PATH is.
New users often assume that xk6 build process will replace their existing k6 installation. This is not the case, instead, the new build is in a $GOPATH directory, which is usually not part of $PATH.

I suggest the following:

  1. At the end of the xk6 build process we print instructions on where to locate the new build and how to use it
  2. [bonus for extra points] Detect the path of globally installed k6 and give the user the exact command to replace it with the new build (sudo cp /gopath/bin/k6 /usr/local/bin/k6)

Suggestion for the message

***************************************************
* BUILD COMPLETE - FOLLOW THESE STEPS TO CONTINUE *
***************************************************
xk6 has now produced a new k6 binary including "{xk6-redis}" extension. 
The new binary is located in {`/home/sniku/go/bin/k6`}. 
To use it you need to call it directly `./home/sniku/go/bin/k6 run script.js` 
or replace your globally installed k6 with the new one. 

Using 'xk6 run --with' should be possible

Currently 'xk6 run --with' will correctly build the binary(including the correct extensions), but then will also send the --with to the k6 which will at that point exit with an error as it doesn't know of such a flag.

Feature Request: to show response_code

Hi,

Thanks for creating this great tool.

I am a new k6 user and noticed we can make use of xk6 for output dashboard.

When I try the test cases, am not able to see the response code on the dashboard.

Is it expected?.
@imiric
Can you able to add the support, if its not present currently, thanks.

Add -buildvcs=false build option

Hi!
There is an error occurred while run build command

$ uname 
5.4.0-125-generic #141-Ubuntu SMP Wed Aug 10 13:42:03 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
$ go version
go version go1.19.1 linux/amd64
$ xk6 build v0.40.0 --with github.com/grafana/xk6-output-prometheus-remote
2022/09/16 20:42:36 [INFO] Temporary folder: /tmp/buildenv_2022-09-16-2042.2109028401
2022/09/16 20:42:36 [INFO] Initializing Go module
2022/09/16 20:42:36 [INFO] exec (timeout=10s): /usr/local/go/bin/go mod init k6 
go: creating new go.mod: module k6
2022/09/16 20:42:36 [INFO] Pinning versions
2022/09/16 20:42:36 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod edit -require github.com/grafana/xk6-output-prometheus-remote@latest 
2022/09/16 20:42:36 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17 
2022/09/16 20:42:37 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17 
2022/09/16 20:42:37 [INFO] Writing main module: /tmp/buildenv_2022-09-16-2042.2109028401/main.go
2022/09/16 20:42:37 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod edit -require go.k6.io/[email protected] 
2022/09/16 20:42:37 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17 
2022/09/16 20:42:37 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17 
2022/09/16 20:42:38 [INFO] Build environment ready
2022/09/16 20:42:38 [INFO] Building k6
2022/09/16 20:42:38 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17 
2022/09/16 20:42:38 [INFO] exec (timeout=0s): /usr/local/go/bin/go build -o /tmp/k6 -ldflags -w -s -trimpath 
error obtaining VCS status: exit status 128
	Use -buildvcs=false to disable VCS stamping.
2022/09/16 20:42:38 [INFO] Cleaning up temporary folder: /tmp/buildenv_2022-09-16-2042.2109028401
2022/09/16 20:42:38 [FATAL] exit status 1

Build k6 binary [xk6-redis]

Is this the last command line to build the published version of xk6-redis?
Because I get some error when I try to run this command!

xk6 build v0.32.0 --with github.com/k6io/xk6-redis

Add integration tests

The current testing situation in xk6 is... practically nonexistent. The Go coverage tool reports a measly 1%. :(

Unit testing is difficult with a tool like this as it depends directly on the Go toolchain and the filesystem, though that could be abstracted in a way to make testing easier.

For now integration/E2E tests seem like the path of least resistance, and @na-- has recently added some in the k6 repo.

The goal of this issue is to essentially use those tests as base, add them to this repo and expand them to specifically test xk6 functionality. For example, testing:

  • that all variations of the --with option work as expected
  • behavior with and without the build argument
  • behavior with and without the version argument
  • that environment variables work as expected

Since changes in xk6 are much less frequent than in k6, it would be good to have the CI task here run on a schedule, say on a weekly basis, or even daily.

The simple extensions used for tests could also serve as documentation and a good starting point for new contributors, so consider having them in a root examples/ directory.

Failure to install xk6

I'm just including this on the xk6 issue tracker for discoverability.

I installed go using the .pkg file and with brew. In both cases, I ended up with the following error:
$ go version
go version go1.17.6 darwin/amd64
$ go install go.k6.io/xk6/cmd/xk6@latest
$ xk6
zsh: command not found: xk6

The solution was not totally clear in this related discussion: https://community.k6.io/t/unable-to-locate-xk6-after-installing-it/1866

Here is a gist which helps handle every case https://gist.github.com/vsouza/77e6b20520d07652ed7d

Allow compiling with debug info

xk6 builder has hardcoded the options for removing debug info:

"-ldflags", "-w -s", // trim debug symbols

This prevent using debugging tools. For example, dlv throws the following error message:

could not launch process: decoding dwarf section info at offset 0x0: too short - debuggee must not be built with 'go run' or -ldflags='-s -w', which strip debug info

It would be convenient to add a flag or environment variable for changing this behavior.

Add possibility to add `replace` to the generated `go.mod`

Certain projects have the need for defining replace directives in their go.mod to build.

I've tried to use the --with argument to pass additional replace directives:

XK6_SKIP_CLEANUP=1 xk6 build \
  --with k8s.io/client-go="k8s.io/client-go v0.21.0" \
  --with k8s.io/api="k8s.io/api v0.21.0" \
  --with github.com/chaudum/xk6-loki="$(pwd)"

This will correctly add replaces in the resulting go.mod:

module k6

go 1.16

replace k8s.io/client-go => k8s.io/client-go v0.21.0

replace k8s.io/api => k8s.io/api v0.21.0

replace github.com/chaudum/xk6-loki => /home/christian/sandbox/chaudum/xk6-loki

require go.k6.io/k6 latest

However it also adds imports in the main.go:

package main

import (
	k6cmd "go.k6.io/k6/cmd"

	// plug in k6 modules here
	// TODO: Create /modules/standard dir structure?
	// _ "go.k6.io/k6/modules/standard"
	_ "k8s.io/client-go"
	_ "k8s.io/api"
	_ "github.com/chaudum/xk6-loki"
)

func main() {
	k6cmd.Execute()
}

The problem with these imports is, that there are no top-level packages for e.g. k8s.io/client-go,
resulting in the following error:

go: finding module for package k8s.io/client-go
k6 imports
	k8s.io/client-go: module k8s.io/client-go@latest found (v0.0.0-00010101000000-000000000000, replaced by k8s.io/[email protected]), but does not contain package k8s.io/client-go

Removing the imports and running go mod tidy && go build -o xk6-loki main.go in the build environment manually solves the problem.

I would like to suggest to add an option to add replace directives without adding the package imports to the main.go file. This could for example be done with a separate --replace argument.

standard_init_linux.go:219: exec user process caused: exec format error

Hi guys,

we have an extension that works well locally but when we are going to test it in a container it fails.

We are compiling the binary as follows:
xk6 build v0.33.0 --with dev.azure.com/SiigoDevOps/Siigo/_git/Siigo.K6.Extensions.git=$(pwd)
and
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 xk6 build v0.33.0 --with dev.azure.com/SiigoDevOps/Siigo/_git/Siigo.K6.Extensions.git=$(pwd)

but the container does not work generating the following error

standard_init_linux.go:219: exec user process caused: exec format error

Do you know what it could be? we have tried everything but we have not succeeded :(

thanks

zsh: command not found: xk6

I have used this guide:

https://github.com/grafana/xk6#install

to install xk6 on my Mac M1 laptop but get this error:

$ go install go.k6.io/xk6/cmd/xk6@latest
$ xk6
zsh: command not found: xk6

$ which go
/opt/homebrew/bin/go
$ go version
go version go1.19 darwin/arm64

$ which k6
/opt/homebrew/bin/k6
$ k6 version
k6 v0.39.0 ((devel), go1.18.3, darwin/arm64)

what am I missing?

Update for k6 org change

I now get this error when building my docker image:

> Step 4/12 : RUN go get -u github.com/k6io/xk6/cmd/xk6 &&     xk6 build v0.29.0 --with github.com/mostafa/xk6-kafka
>  ---> Running in 3ded9e36f3a5
> 2021/03/30 18:33:27 [INFO] Temporary folder: /tmp/buildenv_2021-03-30-1833.563133095
> 2021/03/30 18:33:27 [INFO] Writing main module: /tmp/buildenv_2021-03-30-1833.563133095/main.go
> 2021/03/30 18:33:27 [INFO] Initializing Go module
> 2021/03/30 18:33:27 [INFO] exec (timeout=10s): /usr/local/go/bin/go mod init k6 
> go: creating new go.mod: module k6
> 2021/03/30 18:33:27 [INFO] Pinning versions
> 2021/03/30 18:33:27 [INFO] exec (timeout=0s): /usr/local/go/bin/go get -d -v github.com/k6io/[email protected] 
> go: downloading github.com/k6io/k6 v0.29.0
> go get: github.com/k6io/[email protected]: parsing go.mod:
>         module declares its path as: github.com/loadimpact/k6
>                 but was required as: github.com/k6io/k6
> 2021/03/30 18:33:30 [FATAL] exit status 1

I believe this is related to k6 moving off loadimpact to k6io?

WebSocket assignment to entry in nil map

Greetings!

I'm trying to use xk6 with WebSocket:

package consumer

import (
	"context"
	"fmt"

	"github.com/dop251/goja"
	"go.k6.io/k6/js/common"
	"go.k6.io/k6/js/modules"
	"go.k6.io/k6/js/modules/k6/ws"
)

func init() {
	modules.Register("k6/x/graphql", new(GraphQL))
}

type GraphQL struct{}

type Client struct {
	rt     *goja.Runtime
	socket ws.Socket
	token  string
	query  string
}

func (c *GraphQL) XClient(ctxPtr *context.Context, socket ws.Socket, token string, query string) interface{} {
	rt := common.GetRuntime(*ctxPtr)
	return common.Bind(
		rt,
		&Client{
			rt:     rt,
			socket: socket,
			token:  token,
			query:  query,
		},
		ctxPtr,
	)
}

func (c *Client) Start() {
	c.socket.On("open", c.rt.ToValue(func() {
		c.socket.Send(fmt.Sprintf("{\"type\":\"connection_init\",\"payload\":{\"authToken\":\"%s\"}}", c.token))
	}))
}
export default function(token) {
    const params = {
        headers: {
            "sec-websocket-protocol": "graphql-transport-ws"
        }
    };

    const res = ws.connect(WS_URL, params, function(socket) {
        const client = new GraphQL.Client(socket, token, QUERY);
        client.start();
    });
    check(res, {
        "WebSocket /graphql status is 101": (r) => r && r.status === 101
    });
}

And getting errors like these:

ERRO[0002] panic: assignment to entry in nil map
goroutine 268 [running]:
runtime/debug.Stack(0xc005f014b0, 0xc005f014d0, 0xc005f014a0)
        runtime/debug/stack.go:24 +0x9f
go.k6.io/k6/js.(*VU).runFn.func1(0xc0008a5d60, 0xc005f03c38)
        go.k6.io/[email protected]/js/runner.go:737 +0x1e5
panic(0x10dcc00, 0x147b070)
        runtime/panic.go:965 +0x1b9
github.com/dop251/goja.AssertFunction.func1.1(0xc005f03b30)
        github.com/dop251/[email protected]/runtime.go:2090 +0x95
panic(0x10dcc00, 0x147b070)
        runtime/panic.go:965 +0x1b9
github.com/dop251/goja.(*vm).try.func1(0xc0022eca00, 0x0, 0xc005f039e0, 0x0, 0x0, 0x0, 0xc005f03a78)
        github.com/dop251/[email protected]/vm.go:505 +0xc1d
panic(0x10dcc00, 0x147b070)
        runtime/panic.go:965 +0x1b9
github.com/dop251/goja.AssertFunction.func1.1(0xc005f02b78)
        github.com/dop251/[email protected]/runtime.go:2090 +0x95
panic(0x10dcc00, 0x147b070)
        runtime/panic.go:965 +0x1b9
github.com/dop251/goja.(*vm).try.func1(0xc0022eca00, 0x2, 0xc005f02a28, 0x8, 0x0, 0x0, 0xc005f02ac0)
        github.com/dop251/[email protected]/vm.go:505 +0xc1d
panic(0x10dcc00, 0x147b070)
        runtime/panic.go:965 +0x1b9
go.k6.io/k6/js/modules/k6/ws.(*Socket).On(...)
        go.k6.io/[email protected]/js/modules/k6/ws/ws.go:329
<EXT>.(*Client).Start(0xc007ceed80)
        <EXT>@v0.0.0-00010101000000-000000000000/consumer.go:41 +0xd9
reflect.Value.call(0x1071300, 0xc001939940, 0x13, 0x126180a, 0x4, 0x1de9058, 0x0, 0x0, 0xc005f02500, 0x44fcec, ...)
        reflect/value.go:476 +0x8e7
reflect.Value.Call(0x1071300, 0xc001939940, 0x13, 0x1de9058, 0x0, 0x0, 0xc007e47440, 0x0, 0xc005f026d0)
        reflect/value.go:337 +0xb9
github.com/dop251/goja.(*Runtime).wrapReflectFunc.func1(0x14b7cf8, 0xc0084525a0, 0xc005861750, 0x0, 0xb, 0x1071300, 0xc001939940)
        github.com/dop251/[email protected]/runtime.go:1701 +0x3be
github.com/dop251/goja.(*vm)._nativeCall(0xc0022eca00, 0xc007e47440, 0x0)
        github.com/dop251/[email protected]/vm.go:2357 +0x266
github.com/dop251/goja.call.exec(0x0, 0xc0022eca00)
        github.com/dop251/[email protected]/vm.go:2329 +0x776
github.com/dop251/goja.(*vm).run(0xc0022eca00)
        github.com/dop251/[email protected]/vm.go:395 +0x94
github.com/dop251/goja.(*funcObject).call(0xc002f87ad0, 0x14b8548, 0x1de8ca0, 0xc002463340, 0x1, 0x1, 0x0, 0x0, 0x4de009, 0x11b59e0)
        github.com/dop251/[email protected]/func.go:161 +0x225
github.com/dop251/goja.(*funcObject).Call(...)
        github.com/dop251/[email protected]/func.go:129
github.com/dop251/goja.AssertFunction.func1.2()
        github.com/dop251/[email protected]/runtime.go:2095 +0x96
github.com/dop251/goja.(*vm).try(0xc0022eca00, 0xc005f02ae0, 0x0)
        github.com/dop251/[email protected]/vm.go:511 +0x188
github.com/dop251/goja.AssertFunction.func1(0x14b8548, 0x1de8ca0, 0xc002463340, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0)
        github.com/dop251/[email protected]/runtime.go:2094 +0x10b
go.k6.io/k6/js/modules/k6/ws.(*WS).Connect(0x1de9058, 0x14a6768, 0xc0019d8330, 0xc0005019c0, 0x1b, 0xc0018c8a60, 0x2, 0x2, 0x0, 0x0, ...)
        go.k6.io/[email protected]/js/modules/k6/ws/ws.go:219 +0xb33
reflect.Value.call(0x10beb40, 0x1de9058, 0x213, 0x1267225, 0x9, 0xc0063a9590, 0x3, 0x3, 0xb, 0x11a6ee0, ...)
        reflect/value.go:476 +0x8e7
reflect.Value.CallSlice(0x10beb40, 0x1de9058, 0x213, 0xc0063a9590, 0x3, 0x3, 0x194, 0x1, 0x2)
        reflect/value.go:350 +0xb9
go.k6.io/k6/js/common.Bind.func1(0x14b7cf8, 0xc004242ed0, 0xc0058616d0, 0x3, 0x13, 0x40, 0xc001b6f6c0)
        go.k6.io/[email protected]/js/common/bridge.go:241 +0x749
github.com/dop251/goja.(*vm)._nativeCall(0xc0022eca00, 0xc008146900, 0x3)
        github.com/dop251/[email protected]/vm.go:2357 +0x266
github.com/dop251/goja.call.exec(0x3, 0xc0022eca00)
        github.com/dop251/[email protected]/vm.go:2329 +0x776
github.com/dop251/goja.(*vm).run(0xc0022eca00)
        github.com/dop251/[email protected]/vm.go:395 +0x94
github.com/dop251/goja.(*funcObject).call(0xc004205520, 0x14b8548, 0x1de8ca0, 0xc00107f590, 0x1, 0x1, 0x0, 0x0, 0x442500, 0xc00517e180)
        github.com/dop251/[email protected]/func.go:161 +0x225
github.com/dop251/goja.(*funcObject).Call(...)
        github.com/dop251/[email protected]/func.go:129
github.com/dop251/goja.AssertFunction.func1.2()
        github.com/dop251/[email protected]/runtime.go:2095 +0x96
github.com/dop251/goja.(*vm).try(0xc0022eca00, 0xc005f03a98, 0x0)
        github.com/dop251/[email protected]/vm.go:511 +0x188
github.com/dop251/goja.AssertFunction.func1(0x14b8548, 0x1de8ca0, 0xc00107f590, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0)
        github.com/dop251/[email protected]/runtime.go:2094 +0x10b
go.k6.io/k6/js.(*VU).runFn(0xc0008a5d60, 0x14a6768, 0xc0019d8330, 0x1082001, 0xc00193ac18, 0xc00107f590, 0x1, 0x1, 0x0, 0x0, ...)
        go.k6.io/[email protected]/js/runner.go:742 +0x154
go.k6.io/k6/js.(*ActiveVU).RunOnce(0xc001901c00, 0x0, 0x0)
        go.k6.io/[email protected]/js/runner.go:693 +0x414
go.k6.io/k6/lib/executor.getIterationRunner.func1(0x14a66c0, 0xc0050312c0, 0x1490b60, 0xc001901c00, 0x1)
        go.k6.io/[email protected]/lib/executor/helpers.go:88 +0x5e
go.k6.io/k6/lib/executor.(*vuHandle).runLoopsIfPossible(0xc004efb320, 0xc001e0cc78)
        go.k6.io/[email protected]/lib/executor/vu_handle.go:225 +0x555
created by go.k6.io/k6/lib/executor.RampingVUs.Run
        go.k6.io/[email protected]/lib/executor/ramping_vus.go:579 +0xde6

Goja stack:
native 
ERRO[0002] a panic occurred in VU code but was caught: assignment to entry in nil map  executor=ramping-vus scenario=default

What am I doing wrong?

Default macOS image is not working

macOS 14.3.1 (Sonoma) - Apple M2

Tried the default command in the README:

docker run --rm -it -e GOOS=darwin -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" \
  grafana/xk6 build v0.43.1 \
  --with github.com/mostafa/[email protected] \
  --with github.com/grafana/[email protected]

I got this error:

go: k6 imports
	go.k6.io/k6/cmd imports
	github.com/grafana/xk6-output-prometheus-remote/pkg/remotewrite imports
	go.buf.build/grpc/go/prometheus/prometheus: unrecognized import path "go.buf.build/grpc/go/prometheus/prometheus": https fetch: Get "https://go.buf.build/grpc/go/prometheus/prometheus?go-get=1": dial tcp: lookup go.buf.build on 192.168.65.7:53: no such host
2024/03/11 10:40:46 [INFO] Cleaning up temporary folder: /tmp/buildenv_2024-03-11-1040.2198080098
2024/03/11 10:40:46 [FATAL] exit status 1

Add option to limit verbosity of build output

When running xk6 in a CI for generating multiple binaries (e.g. for multiple target GOOS and GOARCH) the output is noisy and makes hard to detect errors.

To improve this situation some current log outputs can be moved from INFO to DEBUG. Also a new environment variable XK6_LOG_LEVEL can be added to control amount of output generated.

Build failed in Win10 Docker

Build Env:
Win10 X64, Docker Desktop

Build Command:
docker run --rm -it -e GOOS=windows -v "%cd%:/xk6" grafana/xk6 build --output k6.exe --with github.com/topfreegames/xk6-pitaya=. --with github.com/topfreegames/pitaya/v2=../

Error Info:

go: finding module for package github.com/topfreegames/pitaya/v2/session
go: finding module for package github.com/topfreegames/pitaya/v2/client
go: finding module for package github.com/topfreegames/pitaya/v2
go: finding module for package github.com/topfreegames/pitaya/v2/conn/message
k6 imports
github.com/topfreegames/pitaya/v2: module github.com/topfreegames/pitaya/v2@latest found (v2.9.0, replaced by ../), but does not contain package github.com/topfreegames/pitaya/v2
k6 imports
github.com/topfreegames/xk6-pitaya imports
github.com/topfreegames/pitaya/v2/client: module github.com/topfreegames/pitaya/v2@latest found (v2.9.0, replaced by ../), but does not contain package github.com/topfreegames/pitaya/v2/client
k6 imports
github.com/topfreegames/xk6-pitaya imports
github.com/topfreegames/pitaya/v2/conn/message: module github.com/topfreegames/pitaya/v2@latest found (v2.9.0, replaced by ../), but does not contain package github.com/topfreegames/pitaya/v2/conn/message
k6 imports
github.com/topfreegames/xk6-pitaya imports
github.com/topfreegames/pitaya/v2/session: module github.com/topfreegames/pitaya/v2@latest found (v2.9.0, replaced by ../), but does not contain package github.com/topfreegames/pitaya/v2/session

How can I solve this problem? Thanks~

Goreleaser config & tool updates

What?

Currently, the latest goreleaser (v1.14.1) is failing on the check action (https://github.com/grafana/xk6/actions/runs/3985000350/jobs/6831849567) because our config is using the deprecated property.

At some point, it will be good to update the config (and all the required things) to use the new version.

    • DEPRECATED: `archives.replacements` should not be used anymore, check https://goreleaser.com/deprecations#archivesreplacements for more info

image

Why?

If the config will be up to date we could do updates at any point in the time (e.g. security fixes)

"no such host" at xk6 build

I want to build a custom binary with the following extension included:
github.com/gpiechnik2/xk6-proxy@latest

So, what I do is:

  1. mkdir xk6
  2. cd xk6
  3. xk6 build --with github.com/gpiechnik2/xk6-proxy@latest

And the output is always:

<...>
go: k6 imports
	go.k6.io/k6/cmd imports
	github.com/grafana/xk6-output-prometheus-remote/pkg/remotewrite imports
	go.buf.build/grpc/go/prometheus/prometheus: unrecognized import path "go.buf.build/grpc/go/prometheus/prometheus": https fetch: Get "https://go.buf.build/grpc/go/prometheus/prometheus?go-get=1": dial tcp: lookup go.buf.build: no such host
2024/03/05 15:55:36 [INFO] Cleaning up temporary folder: /Users/nikitades/xk6/buildenv_2024-03-05-1555.2159493344
2024/03/05 15:55:36 [FATAL] exit status 1

I tested it from various machines and even geographical locations and it always ends the same way. Can I skip prometheus dependency somehow?

Also, I tried to replace the broken package in the following fashion:

xk6 build \
    --with github.com/gpiechnik2/xk6-proxy@latest \
    --replace go.buf.build/grpc/go/prometheus/prometheus=buf.build/gen/go/prometheus/prometheus/grpc/go@latest

This looks like the new DNS record of the same package.
But still it results in package name mismatch:

go.buf.build/grpc/go/prometheus/prometheus: module go.buf.build/grpc/go/prometheus/prometheus@latest found (v1.4.9, replaced by buf.build/gen/go/prometheus/prometheus/grpc/[email protected]), but does not contain package go.buf.build/grpc/go/prometheus/prometheus

Is there a stable workaround?

Unable to set local path to build from Fork

Hi,

I need to build from source of a specific branch. So I pulled to branch and tried to build it, but it looks like the path is not accepted:

$ XK6_K6_REPO="$PWD" xk6 build

2021/08/24 11:52:11 [INFO] Temporary folder: /tmp/buildenv_2021-08-24-1152.237197142

2021/08/24 11:52:11 [INFO] Writing main module: /tmp/buildenv_2021-08-24-1152.237197142/main.go

2021/08/24 11:52:11 [INFO] Initializing Go module

2021/08/24 11:52:11 [INFO] exec (timeout=10s): /home/jaunius/.gvm/gos/go1.16.6/bin/go mod init k6 

go: creating new go.mod: module k6

go: to add module requirements and sums:

	go mod tidy

2021/08/24 11:52:11 [INFO] Pinning versions

2021/08/24 11:52:11 [INFO] exec (timeout=0s): /home/jaunius/.gvm/gos/go1.16.6/bin/go mod edit -require go.k6.io/k6@latest 

2021/08/24 11:52:11 [INFO] exec (timeout=10s): /home/jaunius/.gvm/gos/go1.16.6/bin/go mod edit -replace 
go.k6.io/k6=/home/jaunius/dev/builds/xk6/k6 

go: errors parsing go.mod:

/tmp/buildenv_2021-08-24-1152.237197142/go.mod:5: require go.k6.io/k6: version "latest" invalid: must be of the form v1.2.3

2021/08/24 11:52:11 [INFO] Cleaning up temporary folder: /tmp/buildenv_2021-08-24-1152.237197142

2021/08/24 11:52:11 [FATAL] exit status 1

xk6 should not put k6 binary in the k6/k6 if k6 is a directory

If there is a directory named k6 in the current directory, xk6 build will put the new k6 binary inside of it, instead of .. erroring out or putting in k6-extented or something like that.

Also maybe have a prompt before replacing files that is overwritten with -y ?

xk6 doesn't use the cached Go build process

This PR's title could be misleading, but here's what's going on. I don't change anything in my local extension directory. In this case, xk6 should use the cached results from the Go build process (it shouldn't recompile), but it seems like it does not.

The following build process happens every time I run xk6 run:

~/someExtension main* 7s ❯ xk6 run examples/script.js
2021/10/18 17:42:59 [INFO] Temporary folder: /Users/inanc/someExtension/buildenv_2021-10-18-1742.147706216
2021/10/18 17:42:59 [INFO] Writing main module: /Users/inanc/someExtension/buildenv_2021-10-18-1742.147706216/main.go
2021/10/18 17:42:59 [INFO] Initializing Go module
2021/10/18 17:42:59 [INFO] exec (timeout=10s): /usr/local/bin/go mod init k6 
go: creating new go.mod: module k6
go: to add module requirements and sums:
        go mod tidy
2021/10/18 17:42:59 [INFO] Replace github.com/grafana/someExtension => /Users/inanc/someExtension
2021/10/18 17:42:59 [INFO] exec (timeout=10s): /usr/local/bin/go mod edit -replace github.com/grafana/someExtension=/Users/inanc/someExtension 
2021/10/18 17:42:59 [INFO] Pinning versions
2021/10/18 17:42:59 [INFO] exec (timeout=0s): /usr/local/bin/go mod edit -require go.k6.io/k6@latest 
2021/10/18 17:42:59 [INFO] exec (timeout=0s): /usr/local/bin/go mod tidy 
go: found github.com/grafana/someExtension in github.com/grafana/someExtension v0.0.0-00010101000000-000000000000
2021/10/18 17:43:00 [INFO] Build environment ready
2021/10/18 17:43:00 [INFO] Building k6
2021/10/18 17:43:00 [INFO] exec (timeout=0s): /usr/local/bin/go mod tidy 
2021/10/18 17:43:00 [INFO] exec (timeout=0s): /usr/local/bin/go build -o /Users/inanc/someExtension/k6 -ldflags -w -s -trimpath 
2021/10/18 17:43:01 [INFO] Build complete: ./k6
2021/10/18 17:43:01 [INFO] Cleaning up temporary folder: /Users/inanc/someExtension/buildenv_2021-10-18-1742.147706216
2021/10/18 17:43:01 [INFO] Running [./k6 run examples/script.js]
...
running (00m01.9s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs  00m01.9s/10m0s  1/1 iters, 1 per VU
xk6 run examples/script.js  4.84s user 2.94s system 138% cpu 5.602 total

Change base image for xk6 image

Currently xk6 image is based on a Debian image, which brings a significant number of unneeded dependencies that increase the surface for vulnerabilities. Consider an image with a smaller attack surface such a alpine

However, this is potentially breaking change as:

  1. Some users can use this image as a base for their own images and relay on some tools available on the debian for their build processes (e.g. git, curl) which are not available in the alpine
  2. Images such as alpine doesn't have compatibility issues with extensions requiring cgo enabled (due to the lack of a proper standard glibc library)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.