Skip to content

Commit

Permalink
feat: Save HTML report feature in replay command
Browse files Browse the repository at this point in the history
  • Loading branch information
szkiba committed Aug 16, 2023
1 parent fa982f5 commit c140888
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ func init() {
return
}

dashboard.Execute(assets.DirUI())
dashboard.Execute(assets.DirUI(), assets.DirBrief())
}
16 changes: 9 additions & 7 deletions dashboard/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
"go.k6.io/k6/lib/consts"
)

func Execute(uiFS fs.FS) {
func Execute(uiFS fs.FS, briefFS fs.FS) {
opts := new(options)

if err := buildRootCmd(opts, uiFS).Execute(); err != nil {
if err := buildRootCmd(opts, uiFS, briefFS).Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
Expand All @@ -32,12 +32,13 @@ const (
flagPeriod = "period"
flagOpen = "open"
flagConfig = "config"
flagReport = "report"

typeMetric = "Metric"
typePoint = "Point"
)

func buildRootCmd(opts *options, uiFS fs.FS) *cobra.Command {
func buildRootCmd(opts *options, uiFS fs.FS, briefFS fs.FS) *cobra.Command {
rootCmd := &cobra.Command{ // nolint:exhaustruct
Use: "k6",
Short: "a next-generation load generator",
Expand All @@ -57,7 +58,7 @@ func buildRootCmd(opts *options, uiFS fs.FS) *cobra.Command {
Long: "The replay command load the saved JSON results and replay it for the dashboard UI.\nThe compressed file will be automatically decompressed if the file extension is .gz",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := replay(opts, uiFS, args[0]); err != nil {
if err := replay(opts, uiFS, briefFS, args[0]); err != nil {
return err
}

Expand All @@ -78,10 +79,11 @@ func buildRootCmd(opts *options, uiFS fs.FS) *cobra.Command {
opts = new(options)

flags.StringVar(&opts.Host, flagHost, defaultHost, "Hostname or IP address for HTTP endpoint (default: '', empty, listen on all interfaces)")
flags.IntVar(&opts.Port, flagPort, defaultPort, "TCP port for HTTP endpoint (default: 5665; 0=random, -1=no HTTP), example: 8080")
flags.DurationVar(&opts.Period, flagPeriod, defaultPeriod, "Event emitting frequency (default: `10s`), example: `1m`")
flags.IntVar(&opts.Port, flagPort, defaultPort, "TCP port for HTTP endpoint (0=random, -1=no HTTP), example: 8080")
flags.DurationVar(&opts.Period, flagPeriod, defaultPeriod, "Event emitting frequency, example: `1m`")
flags.BoolVar(&opts.Open, flagOpen, defaultOpen, "Open browser window automatically")
flags.StringVar(&opts.Config, flagConfig, defaultHost, "UI configuration file location (default: '.dashboard.js')")
flags.StringVar(&opts.Config, flagConfig, defaultConfig, "UI configuration file location")
flags.StringVar(&opts.Report, flagReport, defaultReport, "Report file location (default: '', no report)")

dashboardCmd.AddCommand(replayCmd)

Expand Down
2 changes: 1 addition & 1 deletion dashboard/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Test_buildRootCmd(t *testing.T) {

opts := new(options)

cmd := buildRootCmd(opts, embed.FS{})
cmd := buildRootCmd(opts, embed.FS{}, embed.FS{})

assert.NotNil(t, cmd)

Expand Down
19 changes: 17 additions & 2 deletions dashboard/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type replayer struct {
once sync.Once
}

func replay(opts *options, uiFS fs.FS, filename string) error {
func replay(opts *options, uiFS fs.FS, briefFS fs.FS, filename string) error {
config, err := opts.config()
if err != nil {
return err
Expand All @@ -55,6 +55,12 @@ func replay(opts *options, uiFS fs.FS, filename string) error {
rep.eventSource = new(eventSource)
rep.addEventListener(rep.server)

if len(opts.Report) != 0 {
brf := newBriefer(briefFS, config, opts.Report, rep.logger)

rep.addEventListener(brf)
}

addr, err := rep.server.listenAndServe(rep.options.addr())
if err != nil {
return err
Expand All @@ -68,7 +74,16 @@ func replay(opts *options, uiFS fs.FS, filename string) error {
browser.OpenURL(rep.options.url()) // nolint:errcheck
}

return feed(filename, rep.addMetricSamples)
if err := rep.fireStart(); err != nil {
return err
}

err = feed(filename, rep.addMetricSamples)
if err != nil {
return err
}

return rep.fireStop()
}

func (rep *replayer) addMetricSamples(samples []metrics.SampleContainer) {
Expand Down
2 changes: 1 addition & 1 deletion dashboard/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Test_replay(t *testing.T) {
Report: "",
}

assert.NoError(t, replay(opts, embed.FS{}, "testdata/result.gz"))
assert.NoError(t, replay(opts, embed.FS{}, embed.FS{}, "testdata/result.gz"))

time.Sleep(time.Millisecond)

Expand Down

0 comments on commit c140888

Please sign in to comment.