|
| 1 | +/** |
| 2 | + * Copyright 2018 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import {TimeProfile, TimeProfileNode} from './v8-types'; |
| 18 | +import * as inspector from 'node:inspector'; |
| 19 | + |
| 20 | +const session = new inspector.Session(); |
| 21 | +session.connect(); |
| 22 | + |
| 23 | +// Wrappers around inspector functions |
| 24 | +export function startProfiling(): Promise<void> { |
| 25 | + return new Promise<void>((resolve, reject) => { |
| 26 | + session.post('Profiler.enable', err => { |
| 27 | + if (err !== null) { |
| 28 | + reject(err); |
| 29 | + } |
| 30 | + session.post('Profiler.start', err => { |
| 31 | + if (err !== null) { |
| 32 | + reject(err); |
| 33 | + } |
| 34 | + resolve(); |
| 35 | + }); |
| 36 | + }); |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +export function stopProfiling(): Promise<TimeProfile> { |
| 41 | + // return profiler.timeProfiler.stopProfiling(runName, includeLineInfo || false); |
| 42 | + return new Promise<TimeProfile>((resolve, reject) => { |
| 43 | + session.post('Profiler.stop', (err, {profile}) => { |
| 44 | + if (err !== null) { |
| 45 | + reject(err); |
| 46 | + } |
| 47 | + resolve(translateToTimeProfile(profile)); |
| 48 | + }); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +function translateToTimeProfile( |
| 53 | + profile: inspector.Profiler.Profile |
| 54 | +): TimeProfile { |
| 55 | + const root: inspector.Profiler.ProfileNode | undefined = profile.nodes[0]; |
| 56 | + // Not sure if this could ever happen... |
| 57 | + if (root === undefined) { |
| 58 | + return { |
| 59 | + endTime: profile.endTime, |
| 60 | + startTime: profile.startTime, |
| 61 | + topDownRoot: { |
| 62 | + children: [], |
| 63 | + hitCount: 0, |
| 64 | + scriptName: '', |
| 65 | + }, |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + const nodesById: {[key: number]: inspector.Profiler.ProfileNode} = {}; |
| 70 | + profile.nodes.forEach(node => (nodesById[node.id] = node)); |
| 71 | + |
| 72 | + function translateNode({ |
| 73 | + hitCount, |
| 74 | + children, |
| 75 | + callFrame: {columnNumber, functionName, lineNumber, scriptId, url}, |
| 76 | + }: inspector.Profiler.ProfileNode): TimeProfileNode { |
| 77 | + const parsedScriptId = parseInt(scriptId); |
| 78 | + return { |
| 79 | + name: functionName, |
| 80 | + scriptName: url, |
| 81 | + |
| 82 | + // Add 1 because these are zero-based |
| 83 | + columnNumber: columnNumber + 1, |
| 84 | + lineNumber: lineNumber + 1, |
| 85 | + |
| 86 | + hitCount: hitCount ?? 0, |
| 87 | + scriptId: Number.isNaN(parsedScriptId) ? 0 : parsedScriptId, |
| 88 | + children: |
| 89 | + children?.map(childId => translateNode(nodesById[childId])) ?? [], |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + return { |
| 94 | + endTime: profile.endTime, |
| 95 | + startTime: profile.startTime, |
| 96 | + topDownRoot: translateNode(root), |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +export function setSamplingInterval(intervalMicros: number): Promise<void> { |
| 101 | + return new Promise<void>((resolve, reject) => { |
| 102 | + session.post( |
| 103 | + 'Profiler.setSamplingInterval', |
| 104 | + {interval: intervalMicros}, |
| 105 | + err => { |
| 106 | + if (err !== null) { |
| 107 | + reject(err); |
| 108 | + } |
| 109 | + resolve(); |
| 110 | + } |
| 111 | + ); |
| 112 | + }); |
| 113 | +} |
0 commit comments