diff --git a/Cargo.lock b/Cargo.lock index 67f55adf..869b4c84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2160,6 +2160,7 @@ dependencies = [ "chrono", "clap", "colored", + "criterion", "crossbeam-channel", "eyre", "fancy-regex", @@ -2168,6 +2169,7 @@ dependencies = [ "heimdall-common", "indicatif", "lazy_static", + "memory-stats", "rand", "reqwest 0.11.27", "serde", diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 92b60ccf..e02e382d 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -9,7 +9,6 @@ homepage.workspace = true repository.workspace = true keywords.workspace = true exclude.workspace = true -bench = false [dependencies] clap = { workspace = true, features = ["derive"] } diff --git a/crates/common/Cargo.toml b/crates/common/Cargo.toml index 2e26315d..d7852d1f 100644 --- a/crates/common/Cargo.toml +++ b/crates/common/Cargo.toml @@ -9,7 +9,6 @@ homepage.workspace = true repository.workspace = true keywords.workspace = true exclude.workspace = true -bench = false [lib] bench = false diff --git a/crates/common/src/utils/http.rs b/crates/common/src/utils/http.rs index e6bb892f..afbc9d77 100644 --- a/crates/common/src/utils/http.rs +++ b/crates/common/src/utils/http.rs @@ -17,7 +17,7 @@ static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_P /// // get_json_from_url(url, timeout).await; /// ``` pub async fn get_json_from_url(url: &str, timeout: u64) -> Result, reqwest::Error> { - _get_json_from_url(url, 0, 5, timeout).await + _get_json_from_url(url, 0, 2, timeout).await } #[async_recursion] diff --git a/crates/common/src/utils/strings.rs b/crates/common/src/utils/strings.rs index 41bbbe9f..d9327a9e 100644 --- a/crates/common/src/utils/strings.rs +++ b/crates/common/src/utils/strings.rs @@ -19,7 +19,7 @@ pub fn sign_uint(unsigned: U256) -> I256 { /// ``` pub fn decode_hex(mut s: &str) -> Result> { // normalize - s = s.trim_start_matches("0x"); + s = s.trim_start_matches("0x").trim(); if s.is_empty() { return Ok(vec![]); diff --git a/crates/config/Cargo.toml b/crates/config/Cargo.toml index c14427f2..296756d9 100644 --- a/crates/config/Cargo.toml +++ b/crates/config/Cargo.toml @@ -9,7 +9,6 @@ homepage.workspace = true repository.workspace = true keywords.workspace = true exclude.workspace = true -bench = false [lib] bench = false diff --git a/crates/core/benches/bench_cfg.rs b/crates/core/benches/bench_cfg.rs index 7f9bea12..f53e0e7a 100644 --- a/crates/core/benches/bench_cfg.rs +++ b/crates/core/benches/bench_cfg.rs @@ -13,7 +13,7 @@ fn test_cfg(c: &mut Criterion) { ]; for (name, contract) in contracts.into_iter() { - group.sample_size(100); + group.sample_size(500); group.bench_with_input(BenchmarkId::from_parameter(name), &contract, |b, c| { b.to_async::(Runtime::new().unwrap()).iter(|| async { let start = std::time::Instant::now(); diff --git a/crates/core/benches/bench_decode.rs b/crates/core/benches/bench_decode.rs index 438ae4a5..06a66a98 100644 --- a/crates/core/benches/bench_decode.rs +++ b/crates/core/benches/bench_decode.rs @@ -12,7 +12,7 @@ fn test_decode(c: &mut Criterion) { ]; for (name, calldata) in calldatas.into_iter() { - group.sample_size(500); + group.sample_size(10000); group.bench_with_input(BenchmarkId::from_parameter(name), &calldata, |b, c| { b.to_async::(Runtime::new().unwrap()).iter(|| async { let start = std::time::Instant::now(); diff --git a/crates/core/benches/bench_decompile.rs b/crates/core/benches/bench_decompile.rs index 2f43d4f0..552b3003 100644 --- a/crates/core/benches/bench_decompile.rs +++ b/crates/core/benches/bench_decompile.rs @@ -14,7 +14,7 @@ fn test_decompile(c: &mut Criterion) { // output yul for (name, contract) in contracts.into_iter() { - group.sample_size(100); + group.sample_size(500); group.bench_with_input( BenchmarkId::from_parameter(format!("yul_{}", name)), &contract, diff --git a/crates/core/benches/bench_disassemble.rs b/crates/core/benches/bench_disassemble.rs index d19f07bd..8720eaa9 100644 --- a/crates/core/benches/bench_disassemble.rs +++ b/crates/core/benches/bench_disassemble.rs @@ -13,7 +13,7 @@ fn test_disassemble(c: &mut Criterion) { ]; for (name, contract) in contracts.into_iter() { - group.sample_size(500); + group.sample_size(10000); group.bench_with_input(BenchmarkId::from_parameter(name), &contract, |b, c| { b.to_async::(Runtime::new().unwrap()).iter(|| async { let start = std::time::Instant::now(); diff --git a/crates/core/benches/bench_inspect.rs b/crates/core/benches/bench_inspect.rs index d9743e35..689da5d0 100644 --- a/crates/core/benches/bench_inspect.rs +++ b/crates/core/benches/bench_inspect.rs @@ -11,7 +11,7 @@ fn test_inspect(c: &mut Criterion) { ]; for (name, txid) in txids.into_iter() { - group.sample_size(20); + group.sample_size(100); group.bench_with_input(BenchmarkId::from_parameter(name), &txid, |b, c| { b.to_async::(Runtime::new().unwrap()).iter(|| async { let rpc_url = std::env::var("RPC_URL").unwrap_or_else(|_| { diff --git a/crates/decode/Cargo.toml b/crates/decode/Cargo.toml index 30a649df..16c5ecd0 100644 --- a/crates/decode/Cargo.toml +++ b/crates/decode/Cargo.toml @@ -9,7 +9,6 @@ homepage.workspace = true repository.workspace = true keywords.workspace = true exclude.workspace = true -bench = false [lib] bench = false diff --git a/crates/decompile/Cargo.toml b/crates/decompile/Cargo.toml index 632e6486..00f8dcb9 100644 --- a/crates/decompile/Cargo.toml +++ b/crates/decompile/Cargo.toml @@ -9,7 +9,6 @@ homepage.workspace = true repository.workspace = true keywords.workspace = true exclude.workspace = true -bench = false [lib] bench = false diff --git a/crates/disassemble/Cargo.toml b/crates/disassemble/Cargo.toml index 6be65837..578ee844 100644 --- a/crates/disassemble/Cargo.toml +++ b/crates/disassemble/Cargo.toml @@ -9,7 +9,6 @@ homepage.workspace = true repository.workspace = true keywords.workspace = true exclude.workspace = true -bench = false [lib] bench = false diff --git a/crates/dump/Cargo.toml b/crates/dump/Cargo.toml index c27e3914..d641fbe8 100644 --- a/crates/dump/Cargo.toml +++ b/crates/dump/Cargo.toml @@ -9,7 +9,6 @@ homepage.workspace = true repository.workspace = true keywords.workspace = true exclude.workspace = true -bench = false [lib] bench = false diff --git a/crates/inspect/Cargo.toml b/crates/inspect/Cargo.toml index a283afae..a17511a3 100644 --- a/crates/inspect/Cargo.toml +++ b/crates/inspect/Cargo.toml @@ -9,7 +9,6 @@ homepage.workspace = true repository.workspace = true keywords.workspace = true exclude.workspace = true -bench = false [lib] bench = false diff --git a/crates/tracing/Cargo.toml b/crates/tracing/Cargo.toml index e337f175..f1d3a938 100644 --- a/crates/tracing/Cargo.toml +++ b/crates/tracing/Cargo.toml @@ -9,7 +9,6 @@ homepage.workspace = true repository.workspace = true keywords.workspace = true exclude.workspace = true -bench = false [lib] bench = false diff --git a/crates/vm/Cargo.toml b/crates/vm/Cargo.toml index 46a78e04..08a7b270 100644 --- a/crates/vm/Cargo.toml +++ b/crates/vm/Cargo.toml @@ -9,7 +9,6 @@ homepage.workspace = true repository.workspace = true keywords.workspace = true exclude.workspace = true -bench = false [lib] bench = false @@ -43,3 +42,19 @@ hashbrown = "0.14.5" [features] step-tracing = [] experimental = [] + +[dev-dependencies] +criterion = { version = "0.5.1", features = ["async_futures", "async_tokio"] } +memory-stats = "1.0.0" + +[[bench]] +name = "bench_erc20_transfer" +harness = false + +[[bench]] +name = "bench_fib" +harness = false + +[[bench]] +name = "bench_ten_thousand_hashes" +harness = false diff --git a/crates/vm/benches/bench_erc20_transfer.rs b/crates/vm/benches/bench_erc20_transfer.rs new file mode 100644 index 00000000..8d081113 --- /dev/null +++ b/crates/vm/benches/bench_erc20_transfer.rs @@ -0,0 +1,35 @@ +use alloy::primitives::Address; +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; +use heimdall_common::utils::strings::decode_hex; +use heimdall_vm::core::vm::VM; +use tokio::runtime::Runtime; + +fn test_erc20_transfer(c: &mut Criterion) { + let mut group = c.benchmark_group("heimdall_vm"); + + group.sample_size(10000); + group.bench_function(BenchmarkId::from_parameter("erc20_transfer"), |b| { + b.to_async::(Runtime::new().unwrap()).iter(|| async { + // build the evm + let mut evm = VM::new( + &decode_hex(include_str!("./testdata/weth9.hex")).expect("invalid bytecode"), + &decode_hex("0xa9059cbb0000000000000000000000006666666b0B46056247E7D6cbdb78287F4D12574d0000000000000000000000000000000000000000000000000000000000000000").expect("invalid calldata"), + Address::default(), + Address::default(), + Address::default(), + 0, + u128::MAX, + ); + + // run the evm + let start = std::time::Instant::now(); + let _ = evm.execute().expect("evm panic"); + start.elapsed() + }); + }); + + group.finish(); +} + +criterion_group!(benches, test_erc20_transfer); +criterion_main!(benches); diff --git a/crates/vm/benches/bench_fib.rs b/crates/vm/benches/bench_fib.rs new file mode 100644 index 00000000..79afab00 --- /dev/null +++ b/crates/vm/benches/bench_fib.rs @@ -0,0 +1,45 @@ +use alloy::primitives::Address; +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; +use heimdall_common::utils::strings::decode_hex; +use heimdall_vm::core::vm::VM; +use tokio::runtime::Runtime; + +fn test_fib(c: &mut Criterion) { + let mut group = c.benchmark_group("heimdall_vm"); + + group.sample_size(500); + group.bench_function(BenchmarkId::from_parameter("fib"), |b| { + b.to_async::(Runtime::new().unwrap()).iter(|| async { + // build the evm + let mut evm = VM::new( + &decode_hex(include_str!("./testdata/fib.hex")).expect("invalid bytecode"), + &decode_hex("0x0000000000000000000000000000000000000000000000000000000000000064") + .expect("invalid calldata"), + Address::default(), + Address::default(), + Address::default(), + 0, + u128::MAX, + ); + + // run the evm + let start = std::time::Instant::now(); + let resp = evm.execute().expect("evm panic"); + + assert_eq!( + resp.returndata, + &[ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 51, + 219, 118, 167, 197, 148, 191, 195 + ] + ); + + start.elapsed() + }); + }); + + group.finish(); +} + +criterion_group!(benches, test_fib); +criterion_main!(benches); diff --git a/crates/vm/benches/bench_ten_thousand_hashes.rs b/crates/vm/benches/bench_ten_thousand_hashes.rs new file mode 100644 index 00000000..91ccc1cc --- /dev/null +++ b/crates/vm/benches/bench_ten_thousand_hashes.rs @@ -0,0 +1,37 @@ +use alloy::primitives::Address; +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; +use heimdall_common::utils::strings::decode_hex; +use heimdall_vm::core::vm::VM; +use tokio::runtime::Runtime; + +fn test_fib(c: &mut Criterion) { + let mut group = c.benchmark_group("heimdall_vm"); + + group.sample_size(500); + group.bench_function(BenchmarkId::from_parameter("ten_thousand_hashes"), |b| { + b.to_async::(Runtime::new().unwrap()).iter(|| async { + // build the evm + let mut evm = VM::new( + &decode_hex(include_str!("./testdata/ten_thousand_hashes.hex")) + .expect("invalid bytecode"), + &[], + Address::default(), + Address::default(), + Address::default(), + 0, + u128::MAX, + ); + + // run the evm + let start = std::time::Instant::now(); + let _ = evm.execute().expect("evm panic"); + + start.elapsed() + }); + }); + + group.finish(); +} + +criterion_group!(benches, test_fib); +criterion_main!(benches); diff --git a/crates/vm/benches/testdata/fib.hex b/crates/vm/benches/testdata/fib.hex new file mode 100644 index 00000000..d862c541 --- /dev/null +++ b/crates/vm/benches/testdata/fib.hex @@ -0,0 +1 @@ +0x600035600160009160025b818111601c576001019180930191600a565b505060005260206000f3 diff --git a/crates/vm/benches/testdata/ten_thousand_hashes.hex b/crates/vm/benches/testdata/ten_thousand_hashes.hex new file mode 100644 index 00000000..2455311e --- /dev/null +++ b/crates/vm/benches/testdata/ten_thousand_hashes.hex @@ -0,0 +1 @@ +0x6080604052348015600f57600080fd5b506004361060285760003560e01c80638381f58a146057575b60005b61271081101560555760408051602081018390520160408051601f1981840301905252600101602b565b005b605f60005481565b60405190815260200160405180910390f3fea26469706673582212201b47f50d734d4008a6b029e3e977af039b7a03ec9f888af4ebd131623e011e1364736f6c634300081a0033 diff --git a/crates/vm/benches/testdata/weth9.hex b/crates/vm/benches/testdata/weth9.hex new file mode 100644 index 00000000..f1226112 --- /dev/null +++ b/crates/vm/benches/testdata/weth9.hex @@ -0,0 +1 @@ +0x6060604052600436106100af576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde03146100b9578063095ea7b31461014757806318160ddd146101a157806323b872dd146101ca5780632e1a7d4d14610243578063313ce5671461026657806370a082311461029557806395d89b41146102e2578063a9059cbb14610370578063d0e30db0146103ca578063dd62ed3e146103d4575b6100b7610440565b005b34156100c457600080fd5b6100cc6104dd565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561010c5780820151818401526020810190506100f1565b50505050905090810190601f1680156101395780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561015257600080fd5b610187600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001909190505061057b565b604051808215151515815260200191505060405180910390f35b34156101ac57600080fd5b6101b461066d565b6040518082815260200191505060405180910390f35b34156101d557600080fd5b610229600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001909190505061068c565b604051808215151515815260200191505060405180910390f35b341561024e57600080fd5b61026460048080359060200190919050506109d9565b005b341561027157600080fd5b610279610b05565b604051808260ff1660ff16815260200191505060405180910390f35b34156102a057600080fd5b6102cc600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610b18565b6040518082815260200191505060405180910390f35b34156102ed57600080fd5b6102f5610b30565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561033557808201518184015260208101905061031a565b50505050905090810190601f1680156103625780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561037b57600080fd5b6103b0600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610bce565b604051808215151515815260200191505060405180910390f35b6103d2610440565b005b34156103df57600080fd5b61042a600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610be3565b6040518082815260200191505060405180910390f35b34600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055503373ffffffffffffffffffffffffffffffffffffffff167fe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c346040518082815260200191505060405180910390a2565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156105735780601f1061054857610100808354040283529160200191610573565b820191906000526020600020905b81548152906001019060200180831161055657829003601f168201915b505050505081565b600081600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60003073ffffffffffffffffffffffffffffffffffffffff1631905090565b600081600360008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101515156106dc57600080fd5b3373ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff16141580156107b457507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff600460008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205414155b156108cf5781600460008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015151561084457600080fd5b81600460008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055505b81600360008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600360008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190509392505050565b80600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410151515610a2757600080fd5b80600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055503373ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f193505050501515610ab457600080fd5b3373ffffffffffffffffffffffffffffffffffffffff167f7fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65826040518082815260200191505060405180910390a250565b600260009054906101000a900460ff1681565b60036020528060005260406000206000915090505481565b60018054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610bc65780601f10610b9b57610100808354040283529160200191610bc6565b820191906000526020600020905b815481529060010190602001808311610ba957829003601f168201915b505050505081565b6000610bdb33848461068c565b905092915050565b60046020528160005260406000206020528060005260406000206000915091505054815600a165627a7a72305820deb4c2ccab3c2fdca32ab3f46728389c2fe2c165d5fafa07661e4e004f6c344a0029