/
plugin-timing.js
executable file
·110 lines (101 loc) · 2.45 KB
/
plugin-timing.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
#!/usr/bin/env node
const babel = require("@babel/core");
const preset = require("../packages/babel-preset-minify");
const fs = require("fs");
const Table = require("cli-table");
const hop = (o, key) => Object.hasOwnProperty.call(o, key);
class Benchmark {
constructor({
now = () => process.hrtime(),
diff = start => {
const delta = process.hrtime(start);
return delta[0] * 1e3 + delta[1] / 1e6;
}
} = {}) {
this.events = {};
this.visits = {};
this.results = {};
this.now = now;
this.diff = diff;
}
push(name) {
if (!hop(this.events, name)) {
this.events[name] = [];
this.visits[name] = 0;
}
this.events[name].push(this.now());
this.visits[name]++;
}
pop(name) {
if (hop(this.events, name) && this.events[name].length > 0) {
const start = this.events[name].shift();
const delta = this.diff(start);
if (!hop(this.results, name)) {
this.results[name] = {
aggregate: 0,
values: []
};
}
this.results[name].aggregate += delta;
this.results[name].values.push(delta);
}
}
}
run(process.argv[2]);
function run(file) {
const b = new Benchmark();
babel.transformSync(fs.readFileSync(file).toString(), {
presets: [preset],
babelrc: false,
configFile: false,
minified: true,
compact: true,
wrapPluginVisitorMethod(pluginAlias, visitorType, callback) {
return function(...args) {
b.push(pluginAlias);
callback.call(this, ...args);
b.pop(pluginAlias);
};
}
});
const table = new Table({
head: ["pluginAlias", "time(ms)", "# visits", "time/visit(ms)"],
chars: {
top: "",
"top-mid": "",
"top-left": "",
"top-right": "",
bottom: "",
"bottom-mid": "",
"bottom-left": "",
"bottom-right": "",
left: "",
"left-mid": "",
mid: "",
"mid-mid": "",
right: "",
"right-mid": "",
middle: " "
},
style: {
"padding-left": 0,
"padding-right": 0,
head: ["bold"]
}
});
const results = Object.keys(b.results)
.map(name => [name, b.results[name].aggregate, b.visits[name]])
.sort((a, b) => {
if (a[1] < b[1]) return 1;
if (a[1] > b[1]) return -1;
return 0;
})
.map(arr => [
arr[0],
arr[1].toFixed(3),
arr[2],
(arr[1] / arr[2]).toFixed(3)
]);
table.push(...results);
console.log(table.toString());
}