browser_perf-01.js (3464B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 "use strict"; 5 6 const { ProfilerTestUtils } = ChromeUtils.importESModule( 7 "resource://testing-common/ProfilerTestUtils.sys.mjs" 8 ); 9 10 add_setup(ProfilerTestUtils.assertProfilerInactive); 11 12 // This test is at the edge of timing out, probably because of LUL 13 // initialization on Linux. This is also happening only once, which is why only 14 // this test needs it: for other tests LUL is already initialized because 15 // they're running in the same Firefox instance. 16 // See also bug 1635442. 17 requestLongerTimeout(2); 18 19 async function decompressGzip(buffer) { 20 if (buffer.resizable) { 21 // Not sure why, but we can't use a resizable buffer for streams. 22 buffer = buffer.transferToFixedLength(); 23 } 24 const decompressionStream = new DecompressionStream("gzip"); 25 const decoderStream = new TextDecoderStream(); 26 const decodedStream = decompressionStream.readable.pipeThrough(decoderStream); 27 const writer = decompressionStream.writable.getWriter(); 28 writer.write(buffer); 29 const writePromise = writer.close(); 30 31 let result = ""; 32 for await (const chunk of decodedStream) { 33 result += chunk; 34 } 35 36 await writePromise; 37 return JSON.parse(result); 38 } 39 40 /** 41 * Run through a series of basic recording actions for the perf actor. 42 */ 43 add_task(async function () { 44 const { front, client } = await initPerfFront(); 45 46 // Assert the initial state. 47 is( 48 await front.isSupportedPlatform(), 49 true, 50 "This test only runs on supported platforms." 51 ); 52 is(await front.isActive(), false, "The profiler is not active yet."); 53 54 // Start the profiler. 55 const profilerStarted = once(front, "profiler-started"); 56 await front.startProfiler(); 57 await profilerStarted; 58 is(await front.isActive(), true, "The profiler was started."); 59 60 // Stop the profiler and assert the results. 61 const profilerStopped1 = once(front, "profiler-stopped"); 62 const { profile: gzippedProfile, additionalInformation } = 63 await front.getProfileAndStopProfiler(); 64 await profilerStopped1; 65 is(await front.isActive(), false, "The profiler was stopped."); 66 const profile = await decompressGzip(gzippedProfile); 67 ok("threads" in profile, "The actor was used to record a profile."); 68 ok( 69 additionalInformation.sharedLibraries, 70 "We retrieved some shared libraries as well." 71 ); 72 73 // Restart the profiler. 74 await front.startProfiler(); 75 is(await front.isActive(), true, "The profiler was re-started."); 76 77 // Stop and discard. 78 const profilerStopped2 = once(front, "profiler-stopped"); 79 await front.stopProfilerAndDiscardProfile(); 80 await profilerStopped2; 81 is( 82 await front.isActive(), 83 false, 84 "The profiler was stopped and the profile discarded." 85 ); 86 87 await front.destroy(); 88 await client.close(); 89 }); 90 91 add_task(async function test_error_case() { 92 const { front, client } = await initPerfFront(); 93 94 try { 95 // We try to get the profile without starting the profiler first. This should 96 // trigger an error in the our C++ code. 97 await front.getProfileAndStopProfiler(); 98 ok(false, "Getting the profile should fail"); 99 } catch (e) { 100 Assert.stringContains( 101 e.message, 102 "The profiler is not active.", 103 "The error contains the expected error message." 104 ); 105 } 106 107 await front.destroy(); 108 await client.close(); 109 });