Skip to content

Commit

Permalink
bench(bcrypt): add @cwasm/openbsd-bcrypt and @cwasm/openwall-bcrypt t…
Browse files Browse the repository at this point in the history
…o benchmark (#649)

* Add @cwasm/openbsd-bcrypt and @cwasm/openwall-bcrypt to benchmark

* tinybench

---------

Co-authored-by: LongYinan <[email protected]>
  • Loading branch information
LinusU and Brooooooklyn authored Oct 26, 2024
1 parent 5232a7e commit 50be77d
Show file tree
Hide file tree
Showing 12 changed files with 482 additions and 241 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ resolver = "2"
blowfish = { version = "0.9", features = ["bcrypt"] }
crc32c = { version = "0.6" }
crc32fast = { version = "1.4", features = ["nightly"] }
getrandom = "0.2"
global_alloc = { path = "./crates/alloc" }
indexmap = { version = "2", features = ["serde"] }
jieba-rs = { version = "0.7", default-features = false, features = ["tfidf", "textrank"] }
Expand All @@ -20,6 +19,7 @@ resolver = "2"
napi-derive = { version = "3.0.0-alpha", default-features = false, features = ["type-def"] }
once_cell = "1"
quickcheck = "1.0"
rand = "0.8"
rand_core = { version = "0.6", features = ["getrandom"] }
serde = "1.0"
serde_json = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/bcrypt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ crate-type = ["cdylib"]
base64 = { workspace = true }
bcrypt = { workspace = true }
blowfish = { workspace = true }
getrandom = { workspace = true }
global_alloc = { workspace = true }
napi = { workspace = true, default-features = false, features = ["napi3"] }
napi-derive = { workspace = true }
rand = { workspace = true }

[dev-dependencies]
quickcheck = { workspace = true }
Expand Down
91 changes: 47 additions & 44 deletions packages/bcrypt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@

🚀 Fastest bcrypt in Node.js

## Support matrix

| | node12 | node14 | node16 | node18 |
| ---------------- | ------ | ------ | ------ | ------ |
| Windows x64 |||||
| Windows x32 |||||
| Windows arm64 |||||
| macOS x64 |||||
| macOS arm64 |||||
| Linux x64 gnu |||||
| Linux x64 musl |||||
| Linux arm gnu |||||
| Linux arm64 gnu |||||
| Linux arm64 musl |||||
| Android arm64 |||||
| Android armv7 |||||
| FreeBSD x64 |||||

## Usage

```typescript
Expand Down Expand Up @@ -55,33 +37,54 @@ export function genSalt(round: number, version?: Version): Promise<string>
## Bench

```
Model Name: MacBook Pro
Model Identifier: MacBookPro15,1
Processor Name: Intel Core i7
Processor Speed: 2.6 GHz
Number of Processors: 1
Total Number of Cores: 6
L2 Cache (per Core): 256 KB
L3 Cache: 12 MB
Hyper-Threading Technology: Enabled
Memory: 16 GB
,MMMM. Host - xxxxxxxxxxxxxxxxxxxxxxx
.MMMMMM Machine - Mac15,9
MMMMM, Kernel - 24.0.0
.;MMMMM:' MMMMMMMMMM;. OS - macOS 15.0.1 Sequoia
MMMMMMMMMMMMNWMMMMMMMMMMM: DE - Aqua
.MMMMMMMMMMMMMMMMMMMMMMMMWM. WM - Quartz Compositor
MMMMMMMMMMMMMMMMMMMMMMMMM. Packages - 194 (Homebrew), 32 (cargo)
;MMMMMMMMMMMMMMMMMMMMMMMM: Shell - zsh
:MMMMMMMMMMMMMMMMMMMMMMMM: Terminal - warpterminal (Version v0.2024.10.23.14.49.stable_00)
.MMMMMMMMMMMMMMMMMMMMMMMMM. Resolution - 5120x2880@160fps (as 2560x1440)
MMMMMMMMMMMMMMMMMMMMMMMMMMM. 2992x1934@120fps (as 1496x967)
.MMMMMMMMMMMMMMMMMMMMMMMMMM. 2232x1512@60fps (as 1116x756)
MMMMMMMMMMMMMMMMMMMMMMMM Uptime - 1d 2h 32m
;MMMMMMMMMMMMMMMMMMMM. CPU - Apple M3 Max (16)
.MMMM,. .MMMM,. CPU Load - 16%
Memory - 50.1 GB / 134.2 GB
Battery - 78% & Discharging
Disk Space - 624.0 GB / 994.7 GB
```
```text
@node-rs/bcrypt x 18.55 ops/sec ±1.51% (12 runs sampled)
node bcrypt x 16.37 ops/sec ±2.94% (11 runs sampled)
bcryptjs x 3.61 ops/sec ±4.86% (6 runs sampled)
Async hash round 12 bench suite: Fastest is @node-rs/bcrypt
@node-rs/bcrypt x 18.51 ops/sec ±1.60% (12 runs sampled)
node bcrypt x 16.51 ops/sec ±1.51% (11 runs sampled)
bcryptjs x 3.71 ops/sec ±2.23% (6 runs sampled)
Async verify bench suite: Fastest is @node-rs/bcrypt
@node-rs/bcrypt x 4.68 ops/sec ±4.69% (16 runs sampled)
node bcrypt x 3.94 ops/sec ±6.56% (14 runs sampled)
bcryptjs x 3.56 ops/sec ±2.04% (13 runs sampled)
Hash round 12 bench suite: Fastest is @node-rs/bcrypt
@node-rs/bcrypt x 521,917 ops/sec ±2.27% (82 runs sampled)
node bcrypt x 252,333 ops/sec ±2.05% (82 runs sampled)
bcryptjs x 110,578 ops/sec ±2.37% (82 runs sampled)
genSaltSync bench suite: Fastest is @node-rs/bcrypt
❯ yarn workspace @node-rs/bcrypt bench
Hash benchmark
┌─────────┬───────────────────┬─────────┬───────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼───────────────────┼─────────┼───────────────────┼──────────┼─────────┤
│ 0 │ '@node-rs/bcrypt' │ '20' │ 49142200.63636367 │ '±1.08%' │ 11 │
│ 1 │ 'node bcrypt' │ '20' │ 49259219.81818187 │ '±1.35%' │ 11 │
│ 2 │ 'bcryptjs' │ '17' │ 58142116.79999998 │ '±0.36%' │ 10 │
│ 3 │ 'wasm OpenBSD' │ '17' │ 58318899.99999994 │ '±0.23%' │ 10 │
│ 4 │ 'wasm Openwall' │ '18' │ 53324629.20000016 │ '±0.37%' │ 10 │
└─────────┴───────────────────┴─────────┴───────────────────┴──────────┴─────────┘
Verify benchmark
┌─────────┬───────────────────┬─────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼───────────────────┼─────────┼────────────────────┼──────────┼─────────┤
│ 0 │ '@node-rs/bcrypt' │ '5' │ 192946879.09999993 │ '±0.52%' │ 10 │
│ 1 │ 'node bcrypt' │ '5' │ 199806404.2 │ '±0.36%' │ 10 │
│ 2 │ 'bcryptjs' │ '4' │ 231329516.79999986 │ '±0.13%' │ 10 │
└─────────┴───────────────────┴─────────┴────────────────────┴──────────┴─────────┘
GenSalt benchmark
┌─────────┬───────────────────┬─────────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼───────────────────┼─────────────┼────────────────────┼──────────┼─────────┤
│ 0 │ '@node-rs/bcrypt' │ '4,421,897' │ 226.14727567195578 │ '±0.08%' │ 2210949 │
│ 1 │ 'node bcrypt' │ '4,477,408' │ 223.34345972377827 │ '±0.08%' │ 2238705 │
│ 2 │ 'bcryptjs' │ '821,556' │ 1217.2015511950851 │ '±0.39%' │ 410779 │
│ 3 │ 'wasm OpenBSD' │ '3,685,603' │ 271.3259889021118 │ '±0.23%' │ 1842802 │
│ 4 │ 'wasm Openwall' │ '1,462,251' │ 683.8769076754866 │ '±2.50%' │ 731126 │
└─────────┴───────────────────┴─────────────┴────────────────────┴──────────┴─────────┘
```
144 changes: 0 additions & 144 deletions packages/bcrypt/benchmark/bcrypt.js

This file was deleted.

77 changes: 77 additions & 0 deletions packages/bcrypt/benchmark/bcrypt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import openbsd from '@cwasm/openbsd-bcrypt'
import openwall from '@cwasm/openwall-bcrypt'
import { hashSync, compare, genSaltSync } from 'bcrypt'
import bcryptjs from 'bcryptjs'
import { Bench } from 'tinybench'
import chalk from 'chalk'

import { hashSync as napiHashSync, verifySync, genSaltSync as napiGenSaltSync } from '../binding.js'

const password = 'node-rust-password'

const syncHashSuite = new Bench()
syncHashSuite
.add('@node-rs/bcrypt', () => {
napiHashSync(password, 10)
})
.add('node bcrypt', () => {
hashSync(password, 10)
})
.add('bcryptjs', () => {
bcryptjs.hashSync(password, 10)
})
.add('wasm OpenBSD', () => {
openbsd.hashSync(password, 10)
})
.add('wasm Openwall', () => {
openwall.hashSync(password, 10)
})

await syncHashSuite.warmup()
await syncHashSuite.run()

console.info(chalk.green('Hash benchmark'))
console.table(syncHashSuite.table())

const verifySuite = new Bench()
const hashed = napiHashSync(password, 12)
verifySuite
.add('@node-rs/bcrypt', () => {
verifySync(password, hashed)
})
.add('node bcrypt', () => {
compare(password, hashSync(password, 12))
})
.add('bcryptjs', () => {
bcryptjs.compareSync(password, hashed)
})

await verifySuite.warmup()
await verifySuite.run()

console.info(chalk.green('Verify benchmark'))
console.table(verifySuite.table())

const genSaltSuite = new Bench()
genSaltSuite
.add('@node-rs/bcrypt', () => {
napiGenSaltSync(12)
})
.add('node bcrypt', () => {
genSaltSync(12)
})
.add('bcryptjs', () => {
bcryptjs.genSaltSync(12)
})
.add('wasm OpenBSD', () => {
openbsd.genSaltSync(12)
})
.add('wasm Openwall', () => {
openwall.genSaltSync(12)
})

await genSaltSuite.warmup()
await genSaltSuite.run()

console.info(chalk.green('GenSalt benchmark'))
console.table(genSaltSuite.table())
3 changes: 3 additions & 0 deletions packages/bcrypt/benchmark/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
8 changes: 6 additions & 2 deletions packages/bcrypt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
},
"scripts": {
"artifacts": "napi artifacts -d ../../artifacts",
"bench": "cross-env NODE_ENV=production node benchmark/bcrypt.js",
"bench": "cross-env NODE_ENV=production node --import @swc-node/register/esm-register benchmark/bcrypt.ts",
"build": "napi build --platform --release --js binding.js --dts binding.d.ts",
"build:debug": "napi build --platform",
"prepublishOnly": "napi prepublish",
Expand All @@ -70,11 +70,15 @@
"url": "https://github.com/napi-rs/node-rs/issues"
},
"devDependencies": {
"@cwasm/openbsd-bcrypt": "^0.1.0",
"@cwasm/openwall-bcrypt": "^0.1.0",
"@napi-rs/cli": "^3.0.0-alpha.63",
"@types/bcrypt": "^5.0.2",
"@types/bcryptjs": "^2.4.6",
"bcrypt": "^5.1.1",
"bcryptjs": "^2.4.3",
"cross-env": "^7.0.3"
"cross-env": "^7.0.3",
"tinybench": "^2.9.0"
},
"funding": {
"type": "github",
Expand Down
Loading

0 comments on commit 50be77d

Please sign in to comment.