Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add AsyncIterable benchmarks #361

Merged
merged 6 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .benchrc.yaml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 8 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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'",
Expand Down Expand Up @@ -96,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"
},
Expand Down
113 changes: 113 additions & 0 deletions test/benchmark/asyncIterable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { itBench } from '@dapplion/benchmark'
import { abortableSource } from 'abortable-iterator'
import all from 'it-all'
import { pipe } from 'it-pipe'

/* eslint-disable generator-star-spacing */

describe('abortableSource cost', function () {
const n = 10000
const bytes = new Uint8Array(200)
const controller = new AbortController()

async function* bytesSource (): AsyncGenerator<Uint8Array, void, unknown> {
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 @typescript-eslint/no-unused-expressions
chunk
}
}
})
}
})

describe('pipe extra iterables cost', function () {
const n = 10000

async function* numberSource (): AsyncGenerator<number, void, unknown> {
let i = 0
while (i < n) {
yield i++
}
}

async function* numberTransform (source: AsyncIterable<number>): AsyncIterable<number> {
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, all)
}
})

itBench({
id: `async iterate pipe x2 transforms ${n}`,
fn: async () => {
await pipe(
numberSource,
numberTransform,
numberTransform,
all
)
}
})

itBench({
id: `async iterate pipe x4 transforms ${n}`,
fn: async () => {
await pipe(
numberSource,
numberTransform,
numberTransform,
numberTransform,
numberTransform,
all
)
}
})

itBench({
id: `async iterate pipe x8 transforms ${n}`,
fn: async () => {
await pipe(
numberSource,
numberTransform,
numberTransform,
numberTransform,
numberTransform,
numberTransform,
numberTransform,
numberTransform,
numberTransform,
all
)
}
})
})
12 changes: 2 additions & 10 deletions test/benchmark/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { itBench, setBenchOpts } from '@dapplion/benchmark'
import { itBench } from '@dapplion/benchmark'
import { expect } from 'aegir/chai'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import {
Expand All @@ -9,15 +9,7 @@ import {
} from '../utils/create-pubsub.js'
import { awaitEvents, checkReceivedSubscriptions, checkReceivedSubscription } from '../utils/events.js'

// eslint-disable-next-line no-only-tests/no-only-tests
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
Expand Down
8 changes: 1 addition & 7 deletions test/benchmark/protobuf.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
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 () {
this.timeout(0)
setBenchOpts({
maxMs: 200 * 1000,
minMs: 60 * 1000
})

const testCases: Array<{ name: string, length: number }> = [
// As of Oct 2023, Attestation length = 281
{ name: 'Attestation', length: 300 },
Expand Down
8 changes: 1 addition & 7 deletions test/benchmark/time-cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
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'
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 })
Expand Down
Loading