1 | /**
|
2 | * @fileoverview Tracks performance of individual rules.
|
3 | * @author Brandon Mills
|
4 | */
|
5 |
|
6 | ;
|
7 |
|
8 | //------------------------------------------------------------------------------
|
9 | // Helpers
|
10 | //------------------------------------------------------------------------------
|
11 |
|
12 | /* istanbul ignore next */
|
13 | /**
|
14 | * Align the string to left
|
15 | * @param {string} str string to evaluate
|
16 | * @param {int} len length of the string
|
17 | * @param {string} ch delimiter character
|
18 | * @returns {string} modified string
|
19 | * @private
|
20 | */
|
21 | function alignLeft(str, len, ch) {
|
22 | return str + new Array(len - str.length + 1).join(ch || " ");
|
23 | }
|
24 |
|
25 | /* istanbul ignore next */
|
26 | /**
|
27 | * Align the string to right
|
28 | * @param {string} str string to evaluate
|
29 | * @param {int} len length of the string
|
30 | * @param {string} ch delimiter character
|
31 | * @returns {string} modified string
|
32 | * @private
|
33 | */
|
34 | function alignRight(str, len, ch) {
|
35 | return new Array(len - str.length + 1).join(ch || " ") + str;
|
36 | }
|
37 |
|
38 | //------------------------------------------------------------------------------
|
39 | // Module definition
|
40 | //------------------------------------------------------------------------------
|
41 |
|
42 | const enabled = !!process.env.TIMING;
|
43 |
|
44 | const HEADERS = ["Rule", "Time (ms)", "Relative"];
|
45 | const ALIGN = [alignLeft, alignRight, alignRight];
|
46 |
|
47 | /* istanbul ignore next */
|
48 | /**
|
49 | * display the data
|
50 | * @param {Object} data Data object to be displayed
|
51 | * @returns {string} modified string
|
52 | * @private
|
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((w, index) => {
|
94 | if (index !== 0 && index !== widths.length - 1) {
|
95 | w++;
|
96 | }
|
97 |
|
98 | return ALIGN[index](":", w + 1, "-");
|
99 | }).join("|"));
|
100 |
|
101 | console.log(table.join("\n")); // eslint-disable-line no-console
|
102 | }
|
103 |
|
104 | /* istanbul ignore next */
|
105 | module.exports = (function() {
|
106 |
|
107 | const data = Object.create(null);
|
108 |
|
109 | /**
|
110 | * Time the run
|
111 | * @param {*} key key from the data object
|
112 | * @param {Function} fn function to be called
|
113 | * @returns {Function} function to be executed
|
114 | * @private
|
115 | */
|
116 | function time(key, fn) {
|
117 | if (typeof data[key] === "undefined") {
|
118 | data[key] = 0;
|
119 | }
|
120 |
|
121 | return function() {
|
122 | let t = process.hrtime();
|
123 |
|
124 | fn.apply(null, Array.prototype.slice.call(arguments));
|
125 | t = process.hrtime(t);
|
126 | data[key] += t[0] * 1e3 + t[1] / 1e6;
|
127 | };
|
128 | }
|
129 |
|
130 | if (enabled) {
|
131 | process.on("exit", () => {
|
132 | display(data);
|
133 | });
|
134 | }
|
135 |
|
136 | return {
|
137 | time,
|
138 | enabled
|
139 | };
|
140 |
|
141 | }());
|