Skip to content

Commit

Permalink
upgrade benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Oct 28, 2024
1 parent 31dc55b commit f69021c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 60 deletions.
1 change: 0 additions & 1 deletion packages/argon2/benchmark/argon2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ bench
console.assert(await nodeArgon2.verify(HASHED, PASSWORD))
})

await bench.warmup()
await bench.run()

console.table(bench.table())
20 changes: 10 additions & 10 deletions packages/bcrypt/benchmark/bcrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ 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()
const syncHashSuite = new Bench({
name: 'Hash benchmark',
})

syncHashSuite
.add('@node-rs/bcrypt', () => {
napiHashSync(password, 10)
Expand All @@ -27,13 +29,13 @@ syncHashSuite
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 verifySuite = new Bench({
name: 'Verify benchmark`',
})
const hashed = napiHashSync(password, 12)
verifySuite
.add('@node-rs/bcrypt', () => {
Expand All @@ -46,13 +48,13 @@ verifySuite
bcryptjs.compareSync(password, hashed)
})

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

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

const genSaltSuite = new Bench()
const genSaltSuite = new Bench({
name: 'GenSalt benchmark',
})
genSaltSuite
.add('@node-rs/bcrypt', () => {
napiGenSaltSync(12)
Expand All @@ -70,8 +72,6 @@ genSaltSuite
openwall.genSaltSync(12)
})

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

console.info(chalk.green('GenSalt benchmark'))
console.table(genSaltSuite.table())
33 changes: 16 additions & 17 deletions packages/crc32/benchmark/crc32.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Bench } from 'tinybench'
import chalk from 'chalk'
import { crc32 as crc32Node } from 'crc'
import Sse4Crc32 from 'sse4_crc32'

Expand All @@ -26,66 +25,66 @@ const initialCrc32c = Sse4Crc32.calculate(TEST_BUFFER)
console.assert(crc32(TEST_BUFFER) === initialCrc32)
console.assert(crc32c(TEST_BUFFER) === initialCrc32c)

const suite = new Bench()
const suite = new Bench({
name: 'crc32c without initial crc',
})

await suite
suite
.add('@node/rs crc32c', () => {
crc32c(TEST_BUFFER)
})
.add('sse4_crc32', () => {
Sse4Crc32.calculate(TEST_BUFFER)
})
.warmup()

await suite.run()

console.info(chalk.green('crc32c without initial crc'))
console.table(suite.table())

const suite2 = new Bench()
const suite2 = new Bench({
name: 'crc32c with initial crc',
})

await suite2
suite2
.add('@node/rs crc32c', () => {
crc32c(TEST_BUFFER, initialCrc32c)
})
.add('sse4_crc32', () => {
Sse4Crc32.calculate(TEST_BUFFER, initialCrc32c)
})
.warmup()

await suite2.run()

console.info(chalk.green('crc32c with initial crc'))
console.table(suite2.table())

const suite3 = new Bench()
const suite3 = new Bench({
name: 'crc32 without initial crc',
})

await suite3
suite3
.add('@node/rs crc32', () => {
crc32(TEST_BUFFER)
})
.add('Node crc', () => {
crc32Node(TEST_BUFFER)
})
.warmup()

await suite3.run()

console.info(chalk.green('crc32 without initial crc'))
console.table(suite3.table())

const suite4 = new Bench()
const suite4 = new Bench({
name: 'crc32 with initial crc',
})

await suite4
suite4
.add('@node/rs crc32', () => {
crc32(TEST_BUFFER, initialCrc32)
})
.add('Node crc32', () => {
crc32Node(TEST_BUFFER, initialCrc32)
})
.warmup()

await suite4.run()

console.info(chalk.green('crc32 with initial crc'))
console.table(suite4.table())
8 changes: 3 additions & 5 deletions packages/jieba/benchmark/jieba.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { join } from 'node:path'
import { fileURLToPath } from 'node:url'

import { Bench } from 'tinybench'
import chalk from 'chalk'
import nodejieba from 'nodejieba'

import { Jieba } from '../index.js'
Expand Down Expand Up @@ -32,16 +31,15 @@ async function createBench(
napi: () => any[],
jieba: () => any[],
) {
const suite = new Bench()
const suite = new Bench({
name: suitename,
})
console.assert(transform(napi()) === transform(jieba()))

suite.add('@node-rs/jieba', napi).add('nodejieba', jieba)

await suite.warmup()

await suite.run()

console.info(chalk.green(`Benchmark ${suitename} result`))
console.table(suite.table())
}

Expand Down
17 changes: 8 additions & 9 deletions packages/jsonwebtoken/benchmark/jsonwebtoken.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Bench } from 'tinybench'
import chalk from 'chalk'
import nodeJsonwebtoken, { type PrivateKey, type Secret, type PublicKey, type GetPublicKeyOrSecret } from 'jsonwebtoken'

