git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Tree: 9e2c40485053fde8aca886b9e92447c51112f077

Files: 9e2c40485053fde8aca886b9e92447c51112f077 / benchmark / radixTree.js

2328 bytesRaw
1const crypto = require('crypto')
2const RadixTree = require('../')
3const cbor = require('borc')
4const zlib = require('zlib')
5
6const level = require('level')
7const db = level('./testdb')
8const DAG = require('../dag.js')
9const proof = require('../proofStore.js')
10
11const dag = new DAG(db)
12
13async function main () {
14 const tree = new RadixTree({
15 dag: dag,
16 // root: { '/': 'zdpuArkpWFfw49S1tNLY26YNkHCoKt2CG7rJ6iCaqkcwsGqH7' }
17 })
18
19 const entries = 100000 // 5117051
20 console.log('entries', entries)
21 for (let i = 0; i < entries; i++) {
22 const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
23 await tree.set(key, i)
24 }
25 console.log('flushing')
26 const sr = await tree.flush()
27 console.log('done', sr['/'].toString('hex'))
28
29 let proofSize = 0
30 let binaryProofSize = 0
31 let compressedSize = 0
32
33 for (let i = 0; i < entries; i++) {
34 const tree = new RadixTree({
35 dag: dag,
36 root: {'/': sr['/']}
37 })
38 const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
39 await tree.get(key)
40 const encoded = cbor.encode(tree.root)
41 proofSize += encoded.length
42 const binary = proof.encodeProof(tree.root['/'])
43 // console.log(JSON.stringify(tree.root, null, 2))
44 // console.log(binary)
45 binaryProofSize += binary.length
46
47 const gzip = zlib.createGzip()
48 gzip.on('data', (data) => {
49 compressedSize += data.length
50 })
51 const promise = new Promise((resolve, reject) => {
52 gzip.on('end', () => {
53 resolve()
54 })
55 })
56 gzip.end(encoded)
57 await promise
58 }
59 console.log('cbor size', proofSize / entries)
60 console.log('cbor compressed size', compressedSize / entries)
61 console.log('binary', binaryProofSize / entries)
62}
63main()
64
65// if (i % 10000 === 0) {
66// console.log(i)
67// console.log(JSON.stringify(tree.root, null, 2))
68// try {
69// let start = new Date()
70// let hrstart = process.hrtime()
71// await tree.flush()
72// const end = new Date() - start
73// const hrend = process.hrtime(hrstart)
74
75// console.info('Execution time: %dms', end)
76// console.info('Execution time (hr): %ds %dms', hrend[0], hrend[1] / 1000000)
77// console.log(tree.root)
78// start = new Date()
79// hrstart = process.hrtime()
80// } catch (e) {
81// console.log(e)
82// }
83// }
84

Built with git-ssb-web