Files: e6e068b9aabc855accb38b1e1c5289d738cb142c / benchmark / radixTree-lmza.js
1592 bytesRaw
1 | const IPFS = require('ipfs') |
2 | const crypto = require('crypto') |
3 | const RadixTree = require('../') |
4 | const cbor = require('borc') |
5 | const lzma = require('lzma') |
6 | |
7 | // start ipfs |
8 | const node = new IPFS({ |
9 | start: false, |
10 | repo: './ipfs-repo' |
11 | }) |
12 | |
13 | node.on('ready', async () => { |
14 | const tree = new RadixTree({ |
15 | dag: node.dag, |
16 | // root: { '/': 'zdpuAmHPs6YDFyFoaUHg5YgyNHoUGKzug5n5ZzdjUrk36Gnzg'} |
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 | const root = tree.root |
32 | |
33 | for (let i = 0; i < entries; i++) { |
34 | const tree = new RadixTree({ |
35 | dag: node.dag, |
36 | root: {'/': root['/']} |
37 | }) |
38 | const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20) |
39 | await tree.get(key) |
40 | removeLink(tree.root['/']) |
41 | const encoded = cbor.encode(tree.root['/']) |
42 | proofSize += encoded.length |
43 | compressedSize += lzma.compress(encoded, 9).length |
44 | } |
45 | console.log('cbor size', proofSize / entries) |
46 | console.log('compressed size', compressedSize / entries) |
47 | }) |
48 | |
49 | function removeLink (root) { |
50 | if (root) { |
51 | if (root[0]) { |
52 | root[0] = root[0]['/'] |
53 | } |
54 | |
55 | if (root[1]) { |
56 | root[1] = root[1]['/'] |
57 | } |
58 | |
59 | if (!Buffer.isBuffer(root[1])) { |
60 | removeLink(root[1]) |
61 | } |
62 | |
63 | if (!Buffer.isBuffer(root[0])) { |
64 | removeLink(root[0]) |
65 | } |
66 | } |
67 | } |
68 |
Built with git-ssb-web