This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics
102 lines (85 loc) · 3.47 KB
/
metrics
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
#!/usr/bin/env node
const metrics = require('commander');
const clientCollector = require('./lib/clientMetrics');
const serverCollector = require('./lib/serverMetrics');
const bundleCollector = require('./lib/bundleMetrics');
const table = require('markdown-table');
metrics
.version('2.6.1')
.option('--chromiumPath [chromiumPath]', 'Chromium path for puppeteer')
.option('--ignoreHTTPSErrors', 'Ignore HTTPS errors with puppeteer when true')
.option('--control [control]', 'Specify the URL to be used as the control to compare against')
.option('--controlBundleMetricsUrl [controlBundleMetricsUrl]', 'Specify the URL to get to the bundle metrics for the control')
.option('--prBundleMetricsUrl [prBundleMetricsUrl]', 'Specify the URL to get to the bundle metrics for the pr')
.option('--pr [pr]', 'Specify the URL to be used to compare the control to')
.option('--sampleSize [sampleSize]', 'Number of times to run and average the metrics. Defaults to 1')
.parse(process.argv);
if (!process.argv.slice(2).length) {
metrics.outputHelp();
}
const tableHeader = [['Metric',
'Desktop Control',
'Desktop PR',
'Desktop ∆',
'Mobile Control',
'Mobile PR',
'Mobile ∆']];
let metricsCollectors;
if (metrics.prBundleMetricsUrl || metrics.controlBundleMetricsUrl) {
metricsCollectors = [bundleCollector, serverCollector, clientCollector];
} else {
metricsCollectors = [serverCollector, clientCollector];
}
function sumMetrics(total, metric) {
Object.keys(metric).map((field) => {
if (total[field]) {
total[field] += metric[field];
} else {
total[field] = metric[field];
}
});
return total;
}
function averageSample(sample) {
const total = sample.reduce(function (acc, metric) {
acc['desktop'] = sumMetrics(acc['desktop'], metric['desktop']);
acc['mobile'] = sumMetrics(acc['mobile'], metric['mobile']);
return acc;
});
let average = total;
['desktop', 'mobile'].map((desktopOrMobile) => {
Object.keys(average[desktopOrMobile]).map((field) => {
average[desktopOrMobile][field] /= sample.length;
});
});
return average;
}
async function printMarkdownTable(controlUrl, prUrl, options) {
let formattedTables = metricsCollectors.map(async (collector) => {
let controlSample = [];
let prSample = [];
for (let i = 0; i < options.sampleSize; i++) {
if (collector === bundleCollector) {
controlSample.push(await collector.collect(options['controlBundleMetricsUrl'], options));
prSample.push(await collector.collect(options['prBundleMetricsUrl'], options));
} else {
controlSample.push(await collector.collect(controlUrl, options));
prSample.push(await collector.collect(prUrl, options));
}
}
const control = averageSample(controlSample);
const pr = averageSample(prSample);
return collector.formatTable(control, pr);
});
formattedTables = await Promise.all(formattedTables);
console.log(table([].concat.apply(tableHeader, formattedTables)));
}
printMarkdownTable(metrics.control,
metrics.pr,
{
controlBundleMetricsUrl: metrics.controlBundleMetricsUrl,
prBundleMetricsUrl: metrics.prBundleMetricsUrl,
executablePath: metrics.chromiumPath,
ignoreHTTPSErrors: metrics.ignoreHTTPSErrors,
sampleSize: metrics.sampleSize || 1
});