Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does k6 support golang as programming language? #751

Closed
HuiqianGithub opened this issue Aug 29, 2018 · 10 comments
Closed

Does k6 support golang as programming language? #751

HuiqianGithub opened this issue Aug 29, 2018 · 10 comments

Comments

@HuiqianGithub
Copy link

Do we have plan to support golang as programming language? Currently it only support javascript.

@na--
Copy link
Member

na-- commented Aug 30, 2018

Support for tests written in Go isn't currently planned, but since k6 uses interfaces for the Runner and VU (see the architecture), it's technically possible to implement other non-JS runners in the future. There are a lot of major challenges to overcome before that happens, but it isn't totally out of the question, especially since the Go compiler is written in Go and the standard library contains a lot (all?) of needed parts.

Here are some of the more obvious challenges we have to overcome before adding support for other script languages:

  1. There isn't a very clear separation between the JavaScript interface and goja values (goja being the JS runtime we use in k6) and the actual execution and measurements of the actions (HTTP request, websocket connections, etc.) that k6 performs. That's pretty obvious if you search for goja in the codebase, or even from the fact that the place in which a lot of the "business" logic is implemented is in the js/modules/k6 folder...
    Separating the JavaScript API and components from the "business logic" is the first step in any attempt to support other languages, but even if we don't plan to support other languages any time soon, I think it is a good idea regardless. It would make the code a lot cleaner and more testable and it would give us some implementation flexibility.
    Even if we don't do anything else, if this first step is done well, it would make parts of k6 (like the request measurement and metrics) much easier to use as a Go library from other Go applications.
  2. Since k6 scripts can be executed in the Load Impact cloud (and in the future, in a native clustering mode by k6), they need to be somewhat contained and unable to affect the environment around them very much for security reasons. That means that, for example in the case of Go, a lot of the standard library packages like os, unsafe, etc. shouldn't be available in k6 go-based "scripts".
  3. Additionally, k6 has to be able to measure things like http requests and websocket connections. So any go-based "scripts" can't use the standard library's http package and instead would have to use the k6-provided options, which would limit the usability of any such scripts, especially external libraries, tremendously.

@na--
Copy link
Member

na-- commented Jul 30, 2019

yaegi is a new project that could make this task much simpler:

@mstoykov
Copy link
Contributor

mstoykov commented Mar 9, 2020

WASM executors that might be used for running compiled wasm from golang:

All three need investigation for both speed over goja and features that we will need . I mostly linked the cgo one for completeness, I am highly against using something using cgo , if it can be avoided.

@na--
Copy link
Member

na-- commented Apr 9, 2021

Ah, we probably should have mentioned it here, in case anyone is following the issue... While it's still not possible to write Go "scripts" in k6, it has been possible to write extensions in Go with xk6 since k6 v0.29.0:

Moreover, there is already a basic xk6 extension with support for yaegi, i.e. "go scripting": https://github.com/dgzlopes/xk6-interpret

@jevonearth
Copy link

Has anyone made a basic test example written in go using xk6 that we can use for reference?

"Technically Possible" is a great start to a story of protracted technical adventure. ;)

@na--
Copy link
Member

na-- commented Apr 9, 2021

Not that I know of, but you can examine some of the many xk6 extensions that already exist for inspiration: https://github.com/search?q=xk6&type=Repositories

While most xk6 extensions are meant to be imported from normal JavaScript-based k6 scripts (e.g. import whatever from "k6/x/whatever") or, since recently, used as metric outputs, there's nothing stopping you from cramming your whole test logic in an "extension" and your whole k6 test script just being something like this:

export * from "k6/x/myloadtest";

You can create and push metrics from your extensions (see https://github.com/dgzlopes/xk6-remote-write for example) and really do anything you might like with Go code 🤷‍♂️ Just keep in mind that we don't give any guarantees about the stability of k6 Go APIs, so you might have to adjust the "extension" slightly when new k6 versions are released, though you'd be able to compile it with the old version indefinitely.

@dgzlopes
Copy link
Member

dgzlopes commented Apr 9, 2021

Also, just to reference two basic examples:

On the tutorial that @na-- referenced, there are instructions on how to build the Redis one from scratch.

@na--
Copy link
Member

na-- commented Mar 2, 2022

Some rationale related to this issue and other similar ones: https://k6.io/blog/why-k6-does-not-introduce-multiple-scripting-languages/

@bandorko
Copy link
Contributor

bandorko commented Oct 1, 2023

I've created an experimental tool that can be intresting in this topic. k6-go

@olegbespalov
Copy link
Contributor

Closing as not planned.

There is another extension that allows you to write tests in golang https://github.com/szkiba/xk6-g0.

@olegbespalov olegbespalov closed this as not planned Won't fix, can't repro, duplicate, stale Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants