Skip to content
This repository was archived by the owner on Mar 19, 2025. It is now read-only.

Commit 330aecc

Browse files
fix(wpt): apply devtools factors to network (#5)
1 parent 8fb936d commit 330aecc

File tree

1 file changed

+72
-23
lines changed

1 file changed

+72
-23
lines changed

src/chrome.js

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,94 @@ const chromeLauncher = require('chrome-launcher');
44
const puppeteer = require('puppeteer-core');
55
const lighthouse = require('lighthouse');
66

7-
// via WebPageTest settings
8-
// WPO-Foundation/webpagetest/blob/master/www/settings/connectivity.ini.sample
9-
//
10-
// These are divided by 8 because we need bytes/s for Chrome
11-
//
7+
/**
8+
* Adjustments needed for DevTools network throttling to simulate
9+
* more realistic network conditions, per Lighthouse:
10+
* https://github.com/GoogleChrome/lighthouse/blob/6e5fc878f8cc69e00620b20092bfad1da6c1e4e2/lighthouse-core/config/constants.js#L8-L15
11+
*
12+
* @see https://crbug.com/721112
13+
* @see https://docs.google.com/document/d/10lfVdS1iDWCRKQXPfbxEn4Or99D64mvNlugP1AQuFlE/edit
14+
*
15+
* Cheers @patrickhulce for the heads up! :-)
16+
*/
17+
const DEVTOOLS_RTT_ADJUSTMENT_FACTOR = 3.75;
18+
const DEVTOOLS_THROUGHPUT_ADJUSTMENT_FACTOR = 0.9;
19+
20+
// Per https://chromium.googlesource.com/chromium/src/+/8a4e4200086bc67a61c83d60edfb450844377e5a/third_party/WebKit/Source/devtools/front_end/network_conditions/NetworkConditionsSelector.js#222
21+
const CHROME_SLOW_RTT_ADJUSTMENT_FACTOR = 5;
22+
const CHROME_SLOW_THROUGHPUT_ADJUSTMENT_FACTOR = 0.8;
23+
24+
/**
25+
* via WebPageTest settings
26+
* https://github.com/WPO-Foundation/webpagetest/blob/master/www/settings/connectivity.ini.sample
27+
*/
1228
const NETWORK = {
1329
edge: {
1430
offline: false,
1531
latency: 840,
16-
downloadThroughput: Math.floor(240000 / 8),
17-
uploadThroughput: Math.floor(240000 / 8),
32+
downloadThroughput: 240000,
33+
uploadThroughput: 240000,
34+
rttAdjustFactor: CHROME_SLOW_RTT_ADJUSTMENT_FACTOR,
35+
throughputAdjustment: CHROME_SLOW_THROUGHPUT_ADJUSTMENT_FACTOR,
1836
},
1937
twog: {
2038
offline: false,
2139
latency: 800,
22-
downloadThroughput: Math.floor(280000 / 8),
23-
uploadThroughput: Math.floor(256000 / 8),
40+
downloadThroughput: 280000,
41+
uploadThroughput: 256000,
42+
rttAdjustFactor: CHROME_SLOW_RTT_ADJUSTMENT_FACTOR,
43+
throughputAdjustment: CHROME_SLOW_THROUGHPUT_ADJUSTMENT_FACTOR,
2444
},
2545
threegslow: {
2646
offline: false,
2747
latency: 400,
28-
downloadThroughput: Math.floor(400000 / 8),
29-
uploadThroughput: Math.floor(400000 / 8),
48+
downloadThroughput: 400000,
49+
uploadThroughput: 400000,
50+
rttAdjustFactor: CHROME_SLOW_RTT_ADJUSTMENT_FACTOR,
51+
throughputAdjustment: CHROME_SLOW_THROUGHPUT_ADJUSTMENT_FACTOR,
3052
},
3153
threeg: {
3254
offline: false,
3355
latency: 300,
34-
downloadThroughput: Math.floor(1600000 / 8),
35-
uploadThroughput: Math.floor(768000 / 8),
56+
downloadThroughput: 1600000,
57+
uploadThroughput: 768000,
58+
rttAdjustFactor: DEVTOOLS_RTT_ADJUSTMENT_FACTOR,
59+
throughputAdjustment: DEVTOOLS_THROUGHPUT_ADJUSTMENT_FACTOR,
3660
},
3761
threegfast: {
3862
offline: false,
3963
latency: 170,
40-
downloadThroughput: Math.floor(1600000 / 8),
41-
uploadThroughput: Math.floor(768000 / 8),
64+
downloadThroughput: 1600000,
65+
uploadThroughput: 768000,
66+
rttAdjustFactor: DEVTOOLS_RTT_ADJUSTMENT_FACTOR,
67+
throughputAdjustment: DEVTOOLS_THROUGHPUT_ADJUSTMENT_FACTOR,
4268
},
4369
fourg: {
4470
offline: false,
4571
latency: 170,
46-
downloadThroughput: Math.floor(9000000 / 8),
47-
uploadThroughput: Math.floor(9000000 / 8),
72+
downloadThroughput: 9000000,
73+
uploadThroughput: 9000000,
74+
rttAdjustFactor: DEVTOOLS_RTT_ADJUSTMENT_FACTOR,
75+
throughputAdjustment: DEVTOOLS_THROUGHPUT_ADJUSTMENT_FACTOR,
4876
},
4977
lte: {
5078
offline: false,
5179
latency: 70,
52-
downloadThroughput: Math.floor(12000000 / 8),
53-
uploadThroughput: Math.floor(12000000 / 8),
80+
downloadThroughput: 12000000,
81+
uploadThroughput: 12000000,
82+
rttAdjustFactor: DEVTOOLS_RTT_ADJUSTMENT_FACTOR,
83+
throughputAdjustment: DEVTOOLS_THROUGHPUT_ADJUSTMENT_FACTOR,
5484
},
5585
};
5686

87+
function withDevToolsThroughputAdjustment(bits, factor) {
88+
return Math.floor((bits / 8) * factor);
89+
}
90+
91+
function withDevToolsRttAdjustment(ms, factor) {
92+
return ms * factor;
93+
}
94+
5795
/**
5896
* launch Chrome via Puppeteer, use puppeteer to throttle connection, run
5997
* lighthouse. Not ideal; would prefer adv throttle via comcast os level util
@@ -85,10 +123,21 @@ async function launchChromeAndRunLighthouse(url, opts, config) {
85123
console.log(
86124
`CDP: network conditions set to WPT ${opts.connection} profile.`,
87125
);
88-
return client.send(
89-
'Network.emulateNetworkConditions',
90-
NETWORK[opts.connection],
91-
);
126+
return client.send('Network.emulateNetworkConditions', {
127+
offline: NETWORK[opts.connection].offline,
128+
latency: withDevToolsRttAdjustment(
129+
NETWORK[opts.connection].latency,
130+
NETWORK[opts.connection].rttAdjustFactor,
131+
),
132+
downloadThroughput: withDevToolsThroughputAdjustment(
133+
NETWORK[opts.connection].downloadThroughput,
134+
NETWORK[opts.connection].throughputAdjustment,
135+
),
136+
uploadThroughput: withDevToolsThroughputAdjustment(
137+
NETWORK[opts.connection].uploadThroughput,
138+
NETWORK[opts.connection].throughputAdjustment,
139+
),
140+
});
92141
})
93142
.catch(err => console.error(err));
94143
} else {

0 commit comments

Comments
 (0)