Files: 944bce06413fb71fd0d491577015823816a6e618 / benchmark / ethereum.js
1775 bytesRaw
1 | var Trie = require('merkle-patricia-tree') |
2 | const crypto = require('crypto') |
3 | // const rlp = require('rlp') |
4 | const level = require('level') |
5 | const db = level('./eth-testdb') |
6 | |
7 | db._get = db.get |
8 | let lookups = 0 |
9 | db.get = function (key, opts, cb) { |
10 | lookups++ |
11 | return db._get(key, opts, cb) |
12 | } |
13 | |
14 | let trie = new Trie(db) |
15 | |
16 | const entries = 100000 |
17 | console.log('entries', entries) |
18 | |
19 | async function run () { |
20 | |
21 | let start = new Date() |
22 | let hrstart = process.hrtime() |
23 | |
24 | for (let i = 0; i < entries; i++) { |
25 | const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20) |
26 | await new Promise((resolve, reject) => { |
27 | trie.put(key, i, resolve) |
28 | }) |
29 | } |
30 | |
31 | let end = new Date() - start |
32 | let hrend = process.hrtime(hrstart) |
33 | |
34 | console.info('state root creation time: %dms', end) |
35 | console.info('state root creation time (hr): %ds %dms', hrend[0], hrend[1] / 1000000) |
36 | |
37 | start = new Date() |
38 | hrstart = process.hrtime() |
39 | |
40 | console.log('root', trie.root.toString('hex')) |
41 | |
42 | let proofSize = 0 |
43 | for (let i = 0; i < entries; i++) { |
44 | const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20) |
45 | trie = new Trie(db, trie.root) |
46 | const promise = new Promise((resolve, reject) => { |
47 | trie.get(key, (err, value) => { |
48 | // console.log(value) |
49 | // let proof = stack.map(el => { |
50 | // return el.raw |
51 | // }) |
52 | // let encoded = rlp.encode(proof) |
53 | // proofSize += encoded.length |
54 | resolve() |
55 | }) |
56 | }) |
57 | await promise |
58 | } |
59 | |
60 | end = new Date() - start |
61 | hrend = process.hrtime(hrstart) |
62 | |
63 | console.info('read time: %dms', end) |
64 | console.info('read time (hr): %ds %dms', hrend[0], hrend[1] / 1000000) |
65 | console.info('db lookups', lookups) |
66 | // console.log('rlp size', proofSize / entries) |
67 | } |
68 | |
69 | run() |
70 |
Built with git-ssb-web