Created
May 8, 2014 00:23
-
-
Save rwjblue/78a47d00b83efed4b002 to your computer and use it in GitHub Desktop.
Detailed graphs of broccoli build times...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var broccoli = require('broccoli'); | |
var tree = broccoli.loadBrocfile(); | |
var builder = new broccoli.Builder(tree); | |
var result; | |
builder.build() | |
.then(function(hash) { | |
console.log('\n\n10%') | |
printSlowTrees(hash.graph, 0.1); | |
console.log('\n\n5%') | |
printSlowTrees(hash.graph); | |
console.log('\n\n1%') | |
printSlowTrees(hash.graph, 0.01); | |
console.log('\n\n0.1%') | |
printSlowTrees(hash.graph, 0.001); | |
}) | |
function printSlowTrees(graph, factor) { | |
var sortedTrees = sortResults(graph); | |
var minimumTime = graph.totalTime * (factor || 0.05); | |
var logLines = []; | |
var length = sortedTrees.length; | |
for (var i = 0; i < sortedTrees.length; i++) { | |
var node = sortedTrees[i]; | |
var name = node.tree.description || node.tree.constructor.name; | |
if (node.selfTime > minimumTime) { | |
logLines.push(pad(name, 30) + ' | ' + pad(Math.floor(node.selfTime / 1e6) + 'ms', 15)); | |
} | |
} | |
if (logLines.length > 0) { | |
logLines.unshift(pad('', 30, '-') + '-+-' + pad('', 15, '-')); | |
logLines.unshift(pad('Slowest Trees', 30) + ' | Total Time '); | |
} | |
console.log(logLines.join('\n')) | |
} | |
function sortResults(graph) { | |
var flattenedTrees = []; | |
function process(node) { | |
if (flattenedTrees.indexOf(node) > -1) { return; } // for de-duping | |
flattenedTrees.push(node); | |
var length = node.subtrees.length; | |
for (var i = 0; i < length; i++) { | |
process(node.subtrees[i]); | |
} | |
} | |
process(graph); // kick off with the top item | |
return flattenedTrees.sort(function(a, b) { | |
return b.selfTime - a.selfTime; | |
}); | |
} | |
function pad(str, len, pad, dir) { | |
if (!pad) { pad = ' '} | |
if (len + 1 >= str.length) | |
switch (dir){ | |
case 'left': | |
str = Array(len + 1 - str.length).join(pad) + str; | |
break; | |
case 'both': | |
var right = Math.ceil((padlen = len - str.length) / 2); | |
var left = padlen - right; | |
str = Array(left + 1).join(pad) + str + Array(right + 1).join(pad); | |
break; | |
default: | |
str = str + Array(len + 1 - str.length).join(pad); | |
}; | |
return str; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment