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 2 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
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 { 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<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 as Transform<number, number>, all)
}
})

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

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

itBench({
id: `async iterate pipe x8 transforms ${n}`,
fn: async () => {
await pipe(
numberSource,
numberTransform as Transform<number, number>,
numberTransform as Transform<number, number>,
numberTransform as Transform<number, number>,
numberTransform as Transform<number, number>,
numberTransform as Transform<number, number>,
numberTransform as Transform<number, number>,
numberTransform as Transform<number, number>,
numberTransform as Transform<number, number>,
all
)
}
})
})
11 changes: 2 additions & 9 deletions test/benchmark/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
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'
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
Expand Down
9 changes: 1 addition & 8 deletions test/benchmark/protobuf.test.ts
Original file line number Diff line number Diff line change
@@ -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: [
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