Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
115 lines (92 loc) · 2.61 KB
/
Copy pathindex.js
File metadata and controls
115 lines (92 loc) · 2.61 KB
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
const median = require('stats-median').calc;
const percentile = require('@elstats/percentile');
const mean = require('@elstats/mean');
const variance = require('stats-variance').calc;
const mad = require('stats-mad');
const timingsToCollect = require('./timings');
function twoDigits(value) {
return (value / 1000).toFixed(2) * 1;
}
function getTimings(measurements) {
return (name) => measurements
.map(measurement => {
const found = measurement.find(timing => timing.name === name);
if (found) {
return found.value;
}
return null;
})
.filter(Boolean)
.sort();
}
function p95(data) {
return percentile(data, 95);
}
function analyze(data, custom) {
const getDataTimings = getTimings(data);
return timingsToCollect.concat(custom).map(t => {
const timings = getDataTimings(t);
return {
name: t,
metrics: {
median: twoDigits(median(timings)),
mean: twoDigits(mean(timings)),
p95: twoDigits(p95(timings)),
variance: twoDigits(variance(timings)),
mad: twoDigits(mad(timings))
}
};
});
}
function toTiming({ name, startTime }) {
return {
name,
value: startTime.toFixed(0) * 1
};
}
function getPreset(error, presets, presetName) {
if (!presetName) {
return null;
}
if (!presets[presetName]) {
throw new Error(`Unknown ${error} ${presetName}`);
}
return presets[presetName];
}
async function start({
url,
number,
notify,
custom,
runner,
insecure,
device,
network,
devicePresets,
networkPresets
}) {
const data = [];
const deviceSettings = getPreset('device name', devicePresets, device);
const networkSettings = getPreset('network preset', networkPresets, network);
for (let current = 1; current <= number; current++) {
notify({ current });
const { timing, paint, mark } = await runner({
url,
insecure,
deviceSettings,
networkSettings
});
const timings = Object.keys(timing)
.map(name => ({
name,
value: timing[name] - timing.connectStart
}))
.concat(paint.map(toTiming), mark.map(toTiming))
.filter(t => t.value > 0);
data.push(timings);
notify({ timings });
}
const results = analyze(data, custom);
return { results, deviceSettings, networkSettings };
}
module.exports = params => start(params);