From 2bf8a7e8fc66328b39b345852216c0c413418978 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Fri, 14 Oct 2022 11:02:47 -0500 Subject: [PATCH 1/4] Update benchmark code --- .benchrc.yaml | 10 ++++++++++ package.json | 3 ++- test/benchmark/index.test.ts | 11 ++--------- test/benchmark/protobuf.test.ts | 9 +-------- test/benchmark/time-cache.test.ts | 8 +------- 5 files changed, 16 insertions(+), 25 deletions(-) create mode 100644 .benchrc.yaml diff --git a/.benchrc.yaml b/.benchrc.yaml new file mode 100644 index 00000000..a5e9827b --- /dev/null +++ b/.benchrc.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/package.json b/package.json index 5632d85c..340136fc 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,8 @@ "prepare": "npm run build", "pretest": "npm run build", "pretest:e2e": "npm run build", - "benchmark": "node ./node_modules/.bin/benchmark 'dist/test/benchmark/*.test.js' --local", + "benchmark": "yarn benchmark:files 'test/benchmark/**/*.test.ts'", + "benchmark:files": "NODE_OPTIONS='--max-old-space-size=4096 --loader=ts-node/esm' benchmark --config .benchrc.yaml --defaultBranch master", "test": "aegir test -f './dist/test/*.spec.js'", "test:unit": "aegir test -f './dist/test/unit/*.test.js' --target node", "test:e2e": "aegir test -f './dist/test/e2e/*.spec.js'", diff --git a/test/benchmark/index.test.ts b/test/benchmark/index.test.ts index 26d137a1..4f6ede93 100644 --- a/test/benchmark/index.test.ts +++ b/test/benchmark/index.test.ts @@ -1,4 +1,4 @@ -import { itBench, setBenchOpts } from '@dapplion/benchmark' +import { itBench } from '@dapplion/benchmark' import { GossipSub } from '../../src/index.js' import { connectPubsubNodes, createComponentsArray, denseConnect } from '../utils/create-pubsub.js' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' @@ -6,14 +6,7 @@ import { Components } from '@libp2p/components' import { awaitEvents, checkReceivedSubscriptions, checkReceivedSubscription } from '../utils/events.js' import { expect } from 'aegir/chai' -describe.only('heartbeat', function () { - this.timeout(0) - setBenchOpts({ - maxMs: 200 * 1000, - minMs: 120 * 1000, - minRuns: 200 - }) - +describe('heartbeat', function () { const topic = 'foobar' const numTopic = 70 const numPeers = 50 diff --git a/test/benchmark/protobuf.test.ts b/test/benchmark/protobuf.test.ts index 4a30f37c..1e6867fe 100644 --- a/test/benchmark/protobuf.test.ts +++ b/test/benchmark/protobuf.test.ts @@ -1,14 +1,7 @@ -import { itBench, setBenchOpts } from '@dapplion/benchmark' +import { itBench } from '@dapplion/benchmark' import { IRPC, RPC } from '../../src/message/rpc.js' describe('protobuf', function () { - this.timeout(0) - setBenchOpts({ - maxMs: 200 * 1000, - minMs: 120 * 1000, - minRuns: 200 - }) - const rpc: IRPC = { subscriptions: [], messages: [ diff --git a/test/benchmark/time-cache.test.ts b/test/benchmark/time-cache.test.ts index 1e3291e8..7d294ffa 100644 --- a/test/benchmark/time-cache.test.ts +++ b/test/benchmark/time-cache.test.ts @@ -1,4 +1,4 @@ -import { itBench, setBenchOpts } from '@dapplion/benchmark' +import { itBench } from '@dapplion/benchmark' // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error no types import TimeCache from 'time-cache' @@ -6,12 +6,6 @@ import { SimpleTimeCache } from '../../src/utils/time-cache.js' // TODO: errors with "Error: root suite not found" describe('npm TimeCache vs SimpleTimeCache', () => { - setBenchOpts({ - maxMs: 100 * 1000, - minMs: 60 * 1000, - minRuns: 512 - }) - const iterations = [1_000_000, 4_000_000, 8_000_000, 16_000_000] const timeCache = new TimeCache({ validity: 1 }) const simpleTimeCache = new SimpleTimeCache({ validityMs: 1000 }) From fc1254afd6a64c790554d03478e4be7d3b0af5b5 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Fri, 14 Oct 2022 11:26:41 -0500 Subject: [PATCH 2/4] Add AsyncIterable benchmarks --- test/benchmark/asyncIterable.test.ts | 113 +++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 test/benchmark/asyncIterable.test.ts diff --git a/test/benchmark/asyncIterable.test.ts b/test/benchmark/asyncIterable.test.ts new file mode 100644 index 00000000..94512c05 --- /dev/null +++ b/test/benchmark/asyncIterable.test.ts @@ -0,0 +1,113 @@ +import { itBench } from '@dapplion/benchmark' +import { abortableSource } from 'abortable-iterator' +import { pipe, Transform } from 'it-pipe' +import all from 'it-all' + +/* eslint-disable generator-star-spacing */ + +describe('abortableSource cost', function () { + const n = 10000 + const bytes = new Uint8Array(200) + const controller = new AbortController() + + async function* bytesSource() { + let i = 0 + while (i++ < n) { + yield bytes + } + } + + for (let k = 0; k < 5; k++) { + itBench({ + id: `async iterate abortable x${k} bytesSource ${n}`, + beforeEach: () => { + let source = bytesSource() + for (let i = 0; i < k; i++) { + source = abortableSource(source, controller.signal) + } + return source + }, + fn: async (source) => { + for await (const chunk of source) { + // eslint-disable-next-line no-unused-expressions + chunk + } + } + }) + } +}) + +describe('pipe extra iterables cost', function () { + const n = 10000 + + async function* numberSource() { + let i = 0 + while (i < n) { + yield i++ + } + } + + async function* numberTransform(source: AsyncIterable): AsyncIterable { + for await (const num of source) { + yield num + 1 + } + } + + itBench({ + id: `async iterate pipe x0 transforms ${n}`, + fn: async () => { + await pipe(numberSource, all) + } + }) + + itBench({ + id: `async iterate pipe x1 transforms ${n}`, + fn: async () => { + await pipe(numberSource, numberTransform as Transform, all) + } + }) + + itBench({ + id: `async iterate pipe x2 transforms ${n}`, + fn: async () => { + await pipe( + numberSource, + numberTransform as Transform, + numberTransform as Transform, + all + ) + } + }) + + itBench({ + id: `async iterate pipe x4 transforms ${n}`, + fn: async () => { + await pipe( + numberSource, + numberTransform as Transform, + numberTransform as Transform, + numberTransform as Transform, + numberTransform as Transform, + all + ) + } + }) + + itBench({ + id: `async iterate pipe x8 transforms ${n}`, + fn: async () => { + await pipe( + numberSource, + numberTransform as Transform, + numberTransform as Transform, + numberTransform as Transform, + numberTransform as Transform, + numberTransform as Transform, + numberTransform as Transform, + numberTransform as Transform, + numberTransform as Transform, + all + ) + } + }) +}) From 2b4e3de705cc18fb10701c90d16c16d82a8b082f Mon Sep 17 00:00:00 2001 From: Cayman Date: Mon, 19 Aug 2024 12:32:46 -0400 Subject: [PATCH 3/4] chore: fix lint errors --- package-lock.json | 9 ------- test/benchmark/asyncIterable.test.ts | 40 ++++++++++++++-------------- test/benchmark/index.test.ts | 1 - test/benchmark/protobuf.test.ts | 2 +- 4 files changed, 21 insertions(+), 31 deletions(-) diff --git a/package-lock.json b/package-lock.json index 353e411d..9afbc1ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5968,15 +5968,6 @@ "ajv": ">=5.0.0" } }, - "node_modules/ansi-colors": { - "version": "4.1.3", - "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.3.tgz", - "integrity": "sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw==", - "extraneous": true, - "engines": { - "node": ">=6" - } - }, "node_modules/ansi-escapes": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-3.2.0.tgz", diff --git a/test/benchmark/asyncIterable.test.ts b/test/benchmark/asyncIterable.test.ts index 94512c05..a5cf2ff5 100644 --- a/test/benchmark/asyncIterable.test.ts +++ b/test/benchmark/asyncIterable.test.ts @@ -1,7 +1,7 @@ import { itBench } from '@dapplion/benchmark' import { abortableSource } from 'abortable-iterator' -import { pipe, Transform } from 'it-pipe' import all from 'it-all' +import { pipe } from 'it-pipe' /* eslint-disable generator-star-spacing */ @@ -10,7 +10,7 @@ describe('abortableSource cost', function () { const bytes = new Uint8Array(200) const controller = new AbortController() - async function* bytesSource() { + async function* bytesSource (): AsyncGenerator { let i = 0 while (i++ < n) { yield bytes @@ -29,7 +29,7 @@ describe('abortableSource cost', function () { }, fn: async (source) => { for await (const chunk of source) { - // eslint-disable-next-line no-unused-expressions + // eslint-disable-next-line @typescript-eslint/no-unused-expressions chunk } } @@ -40,14 +40,14 @@ describe('abortableSource cost', function () { describe('pipe extra iterables cost', function () { const n = 10000 - async function* numberSource() { + async function* numberSource (): AsyncGenerator { let i = 0 while (i < n) { yield i++ } } - async function* numberTransform(source: AsyncIterable): AsyncIterable { + async function* numberTransform (source: AsyncIterable): AsyncIterable { for await (const num of source) { yield num + 1 } @@ -63,7 +63,7 @@ describe('pipe extra iterables cost', function () { itBench({ id: `async iterate pipe x1 transforms ${n}`, fn: async () => { - await pipe(numberSource, numberTransform as Transform, all) + await pipe(numberSource, numberTransform, all) } }) @@ -72,8 +72,8 @@ describe('pipe extra iterables cost', function () { fn: async () => { await pipe( numberSource, - numberTransform as Transform, - numberTransform as Transform, + numberTransform, + numberTransform, all ) } @@ -84,10 +84,10 @@ describe('pipe extra iterables cost', function () { fn: async () => { await pipe( numberSource, - numberTransform as Transform, - numberTransform as Transform, - numberTransform as Transform, - numberTransform as Transform, + numberTransform, + numberTransform, + numberTransform, + numberTransform, all ) } @@ -98,14 +98,14 @@ describe('pipe extra iterables cost', function () { fn: async () => { await pipe( numberSource, - numberTransform as Transform, - numberTransform as Transform, - numberTransform as Transform, - numberTransform as Transform, - numberTransform as Transform, - numberTransform as Transform, - numberTransform as Transform, - numberTransform as Transform, + numberTransform, + numberTransform, + numberTransform, + numberTransform, + numberTransform, + numberTransform, + numberTransform, + numberTransform, all ) } diff --git a/test/benchmark/index.test.ts b/test/benchmark/index.test.ts index 07c75c17..583e15e2 100644 --- a/test/benchmark/index.test.ts +++ b/test/benchmark/index.test.ts @@ -1,5 +1,4 @@ import { itBench } from '@dapplion/benchmark' -import { GossipSub } from '../../src/index.js' import { expect } from 'aegir/chai' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { diff --git a/test/benchmark/protobuf.test.ts b/test/benchmark/protobuf.test.ts index 40a38c2e..e94b369a 100644 --- a/test/benchmark/protobuf.test.ts +++ b/test/benchmark/protobuf.test.ts @@ -1,5 +1,5 @@ import crypto from 'node:crypto' -import { itBench, setBenchOpts } from '@dapplion/benchmark' +import { itBench } from '@dapplion/benchmark' import { RPC } from '../../src/message/rpc.js' describe('protobuf', function () { From 1624ce3d71d9d1719d0ac2680317c36662c773bf Mon Sep 17 00:00:00 2001 From: Cayman Date: Mon, 19 Aug 2024 12:42:40 -0400 Subject: [PATCH 4/4] chore: add dev dependencies --- package-lock.json | 16 ++++++++-------- package.json | 4 +++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0b8023cc..d25d4d81 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33,9 +33,11 @@ "@libp2p/peer-id-factory": "^4.0.5", "@libp2p/peer-store": "^10.0.8", "@types/node": "^20.11.6", + "abortable-iterator": "^5.1.0", "aegir": "^42.2.2", "datastore-core": "^9.2.7", "delay": "^6.0.0", + "it-all": "^3.0.6", "mkdirp": "^3.0.1", "p-defer": "^4.0.0", "p-event": "^6.0.0", @@ -5745,17 +5747,14 @@ } }, "node_modules/abortable-iterator": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/abortable-iterator/-/abortable-iterator-5.0.1.tgz", - "integrity": "sha512-hlZ5Z8UwqrKsJcelVPEqDduZowJPBQJ9ZhBC2FXpja3lXy8X6MoI5uMzIgmrA8+3jcVnp8TF/tx+IBBqYJNUrg==", + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/abortable-iterator/-/abortable-iterator-5.1.0.tgz", + "integrity": "sha512-a3nRG0GOGw3IPFA2hdhrZU+QuD3mA6i+5f4YM/Obe+D5lYccxScI32rAIHAW5ttFV7+beiof09gHav4qUEZDwg==", "dev": true, + "license": "Apache-2.0 OR MIT", "dependencies": { "get-iterator": "^2.0.0", "it-stream-types": "^2.0.1" - }, - "engines": { - "node": ">=16.0.0", - "npm": ">=7.0.0" } }, "node_modules/acorn": { @@ -12623,7 +12622,8 @@ "version": "3.0.6", "resolved": "https://registry.npmjs.org/it-all/-/it-all-3.0.6.tgz", "integrity": "sha512-HXZWbxCgQZJfrv5rXvaVeaayXED8nTKx9tj9fpBhmcUJcedVZshMMMqTj0RG2+scGypb9Ut1zd1ifbf3lA8L+Q==", - "dev": true + "dev": true, + "license": "Apache-2.0 OR MIT" }, "node_modules/it-byte-stream": { "version": "1.0.12", diff --git a/package.json b/package.json index 4b9306b5..5af7366b 100644 --- a/package.json +++ b/package.json @@ -97,16 +97,18 @@ "@libp2p/peer-id-factory": "^4.0.5", "@libp2p/peer-store": "^10.0.8", "@types/node": "^20.11.6", + "abortable-iterator": "^5.1.0", "aegir": "^42.2.2", "datastore-core": "^9.2.7", "delay": "^6.0.0", "mkdirp": "^3.0.1", + "it-all": "^3.0.6", "p-defer": "^4.0.0", "p-event": "^6.0.0", "p-retry": "^6.2.0", "p-wait-for": "^5.0.2", - "sinon": "^17.0.1", "protons": "^7.5.0", + "sinon": "^17.0.1", "time-cache": "^0.3.0", "ts-sinon": "^2.0.2" },