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

feat: make benchmark inputs deterministic #13536

Draft
wants to merge 10 commits 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
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/engine/tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ reth-stages = { workspace = true, optional = true }
reth-static-file = { workspace = true, optional = true }
reth-tracing = { workspace = true, optional = true }

proptest.workspace = true


[dev-dependencies]
# reth
reth-chain-state = { workspace = true, features = ["test-utils"] }
Expand Down
8 changes: 6 additions & 2 deletions crates/engine/tree/benches/channel_perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#![allow(missing_docs)]

use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
use proptest::test_runner::TestRunner;
use rand::Rng;
use revm_primitives::{
Account, AccountInfo, AccountStatus, Address, EvmState, EvmStorage, EvmStorageSlot, HashMap,
B256, U256,
Expand All @@ -11,6 +13,8 @@ use std::{hint::black_box, thread};

/// Creates a mock state with the specified number of accounts for benchmarking
fn create_bench_state(num_accounts: usize) -> EvmState {
let mut runner = TestRunner::deterministic();
let mut rng = runner.rng().clone();
let mut state_changes = HashMap::default();

for i in 0..num_accounts {
Expand All @@ -21,14 +25,14 @@ fn create_bench_state(num_accounts: usize) -> EvmState {
info: AccountInfo {
balance: U256::from(100),
nonce: 10,
code_hash: B256::random(),
code_hash: B256::from_slice(&rng.gen::<[u8; 32]>()),
code: Default::default(),
},
storage,
status: AccountStatus::Loaded,
};

let address = Address::random();
let address = Address::with_last_byte(i as u8);
state_changes.insert(address, account);
}

Expand Down
6 changes: 4 additions & 2 deletions crates/engine/tree/benches/state_root_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(missing_docs)]

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use proptest::test_runner::TestRunner;
use reth_engine_tree::tree::root::{StateRootConfig, StateRootTask};
use reth_evm::system_calls::OnStateHook;
use reth_primitives::{Account as RethAccount, StorageEntry};
Expand All @@ -12,7 +13,7 @@ use reth_provider::{
test_utils::{create_test_provider_factory, MockNodeTypesWithDB},
AccountReader, HashingWriter, ProviderFactory,
};
use reth_testing_utils::generators::{self, Rng};
use reth_testing_utils::generators::Rng;
use reth_trie::{
hashed_cursor::HashedPostStateCursorFactory, proof::ProofBlindedProviderFactory,
trie_cursor::InMemoryTrieCursorFactory, TrieInput,
Expand All @@ -35,7 +36,8 @@ struct BenchParams {
/// Generates a series of random state updates with configurable accounts,
/// storage, and self-destructs
fn create_bench_state_updates(params: &BenchParams) -> Vec<EvmState> {
let mut rng = generators::rng();
let mut runner = TestRunner::deterministic();
let mut rng = runner.rng().clone();
let all_addresses: Vec<Address> = (0..params.num_accounts).map(|_| rng.gen()).collect();
let mut updates = Vec::new();

Expand Down
10 changes: 7 additions & 3 deletions crates/net/network/benches/broadcast.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#![allow(missing_docs)]
use alloy_primitives::U256;
use alloy_primitives::{
private::proptest::test_runner::{RngAlgorithm, TestRng},
U256,
};
use criterion::*;
use futures::StreamExt;
use pprof::criterion::{Output, PProfProfiler};
use rand::thread_rng;
use reth_network::{test_utils::Testnet, NetworkEventListenerProvider};
use reth_network_api::Peers;
use reth_provider::test_utils::{ExtendedAccount, MockEthProvider};
Expand Down Expand Up @@ -49,7 +51,9 @@ pub fn broadcast_ingress_bench(c: &mut Criterion) {
}

// prepare some transactions
let mut gen = TransactionGenerator::new(thread_rng());
let mut gen = TransactionGenerator::new(TestRng::deterministic_rng(
RngAlgorithm::ChaCha,
));
let num_broadcasts = 10;
for _ in 0..num_broadcasts {
for _ in 0..2 {
Expand Down
12 changes: 9 additions & 3 deletions crates/net/network/benches/tx_manager_hash_fetching.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#![allow(missing_docs)]
use alloy_primitives::U256;

use alloy_primitives::{
private::proptest::test_runner::{RngAlgorithm, TestRng},
U256,
};
use criterion::*;
use pprof::criterion::{Output, PProfProfiler};
use rand::thread_rng;
use reth_network::{
test_utils::Testnet,
transactions::{
Expand Down Expand Up @@ -61,7 +64,10 @@ pub fn tx_fetch_bench(c: &mut Criterion) {
let peer_pool = peer.pool().unwrap();

for _ in 0..num_tx_per_peer {
let mut gen = TransactionGenerator::new(thread_rng());
let mut gen = TransactionGenerator::new(
TestRng::deterministic_rng(RngAlgorithm::ChaCha),
);

let tx = gen.gen_eip1559_pooled();
let sender = tx.sender();
provider.add_account(
Expand Down
16 changes: 8 additions & 8 deletions crates/stages/stages/benches/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ pub(crate) fn stage_unwind<

// Clear previous run
stage
.unwind(&provider, unwind)
.map_err(|e| {
format!(
"{e}\nMake sure your test database at `{}` isn't too old and incompatible with newer stage changes.",
db.factory.db_ref().path().display()
)
})
.unwrap();
.unwind(&provider, unwind)
.map_err(|e| {
format!(
"{e}\nMake sure your test database at `{}` isn't too old and incompatible with newer stage changes.",
db.factory.db_ref().path().display()
)
})
.unwrap();

provider.commit().unwrap();
})
Expand Down
11 changes: 7 additions & 4 deletions crates/storage/db/benches/hash_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use criterion::{
use pprof::criterion::{Output, PProfProfiler};
use proptest::{
arbitrary::Arbitrary,
prelude::{any_with, ProptestConfig},
prelude::any_with,
strategy::{Strategy, ValueTree},
test_runner::TestRunner,
test_runner::{Config, RngAlgorithm, TestRng, TestRunner},
};
use reth_db::{test_utils::create_test_rw_db_with_path, DatabaseEnv, TransactionHashNumbers};
use reth_db_api::{
Expand All @@ -21,7 +21,6 @@ use reth_db_api::{
};
use reth_fs_util as fs;
use std::hint::black_box;

mod utils;
use utils::*;

Expand Down Expand Up @@ -164,7 +163,11 @@ where
.no_shrink()
.boxed();

let mut runner = TestRunner::new(ProptestConfig::default());
// Use a deterministic TestRunner
let mut runner = TestRunner::new_with_rng(
Config::default(),
TestRng::deterministic_rng(RngAlgorithm::ChaCha),
);
let mut preload = strategy.new_tree(&mut runner).unwrap().current();
let mut input = strategy.new_tree(&mut runner).unwrap().current();

Expand Down
Loading