|
| 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 type * as Protocol from '../../../generated/protocol.js'; |
| 6 | +import {describeWithEnvironment} from '../../../testing/EnvironmentHelpers.js'; |
| 7 | +import {TraceLoader} from '../../../testing/TraceLoader.js'; |
| 8 | +import * as Trace from '../trace.js'; |
| 9 | + |
| 10 | +function shapeStackTraceAsArray(stackTrace: Protocol.Runtime.StackTrace): |
| 11 | + {callFrames: Protocol.Runtime.CallFrame[], description?: string}[] { |
| 12 | + const stackTraceAsArray: {callFrames: Protocol.Runtime.CallFrame[], description?: string}[] = []; |
| 13 | + let currentStackTrace: Protocol.Runtime.StackTrace|undefined = stackTrace; |
| 14 | + while (currentStackTrace) { |
| 15 | + // @ts-ignore `codeType` is not included in the protocol types but |
| 16 | + // occasionally present |
| 17 | + currentStackTrace.callFrames.forEach(callFrame => delete callFrame.codeType); |
| 18 | + stackTraceAsArray.push({callFrames: currentStackTrace.callFrames, description: currentStackTrace.description}); |
| 19 | + currentStackTrace = currentStackTrace.parent; |
| 20 | + } |
| 21 | + |
| 22 | + return stackTraceAsArray; |
| 23 | +} |
| 24 | +describeWithEnvironment('StackTraceForTraceEvent', function() { |
| 25 | + let parsedTrace: Trace.Handlers.Types.ParsedTrace; |
| 26 | + beforeEach(async function() { |
| 27 | + const traceEngineData = await TraceLoader.traceEngine(this, 'async-js-calls.json.gz'); |
| 28 | + parsedTrace = traceEngineData.parsedTrace; |
| 29 | + Trace.Extras.StackTraceForEvent.clearCacheForTrace(parsedTrace); |
| 30 | + }); |
| 31 | + afterEach(async () => { |
| 32 | + Trace.Extras.StackTraceForEvent.clearCacheForTrace(parsedTrace); |
| 33 | + }); |
| 34 | + |
| 35 | + it('correctly builds the stack trace of a profile call when it only has a synchronous stack trace.', |
| 36 | + async function() { |
| 37 | + const jsCall = parsedTrace.Renderer.allTraceEntries.find( |
| 38 | + e => Trace.Types.Events.isProfileCall(e) && e.callFrame.functionName === 'startExample'); |
| 39 | + assert.exists(jsCall); |
| 40 | + const stackTrace = Trace.Extras.StackTraceForEvent.get(jsCall, parsedTrace); |
| 41 | + assert.exists(stackTrace); |
| 42 | + const stackTraceArray = shapeStackTraceAsArray(stackTrace); |
| 43 | + assert.lengthOf(stackTraceArray, 1); |
| 44 | + const callFrames = stackTraceArray[0].callFrames; |
| 45 | + assert.deepEqual(callFrames, [ |
| 46 | + { |
| 47 | + columnNumber: 21, |
| 48 | + functionName: 'startExample', |
| 49 | + lineNumber: 25, |
| 50 | + scriptId: '53' as Protocol.Runtime.ScriptId, |
| 51 | + url: '', |
| 52 | + }, |
| 53 | + {columnNumber: 0, functionName: '', lineNumber: 0, scriptId: '53' as Protocol.Runtime.ScriptId, url: ''}, |
| 54 | + ]); |
| 55 | + }); |
| 56 | + |
| 57 | + it('correctly builds the stack trace of a profile call when it only has an asynchronous stack trace.', |
| 58 | + async function() { |
| 59 | + const jsCall = parsedTrace.Renderer.allTraceEntries.find( |
| 60 | + e => Trace.Types.Events.isProfileCall(e) && e.callFrame.functionName === 'baz'); |
| 61 | + assert.exists(jsCall); |
| 62 | + const stackTrace = Trace.Extras.StackTraceForEvent.get(jsCall, parsedTrace); |
| 63 | + assert.exists(stackTrace); |
| 64 | + const stackTraceArray = shapeStackTraceAsArray(stackTrace); |
| 65 | + assert.lengthOf(stackTraceArray, 4); |
| 66 | + |
| 67 | + assert.deepEqual(stackTraceArray, [ |
| 68 | + { |
| 69 | + callFrames: [{ |
| 70 | + columnNumber: 12, |
| 71 | + functionName: 'baz', |
| 72 | + lineNumber: 13, |
| 73 | + scriptId: '53' as Protocol.Runtime.ScriptId, |
| 74 | + url: '', |
| 75 | + }], |
| 76 | + description: undefined, |
| 77 | + }, |
| 78 | + { |
| 79 | + callFrames: [{ |
| 80 | + columnNumber: 12, |
| 81 | + functionName: 'bar', |
| 82 | + lineNumber: 6, |
| 83 | + scriptId: '53' as Protocol.Runtime.ScriptId, |
| 84 | + url: '', |
| 85 | + }], |
| 86 | + description: 'requestIdleCallback', |
| 87 | + }, |
| 88 | + { |
| 89 | + callFrames: [{ |
| 90 | + columnNumber: 12, |
| 91 | + functionName: 'foo', |
| 92 | + lineNumber: 0, |
| 93 | + scriptId: '53' as Protocol.Runtime.ScriptId, |
| 94 | + url: '', |
| 95 | + }], |
| 96 | + description: 'setTimeout', |
| 97 | + }, |
| 98 | + { |
| 99 | + callFrames: [ |
| 100 | + { |
| 101 | + columnNumber: 21, |
| 102 | + functionName: 'startExample', |
| 103 | + lineNumber: 25, |
| 104 | + scriptId: '53' as Protocol.Runtime.ScriptId, |
| 105 | + url: '', |
| 106 | + }, |
| 107 | + {columnNumber: 0, functionName: '', lineNumber: 0, scriptId: '53', url: ''}, |
| 108 | + ], |
| 109 | + description: 'requestAnimationFrame', |
| 110 | + }, |
| 111 | + ]); |
| 112 | + }); |
| 113 | + |
| 114 | + it('correctly skips frames set to be ignored.', async function() { |
| 115 | + const jsCall = parsedTrace.Renderer.allTraceEntries.find( |
| 116 | + e => Trace.Types.Events.isProfileCall(e) && e.callFrame.functionName === 'baz'); |
| 117 | + assert.exists(jsCall); |
| 118 | + const isIgnoreListedCallback = (e: Trace.Types.Events.Event) => |
| 119 | + Trace.Types.Events.isProfileCall(e) && e.callFrame.functionName === 'bar'; |
| 120 | + const stackTrace = Trace.Extras.StackTraceForEvent.get(jsCall, parsedTrace, {isIgnoreListedCallback}); |
| 121 | + assert.exists(stackTrace); |
| 122 | + const stackTraceArray = shapeStackTraceAsArray(stackTrace); |
| 123 | + assert.lengthOf(stackTraceArray, 4); |
| 124 | + |
| 125 | + assert.deepEqual(stackTraceArray, [ |
| 126 | + { |
| 127 | + callFrames: [ |
| 128 | + {columnNumber: 12, functionName: 'baz', lineNumber: 13, scriptId: '53' as Protocol.Runtime.ScriptId, url: ''}, |
| 129 | + ], |
| 130 | + description: undefined, |
| 131 | + }, |
| 132 | + {callFrames: [], description: 'requestIdleCallback'}, |
| 133 | + { |
| 134 | + callFrames: [ |
| 135 | + {columnNumber: 12, functionName: 'foo', lineNumber: 0, scriptId: '53' as Protocol.Runtime.ScriptId, url: ''}, |
| 136 | + ], |
| 137 | + description: 'setTimeout', |
| 138 | + }, |
| 139 | + { |
| 140 | + callFrames: [ |
| 141 | + { |
| 142 | + columnNumber: 21, |
| 143 | + functionName: 'startExample', |
| 144 | + lineNumber: 25, |
| 145 | + scriptId: '53' as Protocol.Runtime.ScriptId, |
| 146 | + url: '', |
| 147 | + }, |
| 148 | + {columnNumber: 0, functionName: '', lineNumber: 0, scriptId: '53', url: ''}, |
| 149 | + ], |
| 150 | + description: 'requestAnimationFrame', |
| 151 | + }, |
| 152 | + ]); |
| 153 | + }); |
| 154 | + |
| 155 | + it('uses cached data correctly.', async function() { |
| 156 | + const fooCall = parsedTrace.Renderer.allTraceEntries.find( |
| 157 | + e => Trace.Types.Events.isProfileCall(e) && e.callFrame.functionName === 'foo'); |
| 158 | + assert.exists(fooCall); |
| 159 | + const result = |
| 160 | + parsedTrace.AsyncJSCalls.asyncCallToScheduler.get(fooCall as Trace.Types.Events.SyntheticProfileCall); |
| 161 | + assert.exists(result); |
| 162 | + const {scheduler: parentOfFoo} = result; |
| 163 | + |
| 164 | + // Compute stack trace of foo's parent |
| 165 | + const stackTraceOfParent = Trace.Extras.StackTraceForEvent.get(parentOfFoo, parsedTrace); |
| 166 | + assert.exists(stackTraceOfParent); |
| 167 | + const stackTraceArray = shapeStackTraceAsArray(stackTraceOfParent); |
| 168 | + assert.lengthOf(stackTraceArray, 1); |
| 169 | + |
| 170 | + // Modify the cache, to check it's used when possible |
| 171 | + const bottomFrame = stackTraceOfParent.callFrames.at(-1); |
| 172 | + assert.exists(bottomFrame); |
| 173 | + bottomFrame.functionName = 'Overriden name'; |
| 174 | + |
| 175 | + // Compute stack trace of foo, ensure the cache calculated with |
| 176 | + // its parent is used. |
| 177 | + const stackTraceOfFoo = Trace.Extras.StackTraceForEvent.get(fooCall, parsedTrace); |
| 178 | + assert.exists(stackTraceOfFoo); |
| 179 | + const stackTraceArray2 = shapeStackTraceAsArray(stackTraceOfFoo); |
| 180 | + assert.deepEqual(stackTraceArray2, [ |
| 181 | + { |
| 182 | + callFrames: [ |
| 183 | + {columnNumber: 12, functionName: 'foo', lineNumber: 0, scriptId: '53' as Protocol.Runtime.ScriptId, url: ''}, |
| 184 | + ], |
| 185 | + description: undefined, |
| 186 | + }, |
| 187 | + { |
| 188 | + callFrames: [ |
| 189 | + { |
| 190 | + columnNumber: 21, |
| 191 | + functionName: 'startExample', |
| 192 | + lineNumber: 25, |
| 193 | + scriptId: '53' as Protocol.Runtime.ScriptId, |
| 194 | + url: '', |
| 195 | + }, |
| 196 | + { |
| 197 | + columnNumber: 0, |
| 198 | + functionName: bottomFrame.functionName, |
| 199 | + lineNumber: 0, |
| 200 | + scriptId: '53' as Protocol.Runtime.ScriptId, |
| 201 | + url: '', |
| 202 | + }, |
| 203 | + ], |
| 204 | + description: 'requestAnimationFrame', |
| 205 | + }, |
| 206 | + ]); |
| 207 | + }); |
| 208 | +}); |
0 commit comments