Files
mtusi2026lab7/student-tests/argon2_bench.mjs
T

32 lines
1.3 KiB
JavaScript

import assert from 'node:assert/strict';
import { hashPassword, checkPassword } from '../src/modules/passwords.ts';
const sample = 'synthetic-benchmark-password';
const hash = await hashPassword(sample);
assert.match(hash, /^\$argon2id\$v=19\$m=19456,t=2,p=1\$/);
assert.notEqual(await hashPassword(sample), hash);
assert.equal(await checkPassword(sample, hash), true);
assert.equal(await checkPassword('different-password', hash), false);
for (const concurrency of [1, 2]) {
const elapsed = [];
const cpu = process.cpuUsage();
const start = performance.now();
await Promise.all(Array.from({ length: concurrency }, async () => {
for (let i = 0; i < 25; i++) {
const at = performance.now();
assert.equal(await checkPassword(sample, hash), true);
elapsed.push(performance.now() - at);
}
}));
elapsed.sort((a, b) => a - b);
const used = process.cpuUsage(cpu);
console.log(JSON.stringify({
concurrency, runs: elapsed.length, p50_ms: Math.round(elapsed[Math.ceil(elapsed.length * .5) - 1]),
p95_ms: Math.round(elapsed[Math.ceil(elapsed.length * .95) - 1]),
p99_ms: Math.round(elapsed[Math.ceil(elapsed.length * .99) - 1]),
elapsed_ms: Math.round(performance.now() - start), cpu_ms: Math.round((used.user + used.system) / 1000),
max_rss_mib: Math.round(process.resourceUsage().maxRSS / 1024),
}));
}