| <!DOCTYPE html> | |
| <head> | |
| <title>JavaScript Profiler Test</title> | |
| <script async src="black_box_js_test_utils.js"></script> | |
| <script> | |
| const testPrimeProfiler = async () => { | |
| const profiler = new Profiler({ sampleInterval: 10 /**ms**/, maxBufferSize: 10 /**number of samples*/ }); | |
| // sleep 10 seconds | |
| await new Promise(resolve => setTimeout(resolve, 10000 /**ms**/)); | |
| // stop & check | |
| const trace = await profiler.stop(); | |
| assertTrue(trace.samples.length > 0, "expected some stack traces"); | |
| }; | |
| const testSampleBufferFullProfiler = async () => { | |
| const promise = new Promise((resolve, reject) => { | |
| const profiler = new Profiler({ maxBufferSize: 1, sampleInterval: 10 }); | |
| profiler.addEventListener("samplebufferfull", () => { | |
| profiler.stop().then(() => { resolve(true); }); | |
| }); | |
| }); | |
| const timeoutPromise = new Promise((resolve) => { | |
| setTimeout(() => { | |
| resolve(false); | |
| }, 60000); | |
| }); | |
| const result = await Promise.race([timeoutPromise, promise]); | |
| assertTrue(result, "expected sample buffer to be full"); | |
| }; | |
| /** | |
| * Creates a new Profiler and then deletes it before the sample buffer is full. | |
| * Expects the SampleBuffer to be full and the callback to be executed. | |
| */ | |
| const testAbruptGarbageCollection = async () => { | |
| const profilerPromise = new Promise((resolve) => { | |
| window.profiler = new Profiler({ maxBufferSize: 100, sampleInterval: 10 }); | |
| delete window.profiler; // simulates the effects of GC. | |
| resolve(true); | |
| }); | |
| const survivePromise = new Promise((resolve) => { | |
| setTimeout(() => { | |
| resolve(true); | |
| }, 10000); | |
| }); | |
| // must survive for 10 seconds | |
| const result = await Promise.all([survivePromise, profilerPromise]); | |
| assertTrue(result.every(p => p), "expected cobalt to survive 10 sec if GC collects profiler"); | |
| }; | |
| window.addEventListener("load", async () => { | |
| setupFinished(); | |
| // Test Case 1: Prime Profiler | |
| if (window.location.search.includes("mode=testPrimeProfiler")) { | |
| console.log("testPrimeProfiler"); | |
| await testPrimeProfiler(); | |
| } | |
| // Test Case 2: SampleBufferFull Profiler | |
| else if (window.location.search.includes("mode=testSampleBufferFullProfiler")) { | |
| console.log("testSampleBufferFullProfiler"); | |
| await testSampleBufferFullProfiler(); | |
| } | |
| // Test Case 3: GC Profiler | |
| else if (window.location.search.includes("mode=testAbruptGarbageCollection")) { | |
| console.log("testAbruptGarbageCollection"); | |
| await testAbruptGarbageCollection(); | |
| } | |
| else { | |
| console.log("Running all tests"); | |
| await testPrimeProfiler(); | |
| await testSampleBufferFullProfiler(); | |
| await testAbruptGarbageCollection(); | |
| } | |
| console.log("DONE"); | |
| onEndTest(); | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| </body> |