https://github.com/deejiw/xk6-interpret
A k6 extension to interpret Go code from literal string.
https://github.com/deejiw/xk6-interpret
golang interpreter xk6
Last synced: 3 months ago
JSON representation
A k6 extension to interpret Go code from literal string.
- Host: GitHub
- URL: https://github.com/deejiw/xk6-interpret
- Owner: deejiw
- License: apache-2.0
- Created: 2023-06-02T02:51:27.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-12T06:39:10.000Z (11 months ago)
- Last Synced: 2024-07-30T21:05:30.539Z (10 months ago)
- Topics: golang, interpreter, xk6
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# xk6-interpret
This is a [k6](https://k6.io) extension using the [xk6](https://github.com/grafana/xk6) system.
Insprised and updated from https://github.com/dgzlopes/xk6-interpret.
## API
- [run](docs/README.md#run)## Build
To build a `k6` binary with this extension, first ensure you have the prerequisites:
- [Go toolchain](https://go101.org/article/go-toolchain.html)
- GitThen:
1. Install `xk6`:
```shell
$ go install go.k6.io/xk6/cmd/xk6@latest
```2. Build the binary:
```shell
$ xk6 build --with github.com/deejiw/xk6-interpret@latest
```## Example
```javascript
import { Interpret } from 'k6/x/interpret';const interpret = new Interpret()
export default function() {
var result = interpret.run(
`package interpretimport "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
x, y := 0, 1
return func() int {
x, y = y, x + y
fmt.Println("Calculated:",x)
return x
}
}func Run(s ...interface{}) interface{} {
f := fibonacci()
var results []int
var i int64
for i = 0; i < s[0].(int64); i++ {
results = append(results,f())
}
return results
}
`,
10
);console.log(`Results: ${result}`)
}
```Result output:
```
$ ./k6 run script.js/\ |‾‾| /‾‾/ /‾‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (‾) |
/ __________ \ |__| \__\ \_____/ .ioexecution: local
script: ../example.js
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)Calculated: 1
Calculated: 2
Calculated: 4
Calculated: 8
Calculated: 16
Calculated: 32
Calculated: 64
Calculated: 128
Calculated: 256
Calculated: 512
INFO[0000] Results: 1,2,4,8,16,32,64,128,256,512 source=consolerunning (00m00.0s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs 00m00.0s/10m0s 1/1 iters, 1 per VUdata_received........: 0 B 0 B/s
data_sent............: 0 B 0 B/s
iteration_duration...: avg=1.16ms min=1.16ms med=1.16ms max=1.16ms p(90)=1.16ms p(95)=1.16ms
iterations...........: 1 66.559293/s
```