|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 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 | +if (process.env.PW_INSTRUMENT_MODULES) { |
| 18 | + const Module = require('module'); |
| 19 | + const originalLoad = Module._load; |
| 20 | + |
| 21 | + type TreeNode = { name: string, selfMs: number, totalMs: number, childrenMs: number, children: TreeNode[] }; |
| 22 | + const root: TreeNode = { name: '<root>', selfMs: 0, totalMs: 0, childrenMs: 0, children: [] }; |
| 23 | + let current = root; |
| 24 | + const stack: TreeNode[] = []; |
| 25 | + |
| 26 | + Module._load = function(request: any, _parent: any, _isMain: any) { |
| 27 | + const node: TreeNode = { name: request, selfMs: 0, totalMs: 0, childrenMs: 0, children: [] }; |
| 28 | + current.children.push(node); |
| 29 | + stack.push(current); |
| 30 | + current = node; |
| 31 | + const start = performance.now(); |
| 32 | + let result; |
| 33 | + try { |
| 34 | + result = originalLoad.apply(this, arguments); |
| 35 | + } catch (e) { |
| 36 | + // Module load failed (e.g. optional dep not found) — unwind stack. |
| 37 | + current = stack.pop()!; |
| 38 | + current.children.pop(); |
| 39 | + throw e; |
| 40 | + } |
| 41 | + const duration = performance.now() - start; |
| 42 | + node.totalMs = duration; |
| 43 | + node.selfMs = Math.max(0, duration - node.childrenMs); |
| 44 | + current = stack.pop()!; |
| 45 | + current.childrenMs += duration; |
| 46 | + return result; |
| 47 | + }; |
| 48 | + |
| 49 | + process.on('exit', () => { |
| 50 | + function printTree(node: TreeNode, prefix: string, isLast: boolean, lines: string[], depth: number) { |
| 51 | + if (node.totalMs < 1 && depth > 0) |
| 52 | + return; |
| 53 | + const connector = depth === 0 ? '' : isLast ? '└── ' : '├── '; |
| 54 | + const time = `${node.totalMs.toFixed(1).padStart(8)}ms`; |
| 55 | + const self = node.children.length ? ` (self: ${node.selfMs.toFixed(1)}ms)` : ''; |
| 56 | + lines.push(`${time} ${prefix}${connector}${node.name}${self}`); |
| 57 | + const childPrefix = prefix + (depth === 0 ? '' : isLast ? ' ' : '│ '); |
| 58 | + const sorted = node.children.slice().sort((a, b) => b.totalMs - a.totalMs); |
| 59 | + for (let i = 0; i < sorted.length; i++) |
| 60 | + printTree(sorted[i], childPrefix, i === sorted.length - 1, lines, depth + 1); |
| 61 | + } |
| 62 | + |
| 63 | + let totalModules = 0; |
| 64 | + function count(n: TreeNode) { totalModules++; n.children.forEach(count); } |
| 65 | + root.children.forEach(count); |
| 66 | + |
| 67 | + const lines: string[] = []; |
| 68 | + const sorted = root.children.slice().sort((a, b) => b.totalMs - a.totalMs); |
| 69 | + for (let i = 0; i < sorted.length; i++) |
| 70 | + printTree(sorted[i], '', i === sorted.length - 1, lines, 0); |
| 71 | + |
| 72 | + const totalMs = root.children.reduce((s, c) => s + c.totalMs, 0); |
| 73 | + process.stderr.write(`\n--- Module load tree: ${totalModules} modules, ${totalMs.toFixed(0)}ms total ---\n` + lines.join('\n') + '\n'); |
| 74 | + |
| 75 | + // Flat list: aggregate selfMs across all tree nodes by name. |
| 76 | + const flat = new Map<string, { selfMs: number, totalMs: number, count: number }>(); |
| 77 | + function gather(n: TreeNode) { |
| 78 | + const existing = flat.get(n.name); |
| 79 | + if (existing) { |
| 80 | + existing.selfMs += n.selfMs; |
| 81 | + existing.totalMs += n.totalMs; |
| 82 | + existing.count++; |
| 83 | + } else { |
| 84 | + flat.set(n.name, { selfMs: n.selfMs, totalMs: n.totalMs, count: 1 }); |
| 85 | + } |
| 86 | + n.children.forEach(gather); |
| 87 | + } |
| 88 | + root.children.forEach(gather); |
| 89 | + const top50 = [...flat.entries()].sort((a, b) => b[1].selfMs - a[1].selfMs).slice(0, 50); |
| 90 | + const flatLines = top50.map(([mod, { selfMs, totalMs, count }]) => |
| 91 | + `${selfMs.toFixed(1).padStart(8)}ms self ${totalMs.toFixed(1).padStart(8)}ms total (x${String(count).padStart(3)}) ${mod}` |
| 92 | + ); |
| 93 | + process.stderr.write(`\n--- Top 50 modules by self time ---\n` + flatLines.join('\n') + '\n'); |
| 94 | + }); |
| 95 | +} |
0 commit comments