This repository was archived by the owner on Oct 5, 2023. It is now read-only.
forked from orbitdb/orbitdb
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathorbitdb-keyvalue.js
87 lines (67 loc) · 2.09 KB
/
orbitdb-keyvalue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { OrbitDB } from '../src/index.js'
import rmrf from 'rimraf'
import * as IPFS from 'ipfs-core'
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: false,
Interval: 0
},
webRTCStar: {
Enabled: false
}
}
}
}
;(async () => {
console.log('Starting benchmark...')
const entryCount = 1000
await rmrf('./ipfs')
await rmrf('./orbitdb')
const ipfs = await IPFS.create({ ...ipfsConfig, repo: './ipfs' })
const orbitdb = await OrbitDB({ ipfs })
console.log(`Set ${entryCount} keys/values`)
const db1 = await orbitdb.open('benchmark-keyvalue', { type: 'keyvalue' })
const startTime1 = new Date().getTime()
for (let i = 0; i < entryCount; i++) {
await db1.set(i.toString(), 'hello' + i)
}
const endTime1 = new Date().getTime()
const duration1 = endTime1 - startTime1
const operationsPerSecond1 = Math.floor(entryCount / (duration1 / 1000))
const millisecondsPerOp1 = duration1 / entryCount
console.log(`Setting ${entryCount} key/values took ${duration1} ms, ${operationsPerSecond1} ops/s, ${millisecondsPerOp1} ms/op`)
console.log(`Iterate ${entryCount} key/values`)
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(`Iterating ${all.length} key/values 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)
})()