-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.inc.js
309 lines (240 loc) · 7.52 KB
/
converter.inc.js
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
if (typeof window === 'undefined')
{
var StringDecoder = require('string_decoder').StringDecoder;
var TextDecoder = function () {
return {
decode: function (data) {
return new StringDecoder('utf8').write(new Buffer(data));
}
};
};
var JSZip = require('./jszip.min.js');
}
(function(exports){
exports.convert = function(buffer, finish_callback, progress_callback, depth_limit) {
// Keeping the large data in one place, so it's easier to make sure nothing is leaked
var data;
var stack;
var fl_names;
var fn_names;
var process_file = function()
{
var LF = 10;
// If the chunk size is too small, performance suffers. Having one chunk per line (to eliminate the .split())
// yields unbearable performance. It seems that the ArrayBuffer operations are not as optimized as the String
// operations yet.
var chunk_size = 1000000;
var skip_summary;
var chunk;
var chunk_string;
var cleaned_chunk_string;
var lines;
var lf_pos;
var chunk_no;
var offset;
var deleted;
var current_block;
var reading;
var skip_first_block;
offset = 0;
chunk_no = 0;
deleted = 0;
reading = false;
skip_summary = 0;
current_block = [];
stack = [];
fl_names = {};
fn_names = {};
skip_first_block = true;
(function cont() {
chunk_no++;
// Search for first line break of next chunk
lf_pos = data.indexOf(LF, chunk_no * chunk_size - deleted) + deleted;
if (lf_pos == -1 + deleted)
lf_pos = data.length + deleted;
chunk = data.slice(offset - deleted, lf_pos - deleted);
offset = lf_pos + 1;
//noinspection JSUnresolvedFunction
chunk_string = new TextDecoder('utf-8').decode(chunk);
cleaned_chunk_string = chunk_string.split('\r').join(''); // http://jsperf.com/replace-all-vs-split-join
lines = cleaned_chunk_string.split('\n');
lines.forEach(function (line) {
if (reading)
{
if (line != '' && !skip_summary)
{
current_block.push(line);
if (line.substr(-6) == '{main}')
skip_summary = 3;
}
else
{
if (skip_summary)
skip_summary--;
else
{
if (!skip_first_block)
process_block(current_block);
skip_first_block = false;
current_block = [];
}
}
}
else
{
if (line.substr(0, 7) == 'events:')
reading = true;
}
});
if (chunk_no % 100 == 0 && data.length > offset - deleted)
{
data = data.slice(offset - deleted);
deleted += offset - deleted;
}
progress_callback(Math.round(100 / (data.length + deleted) * offset));
if (lf_pos < (data.length + deleted))
setTimeout(cont, 0);
else
{
chunk = undefined;
chunk_string = undefined;
cleaned_chunk_string = undefined;
lines = undefined;
data = undefined;
process_stack();
}
})();
};
var process_block = function(v)
{
var entry;
var child;
var i;
var get_name = function(s, prefix_length, mapping) {
s = s.substr(prefix_length);
var closing_parens_pos = s.indexOf(')');
if (s.substr(0, 1) == '(' && closing_parens_pos != -1)
{
var long_name = s.substr(closing_parens_pos + 2);
if (long_name != '')
mapping[s.substr(1, closing_parens_pos - 1)] = long_name
return s.substr(0, closing_parens_pos + 1)
}
else
return s
};
if (v.length)
{
entry = {};
entry.fl = get_name(v[0], 3, fl_names);
entry.fn = get_name(v[1], 3, fn_names);
entry.self_us = v[2].split(' ')[1] / 1; // this used to be "/ 10" but apparently that is not necessary anymore
if (v.length > 3) // this block describes a return
{
entry.children = [];
// In certain Xdebug versions the lines starting with "cfl=" are missing, in this case the sub-blocks are 3
// lines long and start with the "cfn=" line
if (v[3].substr(0, 4) == 'cfn=')
{
for (i = (v.length-3) / 3 ; i > 0 ; i--)
{
child = stack.pop();
if (child.fn != get_name(v[3 + (i-1)*3], 4, fn_names))
console.log('Mismatch!');
child.cum_us = v[5 + (i-1)*3].split(' ')[1] / 1; // this used to be "/ 10" but apparently that is not necessary anymore
child.called_from_file = entry.fl;
child.called_from_line = parseInt(v[5 + (i-1)*3].split(' ')[0]);
entry.children.push(child);
}
}
else
{
for (i = (v.length-3) / 4 ; i > 0 ; i--)
{
child = stack.pop();
if (child.fl != get_name(v[3 + (i-1)*4], 4, fl_names) || child.fn != get_name(v[4 + (i-1)*4], 4, fn_names))
console.log('Mismatch!');
child.cum_us = v[6 + (i-1)*4].split(' ')[1] / 1; // this used to be "/ 10" but apparently that is not necessary anymore
child.called_from_file = entry.fl;
child.called_from_line = parseInt(v[6 + (i-1)*4].split(' ')[0]);
entry.children.push(child);
}
}
entry.children.reverse();
}
stack.push(entry);
}
};
var process_stack = function() {
var output;
var root_us;
var strange_file;
var unmap_name = function(short_name, mapping)
{
if (short_name != null && short_name.substr(0, 1) == '(' && short_name.substr(-1) == ')' && mapping[short_name.substr(1, short_name.length-2)] != null)
return mapping[short_name.substr(1, short_name.length-2)];
else
return short_name;
};
var write = function (node)
{
var output;
output = {
name: unmap_name(node.fn, fn_names),
value: node.cum_us,
tooltip: unmap_name(node.fn, fn_names) + '<br>' + (Math.round(node.cum_us) / 1000) + ' ms<br>Called from: ' + unmap_name(node.called_from_file, fl_names) + ':' + node.called_from_line + '<br>' + 'Defined in: ' + unmap_name(node.fl, fl_names)
};
if (node.children)
{
output.children = [];
node.children.forEach(function (child)
{
output.children.push(write(child));
});
}
return output;
};
root_us = 0;
// With certain Xdebug versions I have encountered files that were missing the {main} entry. If we ignore that
// fact the file is still somewhat parsable:
strange_file = true;
stack.forEach(function (v) {
if (unmap_name(v.fn, fn_names) == '{main}')
strange_file = false;
});
if (strange_file)
alert('Your file is missing the {main} entry. I\'ll try to parse it anyway, but treat results with suspicion.');
// The file provides no cumulated time for the top level, so we calculate them ourselves:
stack.forEach(function (v) {
var cum_us = 0;
if (v.children) v.children.forEach(function (v) {
cum_us += v.cum_us;
});
v.cum_us = cum_us + v.self_us;
root_us += v.cum_us;
});
output = {name: '', value: root_us, tooltip: (Math.round(root_us) / 1000) + ' ms<br>All scripts and shutdown functions', children: []};
stack.forEach(function (tle) {
output.children.push(write(tle));
});
stack = undefined;
// We don't need to show the root element if there is only one script
if (output.children.length == 1)
output = output.children[0];
finish_callback(output);
};
var filename;
data = new Uint8Array(buffer);
//noinspection JSUnresolvedFunction
if (new TextDecoder('utf8').decode(data.slice(0,2)) == 'PK')
{
//noinspection JSUnresolvedFunction
var zip = new JSZip();
zip.load(data);
//noinspection LoopStatementThatDoesntLoopJS
for (filename in zip.files) break;
data = zip.file(filename).asUint8Array();
}
process_file();
};
})(typeof exports === 'undefined'? this['converter']={}: exports);