revme/cmd/bench/
gas_cost_estimator.rs

1use context::TxEnv;
2use criterion::Criterion;
3use database::{BenchmarkDB, BENCH_CALLER, BENCH_TARGET};
4use revm::{
5    bytecode::Bytecode,
6    primitives::{hex, Bytes, TxKind},
7    Context, ExecuteEvm, MainBuilder, MainContext,
8};
9use std::io::Cursor;
10
11pub fn run(criterion: &mut Criterion) {
12    //let bytecode = Bytecode::new_raw(Bytes::from(hex::decode(BYTES).unwrap()));
13
14    let mut rdr = csv::Reader::from_reader(Cursor::new(SAMPLE_CSV));
15    for result in rdr.records() {
16        let result = result.expect("Failed to read record");
17        let name = &result[0];
18        let bytecode_hex = &result[3];
19        let Ok(hex) = hex::decode(bytecode_hex) else {
20            continue;
21        };
22        let bytecode = Bytecode::new_raw(Bytes::from(hex));
23
24        let mut evm = Context::mainnet()
25            .with_db(BenchmarkDB::new_bytecode(bytecode.clone()))
26            .modify_cfg_chained(|c| c.disable_nonce_check = true)
27            .build_mainnet();
28
29        let tx = TxEnv::builder()
30            .caller(BENCH_CALLER)
31            .kind(TxKind::Call(BENCH_TARGET))
32            .gas_limit(1_000_000_000)
33            .build()
34            .unwrap();
35
36        criterion.bench_function(name, |b| {
37            b.iter_batched(
38                || {
39                    // create a transaction input
40                    tx.clone()
41                },
42                |input| {
43                    let _ = evm.transact_one(input).unwrap();
44                },
45                criterion::BatchSize::SmallInput,
46            );
47        });
48    }
49}
50
51const SAMPLE_CSV: &str = include_str!("gas_cost_estimator_sample.csv");