Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: migrate revme benchmarks to Criterion #1569

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bins/revme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ description = "Rust Ethereum Virtual Machine Executable"
version = "0.6.0"

[dependencies]
criterion = "0.5"
hash-db = "0.15"
hex = "0.4"
hashbrown = "0.14"
indicatif = "0.17"
microbench = "0.5"
plain_hasher = "0.2"
revm = { path = "../../crates/revm", version = "10.0.0", default-features = false, features = [
"ethersdb",
Expand Down
31 changes: 30 additions & 1 deletion bins/revme/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@

`revme` is a binary crate to execute the evm in multiple ways.

Currently it is mainly used to run ethereum tests with the `statetest` subcommand.

## Usage
You can run it directly from the command line with the following command:
```shell
cargo run -p revme <FLAGS> <SUBCOMMAND>
```

or build an optimized bin and re-use with:
```shell
cargo build -p revme --profile release
```

## State Tests

Expand All @@ -24,3 +34,22 @@ cargo run -p revme statetest tests/GeneralStateTests
is ignored so it won't be checked into git.*

[et]: https://github.com/ethereum/tests

## Evm

`evm` executes any given bytecode and returns the result, for example:

```shell
cargo run -p revme evm 60FF60005261000F600020
```

### Benchmarks

Adding the `--bench` flag will run the benchmarks. It is important to run all the benchmarks in the release mode, as the results can be misleading in the debug mode.

Example of running the benchmarks:
```shell
cargo run -p revme --profile release evm 60FF60005261000F600020 --bench
```

The detailed reports and comparisons can be found in the `target/criterion` directory.
57 changes: 44 additions & 13 deletions bins/revme/src/cmd/evmrunner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ use revm::{
db::BenchmarkDB,
inspector_handle_register,
inspectors::TracerEip3155,
primitives::{Address, Bytecode, TxKind},
interpreter::{
analysis::to_analysed, opcode::make_instruction_table, Contract, DummyHost, Interpreter,
EMPTY_SHARED_MEMORY,
},
primitives::{address, Address, Bytecode, Bytes, LatestSpec, TxKind},
Evm,
};
use std::io::Error as IoError;
use std::path::PathBuf;
use std::time::Duration;
use std::{borrow::Cow, fs};
use structopt::StructOpt;

Expand Down Expand Up @@ -75,6 +78,12 @@ impl Cmd {
let input = hex::decode(self.input.trim())
.map_err(|_| Errors::InvalidInput)?
.into();

if self.bench {
run_benchmark(bytecode_str);
return Ok(());
}

// BenchmarkDB is dummy state that implements Database trait.
// the bytecode is deployed at zero address.
let mut evm = Evm::builder()
Expand All @@ -91,17 +100,6 @@ impl Cmd {
})
.build();

if self.bench {
// Microbenchmark
let bench_options = microbench::Options::default().time(Duration::from_secs(3));

microbench::bench(&bench_options, "Run bytecode", || {
let _ = evm.transact().unwrap();
});

return Ok(());
}

let out = if self.trace {
let mut evm = evm
.modify()
Expand All @@ -125,3 +123,36 @@ impl Cmd {
Ok(())
}
}

fn run_benchmark(bytecode_str: Cow<str>) {
let evm = Evm::builder()
.with_db(BenchmarkDB::new_bytecode(Bytecode::new()))
.modify_tx_env(|tx| {
tx.caller = address!("1000000000000000000000000000000000000000");
tx.transact_to = TxKind::Call(address!("0000000000000000000000000000000000000000"));
})
.build();

let host = DummyHost::new(*evm.context.evm.env.clone());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let evm = Evm::builder()
.with_db(BenchmarkDB::new_bytecode(Bytecode::new()))
.modify_tx_env(|tx| {
tx.caller = address!("1000000000000000000000000000000000000000");
tx.transact_to = TxKind::Call(address!("0000000000000000000000000000000000000000"));
})
.build();
let host = DummyHost::new(*evm.context.evm.env.clone());
let mut env = Env::default();
env.tx.caller = address!("1000000000000000000000000000000000000000");
env.tx.transact_to = TxKind::Call(address!("0000000000000000000000000000000000000000"));
let host = DummyHost::new(env);

Dont need builder here.

let instruction_table = make_instruction_table::<DummyHost, LatestSpec>();
let contract = Contract {
input: Bytes::from(hex::decode("").unwrap()),
bytecode: to_analysed(Bytecode::new_raw(
hex::decode(bytecode_str.to_string()).unwrap().into(),
)),
..Default::default()
};
let mut criterion = criterion::Criterion::default();

let mut criterion_group = criterion.benchmark_group("revme");
criterion_group.bench_function("bytecode", |b| {
b.iter(|| {
Interpreter::new(contract.clone(), u64::MAX, false).run(
EMPTY_SHARED_MEMORY,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
EMPTY_SHARED_MEMORY,
SharedMemory::new();

Shared memory is preallocated and shared between multiple calls, so having SharedMemory::new() will have an initialization hit but it will be slightly faster on memory expansion.

&instruction_table,
&mut host.clone(),
);
})
});
criterion_group.finish();
}
Loading