Skip to content

Commit

Permalink
move benchmarks dir to scripts dir
Browse files Browse the repository at this point in the history
add `tsconfig.benchmarks.json` and npm run script `build:benchmarks`
add benchmarks instructions to `developer.md`
  • Loading branch information
ryanio committed Jul 1, 2020
1 parent 026ff50 commit ee21a7b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 14 deletions.
3 changes: 2 additions & 1 deletion packages/vm/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.cachedb
.cachedb
scripts/benchmarks/*.js
23 changes: 23 additions & 0 deletions packages/vm/developer.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,26 @@ An extremely rich and powerful toolbox is the [evmlab](https://github.com/holima
```sh
NODE_OPTIONS="--max-old-space-size=4096" clinic flame -- node ./tests/tester.js --blockchain --excludeDir='GeneralStateTests'
```

## Benchmarks

This helps us see how the VM performs when running mainnet blocks.

We want to use the compiled JS so `ts-node` does not show up in the profile. So run:

`npm run build:benchmarks`

Then:

`node ./scripts/benchmarks/mainnetBlocks.js scripts/benchmarks/fixture/blocks-prestate.json`

If you want to get a more detailed look to find bottlenecks we can use `0x`.

So run:

```
npm i -g 0x
0x scripts/benchmarks/mainnetBlocks.js scripts/benchmarks/fixture/blocks-prestate.json
```

and open the link it generates.
1 change: 1 addition & 0 deletions packages/vm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
],
"scripts": {
"build": "ethereumjs-config-build",
"build:benchmarks": "npm run build && tsc -p tsconfig.benchmarks.json",
"prepublishOnly": "npm run lint && npm run build && npm run test:buildIntegrity",
"coverage": "nyc npm run coverage:test && nyc report --reporter=lcov",
"coverage:test": "npm run build && tape './tests/api/**/*.js' ./tests/tester.js --state --dist",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as fs from 'fs'
import * as path from 'path'
import * as assert from 'assert'
import { merge } from 'lodash'
import Account from '@ethereumjs/account'
import { toBuffer, setLengthLeft } from 'ethereumjs-util'
import { encode } from 'rlp'
import blockFromRPC from '@ethereumjs/block/dist/from-rpc'
import VM from '../'
import { StateManager, DefaultStateManager } from '../state'
import VM from '../../dist'
import { StateManager, DefaultStateManager } from '../../dist/state'
import BN = require('bn.js')

async function main() {
Expand Down Expand Up @@ -41,7 +39,7 @@ async function main() {
throw new Error('Unavailable blockhash requested')
}
return cb(null, { hash: () => toBuffer(bh) })
}
},
}
vm.blockchain = blockchain as any

Expand All @@ -57,18 +55,24 @@ async function main() {
//console.log(`Running tx ${tx.hash()} took ${process.hrtime(start)[0]} s, ${txElapsed.toFixed(3)} ms`)
}
const elapsed = process.hrtime(start)[1] / 1000000
console.log(`Running block ${block.header.number} took ${process.hrtime(start)[0]} s, ${elapsed.toFixed(3)} ms`)
console.log(
`Running block ${block.header.number} took ${process.hrtime(start)[0]} s, ${elapsed.toFixed(
3,
)} ms`,
)
}
}

export interface StateTestPreAccount {
balance: string
code: string
nonce: string
storage: {[k: string]: string}
storage: { [k: string]: string }
}

export async function getPreState(pre: {[k: string]: StateTestPreAccount}): Promise<StateManager> {
export async function getPreState(pre: {
[k: string]: StateTestPreAccount
}): Promise<StateManager> {
const state = new DefaultStateManager()
for (const k in pre) {
const kBuf = toBuffer(k)
Expand All @@ -88,10 +92,7 @@ export async function getPreState(pre: {[k: string]: StateTestPreAccount}): Prom
await storageTrie.put(key, val)
}
acc.stateRoot = storageTrie.root
await state.putAccount(
kBuf,
acc
)
await state.putAccount(kBuf, acc)
await state.putContractCode(kBuf, code)
}
return state
Expand All @@ -105,4 +106,8 @@ const hexToBuffer = (h: string, allowZero: boolean = false): Buffer => {
return buf
}

main().then().catch((e: Error) => { throw e })
main()
.then()
.catch((e: Error) => {
throw e
})
9 changes: 9 additions & 0 deletions packages/vm/tsconfig.benchmarks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@ethereumjs/config-tsc",
"compilerOptions": {
"target": "ES6",
"sourceMap": false,
"declaration": false
},
"include": ["scripts/benchmarks/**.ts"]
}

0 comments on commit ee21a7b

Please sign in to comment.