Skip to content

Commit

Permalink
properly handle different block content (paritytech#6007)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikVolf authored May 13, 2020
1 parent 420086d commit 14e60f8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 46 deletions.
24 changes: 12 additions & 12 deletions bin/node/bench/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> {
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),
}
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
16 changes: 6 additions & 10 deletions bin/node/bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand All @@ -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,
},
Expand Down
62 changes: 38 additions & 24 deletions bin/node/testing/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>) -> BlockContent {
BlockContent {
block_type: self,
size: size,
}
}
}

/// Content of the generated block.
pub struct BlockContent {
block_type: BlockType,
size: Option<usize>,
}

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.
Expand All @@ -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.
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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::<sr25519::Public>(
&format!("random-user//{}", iteration)
Expand All @@ -322,16 +336,16 @@ 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),
node_runtime::ExistentialDeposit::get() + 1,
)
)
},
BlockType::RandomTransfersReaping(_) => {
BlockType::RandomTransfersReaping => {
Call::Balances(
BalancesCall::transfer(
pallet_indices::address::Address::Id(receiver),
Expand All @@ -341,7 +355,7 @@ impl BenchDb {
)
)
},
BlockType::Noop(_) => {
BlockType::Noop => {
Call::System(
SystemCall::remark(Vec::new())
)
Expand All @@ -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!(
Expand Down

0 comments on commit 14e60f8

Please sign in to comment.