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

wip: benchmark ef test. #2084

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
103 changes: 103 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions cmd/ef_tests/blockchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ lazy_static.workspace = true

[dev-dependencies]
datatest-stable = "0.2.9"
criterion = "0.3"

[lib]
path = "./lib.rs"
Expand All @@ -38,3 +39,7 @@ harness = false
[[test]]
name = "prague"
harness = false

[[bench]]
name = "import8k"
harness = false
56 changes: 56 additions & 0 deletions cmd/ef_tests/blockchain/benches/import8k.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::path::Path;

use criterion::{criterion_group, criterion_main, Criterion};

use ef_tests_blockchain::{
test_runner::{build_store_for_test, execute_blocks, parse_test_file},
types::BlockWithRLP,
};
use ethrex_blockchain::Blockchain;
use ethrex_storage::Store;

static LARGE_TEST: (&str, &str) = ("vectors/prague/eip2935_historical_block_hashes_from_state/block_hashes/block_hashes_history.json", "tests/prague/eip2935_historical_block_hashes_from_state/test_block_hashes.py::test_block_hashes_history[fork_Prague-blockchain_test-full_history_plus_one_check_blockhash_first]");

pub fn setup_benchmark() -> (Blockchain, Vec<BlockWithRLP>, Store) {
let tests = parse_test_file(Path::new(LARGE_TEST.0));
let test = tests.get(LARGE_TEST.1).unwrap();

let store = build_store_for_test(&test);

let mut blocks = test.blocks.clone();

// print blocks length and total tx length
println!("blocks length: {}", blocks.len());
let total_txs: usize = blocks
.iter()
.map(|b| b.block().unwrap().transactions.len())
.sum();
println!("total txs: {}", total_txs);
// print avg tx per block
println!("avg tx per block: {}", total_txs / blocks.len());

let last_blocks = blocks.split_off(blocks.len() - 100);
let first_blocks = blocks;

let blockchain = Blockchain::default_with_store(store.clone());
execute_blocks(&blockchain, &first_blocks, store.clone());

(blockchain, last_blocks, store)
}

pub fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("benches");
group.sample_size(10);

group.bench_function("import blocks", |b| {
b.iter_with_setup(
|| setup_benchmark(),
|(blockchain, blocks, store)| {
execute_blocks(&blockchain, &blocks, store.clone());
},
);
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
18 changes: 11 additions & 7 deletions cmd/ef_tests/blockchain/test_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ pub fn run_ef_test(test_key: &str, test: &TestUnit) {
// Check world_state
check_prestate_against_db(test_key, test, &store);

let blockchain = Blockchain::default_with_store(store.clone());
// Execute all blocks in test
for block_fixture in test.blocks.iter() {
let blockchain = Blockchain::default_with_store(store.clone());
execute_blocks(&blockchain, &test.blocks, store.clone());

check_poststate_against_db(test_key, test, &store)
}

pub fn execute_blocks(blockchain: &Blockchain, blocks: &Vec<BlockWithRLP>, store: Store) {
for block_fixture in blocks.iter() {
let expects_exception = block_fixture.expect_exception.is_some();
if exception_in_rlp_decoding(block_fixture) {
return;
Expand All @@ -38,23 +44,21 @@ pub fn run_ef_test(test_key: &str, test: &TestUnit) {
Err(error) => {
assert!(
expects_exception,
"Transaction execution unexpectedly failed on test: {}, with error {}",
test_key, error
"Transaction execution unexpectedly failed with error {}",
error
);
return;
}
Ok(_) => {
assert!(
!expects_exception,
"Expected transaction execution to fail in test: {} with error: {}",
test_key,
"Expected transaction execution to fail with error: {}",
block_fixture.expect_exception.clone().unwrap()
);
apply_fork_choice(&store, hash, hash, hash).unwrap();
}
}
}
check_poststate_against_db(test_key, test, &store)
}

/// Tests the rlp decoding of a block
Expand Down
Loading