import { sign, verify, verifySync, signSync } from '../index.js'
Expand Down Expand Up @@ -43,9 +42,11 @@ const jwtClaims = {
}
const token = nodeJwtSignSync(jwtClaims, secretKey)

const suite = new Bench()
const suite = new Bench({
name: 'Sign token',
})

await suite
suite
.add('@node-rs/jsonwebtoken', async () => {
await sign(jwtClaims, secretKey)
})
Expand All @@ -58,15 +59,15 @@ await suite
.add('jsonwebtoken sync', () => {
nodeJwtSignSync(jwtClaims, secretKey)
})
.warmup()

await suite.run()
console.info(chalk.green('Sign token'))
console.table(suite.table())

const verifySuite = new Bench()
const verifySuite = new Bench({
name: 'Verify token',
})

await verifySuite
verifySuite
.add('@node-rs/jsonwebtoken', async () => {
await verify(token, secretKey)
})
Expand All @@ -79,8 +80,6 @@ await verifySuite
.add('jsonwebtoken sync', () => {
nodeJwtVerifySync(token, secretKey)
})
.warmup()

await verifySuite.run()
console.info(chalk.green('Verify token'))
console.table(verifySuite.table())
34 changes: 16 additions & 18 deletions packages/xxhash/benchmark/xxhash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { join } from 'node:path'
import { fileURLToPath } from 'node:url'

import { Bench } from 'tinybench'
import chalk from 'chalk'
// @ts-expect-error
import createWasmHasher from 'webpack/lib/util/hash/xxhash64.js'
// @ts-expect-error
Expand All @@ -16,9 +15,11 @@ const FX = readFileSync(join(fileURLToPath(import.meta.url), '..', '..', '..', '

const wasmHasher = createWasmHasher()

const suite = new Bench()
const suite = new Bench({
name: 'xxh32 without initial seed',
})

await suite
suite
.add('@node-rs/xxhash h32', () => {
xxh32(FX, 0)
})
Expand All @@ -28,32 +29,31 @@ await suite
.add('xxhashjs h32', () => {
xxhashjs.h32(FX, 0).toNumber()
})
.warmup()

console.info(chalk.green('xxh32 without initial seed'))

await suite.run()
console.table(suite.table())

const multiStepSuite = new Bench()
const multiStepSuite = new Bench({
name: 'xxh32 without initial seed multi step',
})

await multiStepSuite
multiStepSuite
.add('@node-rs/xxhash h32', () => {
new Xxh32().update(FX).digest()
})
.add('xxhashjs h32', () => {
xxhashjs.h32().update(FX).digest().toNumber()
})
.warmup()

await multiStepSuite.run()

console.info(chalk.green('xxh32 without initial seed multi step'))
console.table(multiStepSuite.table())

const xx64Suite = new Bench()
const xx64Suite = new Bench({
name: 'xxh64 without initial seed',
})

await xx64Suite
xx64Suite
.add('@node-rs/xxhash 64', () => {
xxh64(FX).toString(16)
})
Expand All @@ -67,16 +67,16 @@ await xx64Suite
.add('xxhashjs h64', () => {
xxhashjs.h64(FX, 0).toString(16)
})
.warmup()

await xx64Suite.run()

console.info(chalk.green('xxh64 without initial seed'))
console.table(xx64Suite.table())

const multiStepSuite64 = new Bench()
const multiStepSuite64 = new Bench({
name: 'xxh64 without initial seed multi step',
})

await multiStepSuite64
multiStepSuite64
.add('@node-rs/xxhash 64', () => {
new Xxh64().update(FX).digest().toString(16)
})
Expand All @@ -87,9 +87,7 @@ await multiStepSuite64
.add('xxhashjs h64', () => {
xxhashjs.h64(0).update(FX).digest().toString(16)
})
.warmup()

await multiStepSuite64.run()

console.info(chalk.green('xxh64 without initial seed multi step'))
console.table(multiStepSuite64.table())

0 comments on commit f69021c

Please sign in to comment.