turn-bench.mjs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { resolve } from "node:path";
  2. import { pathToFileURL } from "node:url";
  3. import { TURN } from "../kit/turn.mjs";
  4. const argv = process.argv.slice(2);
  5. if (!argv.length || argv.includes("--help")) {
  6. console.log(`Time a 3D figure's runtime in Node: emit and order over 360 angles, CPU time, median of 7 rounds.
  7. node scripts/turn-bench.mjs examples/turning-dial/build.mjs [--rounds 7] [--unit 1]
  8. The build module must export T (from turning()); if it also exports follow(values), every angle is mapped through it.`);
  9. process.exit(argv.length ? 0 : 1);
  10. }
  11. const take = (flag, fallback) => {
  12. const at = argv.indexOf(flag);
  13. return at < 0 ? fallback : Number(argv[at + 1]);
  14. };
  15. const rounds = take("--rounds", 7);
  16. const unit = take("--unit", 1);
  17. const mod = await import(pathToFileURL(resolve(argv[0])).href);
  18. const T = mod.T;
  19. const follow = mod.follow ?? ((values) => values);
  20. const scene = TURN.prepare(T.data, { unit });
  21. const runs = scene.runs;
  22. const frames = runs.map((run) => TURN.frame(run));
  23. const turn = T.groups.find((g) => g.kind === "turn");
  24. const angles = Array.from({ length: 360 }, (_, i) => i + 0.37);
  25. const cams = angles.map((angle) => TURN.cams(scene, follow({ [turn.name]: angle })));
  26. for (let i = 0; i < 20; i++) for (const run of runs) TURN.emit(run, run.group >= 0 ? cams[i][run.group] : cams[i].world, frames[run.index]);
  27. const perPart = runs.map(() => []);
  28. const orderTimes = [];
  29. const frameTimes = [];
  30. const cpuFrames = [];
  31. for (let round = 0; round < rounds; round++) {
  32. const sums = new Float64Array(runs.length);
  33. let orderSum = 0;
  34. const cpu0 = process.cpuUsage();
  35. const wall0 = performance.now();
  36. const previous = scene.layers.map((layer) => layer.rest);
  37. for (let a = 0; a < angles.length; a++) {
  38. const c = cams[a];
  39. for (const run of runs) {
  40. const t0 = performance.now();
  41. TURN.emit(run, run.group >= 0 ? c[run.group] : c.world, frames[run.index]);
  42. sums[run.index] += performance.now() - t0;
  43. }
  44. const t1 = performance.now();
  45. scene.layers.forEach((layer, index) => {
  46. previous[index] = TURN.order(layer, frames, c, previous[index], {}, scene);
  47. });
  48. orderSum += performance.now() - t1;
  49. }
  50. const cpu = process.cpuUsage(cpu0);
  51. cpuFrames.push((cpu.user + cpu.system) / 1000 / angles.length);
  52. frameTimes.push((performance.now() - wall0) / angles.length);
  53. orderTimes.push(orderSum / angles.length);
  54. runs.forEach((run, index) => perPart[index].push(sums[index] / angles.length));
  55. }
  56. const median = (list) => list.slice().sort((x, y) => x - y)[Math.floor(list.length / 2)];
  57. const kinds = new Map();
  58. runs.forEach((run, index) => {
  59. const kind = run.kind === "tube" ? "tube chunk" : run.kind;
  60. if (!kinds.has(kind)) kinds.set(kind, []);
  61. kinds.get(kind).push(median(perPart[index]));
  62. });
  63. console.log(`turn-bench ${argv[0]} · ${runs.length} parts · ${angles.length} angles · median of ${rounds} rounds · unit ${unit}`);
  64. for (const [kind, list] of [...kinds].sort((a, b) => b[1].reduce((s, v) => s + v, 0) - a[1].reduce((s, v) => s + v, 0))) {
  65. const total = list.reduce((s, v) => s + v, 0);
  66. console.log(` ${kind.padEnd(11)} ${String(list.length).padStart(4)} parts · ${(total / list.length).toFixed(4)} ms per part · ${total.toFixed(3)} ms per frame · slowest ${Math.max(...list).toFixed(4)} ms`);
  67. }
  68. const slow = runs.map((run, index) => [run.name, median(perPart[index])]).sort((a, b) => b[1] - a[1]).slice(0, 6);
  69. console.log(` slowest parts: ${slow.map(([name, ms]) => `${name} ${ms.toFixed(3)}`).join(" · ")}`);
  70. console.log(` order ${median(orderTimes).toFixed(3)} ms per frame (${scene.layers.map((layer) => `${layer.name}: ${layer.parts.length} parts, ${layer.planar.length / 5} planes, ${layer.dynamic.length / 2} dynamic`).join("; ")})`);
  71. console.log(` frame ${median(frameTimes).toFixed(3)} ms wall · ${median(cpuFrames).toFixed(3)} ms CPU`);