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

Commit

Permalink
Merge pull request #128 from ethereumjs/add-profiling
Browse files Browse the repository at this point in the history
Add simple profiling / Basic benchmark & README update
  • Loading branch information
ryanio committed Sep 7, 2020
2 parents 0e6e1f7 + 6141ed6 commit aa9e47c
Show file tree
Hide file tree
Showing 5 changed files with 46 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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ Additional examples with detailed explanations are available [here](https://gith

`npm test`

# BENCHMARKS

There are two simple **benchmarks** in the `benchmarks` folder:

- `random.ts` runs random `PUT` operations on the tree.
- `checkpointing.ts` runs checkpoints and commits between `PUT` operations.

A third benchmark using mainnet data to simulate real load is also under consideration.

Benchmarks can be run with:

```shell
npm run benchmarks
```

To run 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
```

0x processes the stacks and generates a profile folder (`<pid>.0x`) containing [`flamegraph.html`](https://github.com/davidmarkclements/0x/blob/master/docs/ui.md).

# 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()

for (let i = 0; i < numOfIter; i++) {
await trie.put(vals[i], keys[i])
trie.checkpoint()
await trie.put(vals[i], keys[i])
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 aa9e47c

Please sign in to comment.