/
index.js
216 lines (198 loc) · 6.21 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
'use strict';
const log = require('intel').getLogger('browsertime');
const merge = require('lodash.merge');
const StorageManager = require('../../support/storageManager');
const FirefoxDelegate = require('../../firefox/webdriver/firefoxDelegate');
const ChromeDelegate = require('../../chrome/webdriver/chromeDelegate');
const { addConnectivity, removeConnectivity } = require('../../connectivity');
const util = require('../../support/util');
const harUtil = require('../../support/har/');
const XVFB = require('../../support/xvfb');
const ExtensionServer = require('../../extensionserver/');
const Iteration = require('./iteration');
const Collector = require('./collector');
const run = require('./run');
const engineUtils = require('../../support/engineUtils');
const defaults = {
scripts: [],
iterations: 3,
delay: 0,
videoParams: {}
};
const delay = ms => new Promise(res => setTimeout(res, ms));
function shouldDelay(index, total, delay) {
const isLast = total - index <= 1;
return delay > 0 && !isLast;
}
function addExtraFieldsToHar(totalResults, har, options) {
if (har) {
let harPageNumber = 0;
// We test one page
// Let's do a better fix for this later on
// right now this fixes https://github.com/sitespeedio/browsertime/issues/754
if (har.log.pages.length === options.iterations) {
for (let harPage of har.log.pages) {
let pageNumber = harPageNumber % totalResults.length;
const visualMetric =
totalResults[pageNumber].visualMetrics[harPageNumber];
const browserScript =
totalResults[pageNumber].browserScripts[harPageNumber];
const cpu = totalResults[pageNumber].cpu[harPageNumber];
harUtil.addExtrasToHAR(
harPageNumber,
harPage,
visualMetric,
browserScript.timings,
cpu,
totalResults[pageNumber].info.url,
options
);
harPageNumber++;
}
} else {
for (let harPage of har.log.pages) {
let pageNumber = harPageNumber % totalResults.length;
for (let iteration = 0; iteration < options.iterations; iteration++) {
const visualMetric =
totalResults[pageNumber].visualMetrics[iteration];
const browserScript =
totalResults[pageNumber].browserScripts[iteration];
const cpu = totalResults[pageNumber].cpu[iteration];
harUtil.addExtrasToHAR(
harPageNumber,
harPage,
visualMetric,
browserScript.timings,
cpu,
totalResults[pageNumber].info.url,
options
);
}
harPageNumber++;
}
}
}
}
/**
* Create a new Browsertime Engine.
* @class
*/
class Engine {
constructor(options) {
this.options = merge({}, defaults, options);
if (log.isEnabledFor(log.DEBUG)) {
log.debug('Running with options: %:2j', this.options);
}
this.myXVFB = new XVFB(this.options);
this.myExtensionServer = new ExtensionServer(this.options);
}
/**
* Start the engine. Will prepare everything before you will start your run:
* * Start XVFB (if it is configured)
* * Set connectivity
* * Start the extension server
*/
async start() {
return Promise.all([
this.myXVFB.start(),
addConnectivity(this.options),
this.myExtensionServer.start()
]);
}
async runByScript(
navigationScript,
name,
scriptsByCategory,
asyncScriptsByCategory
) {
const options = this.options;
const storageManager = new StorageManager(name, options);
const engineDelegate =
this.options.browser === 'firefox'
? new FirefoxDelegate(storageManager.directory, this.options)
: new ChromeDelegate(storageManager, this.options);
const iteration = new Iteration(
storageManager,
this.myExtensionServer,
engineDelegate,
scriptsByCategory,
asyncScriptsByCategory,
options
);
await storageManager.createDataDir();
await engineDelegate.onStart(name, options);
const collector = new Collector(name, storageManager, options);
log.info(
'Running tests using %s - %s iteration(s)',
`${options.browser[0].toUpperCase()}${options.browser.slice(1)}`,
options.iterations
);
for (let index = 1; index < options.iterations + 1; index++) {
const data = await iteration.run(navigationScript, index);
await collector.perIteration(data, index);
if (shouldDelay(index, options.iterations, options.delay)) {
await delay(options.delay);
}
}
const extras = await engineDelegate.onStop();
// Backfill the fully loaded data that we extract from the HAR
if (extras.har) {
const fullyLoadedPerUrl = harUtil.getFullyLoaded(extras.har);
for (let data of fullyLoadedPerUrl) {
collector.addFullyLoaded(data.url, data.fullyLoaded);
}
}
const totalResult = collector.getResults();
// Add extra fields to the HAR
// to make the HAR files better when we use them in
// compare.sitespeed.io
if (extras.har) {
addExtraFieldsToHar(totalResult, extras.har, options);
totalResult.har = extras.har;
}
util.logResultLogLine(totalResult);
return totalResult;
}
async run(url, scriptsByCategory, asyncScriptsByCategory) {
return this.runByScript(
run([url]),
url,
scriptsByCategory,
asyncScriptsByCategory
);
}
async runMultiple(urlOrFiles, scriptsByCategory, asyncScriptsByCategory) {
const scripts = [];
let name;
for (let urlOrFile of urlOrFiles) {
if (urlOrFile.indexOf('http') > -1) {
scripts.push(urlOrFile);
} else {
scripts.push(engineUtils.loadScript(urlOrFile));
}
if (!name) {
name = urlOrFile;
}
}
return this.runByScript(
run(scripts),
name,
scriptsByCategory,
asyncScriptsByCategory
);
}
/**
* Stop the engine. Will stop everything started in start().
* * Stop XVFB (if it is configured)
* * Remove connectivity
* * Stop the extension server
*/
async stop() {
return Promise.all([
this.myXVFB.stop(),
removeConnectivity(this.options),
this.myExtensionServer.stop()
]);
}
}
module.exports = Engine;