Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

turn.ts

34 lines967 Bytes

1export function isMemoryInstrumented() {
2 return (
3 process.env.CODSPEED_ENV !== undefined &&
4 process.env.CODSPEED_RUNNER_MODE === 'memory'
5 )
6}
7​
8// CodSpeed calls beforeEach before its forced GC and measurement marker. End
9// the previous warmup's job so WeakRef targets can be released by that GC.
10export function endMemoryTurn() {
11 return new Promise<void>((resolve) => setImmediate(resolve))
13​
14/**
15 * Per-invocation bench hooks. CodSpeed runs them around every warmup
16 * invocation and the measured one, outside the measurement marker: the turn
17 * ends first, then the workload prepares fresh state before the forced GC.
18 */
19export function memoryBenchHooks(
20 workload: {
21 before?: () => Promise<void> | void
22 after?: () => Promise<void> | void
23 } = {},
24) {
25 return {
26 async beforeEach() {
27 if (isMemoryInstrumented()) {
28 await endMemoryTurn()
29 }
30 await workload.before?.()
31 },
32 afterEach: workload.after,
33 }