Skip to content

Commit

Permalink
Add some basic benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaclennan committed Mar 9, 2022
1 parent ee1043a commit 365249d
Show file tree
Hide file tree
Showing 6 changed files with 1,262 additions and 0 deletions.
33 changes: 33 additions & 0 deletions benchmarks/multi-core-indexer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @ts-check
const MultiCoreIndexer = require('../')
const nanobench = require('nanobench')
const assert = require('assert')
const ram = require('random-access-memory')
const {
generateFixtures,
createMultiple,
throttledIdle,
} = require('../test/helpers')

/** @typedef {import('../lib/types').Entry<'binary'>} Entry */

nanobench('Index 20 cores of 1000 blocks (10 times)', async (b) => {
const cores = await createMultiple(20)
const expected = await generateFixtures(cores, 1000)

b.start()
for (let i = 0; i < 10; i++) {
let count = 0
const indexer = new MultiCoreIndexer(cores, {
batch: async (data) => {
count += data.length
await new Promise((res) => setTimeout(res, 10))
},
maxBatch: 500,
storage: () => ram(),
})
await throttledIdle(indexer)
assert(count === expected.length)
}
b.end()
})
72 changes: 72 additions & 0 deletions benchmarks/multifeed-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const Multifeed = require('multifeed')
const Index = require('multifeed-index')
const nanobench = require('nanobench')
const assert = require('assert')
const { promisify } = require('util')
const ram = require('random-access-memory')
const { generateFixture, blocksToExpected } = require('../test/helpers')

/** @typedef {import('../lib/types').Entry<'binary'>} Entry */

nanobench('Index 20 cores of 1000 blocks (10 times)', async (b) => {
const storages = new Map()

function createStorage(key) {
const storage = storages.get(key) || ram()
storages.set(key, storage)
return storage
}

// Setup cores with fixtures
const multi = new Multifeed(createStorage, { valueEncoding: 'binary' })
const cores = await createCores(multi, 20)
const expected = await generateFixtures(cores, 1000)

b.start()
for (let i = 0; i < 10; i++) {
let count = 0
const index = new Index({
batch: (nodes, next) => {
count += nodes.length
setTimeout(next, 10)
},
log: multi,
maxBatch: 500,
})
await new Promise((res) => {
index.on('state-update', function onState(state) {
if (
state.context.totalBlocks === state.context.indexedBlocks &&
state.context.totalBlocks > 0 &&
state.state === 'idle'
) {
index.removeListener('state-update', onState)
res()
}
})
})
assert(count === expected.length)
}
b.end()
})

async function createCores(multi, count) {
const cores = []
for (let i = 0; i < count; i++) {
const core = await promisify(multi.writer.bind(multi))()
cores.push(core)
}
return cores
}

async function generateFixtures(cores, count) {
/** @type {Entry[]} */
const entries = []
for (const core of cores.values()) {
const offset = core.length
const blocks = generateFixture(offset, offset + count)
await promisify(core.append.bind(core))(blocks)
entries.push.apply(entries, blocksToExpected(blocks, core.key, offset))
}
return entries
}
Loading

0 comments on commit 365249d

Please sign in to comment.