Skip to content

Commit

Permalink
[fix] #3905: Fix apply_blocks and validate_blocks benchmark
Browse files Browse the repository at this point in the history
Signed-off-by: Shanin Roman <[email protected]>
  • Loading branch information
Erigara committed Sep 26, 2023
1 parent 8c0fcec commit 87c165c
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 338 deletions.
8 changes: 4 additions & 4 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,22 @@ harness = false
[[bench]]
name = "apply_blocks"
harness = false
path = "benches/apply_blocks/benchmark.rs"
path = "benches/blocks/apply_blocks_benchmark.rs"

[[bench]]
name = "validate_blocks"
harness = false
path = "benches/validate_blocks/benchmark.rs"
path = "benches/blocks/validate_blocks_benchmark.rs"

[[example]]
name = "apply_blocks"
harness = false
path = "benches/apply_blocks/oneshot.rs"
path = "benches/blocks/apply_blocks_oneshot.rs"

[[example]]
name = "validate_blocks"
harness = false
path = "benches/validate_blocks/oneshot.rs"
path = "benches/blocks/validate_blocks_oneshot.rs"

[package.metadata.cargo-all-features]
denylist = [
Expand Down
212 changes: 0 additions & 212 deletions core/benches/apply_blocks/apply_blocks.rs

This file was deleted.

15 changes: 0 additions & 15 deletions core/benches/apply_blocks/oneshot.rs

This file was deleted.

79 changes: 79 additions & 0 deletions core/benches/blocks/apply_blocks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#![allow(missing_docs, clippy::restriction)]

use eyre::Result;
use iroha_core::{block::CommittedBlock, prelude::*};
use iroha_data_model::prelude::*;

#[path = "./common.rs"]
mod common;

use common::*;

pub struct WsvApplyBlocks {
wsv: WorldStateView,
blocks: Vec<CommittedBlock>,
}

impl WsvApplyBlocks {
/// Create [`WorldStateView`] and blocks for benchmarking
///
/// # Errors
/// - Failed to parse [`AccountId`]
/// - Failed to generate [`KeyPair`]
/// - Failed to create instructions for block
pub fn setup() -> Result<Self> {
let domains = 100;
let accounts_per_domain = 1000;
let assets_per_domain = 1000;
let account_id: AccountId = "alice@wonderland".parse()?;
let key_pair = KeyPair::generate()?;
let wsv = build_wsv(&account_id, &key_pair);

let nth = 100;
let instructions = [
populate_wsv(domains, accounts_per_domain, assets_per_domain, &account_id),
delete_every_nth(domains, accounts_per_domain, assets_per_domain, nth),
restore_every_nth(domains, accounts_per_domain, assets_per_domain, nth),
]
.into_iter()
.collect::<Result<Vec<_>, _>>()?;

let blocks = {
// Clone wsv because it will be changed during creation of block
let mut wsv = wsv.clone();
instructions
.into_iter()
.map(|instructions| {
let block =
create_block(&mut wsv, instructions, account_id.clone(), key_pair.clone());
wsv.apply_without_execution(&block).map(|_| block)
})
.collect::<Result<Vec<_>, _>>()?
};

Ok(Self { wsv, blocks })
}

/// Run benchmark body.
///
/// # Errors
/// - Not enough blocks
/// - Failed to apply block
///
/// # Panics
/// If wsv isn't one block ahead of finalized wsv.
pub fn measure(Self { wsv, blocks }: &Self) -> Result<()> {
let mut finalized_wsv = wsv.clone();
let mut wsv = finalized_wsv.clone();

assert_eq!(wsv.height(), 0);
for (block, i) in blocks.iter().zip(1..) {
finalized_wsv = wsv.clone();
wsv.apply(block)?;
assert_eq!(wsv.height(), i);
assert_eq!(wsv.height(), finalized_wsv.height() + 1);
}

Ok(())
}
}
File renamed without changes.
31 changes: 31 additions & 0 deletions core/benches/blocks/apply_blocks_oneshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! Oneshot execution of `apply_blocks` benchmark.
//! Can be useful to profile using flamegraph.
//!
//! ```bash
//! CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph --root --release --example apply_blocks
//! ```
mod apply_blocks;

use apply_blocks::WsvApplyBlocks;
use iroha_config::base::proxy::Builder;
use iroha_data_model::Level;
use iroha_logger::{Configuration, ConfigurationProxy};

fn main() {
let log_config = Configuration {
max_log_level: Level::INFO.into(),
compact_mode: false,
..ConfigurationProxy::default()
.build()
.expect("Default logger config should always build")
};
// Can't use logger because it's failed to initialize.
#[allow(clippy::print_stderr)]
if let Err(err) = iroha_logger::init(&log_config) {
eprintln!("Failed to initialize logger: {err}");
}
iroha_logger::info!("Starting...");
let bench = WsvApplyBlocks::setup().expect("Failed to setup benchmark");
WsvApplyBlocks::measure(&bench).expect("Failed to execute bnechmark");
}
Loading

0 comments on commit 87c165c

Please sign in to comment.