From fcdcb8d53f34013ffecfe93d7ee80edd3df2244b Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 31 Dec 2024 01:40:43 +0100 Subject: [PATCH] chore(bench): more determinism --- .github/workflows/bench.yml | 1 + crates/transaction-pool/benches/truncate.rs | 29 +++++++-------------- crates/trie/parallel/benches/root.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 +++---- 6 files changed, 18 insertions(+), 27 deletions(-) diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index c96c3b6f8dd6a..0215bf304c11f 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/transaction-pool/benches/truncate.rs b/crates/transaction-pool/benches/truncate.rs index 3c8f807efc3d8..7295b0144efb6 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/parallel/benches/root.rs b/crates/trie/parallel/benches/root.rs index fd2c86e7c5a98..ca96abbbb2147 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/root.rs b/crates/trie/sparse/benches/root.rs index 456cc2a78b0c9..e01f9825d8ee2 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 129bad5a6780e..a50b3cd15bc65 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 bcaeeb81b8eb2..a6610afe9361e 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.