-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee1043a
commit 365249d
Showing
6 changed files
with
1,262 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.