Skip to content

Commit ad3232a

Browse files
Improve the output format to make it more readable
1 parent 7cbbb8b commit ad3232a

File tree

6 files changed

+101
-63
lines changed

6 files changed

+101
-63
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,23 @@ SpeedMeasurePlugin.wrapPlugins(pluginMap, options);
9393

9494
### `options.outputFormat`
9595

96-
Type: `String`<br>
96+
Type: `String|Function`<br>
9797
Default: `"human"`
9898

9999
Determines in what format this plugin prints its measurements
100100

101101
* `"json"` - produces a JSON blob
102102
* `"human"` - produces a human readable output
103+
* `"humanVerbose"` - produces a more verbose version of the human readable output
104+
* If a function, it will call the function with the JSON blob being the first parameter, and just the response of the function as the output
103105

104106
### `options.outputTarget`
105107

106-
Type: `String`<br>
107-
Default: `undefined`
108+
Type: `String|Function`<br>
109+
Default: `console.log`
108110

109-
Specifies the path to a file to output to. If undefined, then output will print to `console.log`
111+
* If a string, it specifies the path to a file to output to.
112+
* If a function, it will call the function with the output as the first parameter
110113

111114
### `options.disable`
112115

colours.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
const green = "\x1b[32m";
2-
const yellow = "\x1b[33m";
3-
const red = "\x1b[31m";
1+
const greenFg = "\x1b[32m";
2+
const yellowFg = "\x1b[33m";
3+
const redFg = "\x1b[31m";
4+
const blackBg = "\x1b[40m";
45
const bold = "\x1b[1m";
56
const end = "\x1b[0m";
67

