Skip to content

Commit

Permalink
feat: Moving subcommands to separate executable
Browse files Browse the repository at this point in the history
k6 does not allow creating sub-commands, so a workaround is used to insert
the dashboard command. This workaround will not work once the dashboard
is included in the official k6 distribution. In order to make the sub-commands
usable, they are transferred to a separate k6-web-dashboard CLI tool.
  • Loading branch information
szkiba committed Dec 20, 2023
1 parent a5a7acc commit 7484030
Show file tree
Hide file tree
Showing 17 changed files with 475 additions and 146 deletions.
4 changes: 4 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# SPDX-FileCopyrightText: 2023 Raintank, Inc. dba Grafana Labs
#
# SPDX-License-Identifier: AGPL-3.0-only

version: 2
updates:
- package-ecosystem: "github-actions"
Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/release-cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# SPDX-FileCopyrightText: 2023 Raintank, Inc. dba Grafana Labs
#
# SPDX-License-Identifier: AGPL-3.0-only

name: release-cli
on:
push:
tags:
- "v*"
permissions:
contents: write

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.21"
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: "1.22.0"
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# SPDX-License-Identifier: MIT

/k6
/k6.exe
/k6-web-dashboard
/k6-web-dashboard.exe
/build
/.bin
/.hintrc
41 changes: 41 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# SPDX-FileCopyrightText: 2023 Raintank, Inc. dba Grafana Labs
#
# SPDX-License-Identifier: AGPL-3.0-only

project_name: k6-web-dashboard
before:
hooks:
- go mod tidy
dist: build/dist
builds:
- env:
- CGO_ENABLED=0
goos: ["darwin", "linux", "windows"]
goarch: ["amd64"]
ldflags:
- "-s -w -X {{.ModulePath}}/cmd.version={{.Version}} -X {{.ModulePath}}/cmd.appname={{.ProjectName}}"
dir: cmd/k6-web-dashboard
source:
enabled: false

archives:
- id: bundle
format: tar.gz
format_overrides:
- goos: windows
format: zip

checksum:
name_template: "{{ .ProjectName }}_{{ .Version }}_checksums.txt"

snapshot:
name_template: "{{ incpatch .Version }}-next+{{.ShortCommit}}{{if .IsGitDirty}}.dirty{{else}}{{end}}"

changelog:
sort: asc
abbrev: -1
filters:
exclude:
- "^chore:"
- "^docs:"
- "^test:"
93 changes: 3 additions & 90 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,97 +217,10 @@ Two kind of events will be emitted:

## Command Line

The xk6-dashboard extension adds a `web-dashboard` command to the k6 command line:
> [!Warning]
> Previous versions of xk6-dashboard added a `dashboard` subcommand to the k6 command line. Currently k6 does not support adding subcommands, xk6-dashboard used a questionable workaround. This workaround was removed from the xk6-dashboard and the dashboard-related subcommands were moved to a separate executable ([k6-web-dashboard](cmd/k6-web-dashboard/README.md)).
```sh
$ ./k6 web-dashboard --help

xk6-dashboard commands

Usage:
k6 web-dashboard [command]

Available Commands:
aggregate convert saved json output to recorded dashboard events
replay load the recorded dashboard events and replay it for the UI
report create report from a recorded event file

Flags:
-h, --help help for dashboard

Use "k6 web-dashboard [command] --help" for more information about a command.
```

At the moment, the `dashboard` command has three subcommand, `replay` (which can be used to play back dashboard events previously saved with `record` parameter), `aggregate` (which can be used to convert test run results previously saved in JSON format from k6 to dashboard events format NDJSON) and `report` (which can be used to generate report from dashboard events previously saved with `record` parameter).


```sh
$ ./k6 web-dashboard replay --help

The replay command load the recorded dashboard events (NDJSON format) and replay it for the dashboard UI.
The compressed file will be automatically decompressed if the file extension is .gz

Usage:
k6 web-dashboard replay file [flags]

Flags:
--host string Hostname or IP address for HTTP endpoint (default: '', empty, listen on all interfaces)
--open Open browser window automatically
--port int TCP port for HTTP endpoint (0=random, -1=no HTTP), example: 8080 (default 5665)
--export string Report file location (default: '', no report)
-h, --help help for replay
```
```
$ ./k6 web-dashboard aggregate --help

The aggregate command converts the file saved by json output to dashboard format events file.
The files will be automatically compressed/decompressed if the file extension is .gz

Usage:
k6 web-dashboard aggregate input-file output-file [flags]

Flags:
--period 1m Event emitting frequency, example: 1m (default 10s)
--tags strings Precomputed metric tags, can be specified more than once (default [group])
-h, --help help for aggregate
```
```
$ ./k6 web-dashboard report --help

The report command loads recorded dashboard events (NDJSON format) and creates a report.
The compressed events file will be automatically decompressed if the file extension is .gz

Usage:
k6 web-dashboard report events-file report-file [flags]

Flags:
--open Open browser window with generated report
-h, --help help for report
```
To visualize the result of a previous test run (using events file):
```
./k6 run --out dashboard=record=test_result.ndjson script.js
./k6 web-dashboard replay test_result.ndjson
```
To visualize the result of a previous test run (using json output):
```sh
./k6 run --out json=test_result.json script.js
./k6 web-dashboard aggregate test_result.json test_result.ndjson
./k6 web-dashboard replay test_result.ndjson
```
To generate report from previous test run (using events file):
```
./k6 run --out web-dashboard=record=test_result.ndjson script.js
./k6 web-dashboard report test_result.ndjson test_result_report.html
```
The CLI tool called [k6-web-dashboard](cmd/k6-web-dashboard/README.md) enables the use of subcommands related to dashboard management (recording playback, creating a report from a recording, etc.) that do not require running k6.

