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

Burntpix criterion bench #1004

Merged
merged 11 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
60 changes: 56 additions & 4 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ ethers-contract = { version = "2.0.11", default-features = false }
anyhow = "1.0.79"
criterion = "0.5"
indicatif = "0.17"
alloy-sol-macro = "0.6.0"
alloy-sol-types = "0.6.0"
lazy_static = "1.4.0"
regex = "1.10.3"

[features]
default = ["std", "c-kzg", "secp256k1"]
Expand Down Expand Up @@ -86,5 +90,5 @@ required-features = ["std", "serde", "ethersdb"]

[[bench]]
name = "bench"
path = "benches/bench.rs"
path = "benches/mod.rs"
harness = false
81 changes: 81 additions & 0 deletions crates/revm/benches/burntpix/genesis_alloc.rs

Large diffs are not rendered by default.

124 changes: 124 additions & 0 deletions crates/revm/benches/burntpix/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use crate::burntpix::genesis_alloc::BURNTPIX_ADDRESS;
use crate::burntpix::genesis_alloc::GENESIS_ALLOCS;
use alloy_sol_macro::sol;
use alloy_sol_types::SolCall;
use criterion::Criterion;
use regex::bytes::Regex;
use revm::{
db::{CacheDB, EmptyDB},
primitives::{
address, hex, keccak256, AccountInfo, Bytecode, ExecutionResult, Output, TransactTo, U256,
},
Evm,
};
use revm_precompile::B256;
use std::error::Error;
use std::fs::File;
use std::time::Duration;
use std::{io::Write, str::FromStr};
pub mod genesis_alloc;

sol! {
#[derive(Debug, PartialEq, Eq)]
interface IBURNTPIX {
function run( uint32 seed, uint256 iterations) returns (string);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This is nice touch!

const DEFAULT_SEED: &str = "0";
const DEFAULT_ITERATIONS: &str = "0x7A120";

pub fn burntpix(c: &mut Criterion) {
let (seed, iterations) = try_init_env_vars().expect("Failed to parse env vars");

let run_call_data = IBURNTPIX::runCall { seed, iterations }.abi_encode();

let db = init_db();

let mut g = c.benchmark_group("burntpix");
g.noise_threshold(0.03)
.warm_up_time(Duration::from_secs(3))
.measurement_time(Duration::from_secs(130))
.sample_size(10);

let mut evm = Evm::builder()
.modify_tx_env(|tx| {
tx.caller = address!("1000000000000000000000000000000000000000");
tx.transact_to = TransactTo::Call(*BURNTPIX_ADDRESS);
tx.data = run_call_data.clone().into();
})
.with_db(db)
.build();

g.bench_function("burntpix".to_string(), |b| {
b.iter(|| evm.transact().unwrap())
});

// transact again to get the return data and create the svg
let tx_result = evm.transact().unwrap().result;
let return_data = match tx_result {
ExecutionResult::Success {
output, gas_used, ..
} => {
println!("Gas used: {:?}", gas_used);
match output {
Output::Call(value) => value,
_ => unreachable!("Unexpected output type"),
}
}
_ => unreachable!("Execution failed: {:?}", tx_result),
};

// remove returndata offset and length from output
let data = &return_data[64..];

// remove trailing zeros
let re = Regex::new(r"[0\x00]+$").unwrap();
let trimmed_data = re.replace_all(data, &[]);
let file_name = format!("{}_{}", seed, iterations);

svg(file_name, &trimmed_data).expect("Failed to store svg");
}
fn svg(filename: String, svg_data: &[u8]) -> Result<(), Box<dyn Error>> {
let current_dir = std::env::current_dir()?;
let svg_dir = current_dir.join("benches").join("burntpix").join("svgs");
std::fs::create_dir_all(&svg_dir)?;

let file_path = svg_dir.join(format!("{}.svg", filename));
let mut file = File::create(file_path)?;
file.write_all(svg_data)?;

Ok(())
}

fn try_init_env_vars() -> Result<(u32, U256), Box<dyn Error>> {
let seed_from_env = std::env::var("SEED").unwrap_or(DEFAULT_SEED.to_string());
let seed: u32 = seed_from_env.parse()?;
let iterations_from_env = std::env::var("ITERATIONS").unwrap_or(DEFAULT_ITERATIONS.to_string());
let iterations = U256::from_str(&iterations_from_env)?;
Ok((seed, iterations))
}

fn init_db() -> CacheDB<EmptyDB> {
let mut cache_db = CacheDB::new(EmptyDB::default());
for (addr, state) in GENESIS_ALLOCS.iter() {
let code = state.code.clone().expect("Code is required");
let code_hash = hex::encode(keccak256(&code));
let account_info = AccountInfo::new(
state.balance,
state.nonce.unwrap_or(0),
B256::from_str(&code_hash).unwrap(),
Bytecode::new_raw(code),
);

cache_db.insert_account_info(*addr, account_info);

if let Some(storage) = &state.storage {
for (key, value) in storage.iter() {
cache_db
.insert_account_storage(*addr, *key, *value)
.unwrap();
}
}
}
cache_db
}
1 change: 1 addition & 0 deletions crates/revm/benches/burntpix/svgs/0_500000.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions crates/revm/benches/bench.rs → crates/revm/benches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use revm::{
};
use revm_interpreter::{opcode::make_instruction_table, SharedMemory, EMPTY_SHARED_MEMORY};
use std::time::Duration;
pub mod burntpix;

fn analysis(c: &mut Criterion) {
let evm = Evm::builder()
Expand Down Expand Up @@ -133,6 +134,7 @@ criterion_group!(
analysis,
snailtracer,
transfer,
burntpix::burntpix,
);
criterion_main!(benches);

Expand Down
Loading