Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from grafana/feat/initial
Browse files Browse the repository at this point in the history
Add initial k6/x/execution module
  • Loading branch information
na-- committed Aug 26, 2021
2 parents f0e0443 + 12c4e88 commit 6f6d81b
Show file tree
Hide file tree
Showing 10 changed files with 1,387 additions and 0 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Test
on:
# Enable manually triggering this workflow via the API or web UI
workflow_dispatch:
push:
branches:
- master
tags:
- v*
pull_request:

defaults:
run:
shell: bash

jobs:
lint:
runs-on: ubuntu-latest
env:
GOLANG_CI_VERSION: "1.41.1"
GO111MODULE: 'on'
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.16.x
- name: Install golangci-lint
working-directory: /tmp
run: go install "github.com/golangci/golangci-lint/cmd/golangci-lint@v$GOLANG_CI_VERSION"
- name: Run linters
run: |
golangci-lint run --out-format=tab ./...
test:
strategy:
fail-fast: false
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.16.x
- name: Run tests
run: |
set -x
go version
export GOMAXPROCS=2
go test -p 2 -race -timeout 60s ./...
39 changes: 39 additions & 0 deletions .github/workflows/xk6-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { sleep } from 'k6';
import exec from 'k6/x/execution';

export let options = {
scenarios: {
shared: {
executor: 'shared-iterations',
vus: 50,
iterations: 500,
},
rar: {
executor: 'ramping-arrival-rate',
startTime: '10s',
startRate: 20,
timeUnit: '1s',
preAllocatedVUs: 0,
maxVUs: 40,
stages: [
{ target: 50, duration: '5s' },
{ target: 0, duration: '5s' },
],
gracefulStop: '0s',
},
}
}

function logObj(msg, o) {
console.log(msg, JSON.stringify(o, Object.keys(o).sort()));
}

export default function () {
const vuStats = exec.getVUStats();
const scStats = exec.getScenarioStats();
const testStats = exec.getTestInstanceStats();
sleep(1);
logObj('VU stats:', vuStats);
logObj('Scenario stats:', scStats);
logObj('Test stats:', testStats);
}
48 changes: 48 additions & 0 deletions .github/workflows/xk6.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: xk6
on:
# Enable manually triggering this workflow via the API or web UI
workflow_dispatch:
push:
branches:
- master
pull_request:

defaults:
run:
shell: bash

jobs:
test-xk6:
strategy:
matrix:
go: [stable, tip]
platform: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.16.x
- name: Install Go tip
if: matrix.go == 'tip'
run: |
go install golang.org/dl/gotip@latest
gotip download
echo "GOROOT=$HOME/sdk/gotip" >> "$GITHUB_ENV"
echo "GOPATH=$HOME/go" >> "$GITHUB_ENV"
echo "$HOME/go/bin" >> "$GITHUB_PATH"
echo "$HOME/sdk/gotip/bin" >> "$GITHUB_PATH"
- name: Run tests
run: |
set -x
which go
go version
go install go.k6.io/xk6/cmd/xk6@master
GOPROXY="direct" xk6 build master \
--output ./k6ext \
--with github.com/k6io/xk6-execution="$(pwd)"
./k6ext version
./k6ext run --quiet --verbose .github/workflows/xk6-test.js
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# xk6-execution

A [k6](https://github.com/grafana/k6) JavaScript extension using the
[xk6](https://github.com/grafana/xk6) system that returns information about a
test run including VU, scenario and test statistics.

| :exclamation: This project is in active development and may break in the future.<br>Please report issues and contribute improvements to help. |
|------|


## Build

To build a `k6` binary with this extension, first ensure you have installed the prerequisites:

- [Go toolchain](https://go101.org/article/go-toolchain.html)
- Git

Then:

1. Install `xk6`:
```shell
go install go.k6.io/xk6/cmd/xk6@latest
```

2. Build the binary:
```shell
xk6 build master \
--with github.com/grafana/xk6-execution
```


## Example

```javascript
import { sleep } from 'k6';
import exec from 'k6/x/execution';
export let options = {
scenarios: {
shared: {
executor: 'shared-iterations',
vus: 50,
iterations: 500,
}
}
}
function logObj(msg, o) {
console.log(msg, JSON.stringify(o, Object.keys(o).sort()));
}
export default function () {
const vuStats = exec.getVUStats();
const scStats = exec.getScenarioStats();
const testStats = exec.getTestInstanceStats();
sleep(1);
logObj('VU stats:', vuStats);
logObj('Scenario stats:', scStats);
logObj('Test stats:', testStats);
}
```
Sample output:
```shell
INFO[0009] VU stats: {"id":36,"idGlobal":36,"iteration":8,"iterationScenario":8} source=console
INFO[0009] Scenario stats: {"executor":"shared-iterations","iteration":429,"iterationGlobal":429,"name":"shared","progress":0.858,"startTime":1624262301.1202478} source=console
INFO[0009] Test stats: {"duration":9035.161124,"iterationsCompleted":429,"iterationsInterrupted":0,"vusActive":50,"vusMax":50} source=console
```
TODO:
- [ ] Document all fields
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/k6io/xk6-execution

go 1.16

require (
github.com/dop251/goja v0.0.0-20210817151038-07a7fd9355b4
github.com/sirupsen/logrus v1.8.1
github.com/spf13/afero v1.1.2
github.com/stretchr/testify v1.7.0
go.k6.io/k6 v0.33.1-0.20210825161650-c932a28ff940
gopkg.in/guregu/null.v3 v3.3.0
)
Loading

0 comments on commit 6f6d81b

Please sign in to comment.