### Docker

Expand Down
165 changes: 165 additions & 0 deletions cmd/k6-web-dashboard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<!--
SPDX-FileCopyrightText: 2023 Raintank, Inc. dba Grafana Labs
SPDX-License-Identifier: AGPL-3.0-only
-->

# k6 web dashboard CLI

The `k6-web-dashboard` is a command-line tool that enables the dashboard event file (saved during the previous k6 run) to be [played back](#k6-web-dashboard-replay) (and displayed in a browser). In addition to playback, it also offers the possibility to [create a single file HTML report](#k6-web-dashboard-report) from the event file.

It is possible to [convert the result saved by k6 JSON output](#k6-web-dashboard-aggregate) to dashboard event file format. This way, the running result saved in JSON format can be displayed later (even on another computer) as a dashboard or a report can be made from it.

## Install

Precompiled binaries can be downloaded and installed from the [Releases](https://github.com/grafana/xk6-dashboard/releases) page.

If you have a go development environment, the installation can also be done with the following command:

```bash
go install github.com/grafana/xk6-dashboard/cmd/k6-web-dashboard@latest
```

## Usage
<!-- begin:cli -->
## k6-web-dashboard

Offline k6 web dashboard management

### Synopsis

k6 web dashboard management that does not require running k6 (recording playback, creating a report from a recording, etc.).

### Options

```
-h, --help help for k6-web-dashboard
```

### SEE ALSO

* [k6-web-dashboard aggregate](#k6-web-dashboard-aggregate) - Convert saved json output to recorded dashboard events
* [k6-web-dashboard replay](#k6-web-dashboard-replay) - Load the recorded dashboard events and replay it for the UI
* [k6-web-dashboard report](#k6-web-dashboard-report) - Create report from a recorded event file

---
## k6-web-dashboard aggregate

Convert saved json output to recorded dashboard events

### Synopsis

The aggregate command converts the file saved by json output to dashboard format events file.
The files will be automatically compressed/decompressed if the file extension is .gz

```
k6-web-dashboard aggregate input-file output-file [flags]
```

### Options

```
--period 1m Event emitting frequency, example: 1m (default 10s)
--tags strings Precomputed metric tags, can be specified more than once (default [group])
-h, --help help for aggregate
```

### SEE ALSO

* [k6-web-dashboard](#k6-web-dashboard) - Offline k6 web dashboard management

---
## k6-web-dashboard help

Help about any command

### Synopsis

Help provides help for any command in the application.
Simply type k6-web-dashboard help [path to command] for full details.

```
k6-web-dashboard help [command] [flags]
```

### Options

```
-h, --help help for help
```

### SEE ALSO

* [k6-web-dashboard](#k6-web-dashboard) - Offline k6 web dashboard management

---
## k6-web-dashboard replay

Load the recorded dashboard events and replay it for the UI

### Synopsis

The replay command load the recorded dashboard events (NDJSON format) and replay it for the dashboard UI.
The compressed file will be automatically decompressed if the file extension is .gz

```
k6-web-dashboard replay file [flags]
```

### Options

```
--export string Report file location (default: '', no report)
--host string Hostname or IP address for HTTP endpoint (default: '', empty, listen on all interfaces)
--open Open browser window automatically
--port int TCP port for HTTP endpoint (0=random, -1=no HTTP), example: 8080 (default 5665)
-h, --help help for replay
```

### SEE ALSO

* [k6-web-dashboard](#k6-web-dashboard) - Offline k6 web dashboard management

---
## k6-web-dashboard report

Create report from a recorded event file

### Synopsis

The report command loads recorded dashboard events (NDJSON format) and creates a report.
The compressed events file will be automatically decompressed if the file extension is .gz

```
k6-web-dashboard report events-file report-file [flags]
```

### Examples

```
# Visualize the result of a previous test run (using events file):
$ k6 run --web-dashboard=record=test_result.ndjson script.js
$ k6-web-dashboard replay test_result.ndjson
# Visualize the result of a previous test run (using json output):
$ k6 run --out json=test_result.json script.js
$ k6-web-dashboard aggregate test_result.json test_result.ndjson
$ k6-web-dashboard replay test_result.ndjson
# Generate report from previous test run (using events file):
$ k6 run --out web-dashboard=record=test_result.ndjson script.js
$ k6-web-dashboard report test_result.ndjson test_result_report.html
```

### Options

```
--open Open browser window with generated report
-h, --help help for report
```

### SEE ALSO

* [k6-web-dashboard](#k6-web-dashboard) - Offline k6 web dashboard management

<!-- end:cli -->
16 changes: 16 additions & 0 deletions cmd/k6-web-dashboard/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: 2023 Raintank, Inc. dba Grafana Labs
//
// SPDX-License-Identifier: AGPL-3.0-only

// Package main contains web-dashboard CLI tool.
package main

import (
"github.com/grafana/xk6-dashboard/cmd"
"github.com/spf13/cobra"
)

func main() {
cmd := cmd.NewRootCommand()
cobra.CheckErr(cmd.Execute())
}
Loading

0 comments on commit 7484030

Please sign in to comment.