bench-test-suite.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Full-suite timing harness for the test-speed research in ../../perf/test-suite.md.
  2. // Use this for periodic sanity checks; use profile-test-files.ts for discovery.
  3. // Env: BENCH_WARMUPS=0 BENCH_RUNS=1 bun run bench:test
  4. const warmups = Number(Bun.env.BENCH_WARMUPS ?? 0)
  5. const runs = Number(Bun.env.BENCH_RUNS ?? 1)
  6. const timings: number[] = []
  7. if (!Number.isInteger(warmups) || warmups < 0) {
  8. console.error("BENCH_WARMUPS must be a non-negative integer")
  9. process.exit(1)
  10. }
  11. if (!Number.isInteger(runs) || runs < 1) {
  12. console.error("BENCH_RUNS must be a positive integer")
  13. process.exit(1)
  14. }
  15. for (const index of Array.from({ length: warmups + runs }, (_, index) => index)) {
  16. const measured = index >= warmups
  17. const label = measured ? `run ${index - warmups + 1}/${runs}` : `warmup ${index + 1}/${warmups}`
  18. const start = performance.now()
  19. console.log(`bench:test ${label}`)
  20. const proc = Bun.spawn(["bun", "test", "--timeout", "30000"], {
  21. cwd: import.meta.dir + "/..",
  22. stdout: "inherit",
  23. stderr: "inherit",
  24. env: Bun.env,
  25. })
  26. const exitCode = await proc.exited
  27. if (exitCode !== 0) {
  28. console.error(`bench:test failed during ${label} with exit code ${exitCode}`)
  29. process.exit(exitCode)
  30. }
  31. const seconds = (performance.now() - start) / 1000
  32. console.log(`bench:test ${label} ${seconds.toFixed(3)}s`)
  33. if (measured) timings.push(seconds)
  34. }
  35. const sorted = timings.toSorted((a, b) => a - b)
  36. const median = sorted[Math.floor(sorted.length / 2)]
  37. const mean = timings.reduce((sum, timing) => sum + timing, 0) / timings.length
  38. const best = sorted[0] ?? median
  39. const worst = sorted.at(-1) ?? median
  40. console.log(
  41. `bench:test median=${median.toFixed(3)}s mean=${mean.toFixed(3)}s best=${best.toFixed(3)}s worst=${worst.toFixed(3)}s`,
  42. )
  43. console.log(`METRIC test_suite_seconds=${median.toFixed(3)}`)
  44. console.log(`METRIC test_suite_best_seconds=${best.toFixed(3)}`)
  45. console.log(`METRIC test_suite_worst_seconds=${worst.toFixed(3)}`)