From 260b6482f2faff2c531a9cf15f25786d63c4538a Mon Sep 17 00:00:00 2001 From: Shanin Roman Date: Thu, 18 Jan 2024 16:07:44 +0300 Subject: [PATCH] [fix] #4211: Fix call blocking methods within async context Signed-off-by: Shanin Roman --- core/benches/blocks/apply_blocks.rs | 4 ++-- core/benches/blocks/apply_blocks_benchmark.rs | 20 ++++++++++--------- core/benches/blocks/apply_blocks_oneshot.rs | 14 +++++++++---- core/benches/blocks/common.rs | 11 ++++++++-- core/benches/blocks/validate_blocks.rs | 4 ++-- .../blocks/validate_blocks_benchmark.rs | 6 +++++- .../benches/blocks/validate_blocks_oneshot.rs | 10 +++++++++- 7 files changed, 48 insertions(+), 21 deletions(-) diff --git a/core/benches/blocks/apply_blocks.rs b/core/benches/blocks/apply_blocks.rs index f255922105c..112a9229d21 100644 --- a/core/benches/blocks/apply_blocks.rs +++ b/core/benches/blocks/apply_blocks.rs @@ -19,13 +19,13 @@ impl WsvApplyBlocks { /// - Failed to parse [`AccountId`] /// - Failed to generate [`KeyPair`] /// - Failed to create instructions for block - pub fn setup() -> Result { + pub fn setup(rt: &tokio::runtime::Handle) -> Result { 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 wsv = build_wsv(rt, &account_id, &key_pair); let nth = 100; let instructions = [ diff --git a/core/benches/blocks/apply_blocks_benchmark.rs b/core/benches/blocks/apply_blocks_benchmark.rs index 730d6f13037..2c9c496f0fb 100644 --- a/core/benches/blocks/apply_blocks_benchmark.rs +++ b/core/benches/blocks/apply_blocks_benchmark.rs @@ -6,17 +6,19 @@ use apply_blocks::WsvApplyBlocks; use criterion::{black_box, criterion_group, criterion_main, Criterion}; fn apply_blocks(c: &mut Criterion) { - tokio::runtime::Runtime::new().unwrap().block_on(async { - let bench = WsvApplyBlocks::setup().expect("Failed to setup benchmark"); - let mut group = c.benchmark_group("apply_blocks"); - group.significance_level(0.1).sample_size(10); - group.bench_function("apply_blocks", |b| { - b.iter(|| { - WsvApplyBlocks::measure(black_box(&bench)).expect("Failed to execute benchmark"); - }); + let rt = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .expect("Failed building the Runtime"); + let bench = WsvApplyBlocks::setup(rt.handle()).expect("Failed to setup benchmark"); + let mut group = c.benchmark_group("apply_blocks"); + group.significance_level(0.1).sample_size(10); + group.bench_function("apply_blocks", |b| { + b.iter(|| { + WsvApplyBlocks::measure(black_box(&bench)).expect("Failed to execute benchmark"); }); - group.finish(); }); + group.finish(); } criterion_group!(wsv, apply_blocks); diff --git a/core/benches/blocks/apply_blocks_oneshot.rs b/core/benches/blocks/apply_blocks_oneshot.rs index f16a5bf5e57..da6ce8527dd 100644 --- a/core/benches/blocks/apply_blocks_oneshot.rs +++ b/core/benches/blocks/apply_blocks_oneshot.rs @@ -9,10 +9,16 @@ mod apply_blocks; use apply_blocks::WsvApplyBlocks; -#[tokio::main] -async fn main() { - iroha_logger::test_logger(); +fn main() { + let rt = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .expect("Failed building the Runtime"); + { + let _guard = rt.enter(); + iroha_logger::test_logger(); + } iroha_logger::info!("Starting..."); - let bench = WsvApplyBlocks::setup().expect("Failed to setup benchmark"); + let bench = WsvApplyBlocks::setup(rt.handle()).expect("Failed to setup benchmark"); WsvApplyBlocks::measure(&bench).expect("Failed to execute benchmark"); } diff --git a/core/benches/blocks/common.rs b/core/benches/blocks/common.rs index 2905c796435..51471fe261d 100644 --- a/core/benches/blocks/common.rs +++ b/core/benches/blocks/common.rs @@ -170,9 +170,16 @@ pub fn restore_every_nth( instructions } -pub fn build_wsv(account_id: &AccountId, key_pair: &KeyPair) -> WorldStateView { +pub fn build_wsv( + rt: &tokio::runtime::Handle, + account_id: &AccountId, + key_pair: &KeyPair, +) -> WorldStateView { let kura = iroha_core::kura::Kura::blank_kura_for_testing(); - let query_handle = LiveQueryStore::test().start(); + let query_handle = { + let _guard = rt.enter(); + LiveQueryStore::test().start() + }; let mut domain = Domain::new(account_id.domain_id.clone()).build(account_id); domain.accounts.insert( account_id.clone(), diff --git a/core/benches/blocks/validate_blocks.rs b/core/benches/blocks/validate_blocks.rs index f39e7eb288e..c5ce48dee56 100644 --- a/core/benches/blocks/validate_blocks.rs +++ b/core/benches/blocks/validate_blocks.rs @@ -22,13 +22,13 @@ impl WsvValidateBlocks { /// - Failed to parse [`AccountId`] /// - Failed to generate [`KeyPair`] /// - Failed to create instructions for block - pub fn setup() -> Result { + pub fn setup(rt: &tokio::runtime::Handle) -> Result { 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 wsv = build_wsv(rt, &account_id, &key_pair); let nth = 100; let instructions = [ diff --git a/core/benches/blocks/validate_blocks_benchmark.rs b/core/benches/blocks/validate_blocks_benchmark.rs index 1417a1a426f..548c1bbb0eb 100644 --- a/core/benches/blocks/validate_blocks_benchmark.rs +++ b/core/benches/blocks/validate_blocks_benchmark.rs @@ -6,7 +6,11 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; use validate_blocks::WsvValidateBlocks; fn validate_blocks(c: &mut Criterion) { - let bench = WsvValidateBlocks::setup().expect("Failed to setup benchmark"); + let rt = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .expect("Failed building the Runtime"); + let bench = WsvValidateBlocks::setup(rt.handle()).expect("Failed to setup benchmark"); let mut group = c.benchmark_group("validate_blocks"); group.significance_level(0.1).sample_size(10); diff --git a/core/benches/blocks/validate_blocks_oneshot.rs b/core/benches/blocks/validate_blocks_oneshot.rs index 403adbd0a22..abfc09f7e7b 100644 --- a/core/benches/blocks/validate_blocks_oneshot.rs +++ b/core/benches/blocks/validate_blocks_oneshot.rs @@ -10,8 +10,16 @@ mod validate_blocks; use validate_blocks::WsvValidateBlocks; fn main() { + let rt = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .expect("Failed building the Runtime"); + { + let _guard = rt.enter(); + iroha_logger::test_logger(); + } iroha_logger::test_logger(); iroha_logger::info!("Starting..."); - let bench = WsvValidateBlocks::setup().expect("Failed to setup benchmark"); + let bench = WsvValidateBlocks::setup(rt.handle()).expect("Failed to setup benchmark"); WsvValidateBlocks::measure(bench).expect("Failed to execute bnechmark"); }