Repository navigation
Expand file tree
/
Copy pathbench.mjs
More file actions
95 lines (90 loc) · 4.84 KB
/
Copy pathbench.mjs
File metadata and controls
95 lines (90 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Performance measurements for @dogejs/node (run after `npm run build`): node bench.mjs
// Uses the 20 real mainnet headers from test/fixtures and a synthetic regtest chain.
import { readFileSync, mkdtempSync, rmSync } from 'node:fs';
import { tmpdir, cpus } from 'node:os';
import { join } from 'node:path';
import {
parseBlockHeader, hexToBytes, checkAuxPowProofOfWork, mainnetParams, powHash, powHashAsync, powPreimage,
HeaderChain, regtestParams, getNextWorkRequired, getConsensus, headerHash, compactToTarget, hashToBigInt,
serializePureHeader,
} from './dist/index.mjs';
const blocks = JSON.parse(readFileSync('test/fixtures/mainnet-blocks.json', 'utf8')).blocks;
const headers = blocks.map((b) => parseBlockHeader(hexToBytes(b.rawHeader)));
const aux = headers.filter((h) => h.auxpow);
const plain = headers.filter((h) => !h.auxpow);
function rate(label, n, fn) {
const t = process.hrtime.bigint();
fn();
const s = Number(process.hrtime.bigint() - t) / 1e9;
console.log(`${label}: ${Math.round(n / s)} /s (${n} in ${s.toFixed(2)} s)`);
return n / s;
}
console.log(`node ${process.version}, ${cpus().length} CPU(s): ${cpus()[0]?.model}`);
const N = 3000;
rate('scrypt PoW hash, node:crypto, one thread', N, () => { for (let i = 0; i < N; i++) powHash(powPreimage(plain[i % plain.length])); });
rate('full check of real plain headers (scrypt + target)', N, () => { for (let i = 0; i < N; i++) checkAuxPowProofOfWork(plain[i % plain.length], mainnetParams); });
rate('full check of real AuxPoW headers (parent scrypt + AuxPoW merkle checks)', N, () => { for (let i = 0; i < N; i++) checkAuxPowProofOfWork(aux[i % aux.length], mainnetParams); });
rate('AuxPoW merkle/coinbase checks only (PoW hash precomputed)', N * 10, () => {
const pows = aux.map((h) => powHash(powPreimage(h)));
for (let i = 0; i < N * 10; i++) checkAuxPowProofOfWork(aux[i % aux.length], mainnetParams, pows[i % aux.length]);
});
{
const t = process.hrtime.bigint();
const M = 8000;
await Promise.all(Array.from({ length: M }, (_, i) => powHashAsync(powPreimage(headers[i % headers.length]))));
const s = Number(process.hrtime.bigint() - t) / 1e9;
console.log(`scrypt PoW hash, node:crypto on the libuv pool (UV_THREADPOOL_SIZE=${process.env.UV_THREADPOOL_SIZE ?? 4}): ${Math.round(M / s)} /s`);
}
// Synthetic regtest chain: mine 6000 headers, then measure acceptHeaders in 2000 batches.
const now = () => 2_000_000_000;
const miner = HeaderChain.memory(regtestParams, { now });
const list = [];
let prev = { hash: miner.tip.hash, height: 0, time: miner.tip.header.time, bits: miner.tip.header.bits };
const views = [{ height: 0, time: prev.time, bits: prev.bits }];
for (let i = 1; i <= 6000; i++) {
const time = prev.time + 3;
const bits = getNextWorkRequired(prev, time, getConsensus(regtestParams, i), regtestParams, (h) => views[h]);
const target = compactToTarget(bits).target;
let h;
for (let nonce = 0; ; nonce++) {
h = { version: 0x00620004, prevHash: prev.hash, merkleRoot: new Uint8Array(32).fill(i & 255), time, bits, nonce };
if (hashToBigInt(powHash(serializePureHeader(h))) <= target) break;
}
list.push(h);
views.push({ height: i, time, bits });
prev = { hash: headerHash(h), height: i, time, bits };
}
for (const [label, mk] of [
['acceptHeaders, memory chain, full verification', () => HeaderChain.memory(regtestParams, { now })],
['acceptHeaders, on-disk store (fsync per batch), full verification', () => HeaderChain.open(regtestParams, mkdtempSync(join(tmpdir(), 'dj-bench-')), { now })],
]) {
const c = mk();
const t = process.hrtime.bigint();
for (let i = 0; i < list.length; i += 2000) {
const r = await c.acceptHeaders(list.slice(i, i + 2000));
if (r.error) throw r.error;
}
const s = Number(process.hrtime.bigint() - t) / 1e9;
console.log(`${label}: ${Math.round(list.length / s)} headers/s`);
if (c.lastOpen) {
const dir = c.lastOpen.store.dir;
c.close();
const t2 = process.hrtime.bigint();
const c2 = HeaderChain.open(regtestParams, dir, { now });
console.log(` reopen ${c2.height + 1} stored headers: ${(Number(process.hrtime.bigint() - t2) / 1e6).toFixed(0)} ms`);
c2.close();
rmSync(dir, { recursive: true, force: true });
}
}
{
const cpHash = Buffer.from(headerHash(list[list.length - 1])).reverse().toString('hex');
const p = { ...regtestParams, checkpoints: [regtestParams.checkpoints[0], { height: list.length, hash: cpHash }] };
const c = HeaderChain.memory(p, { now, trustCheckpoints: true });
const t = process.hrtime.bigint();
for (let i = 0; i < list.length; i += 2000) {
const r = await c.acceptHeaders(list.slice(i, i + 2000));
if (r.error) throw r.error;
}
const s = Number(process.hrtime.bigint() - t) / 1e9;
console.log(`acceptHeaders, memory chain, --trust-checkpoints (no scrypt below the checkpoint): ${Math.round(list.length / s)} headers/s, verified height ${c.verifiedHeight}`);
}