grafana / k6

A modern load testing tool, using Go and JavaScript - https://k6.io
GNU Affero General Public License v3.0
25.16k stars 1.25k forks source link

Does k6 support golang as programming language? #751

Closed HuiqianGithub closed 12 months ago

HuiqianGithub commented 6 years ago

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

na-- commented 6 years 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:

  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-- commented 5 years ago

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

mstoykov commented 4 years ago

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-- commented 3 years ago

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 commented 3 years ago

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-- commented 3 years ago

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.

dgzlopes commented 3 years ago

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-- commented 2 years ago

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

bandorko commented 12 months ago

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

olegbespalov commented 12 months ago

Closing as not planned.

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