|
| 1 | +// Copyright 2024 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import { |
| 6 | + makeCompleteEvent, |
| 7 | + makeFlowEvents, |
| 8 | + makeProfileCall, |
| 9 | +} from '../../../testing/TraceHelpers.js'; |
| 10 | +import * as Trace from '../trace.js'; |
| 11 | +const cat = 'mewtwo'; |
| 12 | +const pid = 1; |
| 13 | +const tid = 1; |
| 14 | +async function buildAsyncCallStacksHandlerData(events: Trace.Types.Events.Event[]): |
| 15 | + Promise<ReturnType<typeof Trace.Handlers.ModelHandlers.AsyncCallStacks.data>> { |
| 16 | + Trace.Handlers.ModelHandlers.Renderer.reset(); |
| 17 | + Trace.Handlers.ModelHandlers.Flows.reset(); |
| 18 | + Trace.Handlers.ModelHandlers.Flows.reset(); |
| 19 | + for (const event of events) { |
| 20 | + Trace.Handlers.ModelHandlers.Renderer.handleEvent(event); |
| 21 | + Trace.Handlers.ModelHandlers.Flows.handleEvent(event); |
| 22 | + } |
| 23 | + await Trace.Handlers.ModelHandlers.Renderer.finalize(); |
| 24 | + await Trace.Handlers.ModelHandlers.Flows.finalize(); |
| 25 | + await Trace.Handlers.ModelHandlers.AsyncCallStacks.finalize(); |
| 26 | + return Trace.Handlers.ModelHandlers.AsyncCallStacks.data(); |
| 27 | +} |
| 28 | +describe('AsyncCallStacksHandler', function() { |
| 29 | + describe('Resolving JS task schedulers to task run entrypoints', function() { |
| 30 | + it('associates a JS task scheduler profile call with the corresponding task run entry point', async function() { |
| 31 | + const jsTaskScheduler = makeProfileCall('setTimeout', 0, 50, pid, tid); |
| 32 | + const asyncTaskScheduled = |
| 33 | + makeCompleteEvent(Trace.Types.Events.Name.DEBUGGER_ASYNC_TASK_SCHEDULED, 0, 0, cat, pid, tid); |
| 34 | + |
| 35 | + const asyncTaskRun = makeCompleteEvent(Trace.Types.Events.Name.DEBUGGER_ASYNC_TASK_RUN, 60, 100, cat, tid, pid); |
| 36 | + const jsTaskRunEntryPoint = makeCompleteEvent(Trace.Types.Events.Name.FUNCTION_CALL, 70, 20, cat, tid, pid); |
| 37 | + |
| 38 | + const flowEvents = makeFlowEvents([asyncTaskScheduled, asyncTaskRun]); |
| 39 | + const rendererEvents = [jsTaskScheduler, asyncTaskScheduled, asyncTaskRun, jsTaskRunEntryPoint]; |
| 40 | + const allEvents = [...rendererEvents, ...flowEvents]; |
| 41 | + |
| 42 | + const asyncCallStacksData = await buildAsyncCallStacksHandlerData(allEvents); |
| 43 | + const testRunEntryPoints = asyncCallStacksData.schedulerToRunEntryPoints.get(jsTaskScheduler); |
| 44 | + assert.strictEqual(testRunEntryPoints?.length, 1); |
| 45 | + assert.strictEqual(testRunEntryPoints?.[0], jsTaskRunEntryPoint); |
| 46 | + }); |
| 47 | + |
| 48 | + it('uses the nearest profile call ancestor of a debuggerTaskScheduled as JS task scheduler', async function() { |
| 49 | + // Three profile call ancestors to the debuggerTaskScheduled event. |
| 50 | + // Test the one closest to the debuggerTaskScheduled in the tree |
| 51 | + // is picked. |
| 52 | + const foo = makeProfileCall('foo', 0, 50, pid, tid); |
| 53 | + const bar = makeProfileCall('bar', 0, 40, pid, tid); |
| 54 | + const jsTaskScheduler = makeProfileCall('setTimeout', 0, 30, pid, tid); |
| 55 | + const asyncTaskScheduled = |
| 56 | + makeCompleteEvent(Trace.Types.Events.Name.DEBUGGER_ASYNC_TASK_SCHEDULED, 0, 0, cat, pid, tid); |
| 57 | + |
| 58 | + const asyncTaskRun = makeCompleteEvent(Trace.Types.Events.Name.DEBUGGER_ASYNC_TASK_RUN, 60, 100, cat, tid, pid); |
| 59 | + const jsTaskRunEntryPoint = makeCompleteEvent(Trace.Types.Events.Name.FUNCTION_CALL, 70, 20, cat, tid, pid); |
| 60 | + |
| 61 | + const flowEvents = makeFlowEvents([asyncTaskScheduled, asyncTaskRun]); |
| 62 | + const rendererEvents = [foo, bar, jsTaskScheduler, asyncTaskScheduled, asyncTaskRun, jsTaskRunEntryPoint]; |
| 63 | + const allEvents = [...rendererEvents, ...flowEvents]; |
| 64 | + |
| 65 | + const asyncCallStacksData = await buildAsyncCallStacksHandlerData(allEvents); |
| 66 | + const testRunEntryPoints = asyncCallStacksData.schedulerToRunEntryPoints.get(jsTaskScheduler); |
| 67 | + assert.strictEqual(testRunEntryPoints?.length, 1); |
| 68 | + assert.strictEqual(testRunEntryPoints?.[0], jsTaskRunEntryPoint); |
| 69 | + }); |
| 70 | + |
| 71 | + it('uses the nearest JS entry point descendant of a debuggerTaskRun as async task run', async function() { |
| 72 | + const jsTaskScheduler = makeProfileCall('setTimeout', 0, 30, pid, tid); |
| 73 | + const asyncTaskScheduled = |
| 74 | + makeCompleteEvent(Trace.Types.Events.Name.DEBUGGER_ASYNC_TASK_SCHEDULED, 0, 0, cat, pid, tid); |
| 75 | + |
| 76 | + const asyncTaskRun = makeCompleteEvent(Trace.Types.Events.Name.DEBUGGER_ASYNC_TASK_RUN, 60, 100, cat, tid, pid); |
| 77 | + |
| 78 | + // Two JS entry points belonging to the same subtree are |
| 79 | + // descendants to the debuggerTaskRun event. Test the one closest |
| 80 | + // to the debuggerTaskRun in the global tree is picked. |
| 81 | + const jsTaskRunEntryPoint = makeCompleteEvent(Trace.Types.Events.Name.FUNCTION_CALL, 70, 20, cat, tid, pid); |
| 82 | + const secondFakeEntryPoint = makeCompleteEvent(Trace.Types.Events.Name.FUNCTION_CALL, 71, 10, cat, tid, pid); |
| 83 | + |
| 84 | + const flowEvents = makeFlowEvents([asyncTaskScheduled, asyncTaskRun]); |
| 85 | + const rendererEvents = |
| 86 | + [jsTaskScheduler, asyncTaskScheduled, asyncTaskRun, jsTaskRunEntryPoint, secondFakeEntryPoint]; |
| 87 | + const allEvents = [...rendererEvents, ...flowEvents]; |
| 88 | + |
| 89 | + const asyncCallStacksData = await buildAsyncCallStacksHandlerData(allEvents); |
| 90 | + const testRunEntryPoints = asyncCallStacksData.schedulerToRunEntryPoints.get(jsTaskScheduler); |
| 91 | + assert.strictEqual(testRunEntryPoints?.length, 1); |
| 92 | + assert.strictEqual(testRunEntryPoints?.[0], jsTaskRunEntryPoint); |
| 93 | + }); |
| 94 | + it('returns multiple JS entry point descendants of a debuggerTaskRun when they are not in the same subtree', |
| 95 | + async function() { |
| 96 | + const jsTaskScheduler = makeProfileCall('setTimeout', 0, 30, pid, tid); |
| 97 | + const asyncTaskScheduled = |
| 98 | + makeCompleteEvent(Trace.Types.Events.Name.DEBUGGER_ASYNC_TASK_SCHEDULED, 0, 0, cat, pid, tid); |
| 99 | + |
| 100 | + const asyncTaskRun = |
| 101 | + makeCompleteEvent(Trace.Types.Events.Name.DEBUGGER_ASYNC_TASK_RUN, 60, 100, cat, tid, pid); |
| 102 | + |
| 103 | + // Two JS entry points belonging to different subtrees are |
| 104 | + // descendants to the debuggerTaskRun event. Test both are |
| 105 | + // used. |
| 106 | + const firstJSTaskRunEntryPoint = |
| 107 | + makeCompleteEvent(Trace.Types.Events.Name.FUNCTION_CALL, 70, 20, cat, tid, pid); |
| 108 | + const secondJSTaskRunEntryPoint = |
| 109 | + makeCompleteEvent(Trace.Types.Events.Name.FUNCTION_CALL, 90, 10, cat, tid, pid); |
| 110 | + |
| 111 | + const flowEvents = makeFlowEvents([asyncTaskScheduled, asyncTaskRun]); |
| 112 | + const rendererEvents = |
| 113 | + [jsTaskScheduler, asyncTaskScheduled, asyncTaskRun, firstJSTaskRunEntryPoint, secondJSTaskRunEntryPoint]; |
| 114 | + const allEvents = [...rendererEvents, ...flowEvents]; |
| 115 | + |
| 116 | + const asyncCallStacksData = await buildAsyncCallStacksHandlerData(allEvents); |
| 117 | + const testRunEntryPoints = asyncCallStacksData.schedulerToRunEntryPoints.get(jsTaskScheduler); |
| 118 | + assert.strictEqual(testRunEntryPoints?.length, 2); |
| 119 | + assert.strictEqual(testRunEntryPoints?.[0], firstJSTaskRunEntryPoint); |
| 120 | + assert.strictEqual(testRunEntryPoints?.[1], secondJSTaskRunEntryPoint); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments