Skip to content

Commit

Permalink
js,lib: migrate to TagMap
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed Oct 21, 2021
1 parent 485407b commit 4023685
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 26 deletions.
2 changes: 2 additions & 0 deletions js/initcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ func TestRequestWithBinaryFile(t *testing.T) {
BPool: bpool.NewBufferPool(1),
Samples: make(chan stats.SampleContainer, 500),
BuiltinMetrics: builtinMetrics,
Tags: lib.NewTagMap(nil),
}

ctx := context.Background()
Expand Down Expand Up @@ -553,6 +554,7 @@ func TestRequestWithMultipleBinaryFiles(t *testing.T) {
BPool: bpool.NewBufferPool(1),
Samples: make(chan stats.SampleContainer, 500),
BuiltinMetrics: builtinMetrics,
Tags: lib.NewTagMap(nil),
}

ctx := context.Background()
Expand Down
1 change: 1 addition & 0 deletions js/modules/k6/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func TestClient(t *testing.T) {
BuiltinMetrics: metrics.RegisterBuiltinMetrics(
metrics.NewRegistry(),
),
Tags: lib.NewTagMap(nil),
}

cwd, err := os.Getwd()
Expand Down
23 changes: 14 additions & 9 deletions js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,16 @@ func newRuntime(
samples := make(chan stats.SampleContainer, 1000)

state := &lib.State{
Options: options,
Logger: logger,
Group: root,
TLSConfig: tb.TLSClientConfig,
Transport: tb.HTTPTransport,
BPool: bpool.NewBufferPool(1),
Samples: samples,
Tags: map[string]string{"group": root.Path},
Options: options,
Logger: logger,
Group: root,
TLSConfig: tb.TLSClientConfig,
Transport: tb.HTTPTransport,
BPool: bpool.NewBufferPool(1),
Samples: samples,
Tags: lib.NewTagMap(map[string]string{
"group": root.Path,
}),
BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry),
}

