From aace41ce8ec0864d38a27d9b0461b705e9e122dc Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Wed, 13 Jan 2021 01:58:28 +0000 Subject: [PATCH] perf(ksuid): add benchmarks --- packages/ksuid/README.md | 29 +++++++++++++++++++++++++++++ packages/ksuid/bench/index.ts | 19 +++++++++++++++++++ packages/ksuid/bench/tsconfig.json | 10 ++++++++++ packages/ksuid/package.json | 3 +++ packages/ksuid/tpl.readme.md | 28 ++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+) create mode 100644 packages/ksuid/bench/index.ts create mode 100644 packages/ksuid/bench/tsconfig.json diff --git a/packages/ksuid/README.md b/packages/ksuid/README.md index 60452c04e5..e0c2f92b75 100644 --- a/packages/ksuid/README.md +++ b/packages/ksuid/README.md @@ -14,6 +14,7 @@ This project is part of the - [Installation](#installation) - [Dependencies](#dependencies) - [API](#api) +- [Benchmarks](#benchmarks) - [Authors](#authors) - [License](#license) @@ -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 diff --git a/packages/ksuid/bench/index.ts b/packages/ksuid/bench/index.ts new file mode 100644 index 0000000000..023c1137c0 --- /dev/null +++ b/packages/ksuid/bench/index.ts @@ -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(); + 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 }); diff --git a/packages/ksuid/bench/tsconfig.json b/packages/ksuid/bench/tsconfig.json new file mode 100644 index 0000000000..9655cbea10 --- /dev/null +++ b/packages/ksuid/bench/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../../tsconfig.json", + "compilerOptions": { + "outDir": "../build", + "module": "commonjs", + "noUnusedLocals": false, + "noUnusedParameters": false + }, + "include": ["./**/*.ts", "../src/**/*.ts"] +} diff --git a/packages/ksuid/package.json b/packages/ksuid/package.json index 7db979a4f8..e996dbbf22 100644 --- a/packages/ksuid/package.json +++ b/packages/ksuid/package.json @@ -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", @@ -65,6 +66,8 @@ "binary", "bytes", "crypto", + "database", + "dynamodb", "encode", "epoch", "generator", diff --git a/packages/ksuid/tpl.readme.md b/packages/ksuid/tpl.readme.md index e24012cfec..afe89017c1 100644 --- a/packages/ksuid/tpl.readme.md +++ b/packages/ksuid/tpl.readme.md @@ -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}