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

Add predefined network profiles #1098

Merged
merged 6 commits into from
Nov 15, 2023
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
8 changes: 5 additions & 3 deletions browser/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ type (

// JSModule exposes the properties available to the JS script.
JSModule struct {
Browser *goja.Object
Devices map[string]common.Device
Browser *goja.Object
Devices map[string]common.Device
NetworkProfiles map[string]common.NetworkProfile `js:"networkProfiles"`
}

// ModuleInstance represents an instance of the JS module.
Expand Down Expand Up @@ -69,7 +70,8 @@ func (m *RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance {
browserRegistry: newBrowserRegistry(vu, m.remoteRegistry, m.PidRegistry),
taskQueueRegistry: newTaskQueueRegistry(vu),
}),
Devices: common.GetDevices(),
Devices: common.GetDevices(),
NetworkProfiles: common.GetNetworkProfiles(),
},
}
}
Expand Down
1 change: 1 addition & 0 deletions browser/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ func TestModuleNew(t *testing.T) {
require.NotNil(t, m.mod, "Module should be set")
require.NotNil(t, m.mod.Browser, "Browser should be set")
require.NotNil(t, m.mod.Devices, "Devices should be set")
require.NotNil(t, m.mod.NetworkProfiles, "Profiles should be set")
}
21 changes: 0 additions & 21 deletions common/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,6 @@ import (
"github.com/dop251/goja"
)

// NetworkProfile is used in ThrottleNetwork.
type NetworkProfile struct {
// Minimum latency from request sent to response headers received (ms).
Latency float64

// Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
Download float64

// Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
Upload float64
}

// NewNetworkProfile creates a non-throttled network profile.
func NewNetworkProfile() NetworkProfile {
return NetworkProfile{
Latency: 0,
Download: -1,
Upload: -1,
}
}

// Credentials holds HTTP authentication credentials.
type Credentials struct {
Username string `js:"username"`
Expand Down
48 changes: 48 additions & 0 deletions common/network_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package common

// NetworkProfile is used in ThrottleNetwork.
type NetworkProfile struct {
// Minimum latency from request sent to response headers received (ms).
Latency float64

// Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
Download float64

// Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
Upload float64
}

// NewNetworkProfile creates a non-throttled network profile.
func NewNetworkProfile() NetworkProfile {
return NetworkProfile{
Latency: 0,
Download: -1,
Upload: -1,
}
}

// GetNetworkProfiles returns NetworkProfiles which are ready to be used to
// throttle the network with page.throttleNetwork.
func GetNetworkProfiles() map[string]NetworkProfile {
return map[string]NetworkProfile{
"No Throttling": {
Download: -1,
Upload: -1,
Latency: 0,
},
"Slow 3G": {
// (500 (Kb/s) * 1000 (to bits/s)) / 8 (to bytes/s)) * 0.8 (20% bandwidth loss)
Download: ((500 * 1000) / 8) * 0.8,
// (500 (Kb/s) * 1000 (to bits/s)) / 8 (to bytes/s)) * 0.8 (20% bandwidth loss)
Upload: ((500 * 1000) / 8) * 0.8,
Latency: 400 * 5,
},
"Fast 3G": {
// ((1.6 (Mb/s) * 1000 (to Kb/s) * 1000 (to bits/s)) / 8 (to bytes/s)) * 0.9 (10% bandwidth loss)
Download: ((1.6 * 1000 * 1000) / 8) * 0.9,
// (750 (Kb/s) * 1000 (to bits/s)) / 8 (to bytes/s)) * 0.9 (10% bandwidth loss)
Upload: ((750 * 1000) / 8) * 0.9,
Latency: 150 * 3.75,
},
}
}
10 changes: 3 additions & 7 deletions examples/throttle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { browser } from 'k6/x/browser';
import { browser, networkProfiles } from 'k6/x/browser';

export const options = {
scenarios: {
Expand Down Expand Up @@ -32,7 +32,7 @@ export const options = {
},
thresholds: {
'browser_http_req_duration{scenario:normal}': ['p(99)<500'],
'browser_http_req_duration{scenario:networkThrottled}': ['p(99)<1500'],
'browser_http_req_duration{scenario:networkThrottled}': ['p(99)<3000'],
'iteration_duration{scenario:normal}': ['p(99)<4000'],
'iteration_duration{scenario:cpuThrottled}': ['p(99)<10000'],
},
Expand All @@ -54,11 +54,7 @@ export async function networkThrottled() {
const page = context.newPage();

try {
page.throttleNetwork({
latency: 750,
download: 250,
upload: 250,
});
page.throttleNetwork(networkProfiles['Slow 3G']);

await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' });
} finally {
Expand Down
Loading