use std::borrow::Cow;
use node_testing::bench::{BenchDb, Profile, BlockType, KeyTypes, DatabaseType};
use node_primitives::Block;
use tc_client_api::backend::Backend;
use tp_runtime::generic::BlockId;
use tp_state_machine::InspectState;
use crate::{
common::SizeType,
core::{self, Path, Mode},
};
pub struct ImportBenchmarkDescription {
pub profile: Profile,
pub key_types: KeyTypes,
pub block_type: BlockType,
pub size: SizeType,
pub database_type: DatabaseType,
}
pub struct ImportBenchmark {
profile: Profile,
database: BenchDb,
block: Block,
block_type: BlockType,
}
impl core::BenchmarkDescription for ImportBenchmarkDescription {
fn path(&self) -> Path {
let mut path = Path::new(&["node", "import"]);
match self.profile {
Profile::Wasm => path.push("wasm"),
Profile::Native => path.push("native"),
}
match self.key_types {
KeyTypes::Sr25519 => path.push("sr25519"),
KeyTypes::Ed25519 => path.push("ed25519"),
}
match self.block_type {
BlockType::RandomTransfersKeepAlive => path.push("transfer_keep_alive"),
BlockType::RandomTransfersReaping => path.push("transfer_reaping"),
BlockType::Noop => path.push("noop"),
}
match self.database_type {
DatabaseType::RocksDb => path.push("rocksdb"),
DatabaseType::TetsyDb => path.push("tetsydb"),
}
path.push(&format!("{}", self.size));
path
}
fn setup(self: Box<Self>) -> Box<dyn core::Benchmark> {
let profile = self.profile;
let mut bench_db = BenchDb::with_key_types(
self.database_type,
50_000,
self.key_types
);
let block = bench_db.generate_block(self.block_type.to_content(self.size.transactions()));
Box::new(ImportBenchmark {
database: bench_db,
block_type: self.block_type,
block,
profile,
})
}
fn name(&self) -> Cow<'static, str> {
format!(
"Block import ({:?}/{}, {:?}, {:?} backend)",
self.block_type,
self.size,
self.profile,
self.database_type,
).into()
}
}
impl core::Benchmark for ImportBenchmark {
fn run(&mut self, mode: Mode) -> std::time::Duration {
let mut context = self.database.create_context(self.profile);
let _ = context.client.runtime_version_at(&BlockId::Number(0))
.expect("Failed to get runtime version")
.spec_version;
if mode == Mode::Profile {
std::thread::park_timeout(std::time::Duration::from_secs(3));
}
let start = std::time::Instant::now();
context.import_block(self.block.clone());
let elapsed = start.elapsed();
context.client
.state_at(&BlockId::number(1))
.expect("state_at failed for block#1")
.inspect_state(|| {
match self.block_type {
BlockType::RandomTransfersKeepAlive => {
assert_eq!(
node_runtime::System::events().len(),
(self.block.extrinsics.len() - 1) * 5 + 1,
);
},
BlockType::Noop => {
assert_eq!(
node_runtime::System::events().len(),
(self.block.extrinsics.len() - 1) * 2 + 1,
);
},
_ => {},
}
}
);
if mode == Mode::Profile {
std::thread::park_timeout(std::time::Duration::from_secs(1));
}
log::info!(
target: "bench-logistics",
"imported block with {} tx, took: {:#?}",
self.block.extrinsics.len(),
elapsed,
);
log::info!(
target: "bench-logistics",
"usage info: {}",
context.backend.usage_info()
.expect("RocksDB backend always provides usage info!"),
);
elapsed
}
}