main
1#!/usr/bin/env node
2/**
3 * Compare streaming SHA-1 backends on identical inputs (correctness first: digests must match).
4 * Usage: node test/bench-sha1-hashers.mjs [bytes=20971520]
5 * Reports ms and MB/s for: js Sha1, DigestStream (if present), crypto.subtle.digest one-shot.
6 */
7import { createHash } from 'node:crypto';
8import { Sha1, streamingSha1, toHex } from '../dist/sha1.js';
10const BYTES = Number(process.argv[2] || 20 * 1024 * 1024);
11const CHUNK = 64 * 1024;
12const pattern = new Uint8Array(CHUNK);
13for (let i = 0; i < CHUNK; i++) pattern[i] = (i * 17 + 3) & 0xff;
15function* chunks(n) {
16 let left = n;
17 while (left > 0) {
18 const take = Math.min(CHUNK, left);
19 yield pattern.subarray(0, take);
20 left -= take;
21 }
22}
24async function runJs(n) {
25 const h = new Sha1();
26 const t0 = performance.now();
27 for (const c of chunks(n)) h.update(c);
28 const hex = h.hex();
29 return { ms: performance.now() - t0, hex };
30}
32async function runStreaming(n) {
33 const h = streamingSha1();
34 const t0 = performance.now();
35 for (const c of chunks(n)) await h.update(c);
36 const hex = await h.hex();
37 return { ms: performance.now() - t0, hex, kind: h.kind };
38}
40async function runSubtle(n) {
41 // one-shot: concatenate (the crypto.subtle.digest shape — not a streaming option)
42 const buf = new Uint8Array(n);
43 let off = 0;
44 for (const c of chunks(n)) { buf.set(c, off); off += c.byteLength; }
45 const t0 = performance.now();
46 const dig = new Uint8Array(await crypto.subtle.digest('SHA-1', buf));
47 return { ms: performance.now() - t0, hex: toHex(dig) };
48}
50async function runNodeCrypto(n) {
51 const h = createHash('sha1');
52 const t0 = performance.now();
53 for (const c of chunks(n)) h.update(c);
54 const hex = h.digest('hex');
55 return { ms: performance.now() - t0, hex };
56}
58const ref = await runJs(BYTES);
59const stream = await runStreaming(BYTES);
60const node = await runNodeCrypto(BYTES);
61let subtle = null;
62try { subtle = await runSubtle(BYTES); } catch (e) { subtle = { error: String(e) }; }
64const mb = BYTES / (1024 * 1024);
65const row = (name, r) => {
66 if (r.error) return { name, error: r.error };
67 const ok = r.hex === ref.hex;
68 return { name, ms: +r.ms.toFixed(1), mib_s: +(mb / (r.ms / 1000)).toFixed(1), hex_ok: ok, kind: r.kind };
69};
71const out = {
72 bytes: BYTES,
73 results: [
74 row('js Sha1', ref),
75 row(`streamingSha1 (${stream.kind})`, stream),
76 row('node:crypto createHash', node),
77 subtle.error ? { name: 'crypto.subtle.digest (one-shot)', error: subtle.error } : row('crypto.subtle.digest (one-shot)', subtle),
78 ],
79};
80console.log(JSON.stringify(out, null, 2));
81if (out.results.some(r => r.hex_ok === false)) process.exit(2);
Diagnostics4 git calls · 222 ms
CallStatusSizeTime0222 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020038 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=38434 git.pkt.payload.max=38339 git.sideband.band1.bytes=38338 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=63 git.pack.bytes=38338 git.pack.ofs_delta=0 git.pack.ref_delta=8 git.pack.chain.max=8 git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77 git.pack.full=55Open pack map
fetch2001.1 KiB217 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=217 git.response.bytes=1162 git.response.cache=MISS git.response.duration_ms=217 git.fetch.wants=29f568c41fd6579ff89d9db37ed267efac3db2d6 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1162 git.pkt.payload.max=1141 git.sideband.band1.bytes=1140 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply.git
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e316ed7ef6cf5b
- Request
- 32a872d3…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format