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

Refactor isRemoteBrowser check #889

Merged
merged 9 commits into from
May 23, 2023
4 changes: 1 addition & 3 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"context"
"errors"
"fmt"
"os"

"github.com/dop251/goja"

"github.com/grafana/xk6-browser/api"
"github.com/grafana/xk6-browser/chromium"
"github.com/grafana/xk6-browser/env"
"github.com/grafana/xk6-browser/k6error"
"github.com/grafana/xk6-browser/k6ext"

Expand All @@ -37,7 +35,7 @@ func mapBrowserToGoja(vu moduleVU) *goja.Object {
obj = rt.NewObject()
// TODO: Use k6 LookupEnv instead of OS package methods.
// See https://github.com/grafana/xk6-browser/issues/822.
wsURL, isRemoteBrowser = env.IsRemoteBrowser(os.LookupEnv)
wsURL, isRemoteBrowser = vu.remoteRegistry.IsRemoteBrowser()
browserType = chromium.NewBrowserType(vu)
)
for k, v := range mapBrowserType(vu, browserType, wsURL, isRemoteBrowser) {
Expand Down
13 changes: 9 additions & 4 deletions browser/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package browser

import (
"os"

"github.com/dop251/goja"

"github.com/grafana/xk6-browser/common"
Expand All @@ -13,7 +15,8 @@ type (
// RootModule is the global module instance that will create module
// instances for each VU.
RootModule struct {
PidRegistry *pidRegistry
PidRegistry *pidRegistry
remoteRegistry *remoteRegistry
}

// JSModule exposes the properties available to the JS script.
Expand All @@ -37,7 +40,8 @@ var (
// New returns a pointer to a new RootModule instance.
func New() *RootModule {
return &RootModule{
PidRegistry: &pidRegistry{},
PidRegistry: &pidRegistry{},
remoteRegistry: newRemoteRegistry(os.LookupEnv),
}
}

Expand All @@ -47,8 +51,9 @@ func (m *RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance {
return &ModuleInstance{
mod: &JSModule{
Chromium: mapBrowserToGoja(moduleVU{
VU: vu,
pidRegistry: m.PidRegistry,
VU: vu,
pidRegistry: m.PidRegistry,
remoteRegistry: m.remoteRegistry,
}),
Devices: common.GetDevices(),
},
Expand Down
1 change: 1 addition & 0 deletions browser/modulevu.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type moduleVU struct {
k6modules.VU

*pidRegistry
*remoteRegistry
}

func (vu moduleVU) Context() context.Context {
Expand Down
18 changes: 18 additions & 0 deletions browser/remoteregistry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package browser

import "github.com/grafana/xk6-browser/env"

type remoteRegistry struct {
isRemoteBrowser bool
wsURL string
ka3de marked this conversation as resolved.
Show resolved Hide resolved
}

func newRemoteRegistry(envLookup env.LookupFunc) *remoteRegistry {
r := &remoteRegistry{}
r.wsURL, r.isRemoteBrowser = env.IsRemoteBrowser(envLookup)
ka3de marked this conversation as resolved.
Show resolved Hide resolved
return r
}

func (r *remoteRegistry) IsRemoteBrowser() (string, bool) {
return r.wsURL, r.isRemoteBrowser
}
2 changes: 1 addition & 1 deletion examples/multiple-scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const options = {
},
},
thresholds: {
webvital_first_contentful_paint: ['max < 1000'],
browser_web_vital_fcp: ['max < 1000'],
checks: ["rate==1.0"]
}
}
Expand Down
12 changes: 6 additions & 6 deletions k6ext/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ type CustomMetrics struct {
// VU Registry and returns our internal struct pointer.
func RegisterCustomMetrics(registry *k6metrics.Registry) *CustomMetrics {
wvs := map[string]string{
webVitalFID: "webvital_first_input_delay",
webVitalTTFB: "webvital_time_to_first_byte",
webVitalLCP: "webvital_largest_content_paint",
webVitalCLS: "webvital_cumulative_layout_shift",
webVitalINP: "webvital_interaction_to_next_paint",
webVitalFCP: "webvital_first_contentful_paint",
webVitalFID: "browser_web_vital_fid", // first input delay
webVitalTTFB: "browser_web_vital_ttfb", // time to first byte
webVitalLCP: "browser_web_vital_lcp", // largest content paint
webVitalCLS: "browser_web_vital_cls", // cumulative layout shift
webVitalINP: "browser_web_vital_inp", // interaction to next paint
webVitalFCP: "browser_web_vital_fcp", // first contentful paint
}
webVitals := make(map[string]*k6metrics.Metric)

Expand Down
20 changes: 10 additions & 10 deletions tests/webvital_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ func TestWebVitalMetric(t *testing.T) {
browser = newTestBrowser(t, withFileServer(), withSamplesListener(samples))
page = browser.NewPage(nil)
expected = map[string]bool{
"webvital_time_to_first_byte": false,
"webvital_time_to_first_byte_good": false,
"webvital_first_contentful_paint": false,
"webvital_first_contentful_paint_good": false,
"webvital_largest_content_paint": false,
"webvital_largest_content_paint_good": false,
"webvital_first_input_delay": false,
"webvital_first_input_delay_good": false,
"webvital_cumulative_layout_shift": false,
"webvital_cumulative_layout_shift_good": false,
"browser_web_vital_ttfb": false,
"browser_web_vital_ttfb_good": false,
"browser_web_vital_fcp": false,
"browser_web_vital_fcp_good": false,
"browser_web_vital_lcp": false,
"browser_web_vital_lcp_good": false,
"browser_web_vital_fid": false,
"browser_web_vital_fid_good": false,
"browser_web_vital_cls": false,
"browser_web_vital_cls_good": false,
}
)

Expand Down