|
| 1 | +const { Suite } = require('benchmark') |
| 2 | +const { hashSync, hash, compare } = require('bcrypt') |
| 3 | +const { cpus } = require('os') |
| 4 | +const chalk = require('chalk') |
| 5 | +const { range } = require('lodash') |
| 6 | + |
| 7 | +const { hash: napiHash, hashSync: napiHashSync, verify } = require('../index') |
| 8 | + |
| 9 | +const hashRounds = [10, 12, 14] |
| 10 | +const parallel = cpus().length |
| 11 | + |
| 12 | +const password = 'node-rust-password' |
| 13 | + |
| 14 | +function runAsync(round) { |
| 15 | + const asyncHashSuite = new Suite(`Async hash round ${round}`) |
| 16 | + return new Promise((resolve) => { |
| 17 | + asyncHashSuite |
| 18 | + .add('@node-rs/bcrypt', { |
| 19 | + defer: true, |
| 20 | + fn: (deferred) => { |
| 21 | + Promise.all(range(parallel).map(() => napiHash(password, round))).then(() => { |
| 22 | + deferred.resolve() |
| 23 | + }) |
| 24 | + }, |
| 25 | + }) |
| 26 | + .add('node bcrypt', { |
| 27 | + defer: true, |
| 28 | + fn: (deferred) => { |
| 29 | + Promise.all(range(parallel).map(() => hash(password, round))).then(() => { |
| 30 | + deferred.resolve() |
| 31 | + }) |
| 32 | + }, |
| 33 | + }) |
| 34 | + .on('cycle', function (event) { |
| 35 | + event.target.hz = event.target.hz * parallel |
| 36 | + console.info(String(event.target)) |
| 37 | + }) |
| 38 | + .on('complete', function () { |
| 39 | + console.info(`${this.name} bench suite: Fastest is ${chalk.green(this.filter('fastest').map('name'))}`) |
| 40 | + resolve() |
| 41 | + }) |
| 42 | + .run({ async: true }) |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +hashRounds |
| 47 | + .reduce(async (acc, cur) => { |
| 48 | + await acc |
| 49 | + return runAsync(cur) |
| 50 | + }, Promise.resolve()) |
| 51 | + .then( |
| 52 | + () => |
| 53 | + new Promise((resolve) => { |
| 54 | + const suite = new Suite('Async verify') |
| 55 | + const hash = napiHashSync(password) |
| 56 | + suite |
| 57 | + .add({ |
| 58 | + name: '@node-rs/bcrypt', |
| 59 | + defer: true, |
| 60 | + fn: (deferred) => { |
| 61 | + Promise.all(range(parallel).map(() => verify(password, hash))).then(() => { |
| 62 | + deferred.resolve() |
| 63 | + }) |
| 64 | + }, |
| 65 | + }) |
| 66 | + .add({ |
| 67 | + name: 'node bcrypt', |
| 68 | + defer: true, |
| 69 | + fn: (deferred) => { |
| 70 | + Promise.all(range(parallel).map(() => compare(password, hash))).then(() => { |
| 71 | + deferred.resolve() |
| 72 | + }) |
| 73 | + }, |
| 74 | + }) |
| 75 | + .on('cycle', function (event) { |
| 76 | + event.target.hz = event.target.hz * parallel |
| 77 | + console.info(String(event.target)) |
| 78 | + }) |
| 79 | + .on('complete', function () { |
| 80 | + resolve() |
| 81 | + console.info(`${this.name} bench suite: Fastest is ${chalk.green(this.filter('fastest').map('name'))}`) |
| 82 | + }) |
| 83 | + .run() |
| 84 | + }), |
| 85 | + ) |
| 86 | + .then(() => { |
| 87 | + for (const round of hashRounds) { |
| 88 | + const syncHashSuite = new Suite(`Hash round ${round}`) |
| 89 | + syncHashSuite |
| 90 | + .add('@node-rs/bcrypt', () => { |
| 91 | + napiHashSync(password, round) |
| 92 | + }) |
| 93 | + .add('node bcrypt', () => { |
| 94 | + hashSync(password, round) |
| 95 | + }) |
| 96 | + .on('cycle', function (event) { |
| 97 | + console.info(String(event.target)) |
| 98 | + }) |
| 99 | + .on('complete', function () { |
| 100 | + console.info(`${this.name} bench suite: Fastest is ${chalk.green(this.filter('fastest').map('name'))}`) |
| 101 | + }) |
| 102 | + .run() |
| 103 | + } |
| 104 | + }) |
0 commit comments