-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf - track times in stats object, rewrite some code away from lodash
- Loading branch information
Showing
9 changed files
with
109 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* function-times.ts | ||
* | ||
* Copyright (C) 2025 Posit Software, PBC | ||
*/ | ||
|
||
import { Stats } from "./stats.ts"; | ||
|
||
export const functionTimes: Record<string, Stats> = {}; | ||
|
||
// deno-lint-ignore no-explicit-any | ||
export const makeTimedFunction = <T extends (...args: any[]) => any>( | ||
name: string, | ||
fn: T, | ||
): T => { | ||
if (Deno.env.get("QUARTO_REPORT_PERFORMANCE_METRICS") === undefined) { | ||
return fn; | ||
} | ||
functionTimes[name] = new Stats(); | ||
return function (...args: Parameters<T>): ReturnType<T> { | ||
const start = performance.now(); | ||
try { | ||
const result = fn(...args); | ||
return result; | ||
} finally { | ||
const end = performance.now(); | ||
functionTimes[name].add(end - start); | ||
} | ||
} as T; | ||
}; | ||
|
||
export const timeCall = <T>(callback: () => T): { result: T; time: number } => { | ||
const start = performance.now(); | ||
const result = callback(); | ||
const end = performance.now(); | ||
return { result, time: end - start }; | ||
}; | ||
|
||
export const makeTimedFunctionAsync = < | ||
// deno-lint-ignore no-explicit-any | ||
T extends (...args: any[]) => Promise<any>, | ||
>( | ||
name: string, | ||
fn: T, | ||
): T => { | ||
if (Deno.env.get("QUARTO_REPORT_PERFORMANCE_METRICS") === undefined) { | ||
return fn; | ||
} | ||
functionTimes[name] = new Stats(); | ||
return async function (...args: Parameters<T>): Promise<ReturnType<T>> { | ||
const start = performance.now(); | ||
try { | ||
const result = await fn(...args); | ||
return result; | ||
} finally { | ||
const end = performance.now(); | ||
functionTimes[name].add(end - start); | ||
} | ||
} as T; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters