forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile-conversion.test.js
416 lines (382 loc) · 12.9 KB
/
profile-conversion.test.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import { unserializeProfileOfArbitraryFormat } from '../../profile-logic/process-profile';
import { isPerfScriptFormat } from '../../profile-logic/import/linux-perf';
import { GECKO_PROFILE_VERSION } from '../../app-logic/constants';
import { storeWithProfile } from '../fixtures/stores';
import { selectedThreadSelectors } from 'firefox-profiler/selectors';
import type {
TracingEventUnion,
CpuProfileEvent,
} from '../../profile-logic/import/chrome';
import type { Profile } from 'firefox-profiler/types';
function checkProfileContainsUniqueTid(profile: Profile) {
const foundTids = new Set();
for (const thread of profile.threads) {
const { tid } = thread;
if (tid === undefined) {
throw new Error('Found an undefined tid!');
}
if (foundTids.has(tid)) {
console.error(`Found a duplicate tid ${tid}!`);
}
foundTids.add(tid);
}
}
describe('converting Linux perf profile', function () {
async function loadProfile(filename: string): Promise<Profile> {
const fs = require('fs');
const zlib = require('zlib');
const buffer = fs.readFileSync(filename);
const decompressedArrayBuffer = zlib.gunzipSync(buffer);
const text = decompressedArrayBuffer.toString('utf8');
const profile = await unserializeProfileOfArbitraryFormat(text);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
return profile;
}
it('should not detect a JSON profile as perf script', function () {
expect(
isPerfScriptFormat('{"meta": {"product": "Cool stuff 123 456:"}}')
).toBe(false);
});
it('should import a perf profile', async function () {
const profile = await loadProfile(
'src/test/fixtures/upgrades/test.perf.gz'
);
expect(profile.meta.version).toEqual(GECKO_PROFILE_VERSION);
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('should import a simple perf profile', async function () {
const profile = await loadProfile(
'src/test/fixtures/upgrades/simple-perf.txt.gz'
);
expect(profile.meta.version).toEqual(GECKO_PROFILE_VERSION);
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('should import a perf profile of gzip', async function () {
const profile = await loadProfile(
'src/test/fixtures/upgrades/gzip.perf.gz'
);
expect(profile.meta.version).toEqual(GECKO_PROFILE_VERSION);
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('should import a perf profile of graphviz with a header', async function () {
const profile = await loadProfile(
'src/test/fixtures/upgrades/graphviz.perf.gz'
);
expect(profile.meta.version).toEqual(GECKO_PROFILE_VERSION);
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
});
describe('converting dhat profiles', function () {
it('should import a dhat profile', async function () {
const fs = require('fs');
const zlib = require('zlib');
const buffer = fs.readFileSync('src/test/fixtures/upgrades/dhat.json.gz');
const decompressedArrayBuffer = zlib.gunzipSync(buffer);
const text = decompressedArrayBuffer.toString('utf8');
const profile = await unserializeProfileOfArbitraryFormat(text);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
});
describe('converting Google Chrome profile', function () {
it('successfully imports a chunked profile (one that uses Profile + ProfileChunk trace events)', async function () {
// Mock out image loading behavior as the screenshots rely on the Image loading
// behavior.
jest
.spyOn(Image.prototype, 'addEventListener')
.mockImplementation((name: string, callback) => {
if (name === 'load') {
callback();
}
});
const fs = require('fs');
const zlib = require('zlib');
const buffer = fs.readFileSync('src/test/fixtures/upgrades/test.chrome.gz');
const decompressedArrayBuffer = zlib.gunzipSync(buffer);
const text = decompressedArrayBuffer.toString('utf8');
const profile = await unserializeProfileOfArbitraryFormat(text);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('successfully imports a chrome profile with an invalid "endTime" entry', async () => {
// Mock out image loading behavior as the screenshots rely on the Image loading
// behavior.
jest
.spyOn(Image.prototype, 'addEventListener')
.mockImplementation((name: string, callback) => {
if (name === 'load') {
callback();
}
});
const fs = require('fs');
const zlib = require('zlib');
const compressedBuffer = fs.readFileSync(
'src/test/fixtures/upgrades/test.chrome.gz'
);
const buffer = zlib.gunzipSync(compressedBuffer);
const events: TracingEventUnion[] = JSON.parse(buffer.toString('utf8'));
events.push({
args: { data: { endTime: 300269884209 } },
cat: 'disabled-by-default-v8.cpu_profiler',
id: '0x2', // same id than in the original profile
name: 'ProfileChunk',
ph: 'P',
pid: 88999, // same pid than in the original profile
tid: 1,
ts: 300269884230,
tts: 24162,
});
const text = JSON.stringify(events);
const profile = await unserializeProfileOfArbitraryFormat(text);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('successfully imports a non-chunked profile (one that uses a CpuProfile trace event)', async function () {
const fs = require('fs');
const buffer = fs.readFileSync(
'src/test/fixtures/upgrades/test.chrome-unchunked.json'
);
const text = buffer.toString('utf8');
const profile = await unserializeProfileOfArbitraryFormat(text);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('successfully imports a single CpuProfile, e.g. from node', async function () {
const fs = require('fs');
let cpuProfile: CpuProfileEvent | void;
{
// Load the existing saved profile, but then find a single CpuProfile entry in it.
const buffer = fs.readFileSync(
'src/test/fixtures/upgrades/test.chrome-unchunked.json'
);
const events: TracingEventUnion[] = JSON.parse(buffer.toString('utf8'));
// Use a for loop to look this up, as Flow is happier than a .find().
for (const event of events) {
if (event.name === 'CpuProfile') {
cpuProfile = event;
break;
}
}
if (!cpuProfile) {
throw new Error('Could not find a CPU profile');
}
}
// Now use the single CpuProfile to test that we can convert a node-like export
// format.
const text = JSON.stringify(cpuProfile.args.data.cpuProfile);
const profile = await unserializeProfileOfArbitraryFormat(text);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('successfully imports a profile using the chrome tracing format', async function () {
const fs = require('fs');
const zlib = require('zlib');
const compressedBuffer = fs.readFileSync(
'src/test/fixtures/upgrades/chrome-tracing.json.gz'
);
const decompressedBuffer = zlib.gunzipSync(compressedBuffer);
const profile = await unserializeProfileOfArbitraryFormat(
decompressedBuffer.buffer
);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('successfully imports a chrome profile using markers of different types', async function () {
const chromeProfile = {
traceEvents: [
{
cat: 'RunTask',
name: 'RunTask',
ph: 'B',
pid: 54782,
scope: 'blink.user_timing',
tid: 259,
ts: 1,
},
{
cat: 'RunTask',
name: 'RunTask',
ph: 'E',
pid: 54782,
scope: 'blink.user_timing',
tid: 259,
ts: 2,
},
{
args: {},
cat: 'disabled-by-default-devtools.timeline',
dur: 2,
name: 'RunTask Complete',
ph: 'X',
pid: 54782,
tdur: 1,
tid: 259,
ts: 3,
tts: 2522144,
},
{
cat: 'Instant',
name: 'Instant 1',
ph: 'i',
pid: 54782,
scope: 'blink.user_timing',
tid: 259,
ts: 7,
},
{
cat: 'Instant',
name: 'Instant 2',
ph: 'I',
pid: 54782,
scope: 'blink.user_timing',
tid: 259,
ts: 8,
},
{
args: { startTime: 3907.5999999940395 },
cat: 'blink.user_timing',
id: '0x2981878b',
name: 'async event',
ph: 'b',
pid: 54782,
scope: 'blink.user_timing',
tid: 259,
ts: 10,
},
{
args: {},
cat: 'blink.user_timing',
id: '0x2981878b',
name: 'async event',
ph: 'e',
pid: 54782,
scope: 'blink.user_timing',
tid: 259,
ts: 11,
},
{
args: {},
cat: 'blink.user_timing',
id: '0x2981878b',
name: 'async event instant',
ph: 'n',
pid: 54782,
scope: 'blink.user_timing',
tid: 259,
ts: 12,
},
{
args: {
data: {
stackTrace: [
{
columnNumber: 38358,
functionName: 'buildFragment',
lineNumber: 40,
scriptId: '117',
url: 'https://journals.physiology.org/products/physio/releasedAssets/js/main.bundle-1bbca34150268d61be9d.js',
},
],
type: 'DOMSubtreeModified',
},
},
cat: 'devtools.timeline',
dur: 51,
name: 'EventDispatch',
ph: 'X',
pid: 54782,
tdur: 4,
tid: 259,
ts: 13,
tts: 3934607,
},
],
};
const strChromeProfile = JSON.stringify(chromeProfile);
const profile = await unserializeProfileOfArbitraryFormat(strChromeProfile);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
const state = storeWithProfile(profile).getState();
const mainGetMarker = selectedThreadSelectors.getMarkerGetter(state);
const markers = selectedThreadSelectors
.getFullMarkerListIndexes(state)
.map(mainGetMarker);
checkProfileContainsUniqueTid(profile);
expect(markers.map(({ name }) => name)).toEqual([
'RunTask',
'RunTask Complete',
'Instant 1',
'Instant 2',
'async event',
'async event instant',
'EventDispatch',
]);
expect(markers[6]).toMatchObject({
name: 'EventDispatch',
data: {
type: 'EventDispatch',
type2: 'DOMSubtreeModified',
},
});
expect(markers).toMatchSnapshot();
});
});
describe('converting ART trace', function () {
it('successfully imports a non-streaming ART trace', async function () {
const fs = require('fs');
const zlib = require('zlib');
const buffer = fs.readFileSync(
'src/test/fixtures/upgrades/art-trace-regular.trace.gz'
);
const arrayBuffer = zlib.gunzipSync(buffer).buffer;
const profile = await unserializeProfileOfArbitraryFormat(arrayBuffer);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('successfully imports a streaming ART trace', async function () {
const fs = require('fs');
const zlib = require('zlib');
const buffer = fs.readFileSync(
'src/test/fixtures/upgrades/art-trace-streaming.trace.gz'
);
const arrayBuffer = zlib.gunzipSync(buffer).buffer;
const profile = await unserializeProfileOfArbitraryFormat(arrayBuffer);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
});