From 1cf86ed5c03d9d9ff0dd2203ff9817846a570f73 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Thu, 31 Oct 2024 19:44:09 -0400 Subject: [PATCH] alt_block --- .../cuprate-blockchain/benches/block.rs | 67 +++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/benches/criterion/cuprate-blockchain/benches/block.rs b/benches/criterion/cuprate-blockchain/benches/block.rs index 2a3c55a..a03bf18 100644 --- a/benches/criterion/cuprate-blockchain/benches/block.rs +++ b/benches/criterion/cuprate-blockchain/benches/block.rs @@ -1,19 +1,20 @@ -//! Benchmarks for [`block`] functions. +//! Benchmarks for [`block`] and [`alt_block`] functions. #![allow(unused_attributes, unused_crate_dependencies)] -use std::time::Instant; +use std::{num::NonZeroU64, time::Instant}; use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use cuprate_helper::cast::usize_to_u64; use function_name::named; use cuprate_blockchain::{ cuprate_database::{Env, EnvInner}, - ops::block, + ops::{alt_block, block}, tables::OpenTables, }; use cuprate_test_utils::data::{BLOCK_V16_TX0, BLOCK_V1_TX2, BLOCK_V9_TX3}; -use cuprate_types::VerifiedBlockInformation; +use cuprate_types::{AltBlockInformation, ChainId, VerifiedBlockInformation}; use cuprate_criterion_blockchain::generate_fake_blocks; @@ -24,6 +25,9 @@ criterion_group! { add_block_v1_tx2, add_block_v9_tx3, add_block_v16_tx0, + add_alt_block_v1_tx2, + add_alt_block_v9_tx3, + add_alt_block_v16_tx0, } criterion_main!(benches); @@ -65,3 +69,58 @@ fn add_block_v9_tx3(c: &mut Criterion) { fn add_block_v16_tx0(c: &mut Criterion) { add_block_inner(c, function_name!(), &BLOCK_V16_TX0); } + +/// Inner function for benchmarking [`alt_block::add_alt_block`]. +#[expect(clippy::significant_drop_tightening)] +fn add_alt_block_inner(c: &mut Criterion, function_name: &str, block: &VerifiedBlockInformation) { + let env = cuprate_criterion_blockchain::TmpEnv::new(); + + c.bench_function(function_name, |b| { + // We use `iter_custom` because we need to generate an + // appropriate amount of blocks and only time the `add_block`. + b.iter_custom(|count| { + // Map the block to a fake alt block. + let blocks = generate_fake_blocks(block, count) + .into_iter() + .enumerate() + .map(|(i, b)| AltBlockInformation { + block: b.block, + block_blob: b.block_blob, + txs: b.txs, + block_hash: b.block_hash, + pow_hash: b.pow_hash, + height: b.height, + weight: b.weight, + long_term_weight: b.long_term_weight, + cumulative_difficulty: b.cumulative_difficulty, + chain_id: ChainId(NonZeroU64::new(usize_to_u64(i) + 1).unwrap()), + }) + .collect::>(); + + let env_inner = env.env.env_inner(); + let tx_rw = env_inner.tx_rw().unwrap(); + let mut tables = env_inner.open_tables_mut(&tx_rw).unwrap(); + + let start = Instant::now(); + for block in &blocks { + black_box(alt_block::add_alt_block(block, &mut tables)).unwrap(); + } + start.elapsed() + }); + }); +} + +#[named] +fn add_alt_block_v1_tx2(c: &mut Criterion) { + add_alt_block_inner(c, function_name!(), &BLOCK_V1_TX2); +} + +#[named] +fn add_alt_block_v9_tx3(c: &mut Criterion) { + add_alt_block_inner(c, function_name!(), &BLOCK_V9_TX3); +} + +#[named] +fn add_alt_block_v16_tx0(c: &mut Criterion) { + add_alt_block_inner(c, function_name!(), &BLOCK_V16_TX0); +}