mirror of
https://github.com/stablyai/orca.git
synced 2026-09-28 00:02:41 +00:00
77 lines
2.1 KiB
TypeScript
77 lines
2.1 KiB
TypeScript
import type {
|
|
PreparedCheckoutOutcome,
|
|
WorktreeCreateTiming,
|
|
WorktreeCreateTimingPhase
|
|
} from '../shared/worktree/create-types'
|
|
|
|
type TimingClock = () => number
|
|
|
|
export type WorktreeCreateTimingRecorder = {
|
|
time<T>(phase: string, operation: () => Promise<T>): Promise<T>
|
|
timeSync<T>(phase: string, operation: () => T): T
|
|
recordPreparedCheckout(outcome: PreparedCheckoutOutcome): void
|
|
finish(): WorktreeCreateTiming
|
|
}
|
|
|
|
function defaultClock(): number {
|
|
return performance.now()
|
|
}
|
|
|
|
function clampDuration(value: number): number {
|
|
return Number.isFinite(value) ? Math.max(0, value) : 0
|
|
}
|
|
|
|
function createPhase(
|
|
phase: string,
|
|
operationStartedAt: number,
|
|
operationEndedAt: number,
|
|
rootStartedAt: number
|
|
): WorktreeCreateTimingPhase {
|
|
return {
|
|
phase,
|
|
startedAtMs: clampDuration(operationStartedAt - rootStartedAt),
|
|
durationMs: clampDuration(operationEndedAt - operationStartedAt)
|
|
}
|
|
}
|
|
|
|
export function createWorktreeCreateTimingRecorder(
|
|
clock: TimingClock = defaultClock
|
|
): WorktreeCreateTimingRecorder {
|
|
const startedAt = clock()
|
|
const phases: WorktreeCreateTimingPhase[] = []
|
|
let preparedCheckout: PreparedCheckoutOutcome | undefined
|
|
|
|
const recordPhase = (phase: string, operationStartedAt: number): void => {
|
|
phases.push(createPhase(phase, operationStartedAt, clock(), startedAt))
|
|
}
|
|
|
|
return {
|
|
async time<T>(phase: string, operation: () => Promise<T>): Promise<T> {
|
|
const operationStartedAt = clock()
|
|
try {
|
|
return await operation()
|
|
} finally {
|
|
recordPhase(phase, operationStartedAt)
|
|
}
|
|
},
|
|
timeSync<T>(phase: string, operation: () => T): T {
|
|
const operationStartedAt = clock()
|
|
try {
|
|
return operation()
|
|
} finally {
|
|
recordPhase(phase, operationStartedAt)
|
|
}
|
|
},
|
|
recordPreparedCheckout(outcome: PreparedCheckoutOutcome): void {
|
|
preparedCheckout = outcome
|
|
},
|
|
finish() {
|
|
return {
|
|
totalDurationMs: clampDuration(clock() - startedAt),
|
|
phases: [...phases],
|
|
...(preparedCheckout ? { preparedCheckout } : {})
|
|
}
|
|
}
|
|
}
|
|
}
|