Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass setup data object into handleSummary callback #2103

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,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) {
summaryDataForJS := summarizeMetricsToObject(summary, r.Bundle.Options)
summaryDataForJS := summarizeMetricsToObject(summary, r.Bundle.Options, r.setupData)

out := make(chan stats.SampleContainer, 100)
defer close(out)
Expand Down
16 changes: 14 additions & 2 deletions js/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"fmt"
"io"
"time"

"encoding/json"
sammieboy97 marked this conversation as resolved.
Show resolved Hide resolved
"github.com/dop251/goja"
"go.k6.io/k6/js/common"
"go.k6.io/k6/lib"
Expand Down Expand Up @@ -79,7 +79,7 @@ func metricValueGetter(summaryTrendStats []string) func(stats.Sink, time.Duratio

// summarizeMetricsToObject transforms the summary objects in a way that's
// suitable to pass to the JS runtime or export to JSON.
func summarizeMetricsToObject(data *lib.Summary, options lib.Options) map[string]interface{} {
func summarizeMetricsToObject(data *lib.Summary, options lib.Options, setupData []byte) map[string]interface{} {
m := make(map[string]interface{})
m["root_group"] = exportGroup(data.RootGroup)
m["options"] = map[string]interface{}{
Expand Down Expand Up @@ -116,6 +116,18 @@ func summarizeMetricsToObject(data *lib.Summary, options lib.Options) map[string
metricsData[name] = metricData
}
m["metrics"] = metricsData

var setupDataI interface{}
if setupData != nil {
if err := json.Unmarshal(setupData, &setupDataI); err != nil {
//TODO: log the error
return m
}
} else {
setupDataI = goja.Undefined()
}

m["setup_data"] = setupDataI

return m
}
Expand Down
146 changes: 146 additions & 0 deletions js/summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,124 @@ const expectedHandleSummaryRawData = `
}
}`

const expectedHandleSummaryDataWithSetup = `
{
"root_group": {
"groups": [
{
"name": "child",
"path": "::child",
"id": "f41cbb53a398ec1c9fb3d33e20c9b040",
"groups": [],
"checks": [
{
"id": "6289a7a06253a1c3f6137dfb25695563",
"passes": 30,
"fails": 0,
"name": "check1",
"path": "::child::check1"
},
{
"fails": 5,
"name": "check3",
"path": "::child::check3",
"id": "c7553eca92d3e034b5808332296d304a",
"passes": 10
},
{
"name": "check2",
"path": "::child::check2",
"id": "06f5922794bef0d4584ba76a49893e1f",
"passes": 5,
"fails": 10
}
]
}
],
"checks": [],
"name": "",
"path": "",
"id": "d41d8cd98f00b204e9800998ecf8427e"
},
"options": {
"summaryTrendStats": [
"avg",
"min",
"med",
"max",
"p(90)",
"p(95)",
"p(99)",
"count"
],
"summaryTimeUnit": "",
"noColor": false
},
"state": {
"isStdErrTTY": false,
"isStdOutTTY": false,
"testRunDurationMs": 1000
},
"setup_data": 5,
"metrics": {
"checks": {
"contains": "default",
"values": {
"passes": 45,
"fails": 15,
"rate": 0.75
},
"type": "rate",
"thresholds": {
"rate>70": {
"ok": true
}
}
},
"my_trend": {
"thresholds": {
"my_trend<1000": {
"ok": false
}
},
"type": "trend",
"contains": "time",
"values": {
"max": 20,
"p(90)": 19,
"p(95)": 19.5,
"p(99)": 19.9,
"count": 3,
"avg": 15,
"min": 10,
"med": 15
}
},
"vus": {
"contains": "default",
"values": {
"value": 1,
"min": 1,
"max": 1
},
"type": "gauge"
},
"http_reqs": {
"type": "counter",
"contains": "default",
"values": {
"count": 3,
"rate": 3
},
"thresholds": {
"rate<100": {
"ok": false
}
}
}
}
}`

func TestRawHandleSummaryData(t *testing.T) {
t.Parallel()
runner, err := getSimpleRunner(
Expand Down Expand Up @@ -432,6 +550,34 @@ func TestRawHandleSummaryData(t *testing.T) {
assert.JSONEq(t, expectedHandleSummaryRawData, string(newRawData))
}

func TestRawHandleSummaryDataWithSetupData(t *testing.T) {
t.Parallel()
runner, err := getSimpleRunner(
t, "/script.js",
`
exports.options = {summaryTrendStats: ["avg", "min", "med", "max", "p(90)", "p(95)", "p(99)", "count"]};
exports.default = function() { /* we don't run this, metrics are mocked */ };
exports.handleSummary = function(data) {
if(data.setup_data != 5) {
throw new Error("handleSummary: wrong data: " + JSON.stringify(data))
}
return {'dataWithSetup.json': JSON.stringify(data)};
};
`,

)
runner.SetSetupData([]byte("5"))
require.NoError(t, err)
sammieboy97 marked this conversation as resolved.
Show resolved Hide resolved

summary := createTestSummary(t)
result, err := runner.HandleSummary(context.Background(), summary)
sammieboy97 marked this conversation as resolved.
Show resolved Hide resolved
dataWithSetup, err := ioutil.ReadAll(result["dataWithSetup.json"])
require.NoError(t, err)
t.Log(expectedHandleSummaryDataWithSetup)
t.Log(string(dataWithSetup))
sammieboy97 marked this conversation as resolved.
Show resolved Hide resolved
assert.JSONEq(t, expectedHandleSummaryDataWithSetup, string(dataWithSetup))
}

func TestWrongSummaryHandlerExportTypes(t *testing.T) {
t.Parallel()
testCases := []string{"{}", `"foo"`, "null", "undefined", "123"}
Expand Down