From 9a062c0844be8ea3c3c96d20e4478fd75769566c Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 31 Dec 2024 03:42:07 +0100 Subject: [PATCH] chore(bench): more determinism (#13603) --- .github/workflows/bench.yml | 1 + crates/primitives/benches/validate_blob_tx.rs | 14 ++------- crates/transaction-pool/benches/truncate.rs | 29 ++++++------------- crates/trie/common/benches/prefix_set.rs | 4 ++- crates/trie/parallel/benches/root.rs | 2 +- crates/trie/sparse/benches/rlp_node.rs | 2 +- crates/trie/sparse/benches/root.rs | 2 ++ crates/trie/trie/benches/hash_post_state.rs | 2 +- testing/testing-utils/src/generators.rs | 9 +++--- 9 files changed, 24 insertions(+), 41 deletions(-) diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index c96c3b6f8dd6..0215bf304c11 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -10,6 +10,7 @@ on: env: CARGO_TERM_COLOR: always BASELINE: base + SEED: reth concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} diff --git a/crates/primitives/benches/validate_blob_tx.rs b/crates/primitives/benches/validate_blob_tx.rs index b38937508f8d..0fdff23ad7b7 100644 --- a/crates/primitives/benches/validate_blob_tx.rs +++ b/crates/primitives/benches/validate_blob_tx.rs @@ -4,20 +4,12 @@ use alloy_consensus::TxEip4844; use alloy_eips::eip4844::{ env_settings::EnvKzgSettings, BlobTransactionSidecar, MAX_BLOBS_PER_BLOCK, }; -use alloy_primitives::hex; use criterion::{ criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion, }; -use proptest::{ - prelude::*, - strategy::ValueTree, - test_runner::{RngAlgorithm, TestRng, TestRunner}, -}; +use proptest::{prelude::*, strategy::ValueTree, test_runner::TestRunner}; use proptest_arbitrary_interop::arb; -// constant seed to use for the rng -const SEED: [u8; 32] = hex!("1337133713371337133713371337133713371337133713371337133713371337"); - /// Benchmarks EIP-48444 blob validation. fn blob_validation(c: &mut Criterion) { let mut group = c.benchmark_group("Blob Transaction KZG validation"); @@ -35,9 +27,7 @@ fn validate_blob_tx( kzg_settings: EnvKzgSettings, ) { let setup = || { - let config = ProptestConfig::default(); - let rng = TestRng::from_seed(RngAlgorithm::ChaCha, &SEED); - let mut runner = TestRunner::new_with_rng(config, rng); + let mut runner = TestRunner::deterministic(); // generate tx and sidecar let mut tx = arb::().new_tree(&mut runner).unwrap().current(); diff --git a/crates/transaction-pool/benches/truncate.rs b/crates/transaction-pool/benches/truncate.rs index 3c8f807efc3d..7295b0144efb 100644 --- a/crates/transaction-pool/benches/truncate.rs +++ b/crates/transaction-pool/benches/truncate.rs @@ -1,27 +1,20 @@ #![allow(missing_docs)] -use alloy_primitives::{hex_literal::hex, Address}; +use alloy_primitives::Address; use criterion::{ criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion, }; use pprof::criterion::{Output, PProfProfiler}; -use proptest::{ - prelude::*, - strategy::ValueTree, - test_runner::{RngAlgorithm, TestRng, TestRunner}, -}; +use proptest::{prelude::*, strategy::ValueTree, test_runner::TestRunner}; use reth_transaction_pool::{ pool::{BasefeeOrd, ParkedPool, PendingPool, QueuedOrd}, test_utils::{MockOrdering, MockTransaction, MockTransactionFactory}, SubPoolLimit, }; -// constant seed to use for the rng -const SEED: [u8; 32] = hex!("1337133713371337133713371337133713371337133713371337133713371337"); - /// Generates a set of `depth` dependent transactions, with the specified sender. Its values are /// generated using [Arbitrary]. fn create_transactions_for_sender( - mut runner: TestRunner, + runner: &mut TestRunner, sender: Address, depth: usize, ) -> Vec { @@ -32,10 +25,8 @@ fn create_transactions_for_sender( assert!(depth > 0); // make sure these are all post-eip-1559 transactions - let mut txs = prop::collection::vec(any::(), depth) - .new_tree(&mut runner) - .unwrap() - .current(); + let mut txs = + prop::collection::vec(any::(), depth).new_tree(runner).unwrap().current(); for (nonce, tx) in txs.iter_mut().enumerate() { // reject pre-eip1559 tx types, if there is a legacy tx, replace it with an eip1559 tx @@ -43,8 +34,8 @@ fn create_transactions_for_sender( *tx = MockTransaction::eip1559(); // set fee values using arbitrary - tx.set_priority_fee(any::().new_tree(&mut runner).unwrap().current()); - tx.set_max_fee(any::().new_tree(&mut runner).unwrap().current()); + tx.set_priority_fee(any::().new_tree(runner).unwrap().current()); + tx.set_max_fee(any::().new_tree(runner).unwrap().current()); } tx.set_sender(sender); @@ -62,9 +53,7 @@ fn create_transactions_for_sender( /// /// This uses [`create_transactions_for_sender`] to generate the transactions. fn generate_many_transactions(senders: usize, max_depth: usize) -> Vec { - let config = ProptestConfig::default(); - let rng = TestRng::from_seed(RngAlgorithm::ChaCha, &SEED); - let mut runner = TestRunner::new_with_rng(config, rng); + let mut runner = TestRunner::deterministic(); let mut txs = Vec::with_capacity(senders); for idx in 0..senders { @@ -79,7 +68,7 @@ fn generate_many_transactions(senders: usize, max_depth: usize) -> Vec>(); let sender = Address::from_slice(&addr_slice); - txs.extend(create_transactions_for_sender(runner.clone(), sender, depth)); + txs.extend(create_transactions_for_sender(&mut runner, sender, depth)); } txs diff --git a/crates/trie/common/benches/prefix_set.rs b/crates/trie/common/benches/prefix_set.rs index 7ae337e77c04..5e8cf65e45c8 100644 --- a/crates/trie/common/benches/prefix_set.rs +++ b/crates/trie/common/benches/prefix_set.rs @@ -2,6 +2,7 @@ use criterion::{ criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion, }; +use prop::test_runner::TestRng; use proptest::{ prelude::*, strategy::ValueTree, @@ -116,7 +117,8 @@ fn generate_test_data(size: usize) -> (Vec, Vec, Vec) { use prop::collection::vec; let config = ProptestConfig { result_cache: basic_result_cache, ..Default::default() }; - let mut runner = TestRunner::new(config); + let rng = TestRng::deterministic_rng(config.rng_algorithm); + let mut runner = TestRunner::new_with_rng(config, rng); let vec_of_nibbles = |range| vec(any_with::(range), size); let mut preload = vec_of_nibbles(32usize.into()).new_tree(&mut runner).unwrap().current(); diff --git a/crates/trie/parallel/benches/root.rs b/crates/trie/parallel/benches/root.rs index fd2c86e7c5a9..ca96abbbb214 100644 --- a/crates/trie/parallel/benches/root.rs +++ b/crates/trie/parallel/benches/root.rs @@ -78,7 +78,7 @@ pub fn calculate_state_root(c: &mut Criterion) { fn generate_test_data(size: usize) -> (HashedPostState, HashedPostState) { let storage_size = 1_000; - let mut runner = TestRunner::new(ProptestConfig::default()); + let mut runner = TestRunner::deterministic(); use proptest::{collection::hash_map, sample::subsequence}; let db_state = hash_map( diff --git a/crates/trie/sparse/benches/rlp_node.rs b/crates/trie/sparse/benches/rlp_node.rs index ac60f562bb00..bebf1cbb989e 100644 --- a/crates/trie/sparse/benches/rlp_node.rs +++ b/crates/trie/sparse/benches/rlp_node.rs @@ -10,7 +10,7 @@ use reth_trie::Nibbles; use reth_trie_sparse::RevealedSparseTrie; fn update_rlp_node_level(c: &mut Criterion) { - let mut rng = generators::rng_with_seed(&12345_u16.to_be_bytes()); + let mut rng = generators::rng(); let mut group = c.benchmark_group("update rlp node level"); group.sample_size(20); diff --git a/crates/trie/sparse/benches/root.rs b/crates/trie/sparse/benches/root.rs index 456cc2a78b0c..e01f9825d8ee 100644 --- a/crates/trie/sparse/benches/root.rs +++ b/crates/trie/sparse/benches/root.rs @@ -169,6 +169,7 @@ fn calculate_root_from_leaves_repeated(c: &mut Criterion) { trie_updates.finalize(hb, node_iter.walker.take_removed_keys()); } } + (storage, storage_updates, trie_updates) }, ) }); @@ -205,6 +206,7 @@ fn calculate_root_from_leaves_repeated(c: &mut Criterion) { } sparse.root().unwrap(); } + sparse }, ) }); diff --git a/crates/trie/trie/benches/hash_post_state.rs b/crates/trie/trie/benches/hash_post_state.rs index 129bad5a6780..a50b3cd15bc6 100644 --- a/crates/trie/trie/benches/hash_post_state.rs +++ b/crates/trie/trie/benches/hash_post_state.rs @@ -51,7 +51,7 @@ fn from_bundle_state_seq(state: &HashMap) -> HashedPostS fn generate_test_data(size: usize) -> HashMap { let storage_size = 1_000; - let mut runner = TestRunner::new(ProptestConfig::default()); + let mut runner = TestRunner::deterministic(); use proptest::collection::hash_map; let state = hash_map( diff --git a/testing/testing-utils/src/generators.rs b/testing/testing-utils/src/generators.rs index bcaeeb81b8eb..a6610afe9361 100644 --- a/testing/testing-utils/src/generators.rs +++ b/testing/testing-utils/src/generators.rs @@ -18,8 +18,7 @@ use reth_primitives::{ use secp256k1::{Keypair, Secp256k1}; use std::{ cmp::{max, min}, - collections::{hash_map::DefaultHasher, BTreeMap}, - hash::Hasher, + collections::BTreeMap, ops::{Range, RangeInclusive}, }; @@ -77,9 +76,9 @@ pub fn rng() -> StdRng { /// Returns a random number generator from a specific seed, as bytes. pub fn rng_with_seed(seed: &[u8]) -> StdRng { - let mut hasher = DefaultHasher::new(); - hasher.write(seed); - StdRng::seed_from_u64(hasher.finish()) + let mut seed_bytes = [0u8; 32]; + seed_bytes[..seed.len().min(32)].copy_from_slice(seed); + StdRng::from_seed(seed_bytes) } /// Generates a range of random [`SealedHeader`]s.