Skip to content

Commit

Permalink
Add network profile state
Browse files Browse the repository at this point in the history
The state of the network profile needs to be stored in memory so that
it isn't overriden when the offline state is changed with setOffline.
  • Loading branch information
ankur22 committed Nov 13, 2023
1 parent d2cc8de commit 93a6c1b
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions common/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ type NetworkProfile struct {
UploadThroughput float64
}

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

// Credentials holds HTTP authentication credentials.
type Credentials struct {
Username string `js:"username"`
Expand Down Expand Up @@ -92,6 +101,7 @@ type NetworkManager struct {

extraHTTPHeaders map[string]string
offline bool
networkProfile NetworkProfile
userCacheDisabled bool
userReqInterceptionEnabled bool
protocolReqInterceptionEnabled bool
Expand Down Expand Up @@ -124,6 +134,7 @@ func NewNetworkManager(
reqIDToRequest: make(map[network.RequestID]*Request),
attemptedAuth: make(map[fetch.RequestID]bool),
extraHTTPHeaders: make(map[string]string),
networkProfile: NewNetworkProfile(),
}
m.initEvents()
if err := m.initDomains(); err != nil {
Expand Down Expand Up @@ -736,7 +747,12 @@ func (m *NetworkManager) SetOfflineMode(offline bool) {
}
m.offline = offline

action := network.EmulateNetworkConditions(m.offline, 0, -1, -1)
action := network.EmulateNetworkConditions(
m.offline,
m.networkProfile.Latency,
m.networkProfile.DownloadThroughput,
m.networkProfile.UploadThroughput,
)
if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil {
k6ext.Panic(m.ctx, "setting offline mode: %w", err)
}
Expand All @@ -745,11 +761,16 @@ func (m *NetworkManager) SetOfflineMode(offline bool) {
// ThrottleNetwork changes the network attributes in chrome to simulate slower
// networks e.g. a slow 3G connection.
func (m *NetworkManager) ThrottleNetwork(networkProfile NetworkProfile) error {
if m.networkProfile == networkProfile {
return nil
}
m.networkProfile = networkProfile

action := network.EmulateNetworkConditions(
m.offline,
networkProfile.Latency,
networkProfile.DownloadThroughput,
networkProfile.UploadThroughput,
m.networkProfile.Latency,
m.networkProfile.DownloadThroughput,
m.networkProfile.UploadThroughput,
)
if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil {
return fmt.Errorf("throttling network: %w", err)
Expand Down

0 comments on commit 93a6c1b

Please sign in to comment.