| 23 | * A `PerfRecorder` that actively tracks performance statistics. |
| 24 | */ |
| 25 | export class ActivePerfRecorder implements PerfRecorder { |
| 26 | private counters: number[]; |
| 27 | private phaseTime: number[]; |
| 28 | private bytes: number[]; |
| 29 | |
| 30 | private currentPhase = PerfPhase.Unaccounted; |
| 31 | private currentPhaseEntered: HrTime; |
| 32 | |
| 33 | /** |
| 34 | * Creates an `ActivePerfRecorder` with its zero point set to the current time. |
| 35 | */ |
| 36 | static zeroedToNow(): ActivePerfRecorder { |
| 37 | return new ActivePerfRecorder(mark()); |
| 38 | } |
| 39 | |
| 40 | private constructor(private zeroTime: HrTime) { |
| 41 | this.currentPhaseEntered = this.zeroTime; |
| 42 | this.counters = Array(PerfEvent.LAST).fill(0); |
| 43 | this.phaseTime = Array(PerfPhase.LAST).fill(0); |
| 44 | this.bytes = Array(PerfCheckpoint.LAST).fill(0); |
| 45 | |
| 46 | // Take an initial memory snapshot before any other compilation work begins. |
| 47 | this.memory(PerfCheckpoint.Initial); |
| 48 | } |
| 49 | |
| 50 | reset(): void { |
| 51 | this.counters = Array(PerfEvent.LAST).fill(0); |
| 52 | this.phaseTime = Array(PerfPhase.LAST).fill(0); |
| 53 | this.bytes = Array(PerfCheckpoint.LAST).fill(0); |
| 54 | this.zeroTime = mark(); |
| 55 | this.currentPhase = PerfPhase.Unaccounted; |
| 56 | this.currentPhaseEntered = this.zeroTime; |
| 57 | } |
| 58 | |
| 59 | memory(after: PerfCheckpoint): void { |
| 60 | this.bytes[after] = process.memoryUsage().heapUsed; |
| 61 | } |
| 62 | |
| 63 | phase(phase: PerfPhase): PerfPhase { |
| 64 | const previous = this.currentPhase; |
| 65 | this.phaseTime[this.currentPhase] += timeSinceInMicros(this.currentPhaseEntered); |
| 66 | this.currentPhase = phase; |
| 67 | this.currentPhaseEntered = mark(); |
| 68 | return previous; |
| 69 | } |
| 70 | |
| 71 | inPhase<T>(phase: PerfPhase, fn: () => T): T { |
| 72 | const previousPhase = this.phase(phase); |
| 73 | try { |
| 74 | return fn(); |
| 75 | } finally { |
| 76 | this.phase(previousPhase); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | eventCount(counter: PerfEvent, incrementBy: number = 1): void { |
| 81 | this.counters[counter] += incrementBy; |
| 82 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…