test: add argon2id benchmark

This commit is contained in:
2026-11-30 16:36:14 +03:00
parent 1743870de7
commit e990ee31ee
+27
View File
@@ -0,0 +1,27 @@
import { performance } from 'perf_hooks';
import { hashPassword, verifyPassword } from './password';
async function measure(task: () => Promise<unknown>): Promise<string> {
const elapsed: number[] = [];
await task();
for (let sample = 0; sample < 20; sample++) {
const start = performance.now();
await task();
elapsed.push(performance.now() - start);
}
elapsed.sort((a, b) => a - b);
return 'p50=' + elapsed[9].toFixed(1) + ' ms, p95=' + elapsed[18].toFixed(1) + ' ms';
}
async function main(): Promise<void> {
const password = 'lab7-synthetic-password';
const stored = await hashPassword(password);
if (!stored.startsWith('$argon2id$v=19$m=19456,t=2,p=1$') || !await verifyPassword(stored, password)) {
throw new Error('Unexpected Argon2id parameters or verification result');
}
console.log('Argon2id m=19456 KiB, t=2, p=1; 20 sequential samples per operation');
console.log('hash: ' + await measure(() => hashPassword(password)));
console.log('verify: ' + await measure(() => verifyPassword(stored, password)));
}
main().catch(() => { process.exitCode = 1; });