Skip to content

Commit faed323

Browse files
author
Robert Jackson
committed
Use heimdalljs-graph to iterate nodes "smarter".
1 parent dbd2058 commit faed323

File tree

3 files changed

+65
-29
lines changed

3 files changed

+65
-29
lines changed

app/components/slow-node-times.js

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,87 @@
11
import Ember from 'ember';
2+
import heimdallGraph from 'heimdalljs-graph';
23

34
const {
45
computed
56
} = Ember;
67

8+
function selfTime(node) {
9+
for (let [statName, value] of node.statsIterator()) {
10+
if (statName === 'time.self') {
11+
return value;
12+
}
13+
}
14+
}
15+
16+
function nodeTime(node) {
17+
let nodeTotal = selfTime(node);
18+
19+
for (let childNode of node.dfsIterator((n) => n.label.broccoliNode)) {
20+
nodeTotal += selfTime(childNode);
21+
}
22+
23+
return nodeTotal;
24+
}
25+
726
export default Ember.Component.extend({
827
nodes: computed('data', 'filter', 'pluginNameFilter', 'groupByPluginName', function() {
928
let data = this.get('data');
10-
let groupByPluginName = this.get('groupByPluginName');
11-
let pluginName = this.get('pluginNameFilter');
1229
if (!data) { return []; }
1330

14-
let nodes = data.nodes.filter((node) => {
15-
if (!node.label.broccoliNode) { return false; }
16-
if (pluginName && node.label.broccoliPluginName !== pluginName) { return false; }
31+
let nodes = [];
32+
let graph = heimdallGraph.loadFromJSON(data);
1733

18-
return true;
19-
})
20-
21-
if (groupByPluginName) {
22-
let pluginNameMap = nodes.reduce((memo, node) => {
23-
let pluginName = node.label.broccoliPluginName;
24-
memo[pluginName] = memo[pluginName] || { count: 0, time: 0 };
25-
memo[pluginName].time += node.stats.time.self;
26-
memo[pluginName].count++;
27-
return memo;
28-
}, {})
29-
30-
nodes = [];
31-
for (let pluginName in pluginNameMap) {
34+
for (let node of graph.dfsIterator()) {
35+
if (node.label.broccoliNode) {
3236
nodes.push({
33-
label: { name: pluginName, broccoliPluginName: pluginNameMap[pluginName].count },
34-
stats: {
35-
time: { self: pluginNameMap[pluginName].time}
36-
}
37-
});
37+
label: node.label,
38+
time: nodeTime(node)
39+
})
3840
}
3941
}
4042

41-
let addonNodes = nodes
43+
//let groupByPluginName = this.get('groupByPluginName');
44+
//let pluginName = this.get('pluginNameFilter');
45+
// nodes = data.nodes.filter((node) => {
46+
// if (!node.label.broccoliNode) { return false; }
47+
// if (pluginName && node.label.broccoliPluginName !== pluginName) { return false; }
48+
49+
// return true;
50+
// })
51+
52+
// if (groupByPluginName) {
53+
// let pluginNameMap = nodes.reduce((memo, node) => {
54+
// let pluginName = node.label.broccoliPluginName;
55+
// memo[pluginName] = memo[pluginName] || { count: 0, time: 0 };
56+
// memo[pluginName].time += node.stats.time.self;
57+
// memo[pluginName].count++;
58+
// return memo;
59+
// }, {})
60+
61+
// nodes = [];
62+
// for (let pluginName in pluginNameMap) {
63+
// nodes.push({
64+
// label: { name: pluginName, broccoliPluginName: pluginNameMap[pluginName].count },
65+
// stats: {
66+
// time: { self: pluginNameMap[pluginName].time}
67+
// }
68+
// });
69+
// }
70+
// }
71+
72+
let sortedNodes = nodes
4273
.sort((a, b) => {
43-
return b.stats.time.self - a.stats.time.self;
74+
return b.time - a.time;
4475
});
4576

46-
return addonNodes;
77+
return sortedNodes;
4778
}),
4879

4980
totalTime: computed('nodes', function() {
5081
let nodes = this.get('nodes');
5182

5283
return nodes.reduce(function(previousValue, node){
53-
return previousValue + node.stats.time.self;
84+
return previousValue + node.time;
5485
}, 0);
5586
}),
5687

app/templates/components/slow-node-times.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<tr>
1616
<td>{{node.label.name}}</td>
1717
<td>{{node.label.broccoliPluginName}}</td>
18-
<td>{{ns-to-ms node.stats.time.self}}</td>
18+
<td>{{ns-to-ms node.time}}</td>
1919
</tr>
2020
{{/each}}
2121
</tbody>

ember-cli-build.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ var EmberApp = require('ember-cli/lib/broccoli/ember-app');
55
module.exports = function(defaults) {
66
var app = new EmberApp(defaults, {
77
// Add options here
8+
trees: {
9+
vendor: 'node_modules/heimdalljs-graph'
10+
}
811
});
912

1013
// Use `app.import` to add additional libraries to the generated
@@ -20,5 +23,7 @@ module.exports = function(defaults) {
2023
// please specify an object with the list of modules as keys
2124
// along with the exports of each module as its value.
2225

26+
app.import('vendor/dist/amd/heimdalljs-graph.js');
27+
2328
return app.toTree();
2429
};

0 commit comments

Comments
 (0)