aboutsummaryrefslogtreecommitdiffstats
path: root/packages/benchmarks/src/utils.ts
blob: cfb00723facdabba5eb66ef94ef716d5402082ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
export function sleep(ms: number): Promise<void> {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

export async function waitUntil(
  fn: () => Promise<boolean>,
  description: string,
  timeoutMs = 60000,
  intervalMs = 1000,
): Promise<void> {
  const start = Date.now();
  while (Date.now() - start < timeoutMs) {
    try {
      if (await fn()) {
        return;
      }
    } catch {
      // Ignore and retry
    }
    await sleep(intervalMs);
  }
  throw new Error(`${description} timed out after ${timeoutMs}ms`);
}

export function formatNumber(num: number, fractionDigits = 2): string {
  return num.toFixed(fractionDigits);
}

export function formatMs(ms: number): string {
  return `${formatNumber(ms, ms >= 10 ? 1 : 2)} ms`;
}