forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarker-schema.test.js
458 lines (432 loc) · 14.1 KB
/
marker-schema.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* 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 {
formatFromMarkerSchema,
formatMarkupFromMarkerSchema,
parseLabel,
markerSchemaFrontEndOnly,
} from '../../profile-logic/marker-schema';
import type { MarkerSchema, Marker } from 'firefox-profiler/types';
import { getDefaultCategories } from '../../profile-logic/data-structures';
import { storeWithProfile } from '../fixtures/stores';
import { getMarkerSchema } from '../../selectors/profile';
import { getProfileFromTextSamples } from '../fixtures/profiles/processed-profile';
import { markerSchemaForTests } from '../fixtures/profiles/marker-schema';
import { UniqueStringArray } from '../../utils/unique-string-array';
/**
* Generally, higher level type of testing is preferred to detailed unit tests of
* implementation behavior, but the marker schema labels use a custom mini-parser, and
* it would be easy for them to have errors. These tests cover a variety of different
* code branches, especially parse errors.
*/
describe('marker schema labels', function () {
type LabelOptions = {|
schemaData: $PropertyType<MarkerSchema, 'data'>,
label: string,
payload: any,
|};
beforeEach(() => {
jest.spyOn(console, 'error').mockImplementation(() => {});
});
function applyLabel(options: LabelOptions): string {
const { schemaData, label, payload } = options;
const categories = getDefaultCategories();
const stringTable = new UniqueStringArray([
'IPC Message',
'MouseDown Event',
]);
const schema = {
name: 'TestDefinedMarker',
display: [],
data: schemaData,
};
const marker: Marker = {
start: 2,
end: 5,
name: 'TestDefinedMarker',
category: 0,
threadId: 1,
data: payload,
};
const getter = parseLabel(schema, categories, stringTable, label);
// There is only one marker, marker 0
return getter(marker);
}
it('can parse very simple labels', function () {
expect(
applyLabel({
label: 'Just text',
schemaData: [],
payload: {},
})
).toEqual('Just text');
expect(console.error).toHaveBeenCalledTimes(0);
});
it('can parse a label with just a lookup value', function () {
expect(
applyLabel({
label: '{marker.data.duration}',
schemaData: [{ key: 'duration', label: 'Duration', format: 'seconds' }],
payload: { duration: 12345 },
})
).toEqual('12.345s');
expect(console.error).toHaveBeenCalledTimes(0);
});
it('can parse a label with surrounding text', function () {
expect(
applyLabel({
label: 'It took {marker.data.duration} for this test.',
schemaData: [{ key: 'duration', label: 'Duration', format: 'seconds' }],
payload: { duration: 12345 },
})
).toEqual('It took 12.345s for this test.');
expect(console.error).toHaveBeenCalledTimes(0);
});
it('can mix and match lookups', function () {
expect(
applyLabel({
label: 'It took {marker.data.duration}, which is {marker.data.ratio}',
schemaData: [
{ key: 'duration', label: 'Duration', format: 'seconds' },
{ key: 'ratio', label: 'Ratio', format: 'percentage' },
],
payload: {
duration: 12345,
ratio: 0.12345,
},
})
).toEqual('It took 12.345s, which is 12%');
expect(console.error).toHaveBeenCalledTimes(0);
});
it('is empty if there is no information in a payload', function () {
expect(
applyLabel({
label: 'This will be nothing: "{marker.data.nokey}"',
schemaData: [{ key: 'duration', label: 'Duration', format: 'seconds' }],
payload: { duration: 12345 },
})
).toEqual('This will be nothing: ""');
expect(console.error).toHaveBeenCalledTimes(0);
});
it('can look up various parts of the marker', function () {
const text = applyLabel({
label: [
'Start: {marker.start}',
'End: {marker.end}',
'Duration: {marker.duration}',
'Name: {marker.name}',
'Category: {marker.category}',
].join('\n'),
schemaData: [],
payload: {},
});
expect(text.split('\n')).toEqual([
'Start: 2ms',
'End: 5ms',
'Duration: 3ms',
'Name: TestDefinedMarker',
'Category: Other',
]);
expect(console.error).toHaveBeenCalledTimes(0);
});
it('can parse labels with unique strings', function () {
expect(
applyLabel({
label: '{marker.data.message} happened because of {marker.data.event}',
schemaData: [
{ key: 'message', label: 'Message', format: 'unique-string' },
{ key: 'event', label: 'Event', format: 'unique-string' },
],
payload: {
message: 0,
event: 1,
},
})
).toEqual('IPC Message happened because of MouseDown Event');
expect(console.error).toHaveBeenCalledTimes(0);
});
describe('parseErrors', function () {
function testParseError(label: string) {
expect(
applyLabel({
label,
schemaData: [
{ key: 'duration', label: 'Duration', format: 'seconds' },
],
payload: { duration: 12345 },
})
).toEqual('Parse error: ""');
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error.mock.calls).toMatchSnapshot();
}
// eslint-disable-next-line jest/expect-expect
it('errors if not looking up into a marker', function () {
testParseError('Parse error: "{duration}"');
});
// eslint-disable-next-line jest/expect-expect
it('errors if looking up into a part of the marker that does not exist', function () {
testParseError('Parse error: "{marker.nothing}"');
});
// eslint-disable-next-line jest/expect-expect
it('errors when accessing random properties', function () {
testParseError('Parse error: "{property.value}"');
});
// eslint-disable-next-line jest/expect-expect
it('errors when accessing twice into a payload', function () {
testParseError('Parse error: "{marker.data.duration.extra}"');
});
});
});
describe('marker schema formatting', function () {
beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation(() => {});
});
it('can apply a variety of formats', function () {
const entries = [
['url', 'http://example.com'],
['file-path', '/Users/me/gecko'],
['file-path', null],
['file-path', undefined],
['duration', 0],
['duration', 10],
['duration', 12.3456789],
['duration', 123456.789],
['duration', 23456789],
['duration', 50000],
['duration', 2000000],
['duration', 50000000],
['duration', 123456789],
['duration', 0.000123456],
['duration', 1000 * 60 - 501], // slightly less than 1min, should not be shown as '60s'
['duration', 1000 * 60 - 500], // slightly less than 1min, should not be shown as '60s'
['duration', 1000 * 60 + 1], // slightly more than 1min, should not be shown as '1min0s'
['duration', 779873.639], // '13min60s' should be rounded to '14min'
['duration', 1000 * 3600 - 501], // 1h - 501ms slightly less than 1h, should be shown as '59min59s'
['duration', 1000 * 3600 - 500], // 1h - 500s slightly less than 1h, should be shown as '1h', not '0h60min'
['duration', 1000 * 3600 + 1], // slightly more than 1h, should not be shown as '1h0min'
['duration', 1000 * 3600 * 24 - 31 * 1000], // 1d - 31s, should be shown as '23h59min'
['duration', 1000 * 3600 * 24 - 30 * 1000], // 1d - 30s, should be shown as '1d', not '0d24h'
['duration', 1000 * 3600 * 24 + 1], // slightly more than 1 day, should not be shown as '1d0h'
['time', 12.3456789],
['seconds', 0],
['seconds', 10],
['seconds', 12.3456789],
['seconds', 123456.789],
['seconds', 0.000123456],
['milliseconds', 0],
['milliseconds', 10],
['milliseconds', 12.3456789],
['milliseconds', 123456.789],
['milliseconds', 0.000123456],
['microseconds', 0],
['microseconds', 10],
['microseconds', 12.3456789],
['microseconds', 123456.789],
['microseconds', 0.000123456],
['nanoseconds', 0],
['nanoseconds', 10],
['nanoseconds', 12.3456789],
['nanoseconds', 123456.789],
['nanoseconds', 0.000123456],
['bytes', 0],
['bytes', 10],
['bytes', 12.3456789],
['bytes', 123456.789],
['bytes', 0.000123456],
['integer', 0],
['integer', 10],
['integer', 12.3456789],
['integer', 123456.789],
['integer', 0.000123456],
['decimal', 0],
['decimal', 10],
['decimal', 12.3456789],
['decimal', 123456.789],
['decimal', 0.000123456],
['percentage', 0],
['percentage', 0.1],
['percentage', 0.123456789],
['percentage', 1234.56789],
['percentage', 0.000123456],
['pid', '2322'],
['tid', '2427'],
['unique-string', 0], // Should be "IPC Message", see "stringTable" in "applyLabel" fn
['unique-string', 1], // Should be "MouseDown Event", see "stringTable" in "applyLabel" fn
['unique-string', null], // Should be "(empty)"
['unique-string', undefined], // Should be "(empty)"
['unique-string', 42], // Should be "(empty)"
];
expect(
entries.map(
([format, value]) =>
format +
' - ' +
formatFromMarkerSchema(
'none',
format,
value,
new UniqueStringArray(['IPC Message', 'MouseDown Event'])
)
)
).toMatchInlineSnapshot(`
Array [
"url - http://example.com",
"file-path - /Users/me/gecko",
"file-path - (empty)",
"file-path - (empty)",
"duration - 0s",
"duration - 10ms",
"duration - 12.346ms",
"duration - 2m3s",
"duration - 6h31m",
"duration - 50s",
"duration - 33m20s",
"duration - 13h53m",
"duration - 1d10h",
"duration - 123.46ns",
"duration - 59.499s",
"duration - 1m",
"duration - 1m",
"duration - 13m",
"duration - 59m59s",
"duration - 1h",
"duration - 1h",
"duration - 23h59m",
"duration - 1d",
"duration - 1d",
"time - 12.346ms",
"seconds - 0.000s",
"seconds - 0.010s",
"seconds - 0.012s",
"seconds - 123.46s",
"seconds - 0.000s",
"milliseconds - 0.000ms",
"milliseconds - 10ms",
"milliseconds - 12ms",
"milliseconds - 123,457ms",
"milliseconds - 0.000ms",
"microseconds - 0.000μs",
"microseconds - 10μs",
"microseconds - 12μs",
"microseconds - 123,457μs",
"microseconds - 0.000μs",
"nanoseconds - 0.0000ns",
"nanoseconds - 10.0ns",
"nanoseconds - 12.3ns",
"nanoseconds - 123,457ns",
"nanoseconds - 0.0001ns",
"bytes - 0.000B",
"bytes - 10B",
"bytes - 12B",
"bytes - 121KB",
"bytes - 0.000B",
"integer - 0",
"integer - 10",
"integer - 12",
"integer - 123,457",
"integer - 0",
"decimal - 0.000",
"decimal - 10",
"decimal - 12",
"decimal - 123,457",
"decimal - 0.000",
"percentage - 0.0%",
"percentage - 10%",
"percentage - 12%",
"percentage - 123,457%",
"percentage - 0.0%",
"pid - PID: 2322",
"tid - TID: 2427",
"unique-string - IPC Message",
"unique-string - MouseDown Event",
"unique-string - (empty)",
"unique-string - (empty)",
"unique-string - (empty)",
]
`);
});
it('supports complex formats', function () {
const entries = [
['url', 'http://example.com'],
['file-path', '/Users/me/gecko'],
['file-path', null],
['file-path', undefined],
['duration', 0],
['duration', 10],
['duration', 12.3456789],
[
{ type: 'table', columns: [{ type: 'string' }, { type: 'integer' }] },
[
['a', 1],
['b', 2],
],
],
[
{
type: 'table',
columns: [
{ type: 'string', label: 'a' },
{ type: 'integer', label: 'b' },
],
},
[['b', 2]],
],
[
{
type: 'table',
columns: [{ type: 'string', label: 'a' }, { type: 'integer' }],
},
[['b', 2]],
],
[
{ type: 'table', columns: [{ type: 'string', label: 'a' }, {}] },
[['b', 2]],
],
['list', []],
['list', ['a', 'b']],
];
expect(
entries.map(([format, value]) => [
format,
value,
formatMarkupFromMarkerSchema(
'none',
format,
value,
new UniqueStringArray()
),
formatFromMarkerSchema('none', format, value, new UniqueStringArray()),
])
).toMatchSnapshot();
});
});
describe('getMarkerSchema', function () {
it('combines front-end and Gecko marker schema', function () {
const { profile } = getProfileFromTextSamples('A');
profile.meta.markerSchema = markerSchemaForTests;
const { getState } = storeWithProfile(profile);
const combinedSchema = getMarkerSchema(getState());
// Find front-end only marker schema.
expect(
profile.meta.markerSchema.find((schema) => schema.name === 'Jank')
).toBeUndefined();
expect(
markerSchemaFrontEndOnly.find((schema) => schema.name === 'Jank')
).toBeTruthy();
expect(
combinedSchema.find((schema) => schema.name === 'Jank')
).toBeTruthy();
// Find the Gecko only marker schema.
expect(
profile.meta.markerSchema.find((schema) => schema.name === 'GCMajor')
).toBeTruthy();
expect(
markerSchemaFrontEndOnly.find((schema) => schema.name === 'GCMajor')
).toBeUndefined();
expect(
combinedSchema.find((schema) => schema.name === 'GCMajor')
).toBeTruthy();
});
});