From da1461b4ea586a3bdf084b950c949fc844b67468 Mon Sep 17 00:00:00 2001 From: Cayman Date: Thu, 11 Jan 2024 13:33:57 -0500 Subject: [PATCH] fix: fix benchmarking (#281) --- package.json | 2 +- packages/enr/.bench.yaml | 10 ++++++++++ packages/enr/bench/index.bench.ts | 25 ++++++++++++++++++++----- packages/enr/package.json | 1 + yarn.lock | 12 ++++++------ 5 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 packages/enr/.bench.yaml diff --git a/package.json b/package.json index c76a5eba..33279b42 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ ], "devDependencies": { "@chainsafe/eslint-plugin-node": "^11.2.3", - "@dapplion/benchmark": "^0.1.6", + "@dapplion/benchmark": "^0.2.4", "@types/chai": "^4.2.0", "@types/debug": "^4.1.5", "@types/eslint": "^6.1.3", diff --git a/packages/enr/.bench.yaml b/packages/enr/.bench.yaml new file mode 100644 index 00000000..892a0274 --- /dev/null +++ b/packages/enr/.bench.yaml @@ -0,0 +1,10 @@ +# Mocha opts +extension: ["ts"] +colors: true +node-option: + - "loader=ts-node/register" + +# benchmark opts +# threshold: 3 +# maxMs: 60_000 +# minRuns: 10 \ No newline at end of file diff --git a/packages/enr/bench/index.bench.ts b/packages/enr/bench/index.bench.ts index f3dfc0ba..e9800ec2 100644 --- a/packages/enr/bench/index.bench.ts +++ b/packages/enr/bench/index.bench.ts @@ -1,6 +1,7 @@ -import { itBench, setBenchOpts } from "@dapplion/benchmark"; +import { itBench } from "@dapplion/benchmark"; import { createSecp256k1PeerId } from "@libp2p/peer-id-factory"; import { + ENR, SignableENR, createPeerIdFromPrivateKey, createPeerIdFromPublicKey, @@ -9,8 +10,6 @@ import { } from "../src/index.js"; describe("ENR", async function () { - setBenchOpts({ runs: 50000 }); - const peerId = await createSecp256k1PeerId(); const { privateKey } = createPrivateKeyFromPeerId(peerId); const enr = SignableENR.createV4(privateKey); @@ -26,8 +25,6 @@ describe("ENR", async function () { }); describe("createPeerIdFromPrivateKey", async function () { - setBenchOpts({ runs: 4000 }); - const peerId = await createSecp256k1PeerId(); const { type, privateKey } = createPrivateKeyFromPeerId(peerId); const { publicKey } = createPublicKeyFromPeerId(peerId); @@ -39,3 +36,21 @@ describe("createPeerIdFromPrivateKey", async function () { return createPeerIdFromPublicKey(type, publicKey); }); }); + +describe("ENR - encode/decode", async function () { + const peerId = await createSecp256k1PeerId(); + const { privateKey } = createPrivateKeyFromPeerId(peerId); + const enr = SignableENR.createV4(privateKey); + enr.ip = "127.0.0.1"; + enr.tcp = 8080; + enr.udp = 8080; + + const encoded = enr.encode(); + + itBench("ENR - encode", () => { + return enr.encode(); + }); + itBench("ENR - decode", () => { + return ENR.decode(encoded); + }); +}); diff --git a/packages/enr/package.json b/packages/enr/package.json index 9edb7aba..3725d633 100644 --- a/packages/enr/package.json +++ b/packages/enr/package.json @@ -27,6 +27,7 @@ "prepublishOnly": "yarn build", "lint": "eslint --color --ext .ts src/ test/", "test": "yarn test:unit && yarn test:e2e", + "test:perf": "NODE_OPTIONS='--max-old-space-size=4096 --loader=ts-node/esm' benchmark 'bench/**/*.bench.ts' --config .bench.yaml", "test:unit": "mocha 'test/unit/**/*.test.ts'" }, "pre-push": [ diff --git a/yarn.lock b/yarn.lock index 56280482..c782b1e6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -314,10 +314,10 @@ dependencies: "@jridgewell/trace-mapping" "0.3.9" -"@dapplion/benchmark@^0.1.6": - version "0.1.6" - resolved "https://registry.yarnpkg.com/@dapplion/benchmark/-/benchmark-0.1.6.tgz#d58cc150732a2d743089b9462cdfef9339ef81fa" - integrity sha512-qBNXvktHxqnfql2dtjdYqPVj+6OZriuKYNB/ZOdZv58kov9/vEVxFstlXQKCnmxVgC3q4NNmvWXIemTAPZah/A== +"@dapplion/benchmark@^0.2.4": + version "0.2.4" + resolved "https://registry.yarnpkg.com/@dapplion/benchmark/-/benchmark-0.2.4.tgz#e75443a067af4c923c5c32c932750d297b3a34c0" + integrity sha512-57nIqyEEgEoSPzLTTgkt5NukLbZuYCg9lmNml+/QR4TcwwFugSuSPmNQTDKyqO92l8XmI8AfSxSoIioAXkJ7lw== dependencies: "@actions/cache" "^1.0.7" "@actions/github" "^5.0.0" @@ -325,7 +325,7 @@ aws-sdk "^2.932.0" csv-parse "^4.16.0" csv-stringify "^5.6.2" - yargs "^17.0.1" + yargs "^17.1.1" "@eslint-community/eslint-utils@^4.2.0": version "4.4.0" @@ -6917,7 +6917,7 @@ yargs@^13.2.2: y18n "^4.0.0" yargs-parser "^13.1.2" -yargs@^17.0.1, yargs@^17.6.2: +yargs@^17.1.1, yargs@^17.6.2: version "17.7.2" resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.7.2.tgz#991df39aca675a192b816e1e0363f9d75d2aa269" integrity sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==