Skip to content

Commit

Permalink
[fix] #4211: Fix call blocking methods within async context
Browse files Browse the repository at this point in the history
Signed-off-by: Shanin Roman <[email protected]>
  • Loading branch information
Erigara committed Jan 26, 2024
1 parent b30d543 commit 260b648
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
4 changes: 2 additions & 2 deletions core/benches/blocks/apply_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ impl WsvApplyBlocks {
/// - Failed to parse [`AccountId`]
/// - Failed to generate [`KeyPair`]
/// - Failed to create instructions for block
pub fn setup() -> Result<Self> {
pub fn setup(rt: &tokio::runtime::Handle) -> 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 wsv = build_wsv(rt, &account_id, &key_pair);

let nth = 100;
let instructions = [
Expand Down
20 changes: 11 additions & 9 deletions core/benches/blocks/apply_blocks_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 10 additions & 4 deletions core/benches/blocks/apply_blocks_oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
11 changes: 9 additions & 2 deletions core/benches/blocks/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions core/benches/blocks/validate_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ impl WsvValidateBlocks {
/// - Failed to parse [`AccountId`]
/// - Failed to generate [`KeyPair`]
/// - Failed to create instructions for block
pub fn setup() -> Result<Self> {
pub fn setup(rt: &tokio::runtime::Handle) -> 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 wsv = build_wsv(rt, &account_id, &key_pair);

let nth = 100;
let instructions = [
Expand Down
6 changes: 5 additions & 1 deletion core/benches/blocks/validate_blocks_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion core/benches/blocks/validate_blocks_oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit 260b648

Please sign in to comment.