-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathoutput.js
169 lines (145 loc) · 4.99 KB
/
output.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
const MS_IN_MINUTE = 60000;
const MS_IN_SECOND = 1000;
const chalk = require("chalk");
const { fg, bg } = require("./colours");
const { groupBy, getAverages, getTotalActiveTime } = require("./utils");
const humanTime = (ms, options = {}) => {
if (options.verbose) {
return ms.toLocaleString() + " ms";
}
const minutes = Math.floor(ms / MS_IN_MINUTE);
const secondsRaw = (ms - minutes * MS_IN_MINUTE) / MS_IN_SECOND;
const secondsWhole = Math.floor(secondsRaw);
const remainderPrecision = secondsWhole > 0 ? 2 : 3;
const secondsRemainder = Math.min(secondsRaw - secondsWhole, 0.99);
const seconds =
secondsWhole +
secondsRemainder
.toPrecision(remainderPrecision)
.replace(/^0/, "")
.replace(/0+$/, "")
.replace(/^\.$/, "");
let time = "";
if (minutes > 0) time += minutes + " min" + (minutes > 1 ? "s" : "") + ", ";
time += seconds + " secs";
return time;
};
const smpTag = () => bg(" SMP ") + " ⏱ ";
module.exports.smpTag = smpTag;
module.exports.getHumanOutput = (outputObj, options = {}) => {
const hT = (x) => humanTime(x, options);
let output = "\n\n" + smpTag() + "\n";
if (outputObj.misc) {
output +=
"General output time took " +
fg(hT(outputObj.misc.compileTime, options), outputObj.misc.compileTime);
output += "\n\n";
}
if (outputObj.plugins) {
output += smpTag() + "Plugins\n";
Object.keys(outputObj.plugins)
.sort(
(name1, name2) => outputObj.plugins[name2] - outputObj.plugins[name1]
)
.forEach((pluginName) => {
output +=
chalk.bold(pluginName) +
" took " +
fg(hT(outputObj.plugins[pluginName]), outputObj.plugins[pluginName]);
output += "\n";
});
output += "\n";
}
if (outputObj.loaders) {
output += smpTag() + "Loaders\n";
outputObj.loaders.build
.sort((obj1, obj2) => obj2.activeTime - obj1.activeTime)
.forEach((loaderObj) => {
output +=
loaderObj.loaders.map(fg).join(", and \n") +
" took " +
fg(hT(loaderObj.activeTime), loaderObj.activeTime) +
"\n";
let xEqualsY = [];
if (options.verbose) {
xEqualsY.push(["median", hT(loaderObj.averages.median)]);
xEqualsY.push(["mean", hT(loaderObj.averages.mean)]);
if (typeof loaderObj.averages.variance === "number")
xEqualsY.push(["s.d.", hT(Math.sqrt(loaderObj.averages.variance))]);
xEqualsY.push([
"range",
"(" +
hT(loaderObj.averages.range.start) +
" --> " +
hT(loaderObj.averages.range.end) +
")",
]);
}
if (loaderObj.loaders.length > 1) {
Object.keys(loaderObj.subLoadersTime).forEach((subLoader) => {
xEqualsY.push([subLoader, hT(loaderObj.subLoadersTime[subLoader])]);
});
}
xEqualsY.push(["module count", loaderObj.averages.dataPoints]);
if (options.loaderTopFiles) {
const loopLen = Math.min(
loaderObj.rawStartEnds.length,
options.loaderTopFiles
);
for (let i = 0; i < loopLen; i++) {
const rawItem = loaderObj.rawStartEnds[i];
xEqualsY.push([rawItem.name, hT(rawItem.end - rawItem.start)]);
}
}
const maxXLength = xEqualsY.reduce(
(acc, cur) => Math.max(acc, cur[0].length),
0
);
xEqualsY.forEach((xY) => {
const padEnd = maxXLength - xY[0].length;
output += " " + xY[0] + " ".repeat(padEnd) + " = " + xY[1] + "\n";
});
});
}
output += "\n\n";
return output;
};
module.exports.getMiscOutput = (data) => ({
compileTime: data.compile[0].end - data.compile[0].start,
});
module.exports.getPluginsOutput = (data) =>
Object.keys(data).reduce((acc, key) => {
const inData = data[key];
const startEndsByName = groupBy("name", inData);
return startEndsByName.reduce((innerAcc, startEnds) => {
innerAcc[startEnds[0].name] =
(innerAcc[startEnds[0].name] || 0) + getTotalActiveTime(startEnds);
return innerAcc;
}, acc);
}, {});
module.exports.getLoadersOutput = (data) => {
const startEndsByLoader = groupBy("loaders", data.build);
const allSubLoaders = data["build-specific"] || [];
const buildData = startEndsByLoader.map((startEnds) => {
const averages = getAverages(startEnds);
const activeTime = getTotalActiveTime(startEnds);
const subLoaders = groupBy(
"loader",
allSubLoaders.filter((l) => startEnds.find((x) => x.name === l.name))
);
const subLoadersActiveTime = subLoaders.reduce((acc, loaders) => {
acc[loaders[0].loader] = getTotalActiveTime(loaders);
return acc;
}, {});
return {
averages,
activeTime,
loaders: startEnds[0].loaders,
subLoadersTime: subLoadersActiveTime,
rawStartEnds: startEnds.sort(
(a, b) => b.end - b.start - (a.end - a.start)
),
};
});
return { build: buildData };
};