diff --git a/cmd/run.go b/cmd/run.go index 2290e432407..66a798d3fed 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -190,7 +190,7 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) { if !testRunState.RuntimeOptions.NoSummary.Bool { defer func() { logger.Debug("Generating the end-of-test summary...") - summaryResult, hsErr := test.initRunner.HandleSummary(globalCtx, &lib.Summary{ + summaryResult, summaryJs, hsErr := test.initRunner.HandleSummary(globalCtx, &lib.Summary{ Metrics: metricsEngine.ObservedMetrics, RootGroup: testRunState.Runner.GetDefaultGroup(), TestRunDuration: executionState.GetCurrentTestRunDuration(), @@ -204,7 +204,7 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) { hsErr = handleSummaryResult(c.gs.FS, c.gs.Stdout, c.gs.Stderr, summaryResult) waitForSummaryGeneratedEvent := emitEvent(&event.Event{ Type: event.TestSummaryGenerated, - Data: &event.SummaryData{Summary: summaryResult}, + Data: &event.SummaryData{Summary: summaryJs}, }) waitForSummaryGeneratedEvent() } diff --git a/event/type.go b/event/type.go index 8755ddea131..dcc4bdc0a7e 100644 --- a/event/type.go +++ b/event/type.go @@ -1,6 +1,5 @@ package event -import "io" // Type represents the different event types emitted by k6. // @@ -48,5 +47,5 @@ type IterData struct { // SummaryData is the data sent in the TestSummaryGenerated event. type SummaryData struct { - Summary map[string]io.Reader + Summary map[string]interface{} } diff --git a/js/runner.go b/js/runner.go index aae07f7a9ed..a704debed1c 100644 --- a/js/runner.go +++ b/js/runner.go @@ -362,7 +362,7 @@ func (r *Runner) IsExecutable(name string) bool { } // HandleSummary calls the specified summary callback, if supplied. -func (r *Runner) HandleSummary(ctx context.Context, summary *lib.Summary) (map[string]io.Reader, error) { +func (r *Runner) HandleSummary(ctx context.Context, summary *lib.Summary) (map[string]io.Reader,map[string]interface{}, error) { summaryDataForJS := summarizeMetricsToObject(summary, r.Bundle.Options, r.setupData) out := make(chan metrics.SampleContainer, 100) @@ -378,7 +378,7 @@ func (r *Runner) HandleSummary(ctx context.Context, summary *lib.Summary) (map[s vu, err := r.newVU(summaryCtx, 0, 0, out) if err != nil { - return nil, err + return nil,nil, err } go func() { @@ -392,7 +392,7 @@ func (r *Runner) HandleSummary(ctx context.Context, summary *lib.Summary) (map[s if fn != nil { handleSummaryFn, ok := goja.AssertFunction(fn) if !ok { - return nil, fmt.Errorf("exported identifier %s must be a function", consts.HandleSummaryFn) + return nil,nil, fmt.Errorf("exported identifier %s must be a function", consts.HandleSummaryFn) } callbackResult, _, _, err = vu.runFn(summaryCtx, false, handleSummaryFn, nil, vu.Runtime.ToValue(summaryDataForJS)) @@ -405,11 +405,11 @@ func (r *Runner) HandleSummary(ctx context.Context, summary *lib.Summary) (map[s wrapper := strings.Replace(summaryWrapperLambdaCode, "/*JSLIB_SUMMARY_CODE*/", jslibSummaryCode, 1) handleSummaryWrapperRaw, err := vu.Runtime.RunString(wrapper) if err != nil { - return nil, fmt.Errorf("unexpected error while getting the summary wrapper: %w", err) + return nil,nil, fmt.Errorf("unexpected error while getting the summary wrapper: %w", err) } handleSummaryWrapper, ok := goja.AssertFunction(handleSummaryWrapperRaw) if !ok { - return nil, fmt.Errorf("unexpected error did not get a callable summary wrapper") + return nil,nil, fmt.Errorf("unexpected error did not get a callable summary wrapper") } wrapperArgs := []goja.Value{ @@ -420,13 +420,14 @@ func (r *Runner) HandleSummary(ctx context.Context, summary *lib.Summary) (map[s rawResult, _, _, err := vu.runFn(summaryCtx, false, handleSummaryWrapper, nil, wrapperArgs...) if deadlineError := r.checkDeadline(summaryCtx, consts.HandleSummaryFn, rawResult, err); deadlineError != nil { - return nil, deadlineError + return nil,nil, deadlineError } if err != nil { - return nil, fmt.Errorf("unexpected error while generating the summary: %w", err) + return nil,nil, fmt.Errorf("unexpected error while generating the summary: %w", err) } - return getSummaryResult(rawResult) + res, err := getSummaryResult(rawResult) + return res,summaryDataForJS, err } func (r *Runner) checkDeadline(ctx context.Context, name string, result goja.Value, err error) error { diff --git a/lib/runner.go b/lib/runner.go index 92bad4a22f2..b89d8e6bc1e 100644 --- a/lib/runner.go +++ b/lib/runner.go @@ -86,7 +86,7 @@ type Runner interface { // function in the script. IsExecutable(string) bool - HandleSummary(context.Context, *Summary) (map[string]io.Reader, error) + HandleSummary(context.Context, *Summary) (map[string]io.Reader, map[string]interface{}, error) } // UIState describes the state of the UI, which might influence what