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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
/* 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/. */
/* globals Assert */
/* globals info */
/**
* This file contains utilities that can be shared between xpcshell tests and mochitests.
*/
// The marker phases.
const INSTANT = 0;
const INTERVAL = 1;
const INTERVAL_START = 2;
const INTERVAL_END = 3;
// This Services declaration may shadow another from head.js, so define it as
// a var rather than a const.
const defaultSettings = {
entries: 8 * 1024 * 1024, // 8M entries = 64MB
interval: 1, // ms
features: [],
threads: ["GeckoMain"],
};
// Effectively `async`: Start the profiler and return the `startProfiler`
// promise that will get resolved when all child process have started their own
// profiler.
async function startProfiler(callersSettings) {
if (Services.profiler.IsActive()) {
Assert.ok(
Services.env.exists("MOZ_PROFILER_STARTUP"),
"The profiler is active at the begining of the test, " +
"the MOZ_PROFILER_STARTUP environment variable should be set."
);
if (Services.env.exists("MOZ_PROFILER_STARTUP")) {
// If the startup profiling environment variable exists, it is likely
// that tests are being profiled.
// Stop the profiler before starting profiler tests.
info(
"This test starts and stops the profiler and is not compatible " +
"with the use of MOZ_PROFILER_STARTUP. " +
"Stopping the profiler before starting the test."
);
await Services.profiler.StopProfiler();
} else {
throw new Error(
"The profiler must not be active before starting it in a test."
);
}
}
const settings = Object.assign({}, defaultSettings, callersSettings);
return Services.profiler.StartProfiler(
settings.entries,
settings.interval,
settings.features,
settings.threads,
0,
settings.duration
);
}
function startProfilerForMarkerTests() {
return startProfiler({
features: ["nostacksampling", "js"],
threads: ["GeckoMain", "DOM Worker"],
});
}
/**
* This is a helper function be able to run `await wait(500)`. Unfortunately
* this is needed as the act of collecting functions relies on the periodic
* sampling of the threads. See:
* https://bugzilla.mozilla.org/show_bug.cgi?id=1529053
*
* @param {number} time
* @returns {Promise}
*/
function wait(time) {
return new Promise(resolve => {
// eslint-disable-next-line mozilla/no-arbitrary-setTimeout
setTimeout(resolve, time);
});
}
/**
* Get the payloads of a type recursively, including from all subprocesses.
*
* @param {Object} profile The gecko profile.
* @param {string} type The marker payload type, e.g. "DiskIO".
* @param {Array} payloadTarget The recursive list of payloads.
* @return {Array} The final payloads.
*/
function getPayloadsOfTypeFromAllThreads(profile, type, payloadTarget = []) {
for (const { markers } of profile.threads) {
for (const markerTuple of markers.data) {
const payload = markerTuple[markers.schema.data];
if (payload && payload.type === type) {
payloadTarget.push(payload);
}
}
}
for (const subProcess of profile.processes) {
getPayloadsOfTypeFromAllThreads(subProcess, type, payloadTarget);
}
return payloadTarget;
}
/**
* Get the payloads of a type from a single thread.
*
* @param {Object} thread The thread from a profile.
* @param {string} type The marker payload type, e.g. "DiskIO".
* @return {Array} The payloads.
*/
function getPayloadsOfType(thread, type) {
const { markers } = thread;
const results = [];
for (const markerTuple of markers.data) {
const payload = markerTuple[markers.schema.data];
if (payload && payload.type === type) {
results.push(payload);
}
}
return results;
}
/**
* Applies the marker schema to create individual objects for each marker
*
* @param {Object} thread The thread from a profile.
* @return {InflatedMarker[]} The markers.
*/
function getInflatedMarkerData(thread) {
const { markers, stringTable } = thread;
return markers.data.map(markerTuple => {
const marker = {};
for (const [key, tupleIndex] of Object.entries(markers.schema)) {
marker[key] = markerTuple[tupleIndex];
if (key === "name") {
// Use the string from the string table.
marker[key] = stringTable[marker[key]];
}
}
return marker;
});
}
/**
* Applies the marker schema to create individual objects for each marker, then
* keeps only the network markers that match the profiler tests.
*
* @param {Object} thread The thread from a profile.
* @return {InflatedMarker[]} The filtered network markers.
*/
function getInflatedNetworkMarkers(thread) {
const markers = getInflatedMarkerData(thread);
return markers.filter(
m =>
m.data &&
m.data.type === "Network" &&
// We filter out network markers that aren't related to the test, to
// avoid intermittents.
m.data.URI.includes("/tools/profiler/")
);
}
/**
* From a list of network markers, this returns pairs of start/stop markers.
* If a stop marker can't be found for a start marker, this will return an array
* of only 1 element.
*
* @param {InflatedMarker[]} networkMarkers Network markers
* @return {InflatedMarker[][]} Pairs of network markers
*/
function getPairsOfNetworkMarkers(allNetworkMarkers) {
// For each 'start' marker we want to find the next 'stop' or 'redirect'
// marker with the same id.
const result = [];
const mapOfStartMarkers = new Map(); // marker id -> id in result array
for (const marker of allNetworkMarkers) {
const { data } = marker;
if (data.status === "STATUS_START") {
if (mapOfStartMarkers.has(data.id)) {
const previousMarker = result[mapOfStartMarkers.get(data.id)][0];
Assert.ok(
false,
`We found 2 start markers with the same id ${data.id}, without end marker in-between.` +
`The first marker has URI ${previousMarker.data.URI}, the second marker has URI ${data.URI}.` +
` This should not happen.`
);
continue;
}
mapOfStartMarkers.set(data.id, result.length);
result.push([marker]);
} else {
// STOP or REDIRECT
if (!mapOfStartMarkers.has(data.id)) {
Assert.ok(
false,
`We found an end marker without a start marker (id: ${data.id}, URI: ${data.URI}). This should not happen.`
);
continue;
}
result[mapOfStartMarkers.get(data.id)].push(marker);
mapOfStartMarkers.delete(data.id);
}
}
return result;
}
/**
* It can be helpful to force the profiler to collect a JavaScript sample. This
* function spins on a while loop until at least one more sample is collected.
*
* @return {number} The index of the collected sample.
*/
function captureAtLeastOneJsSample() {
function getProfileSampleCount() {
const profile = Services.profiler.getProfileData();
return profile.threads[0].samples.data.length;
}
const sampleCount = getProfileSampleCount();
// Create an infinite loop until a sample has been collected.
while (true) {
if (sampleCount < getProfileSampleCount()) {
return sampleCount;
}
}
}
function isJSONWhitespace(c) {
return ["\n", "\r", " ", "\t"].includes(c);
}
function verifyJSONStringIsCompact(s) {
const stateData = 0;
const stateString = 1;
const stateEscapedChar = 2;
let state = stateData;
for (let i = 0; i < s.length; ++i) {
let c = s[i];
switch (state) {
case stateData:
if (isJSONWhitespace(c)) {
Assert.ok(
false,
`"Unexpected JSON whitespace at index ${i} in profile: <<<${s}>>>"`
);
return;
}
if (c == '"') {
state = stateString;
}
break;
case stateString:
if (c == '"') {
state = stateData;
} else if (c == "\\") {
state = stateEscapedChar;
}
break;
case stateEscapedChar:
state = stateString;
break;
}
}
}
/**
* This function pauses the profiler before getting the profile. Then after
* getting the data, the profiler is stopped, and all profiler data is removed.
* @returns {Promise<Profile>}
*/
async function stopNowAndGetProfile() {
// Don't await the pause, because each process will handle it before it
// receives the following `getProfileDataAsArrayBuffer()`.
Services.profiler.Pause();
const profileArrayBuffer =
await Services.profiler.getProfileDataAsArrayBuffer();
await Services.profiler.StopProfiler();
const profileUint8Array = new Uint8Array(profileArrayBuffer);
const textDecoder = new TextDecoder("utf-8", { fatal: true });
const profileString = textDecoder.decode(profileUint8Array);
verifyJSONStringIsCompact(profileString);
return JSON.parse(profileString);
}
/**
* This function ensures there's at least one sample, then pauses the profiler
* before getting the profile. Then after getting the data, the profiler is
* stopped, and all profiler data is removed.
* @returns {Promise<Profile>}
*/
async function waitSamplingAndStopAndGetProfile() {
await Services.profiler.waitOnePeriodicSampling();
return stopNowAndGetProfile();
}
/**
* Verifies that a marker is an interval marker.
*
* @param {InflatedMarker} marker
* @returns {boolean}
*/
function isIntervalMarker(inflatedMarker) {
return (
inflatedMarker.phase === 1 &&
typeof inflatedMarker.startTime === "number" &&
typeof inflatedMarker.endTime === "number"
);
}
/**
* @param {Profile} profile
* @returns {Thread[]}
*/
function getThreads(profile) {
const threads = [];
function getThreadsRecursive(process) {
for (const thread of process.threads) {
threads.push(thread);
}
for (const subprocess of process.processes) {
getThreadsRecursive(subprocess);
}
}
getThreadsRecursive(profile);
return threads;
}
/**
* Find a specific marker schema from any process of a profile.
*
* @param {Profile} profile
* @param {string} name
* @returns {MarkerSchema}
*/
function getSchema(profile, name) {
{
const schema = profile.meta.markerSchema.find(s => s.name === name);
if (schema) {
return schema;
}
}
for (const subprocess of profile.processes) {
const schema = subprocess.meta.markerSchema.find(s => s.name === name);
if (schema) {
return schema;
}
}
console.error("Parent process schema", profile.meta.markerSchema);
for (const subprocess of profile.processes) {
console.error("Child process schema", subprocess.meta.markerSchema);
}
throw new Error(`Could not find a schema for "${name}".`);
}
/**
* This escapes all characters that have a special meaning in RegExps.
* This was stolen from https://github.com/sindresorhus/escape-string-regexp and
* so it is licence MIT and:
* Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com).
* See the full license in https://raw.githubusercontent.com/sindresorhus/escape-string-regexp/main/license.
* @param {string} string The string to be escaped
* @returns {string} The result
*/
function escapeStringRegexp(string) {
if (typeof string !== "string") {
throw new TypeError("Expected a string");
}
// Escape characters with special meaning either inside or outside character
// sets. Use a simple backslash escape when it’s always valid, and a `\xnn`
// escape when the simpler form would be disallowed by Unicode patterns’
// stricter grammar.
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
}
/** ------ Assertions helper ------ */
/**
* This assert helper function makes it easy to check a lot of properties in an
* object. We augment Assert.sys.mjs to make it easier to use.
*/
Object.assign(Assert, {
/*
* It checks if the properties on the right are all present in the object on
* the left. Note that the object might still have other properties (see
* objectContainsOnly below if you want the stricter form).
*
* The basic form does basic equality on each expected property:
*
* Assert.objectContains(fixture, {
* foo: "foo",
* bar: 1,
* baz: true,
* });
*
* But it also has a more powerful form with expectations. The available
* expectations are:
* - any(): this only checks for the existence of the property, not its value
* - number(), string(), boolean(), bigint(), function(), symbol(), object():
* this checks if the value is of this type
* - objectContains(expected): this applies Assert.objectContains()
* recursively on this property.
* - stringContains(needle): this checks if the expected value is included in
* the property value.
* - stringMatches(regexp): this checks if the property value matches this
* regexp. The regexp can be passed as a string, to be dynamically built.
*
* example:
*
* Assert.objectContains(fixture, {
* name: Expect.stringMatches(`Load \\d+:.*${url}`),
* data: Expect.objectContains({
* status: "STATUS_STOP",
* URI: Expect.stringContains("https://"),
* requestMethod: "GET",
* contentType: Expect.string(),
* startTime: Expect.number(),
* cached: Expect.boolean(),
* }),
* });
*
* Each expectation will translate into one or more Assert call. Therefore if
* one expectation fails, this will be clearly visible in the test output.
*
* Expectations can also be normal functions, for example:
*
* Assert.objectContains(fixture, {
* number: value => Assert.greater(value, 5)
* });
*
* Note that you'll need to use Assert inside this function.
*/
objectContains(object, expectedProperties) {
// Basic tests: we don't want to run other assertions if these tests fail.
if (typeof object !== "object") {
this.ok(
false,
`The first parameter should be an object, but found: ${object}.`
);
return;
}
if (typeof expectedProperties !== "object") {
this.ok(
false,
`The second parameter should be an object, but found: ${expectedProperties}.`
);
return;
}
for (const key of Object.keys(expectedProperties)) {
const expected = expectedProperties[key];
if (!(key in object)) {
this.report(
true,
object,
expectedProperties,
`The object should contain the property "${key}", but it's missing.`
);
continue;
}
if (typeof expected === "function") {
// This is a function, so let's call it.
expected(
object[key],
`The object should contain the property "${key}" with an expected value and type.`
);
} else {
// Otherwise, we check for equality.
this.equal(
object[key],
expectedProperties[key],
`The object should contain the property "${key}" with an expected value.`
);
}
}
},
/**
* This is very similar to the previous `objectContains`, but this also looks
* at the number of the objects' properties. Thus this will fail if the
* objects don't have the same properties exactly.
*/
objectContainsOnly(object, expectedProperties) {
// Basic tests: we don't want to run other assertions if these tests fail.
if (typeof object !== "object") {
this.ok(
false,
`The first parameter should be an object but found: ${object}.`
);
return;
}
if (typeof expectedProperties !== "object") {
this.ok(
false,
`The second parameter should be an object but found: ${expectedProperties}.`
);
return;
}
// In objectContainsOnly, we specifically want to check if all properties
// from the fixture object are expected.
// We'll be failing a test only for the specific properties that weren't
// expected, and only fail with one message, so that the test outputs aren't
// spammed.
const extraProperties = [];
for (const fixtureKey of Object.keys(object)) {
if (!(fixtureKey in expectedProperties)) {
extraProperties.push(fixtureKey);
}
}
if (extraProperties.length) {
// Some extra properties have been found.
this.report(
true,
object,
expectedProperties,
`These properties are present, but shouldn't: "${extraProperties.join(
'", "'
)}".`
);
}
// Now, let's carry on the rest of our work.
this.objectContains(object, expectedProperties);
},
});
const Expect = {
any:
() =>
() => {} /* We don't check anything more than the presence of this property. */,
};
/* These functions are part of the Assert object, and we want to reuse them. */
[
"stringContains",
"stringMatches",
"objectContains",
"objectContainsOnly",
].forEach(
assertChecker =>
(Expect[assertChecker] =
expected =>
(actual, ...moreArgs) =>
Assert[assertChecker](actual, expected, ...moreArgs))
);
/* These functions will only check for the type. */
[
"number",
"string",
"boolean",
"bigint",
"symbol",
"object",
"function",
].forEach(type => (Expect[type] = makeTypeChecker(type)));
function makeTypeChecker(type) {
return (...unexpectedArgs) => {
if (unexpectedArgs.length) {
throw new Error(
"Type checkers expectations aren't expecting any argument."
);
}
return (actual, message) => {
const isCorrect = typeof actual === type;
Assert.report(!isCorrect, actual, type, message, "has type");
};
};
}
/* ------ End of assertion helper ------ */
|