Expand Down Expand Up @@ -1119,7 +1121,10 @@ func TestRequestAndBatch(t *testing.T) {
t.Run("tags-precedence", func(t *testing.T) {
oldTags := state.Tags
defer func() { state.Tags = oldTags }()
state.Tags = map[string]string{"runtag1": "val1", "runtag2": "val2"}
state.Tags = lib.NewTagMap(map[string]string{
"runtag1": "val1",
"runtag2": "val2",
})

_, err := rt.RunString(sr(`
var res = http.request("GET", "HTTPBIN_URL/headers", null, { tags: { method: "test", name: "myName", runtag1: "fromreq" } });
Expand Down
4 changes: 2 additions & 2 deletions js/modules/k6/http/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ func TestResponse(t *testing.T) {
if assert.NoError(t, err) {
old := state.Group
state.Group = g
state.Tags["group"] = g.Path
state.Tags.Set("group", g.Path)
defer func() {
state.Group = old
state.Tags["group"] = old.Path
state.Tags.Set("group", old.Path)
}()
}

Expand Down
4 changes: 2 additions & 2 deletions js/modules/k6/k6.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ func (*K6) Group(ctx context.Context, name string, fn goja.Callable) (goja.Value

shouldUpdateTag := state.Options.SystemTags.Has(stats.TagGroup)
if shouldUpdateTag {
state.Tags["group"] = g.Path
state.Tags.Set("group", g.Path)
}
defer func() {
state.Group = old
if shouldUpdateTag {
state.Tags["group"] = old.Path
state.Tags.Set("group", g.Path)
}
}()

Expand Down
10 changes: 8 additions & 2 deletions js/modules/k6/k6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ func TestGroup(t *testing.T) {
assert.NoError(t, err)

rt := goja.New()
state := &lib.State{Group: root, Samples: make(chan stats.SampleContainer, 1000)}
state := &lib.State{
Group: root,
Samples: make(chan stats.SampleContainer, 1000),
Tags: lib.NewTagMap(nil),
}
ctx := context.Background()
ctx = lib.WithState(ctx, state)
ctx = common.WithRuntime(ctx, rt)
Expand Down Expand Up @@ -167,7 +171,9 @@ func checkTestRuntime(t testing.TB, ctxs ...*context.Context) (
SystemTags: &stats.DefaultSystemTagSet,
},
Samples: samples,
Tags: map[string]string{"group": root.Path},
Tags: lib.NewTagMap(map[string]string{
"group": root.Path,
}),
}
ctx := context.Background()
if len(ctxs) == 1 { // hacks
Expand Down
4 changes: 3 additions & 1 deletion js/modules/k6/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ func TestMetrics(t *testing.T) {
state := &lib.State{
Options: lib.Options{},
Samples: samples,
Tags: map[string]string{"key": "value"},
Tags: lib.NewTagMap(map[string]string{
"key": "value",
}),
}

isTimeString := ""
Expand Down
6 changes: 6 additions & 0 deletions js/modules/k6/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ func (*WS) Connect(ctx context.Context, url string, args ...goja.Value) (*WSHTTP

enableCompression := false

if state == nil {
panic("is the tag")
}
if state.Tags == nil {
panic("is the map")
}
tags := state.CloneTags()

// Parse the optional second argument (params)
Expand Down
7 changes: 7 additions & 0 deletions js/modules/k6/ws/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func newRuntime(t testing.TB) (*httpmultibin.HTTPMultiBin, chan stats.SampleCont
Samples: samples,
TLSConfig: tb.TLSClientConfig,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(metrics.NewRegistry()),
Tags: lib.NewTagMap(nil),
}

ctx := new(context.Context)
Expand Down Expand Up @@ -154,6 +155,7 @@ func TestSession(t *testing.T) {
Samples: samples,
TLSConfig: tb.TLSClientConfig,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(metrics.NewRegistry()),
Tags: lib.NewTagMap(nil),
}

ctx := context.Background()
Expand Down Expand Up @@ -545,6 +547,7 @@ func TestSocketSendBinary(t *testing.T) { //nolint: tparallel
Samples: samples,
TLSConfig: tb.TLSClientConfig,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(metrics.NewRegistry()),
Tags: lib.NewTagMap(nil),
}

ctx := context.Background()
Expand Down Expand Up @@ -635,6 +638,7 @@ func TestErrors(t *testing.T) {
},
Samples: samples,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(metrics.NewRegistry()),
Tags: lib.NewTagMap(nil),
}

ctx := context.Background()
Expand Down Expand Up @@ -747,6 +751,7 @@ func TestSystemTags(t *testing.T) {
Samples: samples,
TLSConfig: tb.TLSClientConfig,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(metrics.NewRegistry()),
Tags: lib.NewTagMap(nil),
}

ctx := context.Background()
Expand Down Expand Up @@ -810,6 +815,7 @@ func TestTLSConfig(t *testing.T) {
},
Samples: samples,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(metrics.NewRegistry()),
Tags: lib.NewTagMap(nil),
}

ctx := context.Background()
Expand Down Expand Up @@ -949,6 +955,7 @@ func TestUserAgent(t *testing.T) {
Samples: samples,
TLSConfig: tb.TLSClientConfig,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(metrics.NewRegistry()),
Tags: lib.NewTagMap(nil),
}

ctx := lib.WithState(context.Background(), state)
Expand Down
21 changes: 11 additions & 10 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (r *Runner) newVU(idLocal, idGlobal uint64, samplesOut chan<- stats.SampleC
VUID: vu.ID,
VUIDGlobal: vu.IDGlobal,
Samples: vu.Samples,
Tags: vu.Runner.Bundle.Options.RunTags.CloneTags(),
Tags: lib.NewTagMap(vu.Runner.Bundle.Options.RunTags.CloneTags()),
Group: r.defaultGroup,
BuiltinMetrics: r.builtinMetrics,
}
Expand Down Expand Up @@ -505,7 +505,7 @@ func (r *Runner) runPart(ctx context.Context, out chan<- stats.SampleContainer,
}

if r.Bundle.Options.SystemTags.Has(stats.TagGroup) {
vu.state.Tags["group"] = group.Path
vu.state.Tags.Set("group", group.Path)
}
vu.state.Group = group

Expand Down Expand Up @@ -604,21 +604,21 @@ func (u *VU) Activate(params *lib.VUActivationParams) lib.ActiveVU {

opts := u.Runner.Bundle.Options
// TODO: maybe we can cache the original tags only clone them and add (if any) new tags on top ?
u.state.Tags = opts.RunTags.CloneTags()
u.state.Tags = lib.NewTagMap(opts.RunTags.CloneTags())
for k, v := range params.Tags {
u.state.Tags[k] = v
u.state.Tags.Set(k, v)
}
if opts.SystemTags.Has(stats.TagVU) {
u.state.Tags["vu"] = strconv.FormatUint(u.ID, 10)
u.state.Tags.Set("vu", strconv.FormatUint(u.ID, 10))
}
if opts.SystemTags.Has(stats.TagIter) {
u.state.Tags["iter"] = strconv.FormatInt(u.iteration, 10)
u.state.Tags.Set("iter", strconv.FormatInt(u.iteration, 10))
}
if opts.SystemTags.Has(stats.TagGroup) {
u.state.Tags["group"] = u.state.Group.Path
u.state.Tags.Set("group", u.state.Group.Path)
}
if opts.SystemTags.Has(stats.TagScenario) {
u.state.Tags["scenario"] = params.Scenario
u.state.Tags.Set("scenario", params.Scenario)
}

ctx := common.WithRuntime(params.RunContext, u.Runtime)
Expand Down Expand Up @@ -731,7 +731,7 @@ func (u *VU) runFn(

opts := &u.Runner.Bundle.Options
if opts.SystemTags.Has(stats.TagIter) {
u.state.Tags["iter"] = strconv.FormatInt(u.state.Iteration, 10)
u.state.Tags.Set("iter", strconv.FormatInt(u.state.Iteration, 10))
}

defer func() {
Expand Down Expand Up @@ -769,8 +769,9 @@ func (u *VU) runFn(
u.Transport.CloseIdleConnections()
}

sampleTags := stats.NewSampleTags(u.state.CloneTags())
u.state.Samples <- u.Dialer.GetTrail(
startTime, endTime, isFullIteration, isDefault, stats.NewSampleTags(u.state.Tags), u.Runner.builtinMetrics)
startTime, endTime, isFullIteration, isDefault, sampleTags, u.Runner.builtinMetrics)

return v, isFullIteration, endTime.Sub(startTime), err
}
Expand Down
6 changes: 6 additions & 0 deletions lib/netext/httpext/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func TestMakeRequestError(t *testing.T) {
Options: lib.Options{RunTags: &stats.SampleTags{}},
Transport: srv.Client().Transport,
Logger: logger,
Tags: lib.NewTagMap(nil),
}
ctx = lib.WithState(ctx, state)
req, _ := http.NewRequest("GET", srv.URL, nil)
Expand Down Expand Up @@ -192,6 +193,7 @@ func TestResponseStatus(t *testing.T) {
Logger: logger,
Samples: samples,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry),
Tags: lib.NewTagMap(nil),
}
ctx := lib.WithState(context.Background(), state)
req, err := http.NewRequest("GET", server.URL, nil)
Expand Down Expand Up @@ -272,6 +274,7 @@ func TestMakeRequestTimeoutInTheMiddle(t *testing.T) {
Logger: logger,
BPool: bpool.NewBufferPool(100),
BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry),
Tags: lib.NewTagMap(nil),
}
ctx = lib.WithState(ctx, state)
req, _ := http.NewRequest("GET", srv.URL, nil)
Expand Down Expand Up @@ -349,6 +352,7 @@ func TestTrailFailed(t *testing.T) {
Logger: logger,
BPool: bpool.NewBufferPool(2),
BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry),
Tags: lib.NewTagMap(nil),
}
ctx = lib.WithState(ctx, state)
req, _ := http.NewRequest("GET", srv.URL, nil)
Expand Down Expand Up @@ -415,6 +419,7 @@ func TestMakeRequestDialTimeout(t *testing.T) {
Logger: logger,
BPool: bpool.NewBufferPool(100),
BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry),
Tags: lib.NewTagMap(nil),
}

ctx = lib.WithState(ctx, state)
Expand Down Expand Up @@ -470,6 +475,7 @@ func TestMakeRequestTimeoutInTheBegining(t *testing.T) {
Logger: logger,
BPool: bpool.NewBufferPool(100),
BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry),
Tags: lib.NewTagMap(nil),
}
ctx = lib.WithState(ctx, state)
req, _ := http.NewRequest("GET", srv.URL, nil)
Expand Down

0 comments on commit 4023685

Please sign in to comment.