-
-
Notifications
You must be signed in to change notification settings - Fork 602
/
sitespeed.js
190 lines (164 loc) · 5.26 KB
/
sitespeed.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
import { platform, release } from 'node:os';
import { env, version } from 'node:process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import fs from 'node:fs/promises';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc.js';
import intel from 'intel';
import pullAll from 'lodash.pullall';
import union from 'lodash.union';
import { configure } from './core/logging.js';
import { toArray } from './support/util.js';
import { messageMaker } from './support/messageMaker.js';
import * as filterRegistry from './support/filterRegistry.js';
import * as statsHelpers from './support/statsHelpers.js';
import { QueueHandler } from './core/queueHandler.js';
import { resultsStorage } from './core/resultsStorage/index.js';
import { parsePluginNames, loadPlugins } from './core/pluginLoader.js';
import * as urlSource from './core/url-source.js';
import * as scriptSource from './core/script-source.js';
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const packageJson = JSON.parse(
await fs.readFile(path.resolve(path.join(__dirname, '..', 'package.json')))
);
const log = intel.getLogger('sitespeedio');
dayjs.extend(utc);
const budgetResult = {
working: {},
failing: {},
error: {}
};
function hasFunctionFilter(functionName) {
return object => typeof object[functionName] === 'function';
}
function runOptionalFunction(objects, fN) {
// NOTE: note slicing due to https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments
let arguments_ = Array.from({ length: arguments.length - 2 });
for (let index = 2; index < arguments.length; index++) {
arguments_[index - 2] = arguments[index];
}
return objects
.filter(hasFunctionFilter(fN))
.map(plugin => Promise.resolve(plugin[fN].apply(plugin, arguments_)));
}
export async function run(options) {
const url = options.urls[0];
const timestamp = options.utc ? dayjs.utc() : dayjs();
const { storageManager, resultUrls } = resultsStorage(
url,
timestamp,
options
);
// Setup logging
const logDir = await storageManager.createDirectory('logs');
if (
options.browsertime &&
options.browsertime.tcpdump &&
!env.SSLKEYLOGFILE
) {
env.SSLKEYLOGFILE = path.join(
storageManager.getBaseDir(),
'SSLKEYLOGFILE.txt'
);
}
configure(options, logDir);
// Tell the world what we are using
log.info(
'Versions OS: %s nodejs: %s sitespeed.io: %s browsertime: %s coach: %s',
platform() + ' ' + release(),
version,
packageJson.version,
packageJson.dependencies.browsertime,
packageJson.dependencies['coach-core']
);
if (log.isEnabledFor(log.DEBUG)) {
log.debug('Running with options: %:2j', options);
}
let pluginNames = await parsePluginNames(options);
const plugins = options.plugins;
// Deprecated setup
if (plugins) {
if (plugins.disable) {
log.warn('--plugins.disable is deprecated, use plugins.remove instead.');
plugins.remove = plugins.disable;
}
if (plugins.load) {
log.warn('--plugins.load is deprecated, use plugins.add instead.');
plugins.add = plugins.load;
}
// Finalize the plugins that we wanna run
// First we add the new ones and then remove, that means remove
// always wins
pluginNames = union(pluginNames, toArray(plugins.add));
pullAll(pluginNames, toArray(plugins.remove));
if (plugins.list) {
log.info('The following plugins are enabled: %s', pluginNames.join(', '));
}
}
// This is the contect where we wanna run our tests
const context = {
storageManager,
resultUrls,
timestamp,
budget: budgetResult,
name: url,
log,
intel,
messageMaker,
statsHelpers,
filterRegistry
};
const queueHandler = new QueueHandler(options);
const runningPlugins = await loadPlugins(
pluginNames,
options,
context,
queueHandler
);
const urlSources = [options.multi ? scriptSource : urlSource];
const allPlugins = [...runningPlugins];
queueHandler.setup(runningPlugins);
// Open/start each and every plugin
try {
await Promise.all(
runOptionalFunction(allPlugins, 'open', context, options)
);
// Pass the URLs
const result = await queueHandler.run(urlSources);
// Close the plugins
await Promise.all(
runOptionalFunction(allPlugins, 'close', options, result.errors)
);
if (resultUrls.hasBaseUrl()) {
log.info(
'Find the result at %s',
resultUrls.reportSummaryUrl() + '/index.html'
);
}
if (options.summary && options.summary.out) {
console.log(options.summary.out);
}
let pageSummaryUrl = '';
if (resultUrls.hasBaseUrl() && options.multi) {
pageSummaryUrl = resultUrls.reportSummaryUrl() + '/index.html';
} else if (resultUrls.hasBaseUrl() && url) {
pageSummaryUrl = resultUrls.absoluteSummaryPageUrl(url) + 'index.html';
}
return {
errors: result.errors,
browsertime: result.browsertime,
har: result.browsertimeHAR,
budgetResult,
resultUrl: resultUrls.hasBaseUrl()
? resultUrls.reportSummaryUrl() + '/index.html'
: '',
pageSummaryUrl,
localPath: storageManager.getBaseDir(),
timestamp: timestamp.format()
};
} catch (error) {
log.error(error);
throw error;
}
}