profile-typecheck-packages.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env bun
  2. import path from "path"
  3. const root = path.resolve(import.meta.dir, "..")
  4. const proc = Bun.spawn(
  5. [
  6. "bun",
  7. "turbo",
  8. "typecheck",
  9. "--concurrency=1",
  10. "--force",
  11. "--continue=always",
  12. "--summarize",
  13. "--output-logs=errors-only",
  14. ],
  15. {
  16. cwd: root,
  17. stdout: "pipe",
  18. stderr: "pipe",
  19. },
  20. )
  21. const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited])
  22. const output = stdout + stderr
  23. if (exitCode !== 0) {
  24. process.stdout.write(stdout)
  25. process.stderr.write(stderr)
  26. process.exit(exitCode)
  27. }
  28. const summary = output.match(/Summary:\s+(.+\.json)/)?.[1]?.trim()
  29. if (!summary) {
  30. process.stdout.write(stdout)
  31. process.stderr.write(stderr)
  32. throw new Error("Turbo did not report a run summary")
  33. }
  34. const report = (await Bun.file(summary).json()) as {
  35. tasks: Array<{
  36. taskId: string
  37. execution: { startTime: number; endTime: number; exitCode: number } | null
  38. }>
  39. }
  40. const tasks = report.tasks
  41. .flatMap((task) =>
  42. task.execution
  43. ? [
  44. {
  45. task: task.taskId.replace(/#typecheck$/, ""),
  46. durationMs: task.execution.endTime - task.execution.startTime,
  47. },
  48. ]
  49. : [],
  50. )
  51. .sort((a, b) => b.durationMs - a.durationMs)
  52. const total = tasks.reduce((duration, task) => duration + task.durationMs, 0)
  53. const width = Math.max(...tasks.map((task) => task.task.length), "Package".length)
  54. console.log(`Package${" ".repeat(width - "Package".length)} Time Share`)
  55. tasks.forEach((task) => {
  56. const duration = `${(task.durationMs / 1000).toFixed(2)}s`.padStart(7)
  57. const share = `${((task.durationMs / total) * 100).toFixed(1)}%`.padStart(6)
  58. console.log(`${task.task.padEnd(width)} ${duration} ${share}`)
  59. })
  60. console.log(`\nTotal serial task time: ${(total / 1000).toFixed(2)}s`)
  61. console.log(`Turbo summary: ${path.relative(root, summary)}`)