-
Notifications
You must be signed in to change notification settings - Fork 44
/
cli.js
executable file
·113 lines (93 loc) · 3.25 KB
/
cli.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
#!/usr/bin/env node
'use strict';
var path = require('path');
var meow = require('meow');
var babar = require('babar');
var loudRejection = require('loud-rejection');
var speedIndex = require('.');
const OUTPUT_GREEN = '\x1b[32m';
const OUTPUT_BOLD = '\x1b[1m';
const OUTPUT_RESET = '\x1b[22m\x1b[39m';
/** @param {speedIndex.Output<'all'|'speedIndex'|'perceptualSpeedIndex'>} res */
function display(res) {
const startTs = res.beginning;
const visualProgress = res.frames.map(frame => {
const ts = Math.floor(frame.getTimeStamp() - startTs);
return `${ts}=${Math.floor(frame.getProgress())}%`;
}).join(', ');
const visualPreceptualProgress = res.frames.map(frame => {
const ts = Math.floor(frame.getTimeStamp() - startTs);
return `${ts}=${Math.floor(frame.getPerceptualProgress())}%`;
}).join(', ');
const log = [
`First Visual Change: ${res.first}`,
`Visually Complete: ${res.complete}`,
'',
`Speed Index: ${res.speedIndex.toFixed(1)}`,
`Visual Progress: ${visualProgress}`,
'',
`Perceptual Speed Index: ${res.perceptualSpeedIndex.toFixed(1)}`,
`Perceptual Visual Progress: ${visualPreceptualProgress}`
].join(`\n`);
console.log(log);
}
/** @param {speedIndex.Output<'all'>} res */
function displayPretty(res) {
/** @param {string} content */
const green = (content) => OUTPUT_GREEN + content + OUTPUT_RESET;
/** @param {string} content */
const bold = (content) => OUTPUT_BOLD + content + OUTPUT_RESET;
console.log([
`${bold('Recording duration')}: ${green(res.duration + ' ms')} (${res.frames.length} frames found)`,
`${bold('First visual change')}: ${green(res.first + ' ms')}`,
`${bold('Last visual change')}: ${green(res.complete + ' ms')}`,
`${bold('Speed Index')}: ${green(res.speedIndex.toFixed(1))}`,
`${bold('Perceptual Speed Index')}: ${green(res.perceptualSpeedIndex.toFixed(1))}`,
'',
`${bold('Histogram visual progress:')}`
].join('\n'));
var baseTs = res.frames[0].getTimeStamp();
var progress = res.frames.map(frame => [frame.getTimeStamp() - baseTs, frame.getProgress()]);
console.log(babar(progress, {grid: 'grey'}));
console.log(bold('Histogram perceptual visual progress:'));
var perceptualProgress = res.frames.map(frame => [frame.getTimeStamp() - baseTs, frame.getPerceptualProgress()]);
console.log(babar(perceptualProgress, {grid: 'grey'}));
}
/** @param {Error} err */
function handleError(err) {
console.error(err.message);
console.log(Object.keys(err));
if (err.stack) {
console.log(err.stack);
}
process.exit(1);
}
loudRejection();
var cli = meow([
'Usage',
' $ speedline <timeline>',
'',
'Options',
' --pretty Pretty print the output',
' --fast Skip parsing frames between similar ones',
' Disclaimer: may result in different metrics due to skipped frames',
'',
'Examples',
' $ speedline ./timeline.json'
]);
if (cli.input.length !== 1) {
const err = new Error('You should specify a file path!');
handleError(err);
}
var filePath = path.resolve(process.cwd(), cli.input[0]);
if (cli.flags.fast) {
console.warn('WARNING: using --fast may result in different metrics due to skipped frames');
}
speedIndex(filePath, {fastMode: cli.flags.fast}).then(function (res) {
if (cli.flags.pretty) {
return displayPretty(res);
}
display(res);
}).catch(err => {
handleError(err);
});