Skip to content

Commit

Permalink
Test summary generated fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ameetpal committed May 2, 2024
1 parent 633d338 commit b88ca8a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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()
}
Expand Down
3 changes: 1 addition & 2 deletions event/type.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package event

import "io"

// Type represents the different event types emitted by k6.
//
Expand Down Expand Up @@ -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{}
}
17 changes: 9 additions & 8 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() {
Expand All @@ -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))
Expand All @@ -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{
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b88ca8a

Please sign in to comment.