From 439d890839b8b93ebaa2e9081de080ac85f2a762 Mon Sep 17 00:00:00 2001 From: haad Date: Wed, 17 Apr 2024 12:38:56 +0300 Subject: [PATCH] Add a benchmark for Documents database --- benchmarks/orbitdb-documents.js | 68 +++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 benchmarks/orbitdb-documents.js diff --git a/benchmarks/orbitdb-documents.js b/benchmarks/orbitdb-documents.js new file mode 100644 index 0000000..110b441 --- /dev/null +++ b/benchmarks/orbitdb-documents.js @@ -0,0 +1,68 @@ +import { createOrbitDB } from '../src/index.js' +// import { createOrbitDB, MemoryStorage } from '../src/index.js' +import { rimraf as rmrf } from 'rimraf' +import createHelia from '../test/utils/create-helia.js' + +import { EventEmitter } from 'events' +EventEmitter.defaultMaxListeners = 10000 + +;(async () => { + console.log('Starting benchmark...') + + const entryCount = 1000 + + await rmrf('./ipfs') + await rmrf('./orbitdb') + + const ipfs = await createHelia() + const orbitdb = await createOrbitDB({ ipfs }) + + console.log(`Insert ${entryCount} documents`) + + // const entryStorage = await MemoryStorage() + // const headsStorage = await MemoryStorage() + // const indexStorage = await MemoryStorage() + + // const db1 = await orbitdb.open('benchmark-documents', { type: 'documents', referencesCount: 16, entryStorage, headsStorage, indexStorage }) + + const db1 = await orbitdb.open('benchmark-documents', { type: 'documents' }) + + const startTime1 = new Date().getTime() + + for (let i = 0; i < entryCount; i++) { + const doc = { _id: i.toString(), message: 'hello ' + i } + await db1.put(doc) + } + + const endTime1 = new Date().getTime() + const duration1 = endTime1 - startTime1 + const operationsPerSecond1 = Math.floor(entryCount / (duration1 / 1000)) + const millisecondsPerOp1 = duration1 / entryCount + console.log(`Inserting ${entryCount} documents took ${duration1} ms, ${operationsPerSecond1} ops/s, ${millisecondsPerOp1} ms/op`) + + console.log(`Query ${entryCount} documents`) + const startTime2 = new Date().getTime() + + const all = [] + for await (const { key, value } of db1.iterator()) { + all.unshift({ key, value }) + } + + const endTime2 = new Date().getTime() + const duration2 = endTime2 - startTime2 + const operationsPerSecond2 = Math.floor(entryCount / (duration2 / 1000)) + const millisecondsPerOp2 = duration2 / entryCount + + console.log(`Querying ${all.length} documents took ${duration2} ms, ${operationsPerSecond2} ops/s, ${millisecondsPerOp2} ms/op`) + + await db1.drop() + await db1.close() + + await orbitdb.stop() + await ipfs.stop() + + await rmrf('./ipfs') + await rmrf('./orbitdb') + + process.exit(0) +})()