forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload-simulator.js
95 lines (84 loc) · 3.96 KB
/
load-simulator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* @license Copyright 2018 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
import {makeComputedArtifact} from './computed-artifact.js';
import * as constants from '../config/constants.js';
import {Simulator} from '../lib/dependency-graph/simulator/simulator.js';
import NetworkAnalysis from './network-analysis.js';
class LoadSimulator {
/**
* @param {{devtoolsLog: LH.DevtoolsLog, settings: Immutable<LH.Config.Settings>}} data
* @param {LH.Artifacts.ComputedContext} context
* @return {Promise<Simulator>}
*/
static async compute_(data, context) {
const {throttlingMethod, throttling, precomputedLanternData} = data.settings;
const networkAnalysis = await NetworkAnalysis.request(data.devtoolsLog, context);
/** @type {LH.Gatherer.Simulation.Options} */
const options = {
additionalRttByOrigin: networkAnalysis.additionalRttByOrigin,
serverResponseTimeByOrigin: networkAnalysis.serverResponseTimeByOrigin,
observedThroughput: networkAnalysis.throughput,
};
// If we have precomputed lantern data, overwrite our observed estimates and use precomputed instead
// for increased stability.
if (precomputedLanternData) {
options.additionalRttByOrigin = new Map(Object.entries(
precomputedLanternData.additionalRttByOrigin));
options.serverResponseTimeByOrigin = new Map(Object.entries(
precomputedLanternData.serverResponseTimeByOrigin));
}
switch (throttlingMethod) {
case 'provided':
options.rtt = networkAnalysis.rtt;
options.throughput = networkAnalysis.throughput;
options.cpuSlowdownMultiplier = 1;
options.layoutTaskMultiplier = 1;
break;
case 'devtools':
if (throttling) {
options.rtt =
throttling.requestLatencyMs / constants.throttling.DEVTOOLS_RTT_ADJUSTMENT_FACTOR;
options.throughput =
throttling.downloadThroughputKbps * 1024 /
constants.throttling.DEVTOOLS_THROUGHPUT_ADJUSTMENT_FACTOR;
}
options.cpuSlowdownMultiplier = 1;
options.layoutTaskMultiplier = 1;
break;
case 'simulate':
if (throttling) {
options.rtt = throttling.rttMs;
options.throughput = throttling.throughputKbps * 1024;
options.cpuSlowdownMultiplier = throttling.cpuSlowdownMultiplier;
}
break;
default:
// intentionally fallback to simulator defaults
break;
}
return new Simulator(options);
}
/**
* @param {LH.Artifacts.NetworkAnalysis} networkAnalysis
* @return {LH.PrecomputedLanternData}
*/
static convertAnalysisToSaveableLanternData(networkAnalysis) {
/** @type {LH.PrecomputedLanternData} */
const lanternData = {additionalRttByOrigin: {}, serverResponseTimeByOrigin: {}};
for (const [origin, value] of networkAnalysis.additionalRttByOrigin.entries()) {
if (origin.startsWith('http')) lanternData.additionalRttByOrigin[origin] = value;
}
for (const [origin, value] of networkAnalysis.serverResponseTimeByOrigin.entries()) {
if (origin.startsWith('http')) lanternData.serverResponseTimeByOrigin[origin] = value;
}
return lanternData;
}
}
// TODO(esmodules): For all computed artifacts:
// const LoadSimulatorComputed = makeComputedArtifact(LoadSimulator, ['devtoolsLog', 'settings']);
// export {LoadSimulator as LoadSimulatorComputed};
export default makeComputedArtifact(LoadSimulator, ['devtoolsLog', 'settings']);