From 14e60f88a4e9061bed52bf7d4c0cf7d899f89448 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 13 May 2020 23:48:28 +0300 Subject: [PATCH] properly handle different block content (#6007) --- bin/node/bench/src/import.rs | 24 +++++++------- bin/node/bench/src/main.rs | 16 ++++----- bin/node/testing/src/bench.rs | 62 +++++++++++++++++++++-------------- 3 files changed, 56 insertions(+), 46 deletions(-) diff --git a/bin/node/bench/src/import.rs b/bin/node/bench/src/import.rs index 10f5ee8dca0df..a311bb1b1ba4f 100644 --- a/bin/node/bench/src/import.rs +++ b/bin/node/bench/src/import.rs @@ -50,19 +50,19 @@ pub enum SizeType { #[display(fmt = "full")] Full, #[display(fmt = "custom")] - Custom, + Custom(usize), } impl SizeType { - pub fn transactions(&self) -> usize { + pub fn transactions(&self) -> Option { match self { - SizeType::Empty => 0, - SizeType::Small => 10, - SizeType::Medium => 100, - SizeType::Large => 500, - SizeType::Full => 4000, + SizeType::Empty => Some(0), + SizeType::Small => Some(10), + SizeType::Medium => Some(100), + SizeType::Large => Some(500), + SizeType::Full => None, // Custom SizeType will use the `--transactions` input parameter - SizeType::Custom => 0, + SizeType::Custom(val) => Some(*val), } } } @@ -97,9 +97,9 @@ impl core::BenchmarkDescription for ImportBenchmarkDescription { } match self.block_type { - BlockType::RandomTransfersKeepAlive(_) => path.push("transfer_keep_alive"), - BlockType::RandomTransfersReaping(_) => path.push("transfer_reaping"), - BlockType::Noop(_) => path.push("noop"), + BlockType::RandomTransfersKeepAlive => path.push("transfer_keep_alive"), + BlockType::RandomTransfersReaping => path.push("transfer_reaping"), + BlockType::Noop => path.push("noop"), } match self.database_type { @@ -119,7 +119,7 @@ impl core::BenchmarkDescription for ImportBenchmarkDescription { 50_000, self.key_types ); - let block = bench_db.generate_block(self.block_type); + let block = bench_db.generate_block(self.block_type.to_content(self.size.transactions())); Box::new(ImportBenchmark { database: bench_db, block, diff --git a/bin/node/bench/src/main.rs b/bin/node/bench/src/main.rs index df7e4c2da2be4..ed4f9fe107b65 100644 --- a/bin/node/bench/src/main.rs +++ b/bin/node/bench/src/main.rs @@ -79,19 +79,15 @@ fn main() { SizeType::Medium, SizeType::Large, SizeType::Full, - SizeType::Custom, + SizeType::Custom(opt.transactions.unwrap_or(0)), ].iter() { - let txs = match size { - SizeType::Custom => opt.transactions.unwrap_or(0), - _ => size.transactions() - }; for block_type in [ - BlockType::RandomTransfersKeepAlive(txs), - BlockType::RandomTransfersReaping(txs), - BlockType::Noop(txs), + BlockType::RandomTransfersKeepAlive, + BlockType::RandomTransfersReaping, + BlockType::Noop, ].iter() { for database_type in [BenchDataBaseType::RocksDb, BenchDataBaseType::ParityDb].iter() { - import_benchmarks.push((profile, size, block_type.clone(), database_type)); + import_benchmarks.push((profile, size.clone(), block_type.clone(), database_type)); } } } @@ -102,7 +98,7 @@ fn main() { ImportBenchmarkDescription { profile: *profile, key_types: KeyTypes::Sr25519, - size: *size, + size: size, block_type: block_type, database_type: *database_type, }, diff --git a/bin/node/testing/src/bench.rs b/bin/node/testing/src/bench.rs index b784d9f42b6c6..4355d0e44d29c 100644 --- a/bin/node/testing/src/bench.rs +++ b/bin/node/testing/src/bench.rs @@ -132,11 +132,36 @@ impl Clone for BenchDb { #[derive(Debug, PartialEq, Clone, Copy)] pub enum BlockType { /// Bunch of random transfers. - RandomTransfersKeepAlive(usize), + RandomTransfersKeepAlive, /// Bunch of random transfers that drain all of the source balance. - RandomTransfersReaping(usize), + RandomTransfersReaping, /// Bunch of "no-op" calls. - Noop(usize), + Noop, +} + +impl BlockType { + /// Create block content description with specified number of transactions. + pub fn to_content(self, size: Option) -> BlockContent { + BlockContent { + block_type: self, + size: size, + } + } +} + +/// Content of the generated block. +pub struct BlockContent { + block_type: BlockType, + size: Option, +} + +impl BlockContent { + fn iter_while(&self, mut f: impl FnMut(usize) -> bool) { + match self.size { + Some(v) => { for i in 0..v { if !f(i) { break; }}} + None => { for i in 0.. { if !f(i) { break; }}} + } + } } /// Type of backend database. @@ -162,15 +187,6 @@ impl DatabaseType { } } -impl BlockType { - /// Number of transactions for this block type. - pub fn transactions(&self) -> usize { - match self { - Self::RandomTransfersKeepAlive(v) | Self::RandomTransfersReaping(v) | Self::Noop(v) => *v, - } - } -} - /// Benchmarking task executor. /// /// Uses multiple threads as the regular executable. @@ -271,7 +287,7 @@ impl BenchDb { } /// Generate new block using this database. - pub fn generate_block(&mut self, block_type: BlockType) -> Block { + pub fn generate_block(&mut self, content: BlockContent) -> Block { let (client, _backend) = Self::bench_client( self.database_type, self.directory_guard.path(), @@ -310,10 +326,8 @@ impl BenchDb { block.push(extrinsic).expect("Push inherent failed"); } - let mut iteration = 0; let start = std::time::Instant::now(); - for _ in 0..block_type.transactions() { - + content.iter_while(|iteration| { let sender = self.keyring.at(iteration); let receiver = get_account_id_from_seed::( &format!("random-user//{}", iteration) @@ -322,8 +336,8 @@ impl BenchDb { let signed = self.keyring.sign( CheckedExtrinsic { signed: Some((sender, signed_extra(0, node_runtime::ExistentialDeposit::get() + 1))), - function: match block_type { - BlockType::RandomTransfersKeepAlive(_) => { + function: match content.block_type { + BlockType::RandomTransfersKeepAlive => { Call::Balances( BalancesCall::transfer_keep_alive( pallet_indices::address::Address::Id(receiver), @@ -331,7 +345,7 @@ impl BenchDb { ) ) }, - BlockType::RandomTransfersReaping(_) => { + BlockType::RandomTransfersReaping => { Call::Balances( BalancesCall::transfer( pallet_indices::address::Address::Id(receiver), @@ -341,7 +355,7 @@ impl BenchDb { ) ) }, - BlockType::Noop(_) => { + BlockType::Noop => { Call::System( SystemCall::remark(Vec::new()) ) @@ -361,13 +375,13 @@ impl BenchDb { Err(sp_blockchain::Error::ApplyExtrinsicFailed( sp_blockchain::ApplyExtrinsicFailed::Validity(e) )) if e.exhausted_resources() => { - break; + return false; }, Err(err) => panic!("Error pushing transaction: {:?}", err), - Ok(_) => {}, + Ok(_) => true, } - iteration += 1; - } + }); + let block = block.build().expect("Block build failed").block; log::info!(