1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function alignLeft(str, len, ch) {
|
22 | return str + new Array(len - str.length + 1).join(ch || " ");
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function alignRight(str, len, ch) {
|
35 | return new Array(len - str.length + 1).join(ch || " ") + str;
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | const enabled = !!process.env.TIMING;
|
43 |
|
44 | const HEADERS = ["Rule", "Time (ms)", "Relative"];
|
45 | const ALIGN = [alignLeft, alignRight, alignRight];
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function display(data) {
|
55 | let total = 0;
|
56 | const rows = Object.keys(data)
|
57 | .map(key => {
|
58 | const time = data[key];
|
59 |
|
60 | total += time;
|
61 | return [key, time];
|
62 | })
|
63 | .sort((a, b) => b[1] - a[1])
|
64 | .slice(0, 10);
|
65 |
|
66 | rows.forEach(row => {
|
67 | row.push(`${(row[1] * 100 / total).toFixed(1)}%`);
|
68 | row[1] = row[1].toFixed(3);
|
69 | });
|
70 |
|
71 | rows.unshift(HEADERS);
|
72 |
|
73 | const widths = [];
|
74 |
|
75 | rows.forEach(row => {
|
76 | const len = row.length;
|
77 |
|
78 | for (let i = 0; i < len; i++) {
|
79 | const n = row[i].length;
|
80 |
|
81 | if (!widths[i] || n > widths[i]) {
|
82 | widths[i] = n;
|
83 | }
|
84 | }
|
85 | });
|
86 |
|
87 | const table = rows.map(row => (
|
88 | row
|
89 | .map((cell, index) => ALIGN[index](cell, widths[index]))
|
90 | .join(" | ")
|
91 | ));
|
92 |
|
93 | table.splice(1, 0, widths.map((width, index) => {
|
94 | const extraAlignment = index !== 0 && index !== widths.length - 1 ? 2 : 1;
|
95 |
|
96 | return ALIGN[index](":", width + extraAlignment, "-");
|
97 | }).join("|"));
|
98 |
|
99 | console.log(table.join("\n"));
|
100 | }
|
101 |
|
102 |
|
103 | module.exports = (function() {
|
104 |
|
105 | const data = Object.create(null);
|
106 |
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | function time(key, fn) {
|
115 | if (typeof data[key] === "undefined") {
|
116 | data[key] = 0;
|
117 | }
|
118 |
|
119 | return function(...args) {
|
120 | let t = process.hrtime();
|
121 |
|
122 | fn(...args);
|
123 | t = process.hrtime(t);
|
124 | data[key] += t[0] * 1e3 + t[1] / 1e6;
|
125 | };
|
126 | }
|
127 |
|
128 | if (enabled) {
|
129 | process.on("exit", () => {
|
130 | display(data);
|
131 | });
|
132 | }
|
133 |
|
134 | return {
|
135 | time,
|
136 | enabled
|
137 | };
|
138 |
|
139 | }());
|