Skip to content

Commit

Permalink
chore(bench): more determinism
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Dec 31, 2024
1 parent e9332f9 commit fcdcb8d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 27 deletions.
1 change: 1 addition & 0 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
env:
CARGO_TERM_COLOR: always
BASELINE: base
SEED: reth

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
Expand Down
29 changes: 9 additions & 20 deletions crates/transaction-pool/benches/truncate.rs
Original file line number Diff line number Diff line change
@@ -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<MockTransaction> {
Expand All @@ -32,19 +25,17 @@ fn create_transactions_for_sender(
assert!(depth > 0);

// make sure these are all post-eip-1559 transactions
let mut txs = prop::collection::vec(any::<MockTransaction>(), depth)
.new_tree(&mut runner)
.unwrap()
.current();
let mut txs =
prop::collection::vec(any::<MockTransaction>(), 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
if tx.is_legacy() || tx.is_eip2930() {
*tx = MockTransaction::eip1559();

// set fee values using arbitrary
tx.set_priority_fee(any::<u128>().new_tree(&mut runner).unwrap().current());
tx.set_max_fee(any::<u128>().new_tree(&mut runner).unwrap().current());
tx.set_priority_fee(any::<u128>().new_tree(runner).unwrap().current());
tx.set_max_fee(any::<u128>().new_tree(runner).unwrap().current());
}

tx.set_sender(sender);
Expand All @@ -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<MockTransaction> {
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 {
Expand All @@ -79,7 +68,7 @@ fn generate_many_transactions(senders: usize, max_depth: usize) -> Vec<MockTrans
let addr_slice = [0u8; 12].into_iter().chain(idx_slice.into_iter()).collect::<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
Expand Down
2 changes: 1 addition & 1 deletion crates/trie/parallel/benches/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions crates/trie/sparse/benches/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
)
});
Expand Down Expand Up @@ -205,6 +206,7 @@ fn calculate_root_from_leaves_repeated(c: &mut Criterion) {
}
sparse.root().unwrap();
}
sparse
},
)
});
Expand Down
2 changes: 1 addition & 1 deletion crates/trie/trie/benches/hash_post_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn from_bundle_state_seq(state: &HashMap<Address, BundleAccount>) -> HashedPostS

fn generate_test_data(size: usize) -> HashMap<Address, BundleAccount> {
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(
Expand Down
9 changes: 4 additions & 5 deletions testing/testing-utils/src/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit fcdcb8d

Please sign in to comment.