bash-output-benchmark.cjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // node --expose-gc bash-output-benchmark.cjs <adapter.cjs> <concurrency:1|4> [bash|cmd] [expectedArtifactBytes:default=256MiB]
  2. // 每次调用使用独立父 Node;旧/新 bundle 使用同一脚本、Node、机器与请求参数。
  3. const assert = require("node:assert/strict");
  4. const fs = require("node:fs/promises");
  5. const { tmpdir } = require("node:os");
  6. const { join, resolve } = require("node:path");
  7. const { NodeExecutionAdapter } = require(resolve(process.argv[2]));
  8. const concurrency = Number(process.argv[3] || 1);
  9. assert([1, 4].includes(concurrency));
  10. const bytes = 256 * 1024 ** 2;
  11. // 旧 collector bundle 对照传 67108864;新直写实现应保留全部输出。
  12. const artifactLimit = 64 * 1024 ** 2;
  13. const expectedArtifactBytes = Number(process.argv[5] || bytes);
  14. const windows = process.platform === "win32";
  15. const cmd = windows && process.argv[4] === "cmd";
  16. const shell = {
  17. dialect: cmd ? "cmd" : windows ? "git-bash" : "posix",
  18. path: cmd ? process.env.ComSpec : windows ? "C:\\Program Files\\Git\\bin\\bash.exe" : "/bin/bash",
  19. display: { name: cmd ? "CMD" : "bash" },
  20. source: "user-config",
  21. };
  22. async function main() {
  23. const root = await fs.mkdtemp(join(tmpdir(), "zcode-bash-bench-"));
  24. const writer = join(root, "writer.cjs");
  25. await fs.writeFile(
  26. writer,
  27. `const fs=require('node:fs'),b=Buffer.alloc(65536,120);for(let i=0;i<${bytes / 65536};i++){let p=0;while(p<b.length)p+=fs.writeSync(i%2+1,b,p,b.length-p);}`,
  28. );
  29. const adapter = new NodeExecutionAdapter({
  30. outputRootDir: root,
  31. processEnv: { ...process.env, HOME: root },
  32. });
  33. const request = {
  34. command: {
  35. mode: "shell",
  36. shellProfile: "posix-bash",
  37. shellOverride: shell,
  38. command: cmd ? '"%BENCH_NODE%" "%BENCH_WRITER%"' : '"$BENCH_NODE" "$BENCH_WRITER"',
  39. },
  40. cwd: root,
  41. timeoutMs: 120_000,
  42. env: { set: { BENCH_NODE: process.execPath, BENCH_WRITER: writer } },
  43. outputLimit: {
  44. maxInlineBytes: 30_000,
  45. maxPersistedBytes: 5 * 1024 ** 3,
  46. maxArtifactBytes: artifactLimit,
  47. persistOutput: "on_truncate",
  48. },
  49. };
  50. try {
  51. await adapter.run({
  52. ...request,
  53. command: { ...request.command, command: cmd ? "echo warm" : "printf warm" },
  54. });
  55. global.gc?.();
  56. const baseline = process.memoryUsage();
  57. const peak = { ...baseline };
  58. const sample = () => {
  59. const usage = process.memoryUsage();
  60. for (const key of Object.keys(peak)) peak[key] = Math.max(peak[key], usage[key]);
  61. };
  62. const cpuStart = process.cpuUsage();
  63. const started = performance.now();
  64. const timer = setInterval(sample, 5);
  65. let results;
  66. try {
  67. results = await Promise.all(
  68. Array.from({ length: concurrency }, () =>
  69. adapter.runBashWithBackgroundLifecycle(
  70. { ...request },
  71. { mode: "auto_on_timeout" },
  72. { onEvent: () => undefined },
  73. ),
  74. ),
  75. );
  76. } finally {
  77. sample();
  78. clearInterval(timer);
  79. }
  80. const elapsedMs = performance.now() - started;
  81. const cpu = process.cpuUsage(cpuStart);
  82. for (const outcome of results) {
  83. assert.equal(outcome.kind, "foreground");
  84. const result = outcome.result;
  85. assert.equal(result.status, "completed");
  86. assert.equal(result.stdout.bytes, bytes);
  87. assert.equal(result.stdout.text, "x".repeat(30_000));
  88. assert.equal((await fs.stat(result.stdout.artifactPath)).size, expectedArtifactBytes);
  89. }
  90. console.log(
  91. JSON.stringify({
  92. node: process.version,
  93. platform: process.platform,
  94. arch: process.arch,
  95. concurrency,
  96. bytesPerCommand: bytes,
  97. expectedArtifactBytes,
  98. elapsedMs,
  99. cpuUserMs: cpu.user / 1000,
  100. cpuSystemMs: cpu.system / 1000,
  101. baseline,
  102. peak,
  103. delta: Object.fromEntries(Object.keys(peak).map((key) => [key, peak[key] - baseline[key]])),
  104. maxRssKiB: process.resourceUsage().maxRSS,
  105. }),
  106. );
  107. } finally {
  108. await adapter.close();
  109. await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 });
  110. }
  111. }
  112. const keepAlive = setInterval(() => {}, 1_000);
  113. main()
  114. .catch((error) => {
  115. console.error(error);
  116. process.exitCode = 1;
  117. })
  118. .finally(() => clearInterval(keepAlive));