|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------*/ |
| 4 | + |
| 5 | +import { CodeLens, Range, Position } from 'vscode'; |
| 6 | +import { lowerCaseInsensitivePath } from './path'; |
| 7 | +import { ILocation } from './cpu/model'; |
| 8 | +import { once } from './array'; |
| 9 | +import { getCandidateDiskPaths } from './open-location'; |
| 10 | + |
| 11 | +export interface IProfileInformation { |
| 12 | + selfTime: number; |
| 13 | + aggregateTime: number; |
| 14 | + ticks: number; |
| 15 | +} |
| 16 | + |
| 17 | +const decimalFormat = new Intl.NumberFormat(undefined, { |
| 18 | + maximumFractionDigits: 2, |
| 19 | + minimumFractionDigits: 2, |
| 20 | +}); |
| 21 | + |
| 22 | +const basenameRe = /[^/\\]+$/; |
| 23 | +const getBasename = (pathOrUrl: string) => basenameRe.exec(pathOrUrl)?.[0] ?? pathOrUrl; |
| 24 | + |
| 25 | +/** |
| 26 | + * A collection of profile data. Paths are expanded lazily, as doing so |
| 27 | + * up-front for very large profiles turned out to be costly (mainly in path) |
| 28 | + * manipulation. |
| 29 | + */ |
| 30 | +export class ProfileAnnotations { |
| 31 | + private readonly basenamesToExpand = new Map<string, (() => void)[]>(); |
| 32 | + private readonly data = new Map<string, { position: Position; data: IProfileInformation }[]>(); |
| 33 | + |
| 34 | + public add(rootPath: string | undefined, location: ILocation) { |
| 35 | + const expand = once(() => { |
| 36 | + this.set( |
| 37 | + location.callFrame.url, |
| 38 | + new Position( |
| 39 | + Math.max(0, location.callFrame.lineNumber), |
| 40 | + Math.max(0, location.callFrame.columnNumber), |
| 41 | + ), |
| 42 | + location, |
| 43 | + ); |
| 44 | + |
| 45 | + const src = location.src; |
| 46 | + if (!src || src.source.sourceReference !== 0 || !src.source.path) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + for (const path of getCandidateDiskPaths(rootPath, src.source)) { |
| 51 | + this.set( |
| 52 | + path, |
| 53 | + new Position(Math.max(0, src.lineNumber - 1), Math.max(0, src.columnNumber - 1)), |
| 54 | + location, |
| 55 | + ); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + this.addExpansionFn(getBasename(location.callFrame.url), expand); |
| 60 | + if (location.src?.source.path) { |
| 61 | + this.addExpansionFn(getBasename(location.src.source.path), expand); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Adds a function to expand performance data for the given location. |
| 67 | + */ |
| 68 | + private addExpansionFn(basename: string, expand: () => void) { |
| 69 | + let arr = this.basenamesToExpand.get(basename); |
| 70 | + if (!arr) { |
| 71 | + arr = []; |
| 72 | + this.basenamesToExpand.set(basename, arr); |
| 73 | + } |
| 74 | + |
| 75 | + arr.push(expand); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Adds a new code lens at the given location in the file. |
| 80 | + */ |
| 81 | + private set(file: string, position: Position, data: ILocation) { |
| 82 | + let list = this.data.get(lowerCaseInsensitivePath(file)); |
| 83 | + if (!list) { |
| 84 | + list = []; |
| 85 | + this.data.set(lowerCaseInsensitivePath(file), list); |
| 86 | + } |
| 87 | + |
| 88 | + let index = 0; |
| 89 | + while (index < list.length && list[index].position.line < position.line) { |
| 90 | + index++; |
| 91 | + } |
| 92 | + |
| 93 | + if (list[index]?.position.line === position.line) { |
| 94 | + const existing = list[index]; |
| 95 | + if (position.character < existing.position.character) { |
| 96 | + existing.position = new Position(position.line, position.character); |
| 97 | + } |
| 98 | + existing.data.aggregateTime += data.aggregateTime; |
| 99 | + existing.data.selfTime += data.selfTime; |
| 100 | + existing.data.ticks += data.ticks; |
| 101 | + } else { |
| 102 | + list.splice(index, 0, { |
| 103 | + position: new Position(position.line, position.character), |
| 104 | + data: { |
| 105 | + aggregateTime: data.aggregateTime, |
| 106 | + selfTime: data.selfTime, |
| 107 | + ticks: data.ticks, |
| 108 | + }, |
| 109 | + }); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Get all lenses for a file. Ordered by line number. |
| 115 | + */ |
| 116 | + public getLensesForFile(file: string): CodeLens[] { |
| 117 | + this.expandForFile(file); |
| 118 | + |
| 119 | + return ( |
| 120 | + this.data |
| 121 | + .get(lowerCaseInsensitivePath(file)) |
| 122 | + ?.map(({ position, data }) => { |
| 123 | + if (data.aggregateTime === 0 && data.selfTime === 0) { |
| 124 | + return []; |
| 125 | + } |
| 126 | + |
| 127 | + const range = new Range(position, position); |
| 128 | + return [ |
| 129 | + new CodeLens(range, { |
| 130 | + title: |
| 131 | + `${decimalFormat.format(data.selfTime / 1000)}ms Self Time, ` + |
| 132 | + `${decimalFormat.format(data.aggregateTime / 1000)}ms Total`, |
| 133 | + command: '', |
| 134 | + }), |
| 135 | + new CodeLens(range, { |
| 136 | + title: 'Clear', |
| 137 | + command: 'extension.jsProfileVisualizer.table.clearCodeLenses', |
| 138 | + }), |
| 139 | + ]; |
| 140 | + }) |
| 141 | + .reduce((acc, lenses) => [...acc, ...lenses], []) ?? [] |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + private expandForFile(file: string) { |
| 146 | + const basename = getBasename(file); |
| 147 | + const fns = this.basenamesToExpand.get(basename); |
| 148 | + if (!fns) { |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + for (const fn of fns) { |
| 153 | + fn(); |
| 154 | + } |
| 155 | + |
| 156 | + this.basenamesToExpand.delete(basename); |
| 157 | + } |
| 158 | +} |
0 commit comments