Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Update to k6 v0.38.0, fix breaking changes #302

Merged
merged 3 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
ctxKeyPid
ctxKeyHooks
ctxKeyVU
ctxKeyCustomK6Metrics
)

func WithHooks(ctx context.Context, hooks *Hooks) context.Context {
Expand Down Expand Up @@ -87,6 +88,20 @@ func GetVU(ctx context.Context) k6modules.VU {
return nil
}

// WithCustomK6Metrics attaches the CustomK6Metrics object to the context.
func WithCustomK6Metrics(ctx context.Context, k6m *CustomK6Metrics) context.Context {
return context.WithValue(ctx, ctxKeyCustomK6Metrics, k6m)
}

// GetCustomK6Metrics returns the CustomK6Metrics object attached to the context.
func GetCustomK6Metrics(ctx context.Context) *CustomK6Metrics {
v := ctx.Value(ctxKeyCustomK6Metrics)
if k6m, ok := v.(*CustomK6Metrics); ok {
return k6m
}
return nil
}

// contextWithDoneChan returns a new context that is canceled either
// when the done channel is closed or ctx is canceled.
func contextWithDoneChan(ctx context.Context, done chan struct{}) context.Context {
Expand Down
14 changes: 7 additions & 7 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/chromedp/cdproto/runtime"
"github.com/dop251/goja"
k6modules "go.k6.io/k6/js/modules"
k6stats "go.k6.io/k6/stats"
k6metrics "go.k6.io/k6/metrics"

"github.com/grafana/xk6-browser/api"
)
Expand Down Expand Up @@ -354,8 +354,8 @@ func (f *Frame) cachedDocumentHandle() (*ElementHandle, bool) {
return f.documentHandle, f.documentHandle != nil
}

func (f *Frame) emitMetric(m *k6stats.Metric, t time.Time) {
value := k6stats.D(t.Sub(f.initTime))
func (f *Frame) emitMetric(m *k6metrics.Metric, t time.Time) {
value := k6metrics.D(t.Sub(f.initTime))
f.log.Debugf("Frame:emitMetric", "fid:%s furl:%q m:%s init:%q t:%q v:%f",
f.ID(), f.URL(), m.Name, f.initTime, t, value)

Expand All @@ -370,12 +370,12 @@ func (f *Frame) emitMetric(m *k6stats.Metric, t time.Time) {

state := f.vu.State()
tags := state.CloneTags()
if state.Options.SystemTags.Has(k6stats.TagURL) {
if state.Options.SystemTags.Has(k6metrics.TagURL) {
tags["url"] = f.URL()
}
sampleTags := k6stats.IntoSampleTags(&tags)
k6stats.PushIfNotDone(f.ctx, state.Samples, k6stats.ConnectedSamples{
Samples: []k6stats.Sample{
sampleTags := k6metrics.IntoSampleTags(&tags)
k6metrics.PushIfNotDone(f.ctx, state.Samples, k6metrics.ConnectedSamples{
Samples: []k6metrics.Sample{
{
Metric: m,
Tags: sampleTags,
Expand Down
16 changes: 9 additions & 7 deletions common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (
"github.com/chromedp/cdproto/target"
"github.com/sirupsen/logrus"
k6modules "go.k6.io/k6/js/modules"
k6stats "go.k6.io/k6/stats"
k6metrics "go.k6.io/k6/metrics"

"github.com/grafana/xk6-browser/api"
)
Expand All @@ -62,6 +62,7 @@ type FrameSession struct {
parent *FrameSession
manager *FrameManager
networkManager *NetworkManager
k6Metrics *CustomK6Metrics

targetID target.ID
windowID browser.WindowID
Expand Down Expand Up @@ -103,6 +104,7 @@ func NewFrameSession(
eventCh: make(chan Event),
childSessions: make(map[cdp.FrameID]*FrameSession),
vu: GetVU(ctx),
k6Metrics: GetCustomK6Metrics(ctx),
logger: l,
serializer: &logrus.Logger{
Out: l.log.Out,
Expand Down Expand Up @@ -714,12 +716,12 @@ func (fs *FrameSession) onPageLifecycle(event *cdppage.EventLifecycleEvent) {
fs.manager.frameLifecycleEvent(event.FrameID, LifecycleEventDOMContentLoad)
}

eventToMetric := map[string]*k6stats.Metric{
"load": BrowserLoaded,
"DOMContentLoaded": BrowserDOMContentLoaded,
"firstPaint": BrowserFirstPaint,
"firstContentfulPaint": BrowserFirstContentfulPaint,
"firstMeaningfulPaint": BrowserFirstMeaningfulPaint,
eventToMetric := map[string]*k6metrics.Metric{
"load": fs.k6Metrics.BrowserLoaded,
"DOMContentLoaded": fs.k6Metrics.BrowserDOMContentLoaded,
"firstPaint": fs.k6Metrics.BrowserFirstPaint,
"firstContentfulPaint": fs.k6Metrics.BrowserFirstContentfulPaint,
"firstMeaningfulPaint": fs.k6Metrics.BrowserFirstMeaningfulPaint,
}

if m, ok := eventToMetric[event.Name]; ok {
Expand Down
7 changes: 3 additions & 4 deletions common/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ import (
k6eventloop "go.k6.io/k6/js/eventloop"
k6modulestest "go.k6.io/k6/js/modulestest"
k6lib "go.k6.io/k6/lib"
k6metrics "go.k6.io/k6/lib/metrics"
k6stats "go.k6.io/k6/stats"
k6metrics "go.k6.io/k6/metrics"
"gopkg.in/guregu/null.v3"
)

Expand Down Expand Up @@ -251,7 +250,7 @@ func newMockVU(tb testing.TB) *mockVU {
rt := goja.New()
rt.SetFieldNameMapper(k6common.FieldNameMapper{})

samples := make(chan k6stats.SampleContainer, 1000)
samples := make(chan k6metrics.SampleContainer, 1000)

root, err := k6lib.NewGroup("", nil)
require.NoError(tb, err)
Expand All @@ -261,7 +260,7 @@ func newMockVU(tb testing.TB) *mockVU {
MaxRedirects: null.IntFrom(10),
UserAgent: null.StringFrom("TestUserAgent"),
Throw: null.BoolFrom(true),
SystemTags: &k6stats.DefaultSystemTagSet,
SystemTags: &k6metrics.DefaultSystemTagSet,
Batch: null.IntFrom(20),
BatchPerHost: null.IntFrom(20),
// HTTPDebug: null.StringFrom("full"),
Expand Down
49 changes: 49 additions & 0 deletions common/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* xk6-browser - a browser automation extension for k6
* Copyright (C) 2021 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package common

import k6metrics "go.k6.io/k6/metrics"

// CustomK6Metrics are the custom k6 metrics used by xk6-browser.
type CustomK6Metrics struct {
BrowserDOMContentLoaded *k6metrics.Metric
BrowserFirstPaint *k6metrics.Metric
BrowserFirstContentfulPaint *k6metrics.Metric
BrowserFirstMeaningfulPaint *k6metrics.Metric
BrowserLoaded *k6metrics.Metric
}

// RegisterCustomK6Metrics creates and registers our custom metrics with the k6
// VU Registry and returns our internal struct pointer.
func RegisterCustomK6Metrics(registry *k6metrics.Registry) *CustomK6Metrics {
return &CustomK6Metrics{
BrowserDOMContentLoaded: registry.MustNewMetric(
"browser_dom_content_loaded", k6metrics.Trend, k6metrics.Time),
BrowserFirstPaint: registry.MustNewMetric(
"browser_first_paint", k6metrics.Trend, k6metrics.Time),
BrowserFirstContentfulPaint: registry.MustNewMetric(
"browser_first_contentful_paint", k6metrics.Trend, k6metrics.Time),
BrowserFirstMeaningfulPaint: registry.MustNewMetric(
"browser_first_meaningful_paint", k6metrics.Trend, k6metrics.Time),
BrowserLoaded: registry.MustNewMetric(
"browser_loaded", k6metrics.Trend, k6metrics.Time),
}
}
46 changes: 23 additions & 23 deletions common/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
k6lib "go.k6.io/k6/lib"
k6netext "go.k6.io/k6/lib/netext"
k6types "go.k6.io/k6/lib/types"
k6stats "go.k6.io/k6/stats"
k6metrics "go.k6.io/k6/metrics"
)

// Ensure NetworkManager implements the EventEmitter interface.
Expand Down Expand Up @@ -161,19 +161,19 @@ func (m *NetworkManager) emitRequestMetrics(req *Request) {
state := m.vu.State()

tags := state.CloneTags()
if state.Options.SystemTags.Has(k6stats.TagGroup) {
if state.Options.SystemTags.Has(k6metrics.TagGroup) {
tags["group"] = state.Group.Path
}
if state.Options.SystemTags.Has(k6stats.TagMethod) {
if state.Options.SystemTags.Has(k6metrics.TagMethod) {
tags["method"] = req.method
}
if state.Options.SystemTags.Has(k6stats.TagURL) {
if state.Options.SystemTags.Has(k6metrics.TagURL) {
tags["url"] = req.URL()
}

sampleTags := k6stats.IntoSampleTags(&tags)
k6stats.PushIfNotDone(m.ctx, state.Samples, k6stats.ConnectedSamples{
Samples: []k6stats.Sample{
sampleTags := k6metrics.IntoSampleTags(&tags)
k6metrics.PushIfNotDone(m.ctx, state.Samples, k6metrics.ConnectedSamples{
Samples: []k6metrics.Sample{
{
Metric: state.BuiltinMetrics.DataSent,
Tags: sampleTags,
Expand Down Expand Up @@ -213,32 +213,32 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) {
}

tags := state.CloneTags()
if state.Options.SystemTags.Has(k6stats.TagGroup) {
if state.Options.SystemTags.Has(k6metrics.TagGroup) {
tags["group"] = state.Group.Path
}
if state.Options.SystemTags.Has(k6stats.TagMethod) {
if state.Options.SystemTags.Has(k6metrics.TagMethod) {
tags["method"] = req.method
}
if state.Options.SystemTags.Has(k6stats.TagURL) {
if state.Options.SystemTags.Has(k6metrics.TagURL) {
tags["url"] = url
}
if state.Options.SystemTags.Has(k6stats.TagIP) {
if state.Options.SystemTags.Has(k6metrics.TagIP) {
tags["ip"] = ipAddress
}
if state.Options.SystemTags.Has(k6stats.TagStatus) {
if state.Options.SystemTags.Has(k6metrics.TagStatus) {
tags["status"] = strconv.Itoa(int(status))
}
if state.Options.SystemTags.Has(k6stats.TagProto) {
if state.Options.SystemTags.Has(k6metrics.TagProto) {
tags["proto"] = protocol
}

tags["from_cache"] = strconv.FormatBool(fromCache)
tags["from_prefetch_cache"] = strconv.FormatBool(fromPreCache)
tags["from_service_worker"] = strconv.FormatBool(fromSvcWrk)

sampleTags := k6stats.IntoSampleTags(&tags)
k6stats.PushIfNotDone(m.ctx, state.Samples, k6stats.ConnectedSamples{
Samples: []k6stats.Sample{
sampleTags := k6metrics.IntoSampleTags(&tags)
k6metrics.PushIfNotDone(m.ctx, state.Samples, k6metrics.ConnectedSamples{
Samples: []k6metrics.Sample{
{
Metric: state.BuiltinMetrics.HTTPReqs,
Tags: sampleTags,
Expand All @@ -254,7 +254,7 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) {
// issues with the parsing and conversion to `time.Time`.
// Have not spent time looking for the root cause of this in the Chromium source to file a bug report, and neither
// Puppeteer nor Playwright seems to care about the `responseTime` value and don't use/expose it.
Value: k6stats.D(timestamp.Sub(req.timestamp)),
Value: k6metrics.D(timestamp.Sub(req.timestamp)),
Time: timestamp,
},
{
Expand All @@ -267,30 +267,30 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) {
})

if resp != nil && resp.timing != nil {
k6stats.PushIfNotDone(m.ctx, state.Samples, k6stats.ConnectedSamples{
Samples: []k6stats.Sample{
k6metrics.PushIfNotDone(m.ctx, state.Samples, k6metrics.ConnectedSamples{
Samples: []k6metrics.Sample{
{
Metric: state.BuiltinMetrics.HTTPReqConnecting,
Tags: sampleTags,
Value: k6stats.D(time.Duration(resp.timing.ConnectEnd-resp.timing.ConnectStart) * time.Millisecond),
Value: k6metrics.D(time.Duration(resp.timing.ConnectEnd-resp.timing.ConnectStart) * time.Millisecond),
Time: resp.timestamp,
},
{
Metric: state.BuiltinMetrics.HTTPReqTLSHandshaking,
Tags: sampleTags,
Value: k6stats.D(time.Duration(resp.timing.SslEnd-resp.timing.SslStart) * time.Millisecond),
Value: k6metrics.D(time.Duration(resp.timing.SslEnd-resp.timing.SslStart) * time.Millisecond),
Time: resp.timestamp,
},
{
Metric: state.BuiltinMetrics.HTTPReqSending,
Tags: sampleTags,
Value: k6stats.D(time.Duration(resp.timing.SendEnd-resp.timing.SendStart) * time.Millisecond),
Value: k6metrics.D(time.Duration(resp.timing.SendEnd-resp.timing.SendStart) * time.Millisecond),
Time: resp.timestamp,
},
{
Metric: state.BuiltinMetrics.HTTPReqReceiving,
Tags: sampleTags,
Value: k6stats.D(time.Duration(resp.timing.ReceiveHeadersEnd-resp.timing.SendEnd) * time.Millisecond),
Value: k6metrics.D(time.Duration(resp.timing.ReceiveHeadersEnd-resp.timing.SendEnd) * time.Millisecond),
Time: resp.timestamp,
},
},
Expand Down
31 changes: 0 additions & 31 deletions common/stats.go

This file was deleted.

12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/chromedp/cdproto v0.0.0-20220304215434-892afa710589
github.com/dop251/goja v0.0.0-20220324112439-a18ffb9c5866
github.com/dop251/goja v0.0.0-20220405120441-9037c2b61cbf
github.com/fatih/color v1.13.0
github.com/gorilla/websocket v1.5.0
github.com/hashicorp/go-multierror v1.1.1
Expand All @@ -13,13 +13,14 @@ require (
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.1
go.k6.io/k6 v0.37.1-0.20220329074230-0b7144304e38
go.k6.io/k6 v0.38.0
golang.org/x/net v0.0.0-20220225172249-27dd8689420f
gopkg.in/guregu/null.v3 v3.5.0
)

require (
github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e // indirect
github.com/DataDog/datadog-go v0.0.0-20180330214955-e67964b4021a // indirect
github.com/PuerkitoBio/goquery v1.8.0 // indirect
github.com/Soontao/goHttpDigestClient v0.0.0-20170320082612-6d28bb1415c5 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
Expand All @@ -30,20 +31,27 @@ require (
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/influxdata/influxdb1-client v0.0.0-20190402204710-8ff2fc3824fc // indirect
github.com/jhump/protoreflect v1.12.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.15.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mstoykov/envconfig v1.4.1-0.20220114105314-765c6d8c76f1 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect
github.com/spf13/afero v1.8.1 // indirect
github.com/spf13/cobra v1.4.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/gjson v1.14.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
google.golang.org/genproto v0.0.0-20220308174144-ae0e22291548 // indirect
Expand Down
Loading