git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Tree: e2492b833ea56ac03262f19efeb131600d8d872d

Files: e2492b833ea56ac03262f19efeb131600d8d872d / benchmark / radixTree.js

2065 bytesRaw
1const IPFS = require('ipfs')
2const crypto = require('crypto')
3const RadixTree = require('../')
4const cbor = require('borc')
5const zlib = require('zlib')
6
7// start ipfs
8const node = new IPFS({
9 start: false,
10 repo: './ipfs-repo'
11})
12
13node.on('ready', async () => {
14 const tree = new RadixTree({
15 dag: node.dag,
16 // root: { '/': 'zdpuArkpWFfw49S1tNLY26YNkHCoKt2CG7rJ6iCaqkcwsGqH7' }
17 })
18
19 const entries = 10000 // 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)
28
29 let proofSize = 0
30 let compressedSize = 0
31
32 for (let i = 0; i < entries; i++) {
33 const tree = new RadixTree({
34 dag: node.dag,
35 root: {'/': sr['/']}
36 })
37 const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
38 await tree.get(key)
39 // console.log(JSON.stringify(tree.root, null, 2))
40 const encoded = cbor.encode(tree.root)
41 proofSize += encoded.length
42
43 const gzip = zlib.createGzip()
44 gzip.on('data', (data) => {
45 compressedSize += data.length
46 })
47 const promise = new Promise((resolve, reject) => {
48 gzip.on('end', () => {
49 resolve()
50 })
51 })
52 gzip.end(encoded)
53 await promise
54 }
55 console.log('cbor size', proofSize / entries)
56 console.log('compressed size', compressedSize / entries)
57})
58
59// if (i % 10000 === 0) {
60// console.log(i)
61// console.log(JSON.stringify(tree.root, null, 2))
62// try {
63// let start = new Date()
64// let hrstart = process.hrtime()
65// await tree.flush()
66// const end = new Date() - start
67// const hrend = process.hrtime(hrstart)
68
69// console.info('Execution time: %dms', end)
70// console.info('Execution time (hr): %ds %dms', hrend[0], hrend[1] / 1000000)
71// console.log(tree.root)
72// start = new Date()
73// hrstart = process.hrtime()
74// } catch (e) {
75// console.log(e)
76// }
77// }
78

Built with git-ssb-web