diff --git a/src/auth/benchmark.ts b/src/auth/benchmark.ts new file mode 100644 index 0000000..7659245 --- /dev/null +++ b/src/auth/benchmark.ts @@ -0,0 +1,27 @@ +import { performance } from 'perf_hooks'; +import { hashPassword, verifyPassword } from './password'; + +async function measure(task: () => Promise): Promise { + 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 { + 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; });