Skip to content

Commit

Permalink
alt_block
Browse files Browse the repository at this point in the history
  • Loading branch information
hinto-janai committed Oct 31, 2024
1 parent e5db01a commit 1cf86ed
Showing 1 changed file with 63 additions and 4 deletions.
67 changes: 63 additions & 4 deletions benches/criterion/cuprate-blockchain/benches/block.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);

Expand Down Expand Up @@ -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::<Vec<AltBlockInformation>>();

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);
}

0 comments on commit 1cf86ed

Please sign in to comment.