Skip to content

Commit e82770e

Browse files
committed
Separate debug version, better tooltips, customizable label function
1 parent 36a4c2a commit e82770e

File tree

6 files changed

+22233
-264
lines changed

6 files changed

+22233
-264
lines changed

app/components/flame-graph.js

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
import Ember from 'ember';
2-
// import require from 'require';
3-
// Import the D3 packages we want to use
4-
// import gFlameGraph from '../../vendor/shims/d3-flame-graphs';
5-
// import gFlameGraph from 'npm:d3-flame-graphs';
6-
// const gFlameGraph = require('d3-flame-graphs/dist/d3-flame-graph');
7-
// import d3 from 'd3';
82
import FlameGraph from '../utils/d3-flame-graphs-v4/d3-flame-graph';
93

104
const { run, get, inject } = Ember;
@@ -13,6 +7,7 @@ export default Ember.Component.extend({
137
classNames: ['flame-graph'],
148
graph: inject.service(),
159
flameGraph: null,
10+
totalTime: Ember.computed.alias('graph.data.summary.totalTime'),
1611

1712
init() {
1813
this._super(...arguments);
@@ -24,12 +19,7 @@ export default Ember.Component.extend({
2419
this._scheduleDraw();
2520
},
2621

27-
// didInsertElement() {
28-
// this._scheduleDraw();
29-
// },
30-
3122
_scheduleDraw() {
32-
// let graphData = get(this, 'graphData');
3323
let graphData = get(this, 'graph.graph');
3424

3525
if (this._lastGraphData !== graphData && graphData) {
@@ -39,6 +29,16 @@ export default Ember.Component.extend({
3929
}
4030
},
4131

32+
formatTime(ms) {
33+
if (ms > 1000000000) {
34+
return (Math.round(ms / 1000000000 * 100) / 100).toFixed(1) + 's';
35+
} else if (ms > 1000000) {
36+
return (Math.round(ms / 1000000 * 100) / 100).toFixed(0) + 'ms';
37+
} else {
38+
return (Math.round(ms / 1000000 * 100) / 100).toFixed(1) + 'ms';
39+
}
40+
},
41+
4242
convert(rawData) {
4343
let child, node, subTree, _i, _len, _ref;
4444

@@ -64,30 +64,36 @@ export default Ember.Component.extend({
6464
treeValue += subTree.treeValue;
6565
}
6666
node.treeValue = treeValue;
67+
node.time = this.formatTime(node.treeValue);
68+
node.percent = ((node.treeValue / this.get('totalTime')) * 100).toFixed(1) + "%";
6769
return node;
6870
},
6971

7072
drawFlame(data) {
73+
let _this = this;
7174
let profile = this.convert(data);
72-
console.log('profile: ', profile);
73-
let indent = 0;
7475

75-
let formatTime = function(ms) {
76-
if (ms > 1000000000) {
77-
return (ms / 1000000000).toFixed(3) + 's';
78-
}
79-
return (ms / 1000000).toFixed(1) + 'ms';
80-
};
76+
let indent = -1;
8177
let objToString = function(obj) {
8278
indent++;
8379
let str = '';
80+
let pad = " ";
8481
for (let p in obj) {
8582
if (obj.hasOwnProperty(p) && p !== 'own') {
8683
if (typeof obj[p] === 'object') {
87-
str += '&nbsp;'.repeat(indent) + p + ': ' + (indent <= 1 && p !== 'time' ? '<br/>' : '') + objToString(obj[p]);
84+
if (p !== 'time') {
85+
let padded = p + pad.repeat(13).substring(0, pad.length * 13 - p.length * 6);
86+
str += '&nbsp;'.repeat(indent) + padded + (indent <= 0 ? '<br/>' : '') + objToString(obj[p]);
87+
}
8888
} else {
89-
str += '&nbsp;'.repeat(indent) + p + ': ' + ((p === 'time' || p === 'self') ? formatTime(obj[p]) : obj[p]);
90-
str += p !== 'count' ? '<br/>' : '';
89+
if (p === 'count') {
90+
let padded = pad.repeat(5).substring(0, pad.length * 5 - obj[p].toString().length * 6) + obj[p];
91+
str += padded;
92+
} else if (p === 'time') {
93+
let time = _this.formatTime(obj[p]);
94+
let padded = ' ' + pad.repeat(8).substring(0, pad.length * 8 - time.length * 6) + time + '<br/>';
95+
str += padded;
96+
}
9197
}
9298
}
9399
}
@@ -96,20 +102,23 @@ export default Ember.Component.extend({
96102
};
97103

98104
let tooltip = function(d) {
99-
return "" + d.data.name + " <br/><br/>" + formatTime(d.data.treeValue) + " (" + (((d.data.treeValue / profile.treeValue) * 100).toFixed(2)) + "% of total)<br/>" +
100-
objToString(d.data.stats);
105+
let time = _this.formatTime(d.data.treeValue);
106+
let percent = " [" + (((d.data.treeValue / _this.get('totalTime')) * 100).toFixed(1)) + "%]";
107+
let self = " (self: " + _this.formatTime(d.data.stats.time.self) + ")";
108+
let res = d.data.name + "<br/>" + time + percent + self + "<br/>" + objToString(d.data.stats);
109+
return res;
101110
};
102111

103112
let clientHeight = document.getElementsByClassName('flame-graph')[0].clientHeight;
113+
clientHeight -= clientHeight % 20;
104114
let clientWidth = document.getElementsByClassName('flame-graph')[0].clientWidth;
105115

106116
this.flameGraph = new FlameGraph('#d3-flame-graph', profile, true)
107-
// .size([clientWidth, clientHeight - 3])
108-
.size([1343, 640])
117+
.size([clientWidth, clientHeight])
109118
.cellHeight(20)
110119
.zoomEnabled(true)
111-
.zoomAction(function(node, event) {
112-
return console.log('ZoomAction: ', node, event);
113-
}).tooltip(tooltip).render();
120+
.zoomAction((node, event) => console.log('Zoom: ', node, event))
121+
.labelFunction(d => d.data.name + ' [' + d.data.time + ' / ' + d.data.percent + ']')
122+
.tooltip(tooltip).render();
114123
}
115124
});

app/styles/app.css

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ body {
55
html,
66
body {
77
height: 100%;
8-
/*height: calc(100% - 3.7rem);*/
98
}
109

1110
body > [class="ember-view"] {
12-
/*height: calc(100% - 3.7rem);*/
1311
height: 100%;
1412
}
1513

1614
.flame-graph {
17-
/*height: calc(100% - 3.7rem);*/
1815
height: 100%;
1916
}
2017

18+
.d3-tip {
19+
font-family: "Lucida Console", Monaco, monospace;
20+
font-size: 13px;
21+
}
22+
2123
.global-nav {
2224
position: fixed;
2325
top: 0;

0 commit comments

Comments
 (0)