|
| 1 | +#![allow(missing_docs, unreachable_pub)] |
| 2 | + |
| 3 | +use alloy_primitives::{Address, B256, U256}; |
| 4 | +use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 5 | +use rand::{rngs::StdRng, Rng, SeedableRng}; |
| 6 | +use reth_db::{ |
| 7 | + test_utils::create_test_rw_db, |
| 8 | + transaction::{DbTx, DbTxMut}, |
| 9 | + Database, DatabaseEnv, |
| 10 | +}; |
| 11 | +use reth_db_api::{ |
| 12 | + models::{AccountBeforeTx, BlockNumberAddress}, |
| 13 | + tables, |
| 14 | +}; |
| 15 | +use reth_primitives_traits::{Account, StorageEntry}; |
| 16 | +use reth_trie::{HashedPostStateSorted, KeccakKeyHasher}; |
| 17 | +use reth_trie_db::DatabaseHashedPostState; |
| 18 | +use std::sync::Arc; |
| 19 | + |
| 20 | +// Populate a temporary database with synthetic revert data. |
| 21 | +type TestDb = Arc<reth_db::test_utils::TempDatabase<DatabaseEnv>>; |
| 22 | + |
| 23 | +#[derive(Clone, Copy, Debug)] |
| 24 | +enum Distribution { |
| 25 | + Uniform(usize), |
| 26 | + Skewed { light: usize, heavy: usize }, |
| 27 | +} |
| 28 | + |
| 29 | +fn seed_db(num_accounts: usize, dist: Distribution) -> TestDb { |
| 30 | + let db = create_test_rw_db(); |
| 31 | + let tx = db.tx_mut().expect("failed to create rw tx"); |
| 32 | + |
| 33 | + let mut rng = StdRng::seed_from_u64(12345); |
| 34 | + let block = 1; |
| 35 | + |
| 36 | + for idx in 0..num_accounts { |
| 37 | + let address = Address::random(); |
| 38 | + |
| 39 | + let account = |
| 40 | + Account { nonce: idx as u64, balance: U256::from(idx as u64), bytecode_hash: None }; |
| 41 | + |
| 42 | + tx.put::<tables::AccountChangeSets>( |
| 43 | + block, |
| 44 | + AccountBeforeTx { address, info: Some(account) }, |
| 45 | + ) |
| 46 | + .expect("failed to insert account changeset"); |
| 47 | + |
| 48 | + let slots_count = match dist { |
| 49 | + Distribution::Uniform(n) => n, |
| 50 | + Distribution::Skewed { light, heavy } => { |
| 51 | + if rng.random_bool(0.05) { |
| 52 | + heavy |
| 53 | + } else { |
| 54 | + light |
| 55 | + } |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + for slot_idx in 0..slots_count { |
| 60 | + let slot_key = B256::random(); |
| 61 | + let value = U256::from(slot_idx); |
| 62 | + |
| 63 | + tx.put::<tables::StorageChangeSets>( |
| 64 | + BlockNumberAddress((block, address)), |
| 65 | + StorageEntry { key: slot_key, value }, |
| 66 | + ) |
| 67 | + .expect("failed to insert storage changeset"); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + tx.commit().expect("failed to commit seeded db"); |
| 72 | + db |
| 73 | +} |
| 74 | + |
| 75 | +fn bench_db_execution(c: &mut Criterion) { |
| 76 | + let mut group = c.benchmark_group("Integration_DB_Sort"); |
| 77 | + group.measurement_time(std::time::Duration::from_secs(10)); |
| 78 | + |
| 79 | + // Scenarios to test: |
| 80 | + // - Below Threshold: Should be identical (Sequential) |
| 81 | + // - Above Threshold: Should show some speedup with feature flag |
| 82 | + // - Skewed: Should show speedup with feature flag |
| 83 | + let scenarios = vec![ |
| 84 | + ("Small_Under_Threshold", 1_000, Distribution::Uniform(50)), |
| 85 | + ("Large_Uniform", 10_000, Distribution::Uniform(20)), |
| 86 | + ("Large_Skewed", 10_000, Distribution::Skewed { light: 4, heavy: 2_000 }), |
| 87 | + ]; |
| 88 | + |
| 89 | + for (name, accounts, dist) in scenarios { |
| 90 | + let db = seed_db(accounts, dist); |
| 91 | + let range = 1..=1; |
| 92 | + |
| 93 | + group.bench_function(BenchmarkId::new("from_reverts", name), |b| { |
| 94 | + b.iter(|| { |
| 95 | + let tx = db.tx().expect("failed to create ro tx"); |
| 96 | + |
| 97 | + let state = |
| 98 | + HashedPostStateSorted::from_reverts::<KeccakKeyHasher>(&tx, range.clone()) |
| 99 | + .expect("failed to calculate state"); |
| 100 | + |
| 101 | + black_box(state); |
| 102 | + }); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + group.finish(); |
| 107 | +} |
| 108 | + |
| 109 | +criterion_group!(benches, bench_db_execution); |
| 110 | +criterion_main!(benches); |
0 commit comments