-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
216 lines (201 loc) · 5.18 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
const { remote } = require("webdriverio");
const stats = require("stats-lite");
const ervy = require("ervy");
const { bar, bg } = ervy;
const OCTANE_URL = "http://chromium.github.io/octane/";
const START_OCTANE_SELECTOR = "#main-banner";
const IS_RUNNING_SELECTOR = "#bar-appendix";
const OCTANE_SCORE_TEMPLATE = "Octane Score: ";
const DEFAULT_HOSTNAME = "localhost";
const DEFAULT_PORT = 4444;
const argv = require("yargs").argv;
(async () => {
const headless = argv.s || false; // headless or not
const iterations = argv.i || 10; // number of iterations
const nBrowsersMode = argv.n || false; // 1 or N browsers ?
const hostname = argv.h || DEFAULT_HOSTNAME;
const port = argv.p || DEFAULT_PORT;
const verbose = argv.v || false;
if (nBrowsersMode) {
await runNBrowserNiterations({
headless,
iterations,
hostname,
port,
verbose,
});
} else {
await run1BrowserNiterations({
headless,
iterations,
hostname,
port,
verbose,
});
}
})();
// Open browser, repeat ITERATIONS times: open a tab and calculate Octane
async function run1BrowserNiterations({
headless,
iterations,
hostname,
port,
verbose,
}) {
const scores = await getOctane1BrowserNIterations({
headless,
iterations,
hostname,
port,
verbose,
});
displayResult({
description: `Open ${
headless ? "headless" : ""
} browser, repeat ${iterations} times: open a tab and calculate Octane`,
scores,
});
}
// repeat ITERATIONS times: open browser, open a tab and calculate Octane
async function runNBrowserNiterations({
headless,
iterations,
hostname,
port,
verbose,
}) {
const scores = await getOctaneNBrowserNIterations({
headless,
iterations,
hostname,
port,
verbose,
});
displayResult({
description: `repeat ${iterations} times: open ${
headless ? "headless" : ""
} browser, open a tab and calculate Octane`,
scores,
});
}
// log to the console a bunch of stats (mean/min/max/stdDev)
function displayResult(results) {
console.log(`## ${results.description}`);
const scores = results.scores;
const mean = Math.round(stats.mean(scores));
const stdDev = Math.round(stats.sampleStdev(scores));
const min = Math.min.apply(null, scores);
const max = Math.max.apply(null, scores);
const barData = scores.map((entry, index) => {
return { key: ` ${index + 1} `, value: entry, style: bg("green") };
});
console.log(bar(barData, { barWidth: 5 }));
console.log(
`Statistics:: mean: ${mean}, standard deviation: ${stdDev} (min: ${min}, max ${max})\n`
);
}
async function getOctane1BrowserNIterations({
headless,
iterations,
hostname,
port,
verbose,
}) {
const scores = [];
const browser = await openBrowser({ headless, hostname, port });
for (let i = 0; i < iterations; i++) {
const score = await getOctaneScore(browser);
scores.push(score);
if (verbose) {
console.log(`${i + 1}) ${score}`);
}
}
await closeBrowser(browser);
return scores;
}
async function openBrowser({ headless, hostname, port }) {
let capabilities = {
browserName: "chrome",
};
debugger;
if (headless) {
capabilities["goog:chromeOptions"] = {
// to run chrome headless the following flags are required
// (see https://developers.google.com/web/updates/2017/04/headless-chrome)
args: ["--headless"],
};
}
const options = {
logLevel: "warn", // trace | debug | info | warn | error | silent
capabilities,
};
if (hostname && port) {
options.port = port;
options.hostname = hostname;
}
const browser = await remote(options);
return browser;
}
async function wait(browser, milliseconds) {
await browser.pause(milliseconds);
}
async function closeBrowser(browser) {
await browser.deleteSession();
}
async function getOctaneNBrowserNIterations({
headless,
iterations,
hostname,
port,
verbose,
}) {
const scores = [];
for (let i = 0; i < iterations; i++) {
const browser = await openBrowser({
headless,
hostname,
port,
});
// wait for 15s for startup tasks to complete
wait(browser, 15 * 1000);
const score = await getOctaneScore(browser);
if (score) {
scores.push(score);
if (verbose) {
console.log(`${i + 1}) ${score}`);
}
}
await closeBrowser(browser);
}
return scores;
}
async function getOctaneScore(browser) {
let score = null;
await browser.url(OCTANE_URL);
const start = await browser.$(START_OCTANE_SELECTOR);
await start.click();
await wait(browser, 60 * 1000);
let found = false;
while (found === false) {
const isRunning = await browser.$(IS_RUNNING_SELECTOR);
if (await isRunning.isExisting()) {
await wait(browser, 60 * 1000);
} else {
found = true;
}
}
const octaneResultText = await browser.execute(() => {
const element = document.querySelector("#main-banner");
if (element) {
return element.textContent;
}
return null;
});
if (!octaneResultText) {
console.error("Unexpected error: could not find final Octane Score");
} else {
const octane = octaneResultText.replace(OCTANE_SCORE_TEMPLATE, "");
score = octane;
}
return score;
}