Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Updated random.ts benchmark and removed keccak dependency (distorts b…
Browse files Browse the repository at this point in the history
…enchmarking results), added simple profiling option, added README section on benchmarking
  • Loading branch information
holgerd77 committed Sep 3, 2020
1 parent 0e6e1f7 commit 8bf67a2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist
dist.browser
.nyc_output
coverage
benchmarks/*.js
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ Additional examples with detailed explanations are available [here](https://gith

`npm test`

# BENCHMARKS

There are two simple **benchmarks** for random `PUT` operations on the tree (`random.ts`) as well as checkpointing (`checkpointing.ts`)
in the `benchmarks` folder.

Benchmarks can be run with:

```shell
npm run benchmarks
```

For running a **profiler** on the `random.ts` benchmark and generate a flamegraph with [0x](https://github.com/davidmarkclements/0x)
you can use:

```shell
npm run profiling
```

# REFERENCES

- Wiki
Expand Down
12 changes: 7 additions & 5 deletions benchmarks/checkpointing.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { pseudoRandomBytes } from 'crypto'
import { CheckpointTrie } from '../dist'

const iterations = 500
const samples = 20
const iterations = 5000
const samples = 5

const iterTest = async (numOfIter: number): Promise<Array<number>> => {
return new Promise(async (resolve) => {
Expand All @@ -17,25 +17,27 @@ const iterTest = async (numOfIter: number): Promise<Array<number>> => {
let hrstart = process.hrtime()
let numOfOps = 0
let trie = new CheckpointTrie()

trie.checkpoint()
for (let i = 0; i < numOfIter; i++) {
await trie.put(vals[i], keys[i])
trie.checkpoint()
await trie.get(Buffer.from('test'))
numOfOps++
if (numOfOps === numOfIter) {
const hrend = process.hrtime(hrstart)
resolve(hrend)
}
}
trie.commit()
})
}

const go = async () => {
let i = 0
let i = 1
let avg = [0, 0]

console.log(`Benchmark 'checkpointing' starting...`)
while (i <= samples) {
console.log(`Sample ${i} with ${iterations} iterations.`)
const hrend = await iterTest(iterations)
avg[0] += hrend[0]
avg[1] += hrend[1]
Expand Down
24 changes: 12 additions & 12 deletions benchmarks/random.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
// https://github.com/ethereum/wiki/wiki/Benchmarks
'use strict'
import { keccak256 } from 'ethereumjs-util'
import { BaseTrie } from '../dist'
const crypto = require('crypto')
import { CheckpointTrie } from '../dist'

const ROUNDS = 1000
const ROUNDS = 50000
const SYMMETRIC = true
const ERA_SIZE = 1000
const ERA_SIZE = 10000

const trie = new BaseTrie()
let seed = Buffer.alloc(32).fill(0)
const trie = new CheckpointTrie()

const run = async (): Promise<void> => {
console.log(`Benchmark 'random' starting...`)
let i = 0
while (i <= ROUNDS) {
seed = keccak256(seed)
let key = crypto.randomBytes(32)

const genRoot = () => {
if (i % ERA_SIZE === 0) {
seed = trie.root
key = trie.root
console.log(`${i} rounds.`)
}
}

if (SYMMETRIC) {
await trie.put(seed, seed)
await trie.put(key, key)
genRoot()
} else {
const val = keccak256(seed)
await trie.put(seed, val)
const value = crypto.randomBytes(32)
await trie.put(key, value)
genRoot()
}

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
],
"browser": "dist.browser/index.js",
"scripts": {
"benchmarks": "npm run build && ts-node benchmarks/index.ts",
"benchmarks": "npm run build && ts-node benchmarks/checkpointing.ts && ts-node benchmarks/random.ts",
"profiling": "npm run build && tsc --target ES5 benchmarks/random.ts && 0x benchmarks/random.js",
"build": "tsc -p tsconfig.prod.json && tsc -p tsconfig.browser.json",
"prepublishOnly": "npm run test && npm run build",
"coverage": "nyc --reporter=lcov npm run test:node",
Expand Down Expand Up @@ -58,6 +59,7 @@
"semaphore-async-await": "^1.5.1"
},
"devDependencies": {
"0x": "^4.9.1",
"@ethereumjs/config-nyc": "^1.1.1",
"@ethereumjs/config-prettier": "^1.1.1",
"@ethereumjs/config-tsc": "^1.1.1",
Expand Down

0 comments on commit 8bf67a2

Please sign in to comment.