-
Notifications
You must be signed in to change notification settings - Fork 18
/
cli.js
executable file
·234 lines (210 loc) · 6.63 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env node
/* eslint-disable promise/prefer-await-to-then */
'use strict';
const si = require('systeminformation');
const chalk = require('chalk');
const meow = require('meow');
const logSymbols = require('log-symbols');
const updateNotifier = require('update-notifier');
const Table = require('easy-table');
const impact = require('.');
const cli = meow(
`
Usage
$ impact <script>
Options
--raw Show the report as raw object instead of using
the GUI.
--interval <number> The sampling rate in milliseconds.
Examples
$ impact "console.log('Hello World')"
$ impact --interval=5 "console.log('Hello World')"
`,
{
flags: {
raw: {type: 'boolean', default: false},
interval: {type: 'number', default: 125},
cwd: {type: 'string', default: process.cwd()}
}
}
);
Promise.resolve()
.then(() => {
updateNotifier({pkg: cli.pkg}).notify();
})
.then(() => {
if (cli.input.length !== 1) {
cli.showHelp(0);
}
return impact(cli.input[0], {
interval: cli.flags.interval,
cwd: cli.flags.cwd
});
})
.then(async report => {
if (cli.flags.raw) {
return JSON.stringify(report, null, 2);
}
// SO INFO - BEGIN
const osInfo = await si.osInfo();
const soT = new Table();
soT.cell('Distro', `${osInfo.distro}`);
soT.cell('Release', `${osInfo.release}`);
soT.cell('Release', `${osInfo.release}`);
soT.cell('Platform', `${osInfo.platform}`);
soT.cell('Arch', `${osInfo.arch}`);
soT.newRow();
// SO INFO - END
// CPU INFO - BEGIN
const osCpu = await si.cpu();
const cpuT = new Table();
cpuT.cell('CPU', `${osCpu.manufacturer}`);
cpuT.cell('Brand', `${osCpu.brand}`);
cpuT.cell('Clock', `${osCpu.speed} GHz`);
cpuT.cell('Cores', `${osCpu.cores}`);
cpuT.newRow();
// CPU INFO - BEGIN
// MEM INFO - BEGIN
const osMemLayout = await si.memLayout();
const memT = new Table();
osMemLayout.forEach(mem => {
memT.cell('Memory', `${mem.manufacturer}`);
memT.cell('Type', `${mem.type}`);
memT.cell('Size', `${(mem.size / 1e6).toFixed(3)} MB`);
memT.cell('Clock', `${mem.clockSpeed} MHz`);
memT.newRow();
});
// MEM INFO - BEGIN
// CPU TABLE - BEGIN
const [cpu] = [report.stats.cpu];
const ctAvarageStr = chalk.keyword('orangered')('avarage');
const ctStdevStr = chalk.yellow('σ');
const ctAvarageVal = chalk.keyword('orangered')(cpu.mean.toFixed(2));
const ctStdevVal = chalk.yellow(cpu.stdev.toFixed(2));
const ctMinStr = chalk.cyan('min');
const ctMaxStr = chalk.red('max');
const ctMinVal = chalk.cyan(cpu.min.toFixed(2));
const ctMaxVal = chalk.red(cpu.max.toFixed(2));
const cuseT = new Table();
cuseT.cell(
`CPU Usage (${ctAvarageStr} ± ${ctStdevStr})`,
`${ctAvarageVal} % ± ${ctStdevVal} %`
);
cuseT.cell(
`CPU Usage Range (${ctMinStr} … ${ctMaxStr})`,
`${ctMinVal} % … ${ctMaxVal} %`
);
cuseT.newRow();
// CPU TABLE - END
// RAM TABLE - BEGIN
const [ram] = [report.stats.memory];
const rtAvarageStr = chalk.keyword('olivedrab')('avarage');
const rtStdevStr = chalk.yellow('σ');
const rtAvarageVal = chalk.keyword('olivedrab')(
(ram.mean / 1e6).toFixed(3)
);
const rtStdevVal = chalk.yellow((ram.stdev / 1e6).toFixed(3));
const rtMinStr = chalk.cyan('min');
const rtMaxStr = chalk.red('max');
const rtMinVal = chalk.cyan((ram.min / 1e6).toFixed(3));
const rtMaxVal = chalk.red((ram.max / 1e6).toFixed(3));
const museT = new Table();
museT.cell(
`RAM Usage (${rtAvarageStr} ± ${rtStdevStr})`,
`${rtAvarageVal} MB ± ${rtStdevVal} MB`
);
museT.cell(
`RAM Usage Range (${rtMinStr} … ${rtMaxStr})`,
`${rtMinVal} MB … ${rtMaxVal} MB`
);
museT.newRow();
// RAM TABLE - END
// SAMPLES INFO TABLE - BEGIN
const [times] = [report.times];
const [samples] = [report.samples];
const stVal = chalk.magenta(samples.count);
const stRealStr = chalk.keyword('deeppink')('real');
const stTargetStr = chalk.blue('target');
const stTargetFrequencyVal = chalk.blue((1000 / samples.period).toFixed(3));
const stRealFrequencyVal = chalk.keyword('deeppink')(
(
1000 /
((times.execution.end - times.execution.start) / samples.count)
).toFixed(3)
);
const stTargetPeriodVal = chalk.blue((samples.period / 1000).toFixed(3));
const stRealPeriodVal = chalk.keyword('deeppink')(
(
(times.execution.end - times.execution.start) /
samples.count /
1000
).toFixed(3)
);
const tt = new Table();
tt.cell(
'Execution time',
`${chalk.cyan(
((times.execution.end - times.execution.start) / 1000).toFixed(3)
)} s`
);
tt.cell(
'Sampling time',
`${chalk.cyan(
(times.sampling.end - times.sampling.start) / (1000).toFixed(3)
)} s`
);
tt.cell('Samples', `${stVal} samples`);
tt.newRow();
const st = new Table();
st.cell(
`Frequency (${stRealStr} ∴ ${stTargetStr})`,
`${stRealFrequencyVal} sample/s ∴ ${stTargetFrequencyVal} sample/s`
);
st.cell(
`Period (${stRealStr} ∴ ${stTargetStr})`,
`${stRealPeriodVal} s/sample ∴ ${stTargetPeriodVal} s/sample`
);
st.newRow();
// SAMPLES INFO TABLE - END
// SAMPLES - BEGIN
const frames = Object.keys(samples.list);
const samT = new Table();
frames.forEach(frame => {
const tfInstantVal = chalk.cyan((frame / 1000).toFixed(3));
const tfCPUVal = chalk.keyword('orangered')(
samples.list[frame].cpu.toFixed(2)
);
const tfRAMVal = chalk.keyword('olivedrab')(
(samples.list[frame].memory / 1e6).toFixed(3)
);
const tfPIDSVal = samples.list[frame].processes
.map(proc => chalk.gray(proc.pid))
.sort();
samT.cell('Instant', `${tfInstantVal} s`);
samT.cell('CPU Usage', `${tfCPUVal} %`);
samT.cell('RAM Usage', `${tfRAMVal} MB`);
samT.cell('PIDS', `${tfPIDSVal}`);
samT.newRow();
});
// SAMPLES - END
return (
`\n` +
`► ${chalk.bold('SYSTEM REPORT')}\n\n` +
`${soT}\n` +
`${cpuT}\n` +
`${memT}\n` +
`► ${chalk.bold('USAGE STATISTICS')}\n\n` +
`${cuseT}\n` +
`${museT}\n` +
`► ${chalk.bold('SAMPLING STATISTICS')}\n\n` +
`${tt}\n` +
`${st}\n` +
`► ${chalk.bold('SAMPLES')}\n\n` +
`${samT}`
);
})
.then(console.log)
.catch(err => {
console.error(`\n${logSymbols.error} ${err.stack}`);
process.exit(1);
});