|
| 1 | +import { writeFileSync } from "node:fs"; |
| 2 | +import { createRequire } from "node:module"; |
| 3 | +import { dirname, resolve } from "node:path"; |
| 4 | +import { performance } from "node:perf_hooks"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | + |
| 7 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 8 | +const coreDir = resolve(__dirname, ".."); |
| 9 | +const outputPath = resolve(coreDir, "planning", "checksums.md"); |
| 10 | + |
| 11 | +const require = createRequire(import.meta.url); |
| 12 | +const checksum = require(resolve(coreDir, "dist-cjs/submodules/checksum/index.js")); |
| 13 | +const { Crc32Js, Crc32Node, Sha256Js, Sha256Node, Md5Js, Md5Node } = checksum; |
| 14 | +const { AwsCrc32 } = require("@aws-crypto/crc32"); |
| 15 | +const { Sha256: AwsSha256 } = require("@aws-crypto/sha256-js"); |
| 16 | + |
| 17 | +const SIZES = [32, 256, 1024, 64 * 1024, 1024 * 1024, 10 * 1024 * 1024, 50 * 1024 * 1024]; |
| 18 | +const ITERATIONS = [10000, 5000, 2000, 200, 20, 5, 3 ]; |
| 19 | + |
| 20 | +function generateData(size) { |
| 21 | + const buf = new Uint8Array(size); |
| 22 | + for (let i = 0; i < size; ++i) { |
| 23 | + buf[i] = i & 0xff; |
| 24 | + } |
| 25 | + return buf; |
| 26 | +} |
| 27 | + |
| 28 | +const WARMUP_DATA = generateData(64 * 1024); |
| 29 | + |
| 30 | +/** |
| 31 | + * Runs algo on 64KB data for 1 second to ensure V8 optimization. |
| 32 | + */ |
| 33 | +async function warmup(Impl, ...ctorArgs) { |
| 34 | + const deadline = performance.now() + 1000; |
| 35 | + while (performance.now() < deadline) { |
| 36 | + const h = new Impl(...ctorArgs); |
| 37 | + h.update(WARMUP_DATA); |
| 38 | + await h.digest(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +async function bench(Impl, data, iterations, ...ctorArgs) { |
| 43 | + const start = performance.now(); |
| 44 | + for (let i = 0; i < iterations; ++i) { |
| 45 | + const h = new Impl(...ctorArgs); |
| 46 | + h.update(data); |
| 47 | + await h.digest(); |
| 48 | + } |
| 49 | + return performance.now() - start; |
| 50 | +} |
| 51 | + |
| 52 | +function formatSize(bytes) { |
| 53 | + if (bytes >= 1024 * 1024) return `${bytes / (1024 * 1024)}MB`; |
| 54 | + if (bytes >= 1024) return `${bytes / 1024}KB`; |
| 55 | + return `${bytes}B`; |
| 56 | +} |
| 57 | + |
| 58 | +function formatThroughput(bytesPerSec) { |
| 59 | + if (bytesPerSec >= 1024 * 1024 * 1024) return `${(bytesPerSec / (1024 * 1024 * 1024)).toFixed(2)} GB/s`; |
| 60 | + if (bytesPerSec >= 1024 * 1024) return `${(bytesPerSec / (1024 * 1024)).toFixed(1)} MB/s`; |
| 61 | + return `${(bytesPerSec / 1024).toFixed(1)} KB/s`; |
| 62 | +} |
| 63 | + |
| 64 | +function alignedTable(headers, rows) { |
| 65 | + const widths = headers.map((h, i) => Math.max(h.length, ...rows.map((r) => r[i].length))); |
| 66 | + const pad = (s, i) => s.padEnd(widths[i]); |
| 67 | + const sep = widths.map((w) => "-".repeat(w)); |
| 68 | + const out = []; |
| 69 | + out.push("| " + headers.map(pad).join(" | ") + " |"); |
| 70 | + out.push("| " + sep.join(" | ") + " |"); |
| 71 | + for (const row of rows) { |
| 72 | + out.push("| " + row.map(pad).join(" | ") + " |"); |
| 73 | + } |
| 74 | + return out; |
| 75 | +} |
| 76 | + |
| 77 | +async function runSection(label, impls, extraCtorArgs) { |
| 78 | + const rows = []; |
| 79 | + for (let si = 0; si < SIZES.length; ++si) { |
| 80 | + const size = SIZES[si]; |
| 81 | + const data = generateData(size); |
| 82 | + const iters = ITERATIONS[si]; |
| 83 | + const row = [formatSize(size)]; |
| 84 | + const parts = []; |
| 85 | + for (const { name, Impl, ctorArgs } of impls) { |
| 86 | + const ms = await bench(Impl, data, iters, ...(ctorArgs || [])); |
| 87 | + const tp = formatThroughput((size * iters * 1000) / ms); |
| 88 | + row.push(tp); |
| 89 | + parts.push(`${name}=${tp}`); |
| 90 | + } |
| 91 | + rows.push(row); |
| 92 | + console.log(` ${label} ${formatSize(size)}: ${parts.join(", ")}`); |
| 93 | + } |
| 94 | + return rows; |
| 95 | +} |
| 96 | + |
| 97 | +// --- Benchmark definitions --- |
| 98 | + |
| 99 | +const sections = [ |
| 100 | + { |
| 101 | + title: "CRC-32", |
| 102 | + headers: ["Size", "Crc32Js (JS)", "Crc32Node (node:zlib)", "@aws-crypto/crc32"], |
| 103 | + impls: [ |
| 104 | + { name: "Crc32Js", Impl: Crc32Js }, |
| 105 | + { name: "Crc32Node", Impl: Crc32Node }, |
| 106 | + { name: "@aws-crypto/crc32", Impl: AwsCrc32 }, |
| 107 | + ], |
| 108 | + }, |
| 109 | + { |
| 110 | + title: "SHA-256 (hash)", |
| 111 | + headers: ["Size", "Sha256Js (JS)", "Sha256Node (node:crypto)", "@aws-crypto/sha256-js"], |
| 112 | + impls: [ |
| 113 | + { name: "Sha256Js", Impl: Sha256Js }, |
| 114 | + { name: "Sha256Node", Impl: Sha256Node }, |
| 115 | + { name: "@aws-crypto/sha256-js", Impl: AwsSha256 }, |
| 116 | + ], |
| 117 | + }, |
| 118 | + { |
| 119 | + title: "SHA-256 (HMAC)", |
| 120 | + headers: ["Size", "Sha256Js (JS)", "Sha256Node (node:crypto)", "@aws-crypto/sha256-js"], |
| 121 | + impls: [ |
| 122 | + { name: "Sha256Js", Impl: Sha256Js, ctorArgs: [generateData(32)] }, |
| 123 | + { name: "Sha256Node", Impl: Sha256Node, ctorArgs: [generateData(32)] }, |
| 124 | + { name: "@aws-crypto/sha256-js", Impl: AwsSha256, ctorArgs: [generateData(32)] }, |
| 125 | + ], |
| 126 | + }, |
| 127 | + { |
| 128 | + title: "MD5", |
| 129 | + note: "Md5Js vs old @smithy/md5-js (unrolled rounds): 0.9x (32B), 2.2x (256B), 2.2x (1KB), 2.1x (64KB), 2.1x (1MB)", |
| 130 | + headers: ["Size", "Md5Js (JS)", "Md5Node (node:crypto)"], |
| 131 | + impls: [ |
| 132 | + { name: "Md5Js", Impl: Md5Js }, |
| 133 | + { name: "Md5Node", Impl: Md5Node }, |
| 134 | + ], |
| 135 | + }, |
| 136 | +]; |
| 137 | + |
| 138 | +// --- Run --- |
| 139 | + |
| 140 | +console.log("Warming up..."); |
| 141 | +const allImpls = sections.flatMap((s) => s.impls); |
| 142 | +for (const { name, Impl, ctorArgs } of allImpls) { |
| 143 | + process.stdout.write(` ${name}...`); |
| 144 | + await warmup(Impl, ...(ctorArgs || [])); |
| 145 | + console.log(" done"); |
| 146 | +} |
| 147 | +await new Promise((r) => setTimeout(r, 1000)); |
| 148 | + |
| 149 | +console.log("Running checksum benchmarks...\n"); |
| 150 | + |
| 151 | +const lines = [ |
| 152 | + "# Checksum Benchmarks\n", |
| 153 | + `Platform: Node.js ${process.version} (${process.platform} ${process.arch})\n`, |
| 154 | + `Date: ${new Date().toISOString()}\n`, |
| 155 | + `Iterations per size: [${ITERATIONS.join(", ")}], Warmup: 1s per algo\n`, |
| 156 | +]; |
| 157 | + |
| 158 | +for (const { title, headers, impls, note } of sections) { |
| 159 | + lines.push(`\n## ${title}\n`); |
| 160 | + if (note) { |
| 161 | + lines.push(`${note}\n`); |
| 162 | + } |
| 163 | + const rows = await runSection(title, impls); |
| 164 | + lines.push(...alignedTable(headers, rows)); |
| 165 | +} |
| 166 | + |
| 167 | +const md = lines.join("\n") + "\n"; |
| 168 | +writeFileSync(outputPath, md); |
| 169 | +console.log(`\nResults written to ${outputPath}`); |
0 commit comments