Skip to content

Commit 30c6600

Browse files
committed
profiling is back
1 parent 5cdb8cb commit 30c6600

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

bin/config-optimist.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ module.exports = function(optimist) {
6161

6262
.boolean("bail").describe("bail")
6363

64+
.boolean("profile").describe("profile")
65+
6466
.boolean("d").describe("d", "shortcut for --debug --devtool sourcemap --output-pathinfo")
6567

6668
.boolean("p").describe("p", "shortcut for --optimize-minimize");

bin/convert-argv.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ module.exports = function(optimist, argv, convertOptions) {
274274

275275
mapArgToBoolean("bail", "bail");
276276

277+
mapArgToBoolean("profile", "profile");
278+
277279
if(!options.output || !options.output.filename) {
278280
ensureObject(options, "output");
279281
if(convertOptions && convertOptions.outputFilename) {

lib/Compilation.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function Compilation(compiler) {
2727
var options = this.options = compiler.options;
2828
this.outputOptions = options && options.output;
2929
this.bail = options && options.bail;
30+
this.profile = options && options.profile;
3031
this.entries = [];
3132
this.chunks = [];
3233
this.namedChunks = {};
@@ -154,6 +155,7 @@ Compilation.prototype.processModuleDependencies = function(module, callback) {
154155
}.bind(this);
155156

156157
var factory = item[0];
158+
if(this.profile) var start = +new Date();
157159
factory.create(module.context, dependencies[0], function(err, dependantModule) {
158160
function isOptional() {
159161
return dependencies.filter(function(d) { return !d.optional }).length == 0;
@@ -166,7 +168,13 @@ Compilation.prototype.processModuleDependencies = function(module, callback) {
166168
}
167169
if(err) return errorOrWarningAndCallback(new ModuleNotFoundError(module, err));
168170
if(!dependantModule) return callback();
171+
if(this.profile) {
172+
if(!dependantModule.profile) dependantModule.profile = {};
173+
var afterFactory = +new Date();
174+
dependantModule.profile.factory = afterFactory - start;
175+
}
169176

177+
dependantModule.issuer = module.identifier();
170178
var newModule = this.addModule(dependantModule);
171179

172180
if(!newModule) {
@@ -177,10 +185,21 @@ Compilation.prototype.processModuleDependencies = function(module, callback) {
177185
dependantModule.addReason(module, dep);
178186
});
179187

188+
if(this.profile) {
189+
if(!module.profile) module.profile = {};
190+
var time = +new Date() - start;
191+
if(!module.profile.dependencies || time > module.profile.dependencies)
192+
module.profile.dependencies = time;
193+
}
194+
180195
return callback();
181196
}
182197

183198
if(newModule instanceof Module) { // from cache
199+
if(this.profile)
200+
newModule.profile = dependantModule.profile;
201+
202+
newModule.issuer = dependantModule.issuer;
184203
dependantModule = newModule;
185204

186205
dependencies.forEach(function(dep) {
@@ -199,6 +218,11 @@ Compilation.prototype.processModuleDependencies = function(module, callback) {
199218
dependantModule.addReason(module, dep);
200219
});
201220

221+
if(this.profile) {
222+
var afterBuilding = +new Date();
223+
dependantModule.profile.building = afterBuilding - afterFactory;
224+
}
225+
202226
this.processModuleDependencies(dependantModule, callback);
203227
}.bind(this));
204228

lib/Stats.js

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ Stats.prototype.toJson = function toJson(options, forToString) {
116116
built: !!module.built,
117117
chunks: module.chunks.map(function(chunk) {
118118
return chunk.id;
119-
})
119+
}),
120+
issuer: module.issuer,
121+
profile: module.profile
120122
};
121123
if(showReasons) {
122124
obj.reasons = module.reasons.filter(function(reason) {
@@ -227,6 +229,22 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
227229
buf.push(str);
228230
if(useColors) buf.push("\033[39m\033[22m");
229231
}
232+
function coloredTime(time) {
233+
var times = [800, 400, 200, 100];
234+
if(obj.time) {
235+
times = [obj.time/2, obj.time/4, obj.time/8, obj.time/16];
236+
}
237+
if(time < times[3])
238+
normal(time + "ms");
239+
else if(time < times[2])
240+
bold(time + "ms");
241+
else if(time < times[1])
242+
green(time + "ms");
243+
else if(time < times[0])
244+
yellow(time + "ms");
245+
else
246+
red(time + "ms");
247+
}
230248
function newline() {
231249
buf.push("\n");
232250
}
@@ -291,6 +309,57 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
291309
});
292310
table(t, [green, normal, bold, green, normal], "rrrll");
293311
}
312+
var modulesByIdentifier = {};
313+
if(obj.modules) {
314+
obj.modules.forEach(function(module) {
315+
modulesByIdentifier["$"+module.identifier] = module;
316+
});
317+
} else {
318+
obj.chunks.forEach(function(chunk) {
319+
chunk.modules.forEach(function(module) {
320+
modulesByIdentifier["$"+module.identifier] = module;
321+
});
322+
});
323+
}
324+
function processProfile(module) {
325+
if(module.profile) {
326+
normal(" ");
327+
var sum = 0, allowSum = true;
328+
var path = [];
329+
var current = module;
330+
while(current.issuer) {
331+
if(!modulesByIdentifier["$"+current.issuer]) {
332+
normal(" ... ->");
333+
allowSum = false;
334+
break;
335+
}
336+
path.unshift(current = modulesByIdentifier["$"+current.issuer]);
337+
}
338+
path.forEach(function(module) {
339+
normal(" [");
340+
normal(module.id);
341+
normal("] ");
342+
if(module.profile) {
343+
var time = (module.profile.factory || 0) + (module.profile.building || 0);
344+
coloredTime(time);
345+
sum += time;
346+
normal(" ");
347+
}
348+
normal("->");
349+
});
350+
Object.keys(module.profile).forEach(function(key) {
351+
normal(" " + key + ":");
352+
var time = module.profile[key];
353+
coloredTime(time);
354+
sum += time;
355+
});
356+
if(allowSum) {
357+
normal(" = ");
358+
coloredTime(sum);
359+
}
360+
newline();
361+
}
362+
}
294363
if(obj.chunks) {
295364
obj.chunks.forEach(function(chunk) {
296365
normal("chunk ");
@@ -360,6 +429,7 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
360429
newline();
361430
});
362431
}
432+
processProfile(module);
363433
});
364434
}
365435
});
@@ -406,6 +476,7 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
406476
newline();
407477
});
408478
}
479+
processProfile(module);
409480
});
410481
}
411482
if(obj.warnings) {

0 commit comments

Comments
 (0)