diff --git a/benchmarks/orbitdb-replicate.js b/benchmarks/orbitdb-replicate.js new file mode 100644 index 0000000..2c75628 --- /dev/null +++ b/benchmarks/orbitdb-replicate.js @@ -0,0 +1,109 @@ +import { OrbitDB } from '../src/index.js' +import rmrf from 'rimraf' +import * as IPFS from 'ipfs' +import connectPeers from '../test/utils/connect-nodes.js' +import waitFor from '../test/utils/wait-for.js' + +import { EventEmitter } from 'events' + +EventEmitter.defaultMaxListeners = 10000 + +const ipfsConfig = { + preload: { + enabled: false + }, + EXPERIMENTAL: { + pubsub: true + }, + config: { + Addresses: { + API: '/ip4/127.0.0.1/tcp/0', + Swarm: ['/ip4/0.0.0.0/tcp/0'], + Gateway: '/ip4/0.0.0.0/tcp/0' + }, + Bootstrap: [], + Discovery: { + MDNS: { + Enabled: true, + Interval: 0 + }, + webRTCStar: { + Enabled: false + } + } + } +} + +;(async () => { + console.log('Starting benchmark...') + + const entryCount = 1000 + + await rmrf('./ipfs1') + await rmrf('./ipfs2') + await rmrf('./orbitdb1') + await rmrf('./orbitdb2') + + const ipfs1 = await IPFS.create({ ...ipfsConfig, repo: './ipfs1' }) + const ipfs2 = await IPFS.create({ ...ipfsConfig, repo: './ipfs2' }) + const orbitdb1 = await OrbitDB({ ipfs: ipfs1, directory: './orbitdb1' }) + const orbitdb2 = await OrbitDB({ ipfs: ipfs2, directory: './orbitdb2' }) + + await connectPeers(ipfs1, ipfs2) + + console.log(`Add ${entryCount} events`) + + const db1 = await orbitdb1.open('benchmark-replication', { type: 'events' }) + + const startTime1 = new Date().getTime() + for (let i = 0; i < entryCount; i++) { + await db1.add('hello' + i) + } + const endTime1 = new Date().getTime() + const duration1 = endTime1 - startTime1 + const operationsPerSecond1 = Math.floor(entryCount / (duration1 / 1000)) + const millisecondsPerOp1 = duration1 / entryCount + console.log(`Adding ${entryCount} events took ${duration1} ms, ${operationsPerSecond1} ops/s, ${millisecondsPerOp1} ms/op`) + + const db2 = await orbitdb2.open(db1.address) + + let replicated = false + + const onJoin = async (peerId) => { + replicated = true + } + + db2.events.on('join', onJoin) + + await waitFor(() => replicated, () => true) + + console.log(`Iterate ${entryCount} events`) + const startTime2 = new Date().getTime() + const all = [] + for await (const { value } of db2.iterator()) { + all.unshift(value) + } + const endTime2 = new Date().getTime() + const duration2 = endTime2 - startTime2 + const operationsPerSecond2 = Math.floor(entryCount / (duration2 / 1000)) + const millisecondsPerOp2 = duration2 / entryCount + + console.log(`Iterating ${all.length} events took ${duration2} ms, ${operationsPerSecond2} ops/s, ${millisecondsPerOp2} ms/op`) + + await db1.drop() + await db1.close() + await db2.drop() + await db2.close() + + await orbitdb1.stop() + await orbitdb2.stop() + await ipfs1.stop() + await ipfs2.stop() + + await rmrf('./ipfs1') + await rmrf('./ipfs2') + await rmrf('./orbitdb1') + await rmrf('./orbitdb2') + + process.exit(0) +})()