Closed HuiqianGithub closed 12 months ago
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:
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.os
, unsafe
, etc. shouldn't be available in k6 go-based "scripts".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.yaegi
is a new project that could make this task much simpler:
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.
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
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. ;)
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 :man_shrugging: 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.
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.
Some rationale related to this issue and other similar ones: https://k6.io/blog/why-k6-does-not-introduce-multiple-scripting-languages/
I've created an experimental tool that can be intresting in this topic. k6-go
Closing as not planned.
There is another extension that allows you to write tests in golang https://github.com/szkiba/xk6-g0.
Do we have plan to support golang as programming language? Currently it only support javascript.