Skip to content

Commit

Permalink
perf(ksuid): add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 13, 2021
1 parent 4d095da commit aace41c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/ksuid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This project is part of the
- [Installation](#installation)
- [Dependencies](#dependencies)
- [API](#api)
- [Benchmarks](#benchmarks)
- [Authors](#authors)
- [License](#license)

Expand Down Expand Up @@ -102,6 +103,34 @@ const id36 = defKSUID({ base: BASE36, epoch: 0, bytes: 8 });
// '2VOUKH4K59AG0RXR4XH'
```

## Benchmarks

```text
yarn bench
benchmarking: b62, 128bit, n=10000
warmup... 659.22ms (10 runs)
executing...
total: 6402.18ms, runs: 100
mean: 64.02ms, median: 63.50ms, range: [59.98..96.15]
q1: 62.64ms, q3: 64.41ms
sd: 6.93%
benchmarking: b62, 64bit, n=10000
warmup... 363.35ms (10 runs)
executing...
total: 3469.28ms, runs: 100
mean: 34.69ms, median: 34.41ms, range: [32.61..56.58]
q1: 33.35ms, q3: 35.41ms
sd: 7.47%
benchmarking: b62, 32bit, n=10000
warmup... 218.78ms (10 runs)
executing...
total: 2118.93ms, runs: 100
mean: 21.19ms, median: 20.95ms, range: [20.20..25.74]
q1: 20.71ms, q3: 21.30ms
sd: 4.14%
```

## Authors

Karsten Schmidt
Expand Down
19 changes: 19 additions & 0 deletions packages/ksuid/bench/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// import { benchmark } from "@thi.ng/bench";
import { assert } from "@thi.ng/api";
import { benchmark } from "@thi.ng/bench";
import { defKSUID, KSUID } from "../src";

const opts = { iter: 100, warmup: 10 };

const bench = (id: KSUID, n = 10000) => {
const acc = new Set<string>();
for (let i = 0; i < n; i++) acc.add(id.next());
assert(acc.size === n, `collision`);
};

// prettier-ignore
benchmark(() => bench(defKSUID({ bytes:16 })), { title: "b62, 128bit, n=10000", ...opts });
// prettier-ignore
benchmark(() => bench(defKSUID({ bytes: 8 })), { title: "b62, 64bit, n=10000", ...opts });
// prettier-ignore
benchmark(() => bench(defKSUID({ bytes: 4 })), { title: "b62, 32bit, n=10000", ...opts });
10 changes: 10 additions & 0 deletions packages/ksuid/bench/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "../build",
"module": "commonjs",
"noUnusedLocals": false,
"noUnusedParameters": false
},
"include": ["./**/*.ts", "../src/**/*.ts"]
}
3 changes: 3 additions & 0 deletions packages/ksuid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"build:check": "tsc --isolatedModules --noEmit",
"test": "mocha test",
"cover": "nyc mocha test && nyc report --reporter=lcov",
"bench": "ts-node -P bench/tsconfig.json bench/index.ts",
"clean": "rimraf *.js *.d.ts *.map .nyc_output build coverage doc lib",
"doc:readme": "ts-node -P ../../tools/tsconfig.json ../../tools/src/readme.ts",
"doc:ae": "mkdir -p .ae/doc .ae/temp && node_modules/.bin/api-extractor run --local --verbose",
Expand Down Expand Up @@ -65,6 +66,8 @@
"binary",
"bytes",
"crypto",
"database",
"dynamodb",
"encode",
"epoch",
"generator",
Expand Down
28 changes: 28 additions & 0 deletions packages/ksuid/tpl.readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,34 @@ const id36 = defKSUID({ base: BASE36, epoch: 0, bytes: 8 });
// '2VOUKH4K59AG0RXR4XH'
```

## Benchmarks

```text
yarn bench
benchmarking: b62, 128bit, n=10000
warmup... 659.22ms (10 runs)
executing...
total: 6402.18ms, runs: 100
mean: 64.02ms, median: 63.50ms, range: [59.98..96.15]
q1: 62.64ms, q3: 64.41ms
sd: 6.93%
benchmarking: b62, 64bit, n=10000
warmup... 363.35ms (10 runs)
executing...
total: 3469.28ms, runs: 100
mean: 34.69ms, median: 34.41ms, range: [32.61..56.58]
q1: 33.35ms, q3: 35.41ms
sd: 7.47%
benchmarking: b62, 32bit, n=10000
warmup... 218.78ms (10 runs)
executing...
total: 2118.93ms, runs: 100
mean: 21.19ms, median: 20.95ms, range: [20.20..25.74]
q1: 20.71ms, q3: 21.30ms
sd: 4.14%
```

## Authors

${authors}
Expand Down

0 comments on commit aace41c

Please sign in to comment.