-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.html
329 lines (290 loc) · 9.72 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Xdebug Trace Visualizer</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="raphael-zpd.js"></script>
<style>
HTML, BODY {
height: 100%;
font-family: sans-serif;
}
#flamegraph {
background-color: #464646;
}
rect {
stroke: none;
}
#highlight {
float:right;
}
h1 {
text-align: center;
margin: 0 auto;
padding: 20px 0;
display: block;
}
#controls {
width: 80%;
margin: 0 auto 20px;
padding: 0 0 20px;
border-bottom: 2px solid #000000;
}
.optional-parameters {
white-space: pre;
}
</style>
</head>
<body>
<h1 id="header">Drag and drop a trace-file here</h1>
<div id="controls">
<label>
<input type="checkbox" id="render-all">
Render <i>all</i> function calls (takes a lot more time)
</label>
</div>
<div id="flamegraph"></div>
<div id="status"></div>
<div id="listview"></div>
<script>
$(function(){
var levelBounds = {low: Number.POSITIVE_INFINITY, high: Number.NEGATIVE_INFINITY},
timeBounds = {low: Number.POSITIVE_INFINITY, high: Number.NEGATIVE_INFINITY},
memoryBounds = {low: Number.POSITIVE_INFINITY, high: Number.NEGATIVE_INFINITY};
var $status = $('#status');
var frozenCall = null,
frozenNode = null;
function Call() {
this.internals = [];
}
$.extend(Call.prototype, {
setStart: function (fields) {
this.level = +fields[0];
levelBounds.low = Math.min(levelBounds.low, this.level);
levelBounds.high = Math.max(levelBounds.high, this.level);
this.functionNumber = +fields[1];
this.timeIndex = {start: +fields[3]};
timeBounds.low = Math.min(timeBounds.low, this.timeIndex.start);
this.memoryUsage = {start: +fields[4]};
memoryBounds.low = Math.min(memoryBounds.low, this.memoryUsage.start);
memoryBounds.high = Math.max(memoryBounds.high, this.memoryUsage.start);
this.functionName = fields[5];
this.userDefined = fields[6] === '1';
this.includeRequireFile = fields[7];
this.fileName = fields[8];
this.lineNumber = +fields[9];
this.optionalParameters = fields.slice(11);
},
addInternal: function (line) {
this.internals.push(line);
},
setEnd: function (fields) {
this.timeIndex.end = +fields[3];
timeBounds.high = Math.max(timeBounds.high, this.timeIndex.end);
this.memoryUsage.end = +fields[4];
memoryBounds.low = Math.min(memoryBounds.low, this.memoryUsage.end);
memoryBounds.high = Math.max(memoryBounds.high, this.memoryUsage.end);
var int = this.internals;
delete this.internals;
this.calls = getCalls(int);
}
});
function Trace(traceStr) {
this.calls = getCalls(traceStr.split(/\r\n|\r|\n/g));
this.stackBounds = levelBounds;
this.timeBounds = timeBounds;
this.memoryBounds = memoryBounds;
}
function getCalls(lines) {
var calls = [],
SEEKING = 'seeking',
MATCHING = 'matching',
mode = SEEKING,
line,
i = 0,
fields,
call,
matchPrefix,
matchPrefixLength;
for (;(line = lines[i++]);) {
if (mode === SEEKING) {
fields = line.split('\t');
if (fields.length >= 10) {
call = new Call();
call.setStart(fields);
mode = MATCHING;
matchPrefix = call.level + '\t' + call.functionNumber + '\t1\t';
matchPrefixLength = matchPrefix.length;
}
} else if (mode == MATCHING) {
if (line.substr(0, matchPrefixLength) === matchPrefix) {
call.setEnd(line.split('\t'));
calls.push(call);
call = undefined;
mode = SEEKING;
} else {
call.addInternal(line);
}
}
}
if (call !== undefined) {
call.setEnd(lines[i-4].split('\t'));
calls.push(call);
}
return calls;
}
function timePercentage(duration) {
return duration / (timeBounds.high - timeBounds.low);
}
function Translator(){}
$.extend(Translator.prototype, {
setInputCoordinates: function (t, r, b, l) {
this.inputCoordinates = {
top: t,
right: r,
bottom: b,
left: l
};
},
setOutputCoordinates: function (t, r, b, l) {
this.outputCoordinates = {
top: t,
right: r,
bottom: b,
left: l
};
},
x: function (inputX) {
return (((inputX - this.inputCoordinates.left) / (this.inputCoordinates.right - this.inputCoordinates.left)) * (this.outputCoordinates.right - this.outputCoordinates.left)) + this.outputCoordinates.left;
},
y: function (inputY) {
return (((inputY - this.inputCoordinates.top) / (this.inputCoordinates.bottom - this.inputCoordinates.top)) * (this.outputCoordinates.bottom - this.outputCoordinates.top)) + this.outputCoordinates.top;
},
width: function (inputWidth) {
return (inputWidth / (this.inputCoordinates.right - this.inputCoordinates.left)) * (this.outputCoordinates.right - this.outputCoordinates.left);
},
height: function (inputHeight) {
return (inputHeight / (this.inputCoordinates.bottom - this.inputCoordinates.top)) * (this.outputCoordinates.bottom - this.outputCoordinates.top);
}
});
function rand_between(min, max) {
return min + Math.ceil( (max-min+1) * Math.random() ) - 1;
}
function clear(call){
return function() {
//never clear a frozen call
if(call === frozenCall) {
return;
}
this.node.style.cssText = '';
//don't clear the text of a frozen call status
if(frozenCall) {
return;
}
$status.text('');
}
}
function statusFn(call){
return function () {
//don't update the status if we currently have a frozen call
if(frozenCall) {
call = frozenCall;
}
//console.log(this);
this.node.style.cssText = 'stroke: #000; stroke-width: 1;';
var optionalParameters = '';
if(call.optionalParameters.length) {
//convert any HTML into text, and display that
var field_html = $('<span class="optional-parameters"></span>').text(call.optionalParameters.join('\n')).get(0).outerHTML;
optionalParameters = '<br>Parameters: ' + field_html;
}
$status.html(
'Function: ' + call.functionName + '<br>' +
'File: ' + call.fileName + '<br>' +
'Line: ' + call.lineNumber + '<br>' +
'Time: ' + (call.timeIndex.end - call.timeIndex.start) + ' seconds (' + (timePercentage(call.timeIndex.end - call.timeIndex.start) * 100) + '%)' +
optionalParameters
);
};
}
//clicking a call should freeze the status until another call is clicked
function statusFreeze(call) {
return function() {
if(call === frozenCall) {
//unfreeze the status because we clicked the frozen call again
frozenCall = null;
} else {
var old_call = frozenCall;
frozenCall = call;
if(frozenNode) {
//clear any previously frozen calls
clear(old_call).call(frozenNode);
}
frozenNode = this;
//make sure we show the correct status
statusFn(call).call(this);
}
}
}
function render(trace) {
var translate = new Translator(),
$container = $('#flamegraph'),
outputWidth = $container.width(),
outputHeight = Math.max($container.height(), 500),
paper = new Raphael($container[0], outputWidth, outputHeight);
var zpd = new RaphaelZPD(paper, { zoom: true, pan: true, drag: false });
translate.setOutputCoordinates(0, outputWidth, outputHeight, 0);
translate.setInputCoordinates(trace.stackBounds.high, trace.timeBounds.high, trace.stackBounds.low - 1, trace.timeBounds.low);
render.calls(paper, trace.calls, translate, $('#render-all').is(':checked'));
}
render.calls = function (paper, calls, translate, render_all) {
var call,
i = 0,
duration_pct,
rect;
for (;(call = calls[i++]);) {
duration_pct = timePercentage(call.timeIndex.end - call.timeIndex.start);
if(!render_all && duration_pct < 0.0008) {
continue;
}
rect = paper.rect(
translate.x(call.timeIndex.start),
translate.y(call.level),
translate.width(call.timeIndex.end - call.timeIndex.start),
translate.height(-1)
).attr({
fill: 'rgb(' + rand_between(90, 100) + '%,' + ((1-duration_pct) * 80) + '%,0%)'
}).hover(statusFn(call), clear(call)).click(statusFreeze(call));
$(rect.node).attr('class', call.fileName);
render.calls(paper, call.calls, translate, render_all);
}
$('#header').html('Drag and drop a trace-file here');
};
function read(files) {
var file = files[0],
reader = new FileReader();
reader.addEventListener('loadend', function () {
var trace = new Trace(reader.result);
render(trace);
});
reader.readAsText(file);
}
$('body')
.on('dragenter dragover', function (e) {
e.preventDefault();
e.stopPropagation();
})
.on('drop', function (e) {
if (e.originalEvent.dataTransfer && e.originalEvent.dataTransfer.files.length) {
$('#header').html('Loading...');
e.preventDefault();
e.stopPropagation();
read(e.originalEvent.dataTransfer.files);
}
});
});
</script>
</body>
</html>