|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------*/ |
| 4 | + |
| 5 | +import { CodeLens, Position, Range } from 'vscode'; |
| 6 | +import { ILocation } from '../cpu/model'; |
| 7 | +import { lowerCaseInsensitivePath } from '../path'; |
| 8 | +import { ProfileAnnotations } from '../profileAnnotations'; |
| 9 | +import { decimalFormat } from './display'; |
| 10 | + |
| 11 | +export interface IProfileInformation { |
| 12 | + selfTime: number; |
| 13 | + aggregateTime: number; |
| 14 | + ticks: number; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * A collection of profile data. Paths are expanded lazily, as doing so |
| 19 | + * up-front for very large profiles turned out to be costly (mainly in path) |
| 20 | + * manipulation. |
| 21 | + */ |
| 22 | +export class CpuProfileAnnotations extends ProfileAnnotations<IProfileInformation, ILocation> { |
| 23 | + /** |
| 24 | + * Adds a new code lens at the given location in the file. |
| 25 | + */ |
| 26 | + protected set(file: string, position: Position, data: ILocation) { |
| 27 | + let list = this.data.get(lowerCaseInsensitivePath(file)); |
| 28 | + if (!list) { |
| 29 | + list = []; |
| 30 | + this.data.set(lowerCaseInsensitivePath(file), list); |
| 31 | + } |
| 32 | + |
| 33 | + let index = 0; |
| 34 | + while (index < list.length && list[index].position.line < position.line) { |
| 35 | + index++; |
| 36 | + } |
| 37 | + |
| 38 | + if (list[index]?.position.line === position.line) { |
| 39 | + const existing = list[index]; |
| 40 | + if (position.character < existing.position.character) { |
| 41 | + existing.position = new Position(position.line, position.character); |
| 42 | + } |
| 43 | + existing.data.aggregateTime += data.aggregateTime; |
| 44 | + existing.data.selfTime += data.selfTime; |
| 45 | + existing.data.ticks += data.ticks; |
| 46 | + } else { |
| 47 | + list.splice(index, 0, { |
| 48 | + position: new Position(position.line, position.character), |
| 49 | + data: { |
| 50 | + aggregateTime: data.aggregateTime, |
| 51 | + selfTime: data.selfTime, |
| 52 | + ticks: data.ticks, |
| 53 | + }, |
| 54 | + }); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Get all lenses for a file. Ordered by line number. |
| 60 | + */ |
| 61 | + public getLensesForFile(file: string): CodeLens[] { |
| 62 | + this.expandForFile(file); |
| 63 | + |
| 64 | + return ( |
| 65 | + this.data |
| 66 | + .get(lowerCaseInsensitivePath(file)) |
| 67 | + ?.map(({ position, data }) => { |
| 68 | + if (data.aggregateTime === 0 && data.selfTime === 0) { |
| 69 | + return []; |
| 70 | + } |
| 71 | + |
| 72 | + const range = new Range(position, position); |
| 73 | + return [ |
| 74 | + new CodeLens(range, { |
| 75 | + title: |
| 76 | + `${decimalFormat.format(data.selfTime / 1000)}ms Self Time, ` + |
| 77 | + `${decimalFormat.format(data.aggregateTime / 1000)}ms Total`, |
| 78 | + command: '', |
| 79 | + }), |
| 80 | + new CodeLens(range, { |
| 81 | + title: 'Clear', |
| 82 | + command: 'extension.jsProfileVisualizer.table.clearCodeLenses', |
| 83 | + }), |
| 84 | + ]; |
| 85 | + }) |
| 86 | + .reduce((acc, lenses) => [...acc, ...lenses], []) ?? [] |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
0 commit comments