7-
module.exports.b = (text, time) => {
8+
module.exports.fg = (text, time) => {
89
let colour;
9-
if (time >= 0) colour = green;
10-
if (time > 2000) colour = yellow;
11-
if (time > 10000) colour = red;
10+
if (time >= 0) colour = greenFg;
11+
if (time > 2000) colour = yellowFg;
12+
if (time > 10000) colour = redFg;
1213

1314
return (colour || "") + bold + text + end;
1415
};
1516

17+
module.exports.bg = text => blackBg + greenFg + bold + text + end;
18+
1619
module.exports.stripColours = text => text.replace(/\x1b\[[0-9]+m/g, "");

index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ module.exports = class SpeedMeasurePlugin {
5555
if (this.timeEventData.loaders)
5656
outputObj.loaders = getLoadersOutput(this.timeEventData.loaders);
5757

58-
return this.options.outputFormat === "json"
59-
? JSON.stringify(outputObj, null, 2)
60-
: getHumanOutput(outputObj);
58+
if (this.options.outputFormat === "json")
59+
return JSON.stringify(outputObj, null, 2);
60+
if (typeof this.options.outputFormat === "function")
61+
return this.options.outputFormat(outputObj);
62+
return getHumanOutput(outputObj, {
63+
verbose: this.options.outputFormat === "humanVerbose",
64+
});
6165
}
6266

6367
addTimeEvent(category, event, eventType, data = {}) {
@@ -99,7 +103,7 @@ module.exports = class SpeedMeasurePlugin {
99103
this.addTimeEvent("misc", "compile", "end");
100104

101105
const output = this.getOutput();
102-
if (this.options.outputTarget) {
106+
if (typeof this.options.outputTarget === "string") {
103107
const strippedOutput = stripColours(output);
104108
const writeMethod = fs.existsSync(this.options.outputTarget)
105109
? fs.appendFile
@@ -109,7 +113,8 @@ module.exports = class SpeedMeasurePlugin {
109113
console.log("Outputted timing info to " + this.options.outputTarget);
110114
});
111115
} else {
112-
console.log(output);
116+
const outputFunc = this.options.outputTarget || console.log;
117+
outputFunc(output);
113118
}
114119

115120
this.timeEventData = {};

output.js

Lines changed: 73 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,94 @@
1-
const { b } = require("./colours");
1+
const MS_IN_MINUTE = 60000;
2+
const MS_IN_SECOND = 1000;
3+
4+
const { fg, bg } = require("./colours");
25
const { groupBy, getAverages, getTotalActiveTime } = require("./utils");
36

4-
const humanTime = ms => {
5-
const hours = ms / 3600000;
6-
const minutes = ms / 60000;
7-
const seconds = ms / 1000;
7+
const humanTime = (ms, options = {}) => {
8+
if (options.verbose) {
9+
return ms.toLocaleString() + " ms";
10+
}
11+
12+
const minutes = Math.floor(ms / MS_IN_MINUTE);
13+
const secondsRaw = (ms - minutes * MS_IN_MINUTE) / MS_IN_SECOND;
14+
const secondsWhole = Math.floor(secondsRaw);
15+
const remainderPrecision = secondsWhole > 0 ? 2 : 3;
16+
const secondsRemainder = Math.min(secondsRaw - secondsWhole, 0.99);
17+
const seconds =
18+
secondsWhole +
19+
secondsRemainder
20+
.toPrecision(remainderPrecision)
21+
.replace(/^0/, "")
22+
.replace(/0+$/, "")
23+
.replace(/^\.$/, "");
24+
25+
let time = "";
26+
27+
if (minutes > 0) time += minutes + " mins, ";
28+
time += seconds + " secs";
829

9-
if (hours > 0.5) return hours.toFixed(2) + " hours";
10-
if (minutes > 0.5) return minutes.toFixed(2) + " minutes";
11-
if (seconds > 0.5) return seconds.toFixed(2) + " seconds";
12-
return ms.toFixed(0) + " milliseconds";
30+
return time;
1331
};
1432

15-
module.exports.getHumanOutput = outputObj => {
16-
const delim = "----------------------------";
17-
let output = delim + "\n";
33+
module.exports.getHumanOutput = (outputObj, options = {}) => {
34+
const hT = x => humanTime(x, options);
35+
const smpTag = bg(" SMP ") + " ⏱ ";
36+
let output = "\n\n" + smpTag + "\n";
1837

1938
if (outputObj.misc) {
2039
output +=
2140
"General output time took " +
22-
b(humanTime(outputObj.misc.compileTime), outputObj.misc.compileTime);
41+
fg(hT(outputObj.misc.compileTime, options), outputObj.misc.compileTime);
2342
output += "\n\n";
2443
}
2544
if (outputObj.plugins) {
26-
Object.keys(outputObj.plugins).forEach(pluginName => {
27-
output +=
28-
b(pluginName) +
29-
" took " +
30-
b(
31-
humanTime(outputObj.plugins[pluginName]),
32-
outputObj.plugins[pluginName]
33-
);
34-
output += "\n";
35-
});
45+
output += smpTag + " Plugins\n";
46+
Object.keys(outputObj.plugins)
47+
.sort(
48+
(name1, name2) => outputObj.plugins[name2] - outputObj.plugins[name1]
49+
)
50+
.forEach(pluginName => {
51+
output +=
52+
fg(pluginName) +
53+
" took " +
54+
fg(hT(outputObj.plugins[pluginName]), outputObj.plugins[pluginName]);
55+
output += "\n";
56+
});
3657
output += "\n";
3758
}
3859
if (outputObj.loaders) {
39-
outputObj.loaders.build.forEach(loaderObj => {
40-
output +=
41-
loaderObj.loaders.map(b).join(", and \n") +
42-
" took " +
43-
b(humanTime(loaderObj.activeTime), loaderObj.activeTime);
44-
output += "\n";
45-
output +=
46-
" median = " + humanTime(loaderObj.averages.median) + ",\n";
47-
output +=
48-
" mean = " + humanTime(loaderObj.averages.mean) + ",\n";
49-
if (typeof loaderObj.averages.variance === "number")
60+
output += smpTag + " Loaders\n";
61+
outputObj.loaders.build
62+
.sort((obj1, obj2) => obj2.activeTime - obj1.activeTime)
63+
.forEach(loaderObj => {
5064
output +=
51-
" s.d = " +
52-
humanTime(Math.sqrt(loaderObj.averages.variance)) +
53-
", \n";
54-
output +=
55-
" range = (" +
56-
humanTime(loaderObj.averages.range.start) +
57-
", " +
58-
humanTime(loaderObj.averages.range.end) +
59-
"), \n";
60-
output += " module count = " + loaderObj.averages.dataPoints + "\n";
61-
});
65+
loaderObj.loaders.map(fg).join(", and \n") +
66+
" took " +
67+
fg(hT(loaderObj.activeTime), loaderObj.activeTime) +
68+
"\n";
69+
70+
if (options.verbose) {
71+
output +=
72+
" median = " + hT(loaderObj.averages.median) + ",\n";
73+
output += " mean = " + hT(loaderObj.averages.mean) + ",\n";
74+
if (typeof loaderObj.averages.variance === "number")
75+
output +=
76+
" s.d = " +
77+
hT(Math.sqrt(loaderObj.averages.variance)) +
78+
", \n";
79+
output +=
80+
" range = (" +
81+
hT(loaderObj.averages.range.start) +
82+
" --> " +
83+
hT(loaderObj.averages.range.end) +
84+
"), \n";
85+
}
86+
87+
output += " module count = " + loaderObj.averages.dataPoints + "\n";
88+
});
6289
}
6390

64-
output += delim + "\n";
91+
output += "\n\n";
6592

6693
return output;
6794
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "speed-measure-webpack-plugin",
3-
"version": "0.2.2",
3+
"version": "0.3.0",
44
"description": "Measure + analyse the speed of your webpack loaders and plugins",
55
"main": "index.js",
66
"repository": {

preview.png

-7.74 KB
Loading

0 commit comments

Comments
 (0)