|
| 1 | +use std::hint::black_box; |
| 2 | + |
| 3 | +use bytes::Bytes; |
| 4 | +use criterion::BatchSize; |
| 5 | +use criterion::Criterion; |
| 6 | +use criterion::criterion_group; |
| 7 | +use criterion::criterion_main; |
| 8 | +use octez_riscv_durable_storage::database::Database; |
| 9 | +use octez_riscv_durable_storage::merkle_layer::Key; |
| 10 | +use octez_riscv_durable_storage::persistence_layer::utils::TestableTmpdir; |
| 11 | +use octez_riscv_durable_storage::random::generate_keys; |
| 12 | +use octez_riscv_durable_storage::random::generate_random_bytes; |
| 13 | +use octez_riscv_durable_storage::repo::DirectoryManager; |
| 14 | +use rand::prelude::*; |
| 15 | +use rand::rng; |
| 16 | +use tokio::runtime::Handle; |
| 17 | + |
| 18 | +struct BenchmarkState { |
| 19 | + database: Database, |
| 20 | + large_read_buffer: Vec<u8>, |
| 21 | + large_read_choices: Vec<usize>, |
| 22 | + prepopulated_large_node_keys: Vec<Key>, |
| 23 | + prepopulated_node_keys: Vec<Key>, |
| 24 | + prepopulated_values: Vec<Bytes>, |
| 25 | + small_read_buffer: Vec<u8>, |
| 26 | + small_read_choices: Vec<usize>, |
| 27 | + small_write_choices: Vec<(usize, usize)>, |
| 28 | +} |
| 29 | + |
| 30 | +impl BenchmarkState { |
| 31 | + fn clone_with(&self, handle: &Handle, repo: &DirectoryManager) -> Self { |
| 32 | + Self { |
| 33 | + database: self |
| 34 | + .database |
| 35 | + .try_clone_with(handle, repo) |
| 36 | + .expect("Cloning the database should work"), |
| 37 | + large_read_buffer: vec![0u8; LARGE_READ_MIN_SIZE], |
| 38 | + large_read_choices: self.large_read_choices.clone(), |
| 39 | + prepopulated_large_node_keys: self.prepopulated_large_node_keys.clone(), |
| 40 | + prepopulated_node_keys: self.prepopulated_node_keys.clone(), |
| 41 | + prepopulated_values: self.prepopulated_values.clone(), |
| 42 | + small_read_buffer: vec![0u8; SMALL_READ_SIZE], |
| 43 | + small_read_choices: self.small_read_choices.clone(), |
| 44 | + small_write_choices: self.small_write_choices.clone(), |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +const BLOCK_FREQUENCY: usize = 5_000; |
| 50 | +const ERC_20_TRANSACTIONS: usize = 10_000; |
| 51 | +const LARGE_READ_MIN_SIZE: usize = 48000; |
| 52 | +const LARGE_READ_MAX_SIZE: usize = LARGE_READ_MIN_SIZE * 2; |
| 53 | +const PREPOPULATED_LARGE_NODE_KEYS_COUNT: usize = 100; |
| 54 | +const PREPOPULATED_NODE_KEYS_COUNT: usize = 1_000_000 - PREPOPULATED_LARGE_NODE_KEYS_COUNT; |
| 55 | +const SMALL_READS: usize = 19; |
| 56 | +const SMALL_READ_SIZE: usize = 32; |
| 57 | +const SMALL_WRITES: usize = 20; |
| 58 | + |
| 59 | +fn setup_benchmark_state(handle: &Handle, repo: &DirectoryManager) -> BenchmarkState { |
| 60 | + let mut database = Database::try_new(handle, repo).expect("Creating a database should succeed"); |
| 61 | + let mut rng = rng(); |
| 62 | + |
| 63 | + const VALUES_COUNT: usize = 100; |
| 64 | + let mut prepopulated_node_keys = Vec::with_capacity(PREPOPULATED_NODE_KEYS_COUNT); |
| 65 | + let mut prepopulated_large_node_keys = Vec::with_capacity(PREPOPULATED_LARGE_NODE_KEYS_COUNT); |
| 66 | + let mut prepopulated_values = Vec::with_capacity(VALUES_COUNT); |
| 67 | + |
| 68 | + // Pre-populate a small number of values of random small sizes |
| 69 | + for _ in 0..VALUES_COUNT { |
| 70 | + let value = Bytes::from(generate_random_bytes(&mut rng, 1..32)); |
| 71 | + prepopulated_values.push(value); |
| 72 | + } |
| 73 | + |
| 74 | + // Pre-populate the database with a large number of random keys with small values |
| 75 | + let keys = generate_keys(&mut rng, PREPOPULATED_NODE_KEYS_COUNT); |
| 76 | + for key in keys { |
| 77 | + let value = prepopulated_values |
| 78 | + .choose(&mut rng) |
| 79 | + .expect("The value should exist"); |
| 80 | + database.write(key.clone(), 0, value.clone()).ok(); |
| 81 | + prepopulated_node_keys.push(key); |
| 82 | + } |
| 83 | + |
| 84 | + // Pre-populate the database with a small number of random keys with large values |
| 85 | + let keys = generate_keys(&mut rng, PREPOPULATED_LARGE_NODE_KEYS_COUNT); |
| 86 | + for key in keys { |
| 87 | + let value = Bytes::from(generate_random_bytes( |
| 88 | + &mut rng, |
| 89 | + LARGE_READ_MIN_SIZE..LARGE_READ_MAX_SIZE, |
| 90 | + )); |
| 91 | + database.write(key.clone(), 0, value).ok(); |
| 92 | + prepopulated_large_node_keys.push(key); |
| 93 | + } |
| 94 | + |
| 95 | + let large_read_choices = (0..ERC_20_TRANSACTIONS) |
| 96 | + .map(|_| rng.random_range(0..prepopulated_large_node_keys.len())) |
| 97 | + .collect(); |
| 98 | + |
| 99 | + let small_read_choices = (0..ERC_20_TRANSACTIONS * SMALL_READS) |
| 100 | + .map(|_| rng.random_range(0..prepopulated_node_keys.len())) |
| 101 | + .collect(); |
| 102 | + |
| 103 | + let small_write_choices = (0..ERC_20_TRANSACTIONS * SMALL_WRITES) |
| 104 | + .map(|_| { |
| 105 | + ( |
| 106 | + rng.random_range(0..prepopulated_node_keys.len()), |
| 107 | + rng.random_range(0..prepopulated_values.len()), |
| 108 | + ) |
| 109 | + }) |
| 110 | + .collect(); |
| 111 | + |
| 112 | + BenchmarkState { |
| 113 | + database, |
| 114 | + large_read_buffer: vec![0u8; LARGE_READ_MAX_SIZE], |
| 115 | + large_read_choices, |
| 116 | + prepopulated_large_node_keys, |
| 117 | + prepopulated_node_keys, |
| 118 | + prepopulated_values, |
| 119 | + small_read_buffer: vec![0u8; SMALL_READ_SIZE], |
| 120 | + small_read_choices, |
| 121 | + small_write_choices, |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +#[inline(never)] |
| 126 | +fn bench_run(mut state: BenchmarkState) { |
| 127 | + let large_node_keys = &state.prepopulated_large_node_keys; |
| 128 | + let node_keys = &state.prepopulated_node_keys; |
| 129 | + let values = &state.prepopulated_values; |
| 130 | + |
| 131 | + for i in 0..ERC_20_TRANSACTIONS { |
| 132 | + let key = &large_node_keys[state.large_read_choices[i]]; |
| 133 | + state |
| 134 | + .database |
| 135 | + .read(key, 0, &mut state.large_read_buffer) |
| 136 | + .expect("The read should succeed"); |
| 137 | + |
| 138 | + for _ in 0..SMALL_READS { |
| 139 | + let key = &node_keys[state.small_read_choices[i]]; |
| 140 | + state |
| 141 | + .database |
| 142 | + .read(key, 0, &mut state.small_read_buffer) |
| 143 | + .expect("The read should succeed"); |
| 144 | + } |
| 145 | + |
| 146 | + for _ in 0..SMALL_WRITES { |
| 147 | + let key = node_keys[state.small_write_choices[i].0].clone(); |
| 148 | + let value = values[state.small_write_choices[i].1].clone(); |
| 149 | + state |
| 150 | + .database |
| 151 | + .write(key, 0, value) |
| 152 | + .expect("The read should succeed"); |
| 153 | + } |
| 154 | + |
| 155 | + if i % BLOCK_FREQUENCY == 0 { |
| 156 | + black_box(state.database.hash()); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +fn database_benchmark(c: &mut Criterion) { |
| 162 | + let runtime = tokio::runtime::Builder::new_multi_thread() |
| 163 | + .worker_threads(2) |
| 164 | + .build() |
| 165 | + .expect("Creating a Tokio runtime should succeed"); |
| 166 | + |
| 167 | + let tmpdir = TestableTmpdir::new(); |
| 168 | + let repo = DirectoryManager::new(tmpdir.path()).expect("Failed to create directory manager"); |
| 169 | + let initial_state = setup_benchmark_state(runtime.handle(), &repo); |
| 170 | + |
| 171 | + let setup = || initial_state.clone_with(runtime.handle(), &repo); |
| 172 | + |
| 173 | + let mut group = c.benchmark_group("ERC-20"); |
| 174 | + group.sample_size(10); |
| 175 | + group.measurement_time(std::time::Duration::from_secs(40)); |
| 176 | + group.bench_function("10_000-transactions", |b| { |
| 177 | + b.iter_batched(setup, bench_run, BatchSize::SmallInput) |
| 178 | + }); |
| 179 | + |
| 180 | + group.finish(); |
| 181 | +} |
| 182 | + |
| 183 | +criterion_group!(benches, database_benchmark); |
| 184 | +criterion_main!(benches); |
0 commit comments