#![cfg_attr(not(feature = "std"), no_std)]
mod tests;
pub use frame_benchmarking::{
benchmarking, whitelisted_caller, BenchmarkBatch, BenchmarkConfig, BenchmarkError, BenchmarkList,
BenchmarkMetadata, BenchmarkParameter, BenchmarkResult, Benchmarking, BenchmarkingSetup,
};
#[cfg(feature = "std")]
pub use frame_benchmarking::{Analysis, BenchmarkSelector};
#[doc(hidden)]
pub use frame_support;
#[doc(hidden)]
pub use log;
#[doc(hidden)]
pub use parity_scale_codec;
#[doc(hidden)]
pub use paste;
#[doc(hidden)]
pub use sp_io::storage::root as storage_root;
#[doc(hidden)]
pub use sp_runtime::traits::Zero;
#[doc(hidden)]
pub use sp_std::{self, boxed::Box, prelude::Vec, str, vec};
#[doc(hidden)]
pub use sp_storage::{StateVersion, TrackedStorageKey};
#[macro_export]
macro_rules! whitelist_account {
($acc:ident) => {
frame_benchmarking::benchmarking::add_to_whitelist(
frame_system::Account::<Runtime>::hashed_key_for(&$acc).into(),
);
};
}
#[macro_export]
macro_rules! runtime_benchmarks {
(
{ $runtime:ident, $pallet:ident }
$( $rest:tt )*
) => {
$crate::benchmarks_iter!(
{ }
$runtime
$pallet
( )
( )
( )
$( $rest )*
);
}
}
#[macro_export]
macro_rules! runtime_benchmarks_instance {
(
{ $runtime:ident, $pallet:ident, $instance:ident }
$( $rest:tt )*
) => {
$crate::benchmarks_iter!(
{ $instance }
$runtime
$pallet
( )
( )
( )
$( $rest )*
);
}
}
#[macro_export]
#[doc(hidden)]
macro_rules! benchmarks_iter {
(
{ $( $instance:ident )? }
$runtime:ident
$pallet:ident
( $( $names:tt )* )
( $( $names_extra:tt )* )
( $( $names_skip_meta:tt )* )
#[extra]
$name:ident
$( $rest:tt )*
) => {
$crate::benchmarks_iter! {
{ $( $instance)? }
$runtime
$pallet
( $( $names )* )
( $( $names_extra )* $name )
( $( $names_skip_meta )* )
$name
$( $rest )*
}
};
(
{ $( $instance:ident )? }
$runtime:ident
$pallet:ident
( $( $names:tt )* ) ( $( $names_extra:tt )* )
( $( $names_skip_meta:tt )* )
$name:ident { $( $code:tt )* }: _ $(< $origin_type:ty>)? ( $origin:expr $( , $arg:expr )* )
verify $postcode:block
$( $rest:tt )*
) => {
$crate::benchmarks_iter! {
{ $( $instance)? }
$runtime
$pallet
( $( $names )* )
( $( $names_extra )* )
( $( $names_skip_meta )* )
$name { $( $code )* }: $name $(< $origin_type >)? ( $origin $( , $arg )* )
verify $postcode
$( $rest )*
}
};
(
{ $( $instance:ident )? }
$runtime:ident
$pallet:ident
( $( $names:tt )* )
( $( $names_extra:tt )* )
( $( $names_skip_meta:tt )* )
$name:ident { $( $code:tt )* }: $dispatch:ident $(< $origin_type:ty>)? ( $origin:expr $( , $arg:expr )* )
verify $postcode:block
$( $rest:tt )*
) => {
$crate::paste::paste! {
$crate::benchmarks_iter! {
{ $( $instance)? }
$runtime
$pallet
( $( $names )* )
( $( $names_extra )* )
( $( $names_skip_meta )* )
$name {
$( $code )*
let __call =
$pallet::Call::<$runtime $(, $instance )?
>:: [< new_call_variant_ $dispatch >] (
$($arg),*
);
let __benchmarked_call_encoded = $crate::parity_scale_codec::Encode::encode(
&__call
);
}: {
let __call_decoded = <
$pallet::Call::<$runtime $(, $instance )?>
as $crate::parity_scale_codec::Decode
>::decode(&mut &__benchmarked_call_encoded[..])
.expect("call is encoded above, encoding must be correct");
let __origin = $crate::to_origin!($origin $(, $origin_type)?);
<$pallet::Call::<$runtime $(, $instance )?> as $crate::frame_support::traits::UnfilteredDispatchable
>::dispatch_bypass_filter(__call_decoded, __origin)?;
}
verify $postcode
$( $rest )*
}
}
};
(
{ $( $instance:ident )? }
$runtime:ident
$pallet:ident
( $( $names:tt )* )
( $( $names_extra:tt )* )
( $( $names_skip_meta:tt )* )
$name:ident { $( $code:tt )* }: $eval:block
verify $postcode:block
$( $rest:tt )*
) => {
$crate::benchmark_backend! {
{ $( $instance)? }
$name
$runtime
$pallet
{ }
{ $eval }
{ $( $code )* }
$postcode
}
#[cfg(test)]
$crate::impl_benchmark_test!(
$runtime
$pallet
{ $( $instance)? }
$name
);
$crate::benchmarks_iter!(
{ $( $instance)? }
$runtime
$pallet
( $( $names )* { $( $instance )? } $name )
( $( $names_extra )* )
( $( $names_skip_meta )* )
$( $rest )*
);
};
(
{ $( $instance:ident )? }
$runtime:ident
$pallet:ident
( $( $names:tt )* )
( $( $names_extra:tt )* )
( $( $names_skip_meta:tt )* )
) => {
$crate::selected_benchmark!(
$runtime
$pallet
{ $( $instance)? }
$( $names )*
);
$crate::impl_benchmark!(
$runtime
$pallet
{ $( $instance)? }
( $( $names )* )
( $( $names_extra ),* )
( $( $names_skip_meta )* )
);
};
(
{ $( $instance:ident )? }
$runtime:ident
$pallet:ident
( $( $names:tt )* )
( $( $names_extra:tt )* )
( $( $names_skip_meta:tt )* )
$name:ident { $( $code:tt )* }: _ ( $origin:expr $( , $arg:expr )* )
$( $rest:tt )*
) => {
$crate::benchmarks_iter! {
{ $( $instance)? }
$runtime
$pallet
( $( $names )* )
( $( $names_extra )* )
( $( $names_skip_meta )* )
$name { $( $code )* }: _ ( $origin $( , $arg )* )
verify { }
$( $rest )*
}
};
(
{ $( $instance:ident )? }
$runtime:ident
$pallet:ident
( $( $names:tt )* )
( $( $names_extra:tt )* )
( $( $names_skip_meta:tt )* )
$name:ident { $( $code:tt )* }: $dispatch:ident ( $origin:expr $( , $arg:expr )* )
$( $rest:tt )*
) => {
$crate::benchmarks_iter! {
{ $( $instance)? }
$runtime
$pallet
( $( $names )* )
( $( $names_extra )* )
( $( $names_skip_meta )* )
$name { $( $code )* }: $dispatch ( $origin $( , $arg )* )
verify { }
$( $rest )*
}
};
(
{ $( $instance:ident )? }
$runtime:ident
$pallet:ident
( $( $names:tt )* )
( $( $names_extra:tt )* )
( $( $names_skip_meta:tt )* )
$name:ident { $( $code:tt )* }: $eval:block
$( $rest:tt )*
) => {
$crate::benchmarks_iter!(
{ $( $instance)? }
$runtime
$pallet
( $( $names )* )
( $( $names_extra )* )
( $( $names_skip_meta )* )
$name { $( $code )* }: $eval
verify { }
$( $rest )*
);
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! to_origin {
($origin:expr) => {
$origin.into()
};
($origin:expr, $origin_type:ty) => {
<<$runtime as frame_system::Config>::RuntimeOrigin as From<$origin_type>>::from($origin)
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! benchmark_backend {
(
{ $( $instance:ident )? }
$name:ident
$runtime:ident
$pallet:ident
{ $( PRE { $( $pre_parsed:tt )* } )* }
{ $eval:block }
{
let $pre_id:tt : $pre_ty:ty = $pre_ex:expr;
$( $rest:tt )*
}
$postcode:block
) => {
$crate::benchmark_backend! {
{ $( $instance)? }
$name
$runtime
$pallet
{
$( PRE { $( $pre_parsed )* } )*
PRE { $pre_id , $pre_ty , $pre_ex }
}
{ $eval }
{ $( $rest )* }
$postcode
}
};
(
{ $( $instance:ident )? }
$name:ident
$runtime:ident
$pallet:ident
{ $( $parsed:tt )* }
{ $eval:block }
{
let $param:ident in ( $param_from:expr ) .. $param_to:expr => $param_instancer:expr;
$( $rest:tt )*
}
$postcode:block
) => {
$crate::benchmark_backend! {
{ $( $instance)? }
$name
$runtime
$pallet
{
$( $parsed )*
PARAM { $param , $param_from , $param_to , $param_instancer }
}
{ $eval }
{ $( $rest )* }
$postcode
}
};
(
{ $( $instance:ident )? }
$name:ident
$runtime:ident
$pallet:ident
{ $( $parsed:tt )* }
{ $eval:block }
{
let $param:ident in $param_from:tt .. $param_to:expr => $param_instancer:expr ;
$( $rest:tt )*
}
$postcode:block
) => {
$crate::benchmark_backend! {
{ $( $instance)? }
$name
$runtime
$pallet
{ $( $parsed )* }
{ $eval }
{
let $param in ( $param_from ) .. $param_to => $param_instancer;
$( $rest )*
}
$postcode
}
};
(
{ $( $instance:ident )? }
$name:ident
$runtime:ident
$pallet:ident
{ $( $parsed:tt )* }
{ $eval:block }
{
let $param:ident in $param_from:tt .. $param_to:expr;
$( $rest:tt )*
}
$postcode:block
) => {
$crate::benchmark_backend! {
{ $( $instance)? }
$name
$runtime
$pallet
{ $( $parsed )* }
{ $eval }
{
let $param in $param_from .. $param_to => ();
$( $rest )*
}
$postcode
}
};
(
{ $( $instance:ident )? }
$name:ident
$runtime:ident
$pallet:ident
{ $( $parsed:tt )* }
{ $eval:block }
{
let $pre_id:tt = $pre_ex:expr;
$( $rest:tt )*
}
$postcode:block
) => {
$crate::benchmark_backend! {
{ $( $instance)? }
$name
$runtime
$pallet
{ $( $parsed )* }
{ $eval }
{
let $pre_id : _ = $pre_ex;
$( $rest )*
}
$postcode
}
};
(
{ $( $instance:ident )? }
$name:ident
$runtime:ident
$pallet:ident
{
$( PRE { $pre_id:tt , $pre_ty:ty , $pre_ex:expr } )*
$( PARAM { $param:ident , $param_from:expr , $param_to:expr , $param_instancer:expr } )*
}
{ $eval:block }
{ $( $post:tt )* }
$postcode:block
) => {
#[allow(non_camel_case_types)]
struct $name;
#[allow(unused_variables)]
impl $crate::BenchmarkingSetup<$runtime $(, $instance)?> for $name {
fn components(&self) -> $crate::Vec<($crate::BenchmarkParameter, u32, u32)> {
$crate::vec! [
$(
($crate::BenchmarkParameter::$param, $param_from, $param_to)
),*
]
}
fn instance(
&self,
components: &[($crate::BenchmarkParameter, u32)],
verify: bool
) -> Result<$crate::Box<dyn FnOnce() -> Result<(), $crate::BenchmarkError>>, $crate::BenchmarkError> {
$(
let $param = components.iter()
.find(|&c| c.0 == $crate::BenchmarkParameter::$param)
.ok_or("Could not find component in benchmark preparation.")?
.1;
)*
$(
let $pre_id : $pre_ty = $pre_ex;
)*
$( $param_instancer ; )*
$( $post )*
Ok($crate::Box::new(move || -> Result<(), $crate::BenchmarkError> {
$eval;
if verify {
$postcode;
}
Ok(())
}))
}
}
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! selected_benchmark {
(
$runtime:ident
$pallet:ident
{ $( $instance:ident )? }
$( { $( $bench_inst:ident )? } $bench:ident )*
) => {
#[allow(non_camel_case_types)]
enum SelectedBenchmark {
$( $bench, )*
}
impl $crate::BenchmarkingSetup<$runtime $(, $instance)?> for SelectedBenchmark {
fn components(&self) -> $crate::Vec<($crate::BenchmarkParameter, u32, u32)> {
match self {
$(
Self::$bench => <
$bench as $crate::BenchmarkingSetup<$runtime $(, $bench_inst)? >
>::components(&$bench),
)*
}
}
fn instance(
&self,
components: &[($crate::BenchmarkParameter, u32)],
verify: bool
) -> Result<$crate::Box<dyn FnOnce() -> Result<(), $crate::BenchmarkError>>, $crate::BenchmarkError> {
match self {
$(
Self::$bench => <
$bench as $crate::BenchmarkingSetup<$runtime $(, $bench_inst)? >
>::instance(&$bench, components, verify),
)*
}
}
}
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! impl_benchmark {
(
$runtime:ident
$pallet:ident
{ $( $instance:ident )? }
( $( { $( $name_inst:ident )? } $name:ident )* )
( $( $name_extra:ident ),* )
( $( $name_skip_meta:ident ),* )
) => {
pub struct Benchmark;
impl $crate::Benchmarking for Benchmark {
fn benchmarks(extra: bool) -> $crate::Vec<$crate::BenchmarkMetadata> {
let mut all_names = $crate::vec![ $( stringify!($name).as_ref() ),* ];
if !extra {
let extra = [ $( stringify!($name_extra).as_ref() ),* ];
all_names.retain(|x| !extra.contains(x));
}
all_names.into_iter().map(|benchmark| {
let selected_benchmark = match benchmark {
$( stringify!($name) => SelectedBenchmark::$name, )*
_ => panic!("all benchmarks should be selectable"),
};
let components = <
SelectedBenchmark as $crate::BenchmarkingSetup<$runtime $(, $instance)?>
>::components(&selected_benchmark);
$crate::BenchmarkMetadata {
name: benchmark.as_bytes().to_vec(),
components,
pov_modes: vec![],
}
}).collect::<$crate::Vec<_>>()
}
fn run_benchmark(
extrinsic: &[u8],
c: &[($crate::BenchmarkParameter, u32)],
whitelist: &[$crate::TrackedStorageKey],
verify: bool,
internal_repeats: u32,
) -> Result<$crate::Vec<$crate::BenchmarkResult>, $crate::BenchmarkError> {
let extrinsic = $crate::str::from_utf8(extrinsic)
.map_err(|_| "`extrinsic` is not a valid utf8 string!")?;
let selected_benchmark = match extrinsic {
$( stringify!($name) => SelectedBenchmark::$name, )*
_ => return Err("Could not find extrinsic.".into()),
};
let mut whitelist = whitelist.to_vec();
let whitelisted_caller_key =
<frame_system::Account::<$runtime> as $crate::frame_support::storage::StorageMap<_,_>>::hashed_key_for(
$crate::whitelisted_caller::<<$runtime as frame_system::Config>::AccountId>()
);
whitelist.push(whitelisted_caller_key.into());
let transactional_layer_key = $crate::TrackedStorageKey::new(
$crate::frame_support::storage::transactional::TRANSACTION_LEVEL_KEY.into()
);
whitelist.push(transactional_layer_key);
$crate::benchmarking::set_whitelist(whitelist);
let mut results: $crate::Vec<$crate::BenchmarkResult> = $crate::Vec::new();
for _ in 0 .. internal_repeats.max(1) {
let closure_to_benchmark = <
SelectedBenchmark as $crate::BenchmarkingSetup<$runtime $(, $instance)?>
>::instance(&selected_benchmark, c, verify)?;
if $crate::Zero::is_zero(&frame_system::Pallet::<$runtime>::block_number()) {
frame_system::Pallet::<$runtime>::set_block_number(1u32.into());
}
$crate::benchmarking::commit_db();
$crate::benchmarking::reset_read_write_count();
$crate::log::trace!(
target: "benchmark",
"Start Benchmark: {:?}", c
);
let start_pov = $crate::benchmarking::proof_size();
let start_extrinsic = $crate::benchmarking::current_time();
closure_to_benchmark()?;
let finish_extrinsic = $crate::benchmarking::current_time();
let end_pov = $crate::benchmarking::proof_size();
let elapsed_extrinsic = finish_extrinsic.saturating_sub(start_extrinsic);
let diff_pov = match (start_pov, end_pov) {
(Some(start), Some(end)) => end.saturating_sub(start),
_ => Default::default(),
};
$crate::benchmarking::commit_db();
$crate::log::trace!(
target: "benchmark",
"End Benchmark: {} ns", elapsed_extrinsic
);
let read_write_count = $crate::benchmarking::read_write_count();
$crate::log::trace!(
target: "benchmark",
"Read/Write Count {:?}", read_write_count
);
let start_storage_root = $crate::benchmarking::current_time();
$crate::storage_root($crate::StateVersion::V0);
let finish_storage_root = $crate::benchmarking::current_time();
let elapsed_storage_root = finish_storage_root - start_storage_root;
let skip_meta = [ $( stringify!($name_skip_meta).as_ref() ),* ];
let read_and_written_keys = if skip_meta.contains(&extrinsic) {
$crate::vec![(b"Skipped Metadata".to_vec(), 0, 0, false)]
} else {
$crate::benchmarking::get_read_and_written_keys()
};
results.push($crate::BenchmarkResult {
components: c.to_vec(),
extrinsic_time: elapsed_extrinsic,
storage_root_time: elapsed_storage_root,
reads: read_write_count.0,
repeat_reads: read_write_count.1,
writes: read_write_count.2,
repeat_writes: read_write_count.3,
proof_size: diff_pov,
keys: read_and_written_keys,
});
$crate::benchmarking::wipe_db();
}
return Ok(results);
}
}
#[cfg(test)]
impl Benchmark {
#[allow(unused)]
fn test_bench_by_name(name: &[u8]) -> Result<(), $crate::BenchmarkError> {
let name = $crate::str::from_utf8(name)
.map_err(|_| -> $crate::BenchmarkError { "`name` is not a valid utf8 string!".into() })?;
match name {
$( stringify!($name) => {
$crate::paste::paste! { Self::[< test_benchmark_ $name >]() }
} )*
_ => Err("Could not find test for requested benchmark.".into()),
}
}
}
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! impl_benchmark_test {
(
$runtime:ident
$pallet:ident
{ $( $instance:ident )? }
$name:ident
) => {
$crate::paste::item! {
impl Benchmark {
#[allow(unused)]
fn [<test_benchmark_ $name>] () -> Result<(), $crate::BenchmarkError> {
let selected_benchmark = SelectedBenchmark::$name;
let components = <
SelectedBenchmark as $crate::BenchmarkingSetup<$runtime, _>
>::components(&selected_benchmark);
let execute_benchmark = |
c: $crate::Vec<($crate::BenchmarkParameter, u32)>
| -> Result<(), $crate::BenchmarkError> {
let closure_to_verify = <
SelectedBenchmark as $crate::BenchmarkingSetup<$runtime, _>
>::instance(&selected_benchmark, &c, true)?;
if $crate::Zero::is_zero(&frame_system::Pallet::<$runtime>::block_number()) {
frame_system::Pallet::<$runtime>::set_block_number(1u32.into());
}
closure_to_verify()?;
$crate::benchmarking::wipe_db();
Ok(())
};
if components.is_empty() {
execute_benchmark(Default::default())?;
} else {
for (name, low, high) in components.iter() {
for component_value in $crate::vec![low, high] {
let c: $crate::Vec<($crate::BenchmarkParameter, u32)> = components
.iter()
.map(|(n, _, h)|
if n == name {
(*n, *component_value)
} else {
(*n, *h)
}
)
.collect();
execute_benchmark(c)?;
}
}
}
Ok(())
}
}
}
};
}
#[macro_export]
macro_rules! impl_benchmark_test_suite {
(
$new_test_ext:expr,
$(, $( $rest:tt )* )?
) => {
$crate::impl_benchmark_test_suite!(
@selected:
$new_test_ext,
benchmarks_path = super,
extra = true,
exec_name = execute_with,
@user:
$( $( $rest )* )?
);
};
(
@selected:
$new_test_ext:expr,
benchmarks_path = $old:ident,
extra = $extra:expr,
exec_name = $exec_name:ident,
@user:
benchmarks_path = $benchmarks_path:ident
$(, $( $rest:tt )* )?
) => {
$crate::impl_benchmark_test_suite!(
@selected:
$new_test_ext,
benchmarks_path = $benchmarks_path,
extra = $extra,
exec_name = $exec_name,
@user:
$( $( $rest )* )?
);
};
(
@selected:
$new_test_ext:expr,
benchmarks_path = $benchmarks_path:ident,
extra = $old:expr,
exec_name = $exec_name:ident,
@user:
extra = $extra:expr
$(, $( $rest:tt )* )?
) => {
$crate::impl_benchmark_test_suite!(
@selected:
$new_test_ext,
benchmarks_path = $benchmarks_path,
extra = $extra,
exec_name = $exec_name,
@user:
$( $( $rest )* )?
);
};
(
@selected:
$new_test_ext:expr,
benchmarks_path = $benchmarks_path:ident,
extra = $extra:expr,
exec_name = $old:ident,
@user:
exec_name = $exec_name:ident
$(, $( $rest:tt )* )?
) => {
$crate::impl_benchmark_test_suite!(
@selected:
$new_test_ext,
benchmarks_path = $benchmarks_path,
extra = $extra,
exec_name = $exec_name,
@user:
$( $( $rest )* )?
);
};
(
@selected:
$new_test_ext:expr,
benchmarks_path = $path_to_benchmarks_invocation:ident,
extra = $extra:expr,
exec_name = $exec_name:ident,
@user:
$(,)?
) => {
#[cfg(test)]
mod benchmark_tests {
use super::*;
#[test]
fn test_benchmarks() {
$new_test_ext.$exec_name(|| {
use $crate::Benchmarking;
let mut anything_failed = false;
println!("failing benchmark tests:");
for benchmark_metadata in $path_to_benchmarks_invocation::Benchmark::benchmarks($extra) {
let benchmark_name = &benchmark_metadata.name;
match std::panic::catch_unwind(|| {
Benchmark::test_bench_by_name(benchmark_name)
}) {
Err(err) => {
println!(
"{}: {:?}",
$crate::str::from_utf8(benchmark_name)
.expect("benchmark name is always a valid string!"),
err,
);
anything_failed = true;
},
Ok(Err(err)) => {
match err {
$crate::BenchmarkError::Stop(err) => {
println!(
"{}: {:?}",
$crate::str::from_utf8(benchmark_name)
.expect("benchmark name is always a valid string!"),
err,
);
anything_failed = true;
},
$crate::BenchmarkError::Override(_) => {
$crate::log::error!(
"WARNING: benchmark error overrided - {}",
$crate::str::from_utf8(benchmark_name)
.expect("benchmark name is always a valid string!"),
);
},
$crate::BenchmarkError::Skip => {
$crate::log::error!(
"WARNING: benchmark error skipped - {}",
$crate::str::from_utf8(benchmark_name)
.expect("benchmark name is always a valid string!"),
);
},
$crate::BenchmarkError::Weightless => {
$crate::log::error!(
"WARNING: benchmark error weightless skipped - {}",
$crate::str::from_utf8(benchmark_name)
.expect("benchmark name is always a valid string!"),
);
}
}
},
Ok(Ok(())) => (),
}
}
assert!(!anything_failed);
});
}
}
};
}
pub fn show_benchmark_debug_info(
instance_string: &[u8],
benchmark: &[u8],
components: &[(BenchmarkParameter, u32)],
verify: &bool,
error_message: &str,
) -> sp_runtime::RuntimeString {
sp_runtime::format_runtime_string!(
"\n* Pallet: {}\n\
* Benchmark: {}\n\
* Components: {:?}\n\
* Verify: {:?}\n\
* Error message: {}",
sp_std::str::from_utf8(instance_string).expect("it's all just strings ran through the wasm interface. qed"),
sp_std::str::from_utf8(benchmark).expect("it's all just strings ran through the wasm interface. qed"),
components,
verify,
error_message,
)
}
#[macro_export]
macro_rules! add_benchmark {
( $params:ident, $batches:ident, $name:path, $( $location:tt )* ) => (
let name_string = stringify!($name).as_bytes();
let instance_string = stringify!( $( $location )* ).as_bytes();
let (config, whitelist) = $params;
let $crate::BenchmarkConfig {
pallet,
benchmark,
selected_components,
verify,
internal_repeats,
} = config;
if &pallet[..] == &name_string[..] {
let benchmark_result = $( $location )*::Benchmark::run_benchmark(
&benchmark[..],
&selected_components[..],
whitelist,
*verify,
*internal_repeats,
);
let final_results = match benchmark_result {
Ok(results) => Some(results),
Err($crate::BenchmarkError::Override(mut result)) => {
$crate::log::error!(
"WARNING: benchmark error overrided - {}",
$crate::str::from_utf8(benchmark)
.expect("benchmark name is always a valid string!")
);
result.keys.insert(0,
(b"Benchmark Override".to_vec(), 0, 0, false)
);
Some($crate::vec![result])
},
Err($crate::BenchmarkError::Stop(e)) => {
$crate::show_benchmark_debug_info(
instance_string,
benchmark,
selected_components,
verify,
e,
);
return Err(e.into());
},
Err($crate::BenchmarkError::Skip) => {
$crate::log::error!(
"WARNING: benchmark error skipped - {}",
$crate::str::from_utf8(benchmark)
.expect("benchmark name is always a valid string!")
);
None
},
Err($crate::BenchmarkError::Weightless) => {
$crate::log::error!(
"WARNING: benchmark weightless skipped - {}",
$crate::str::from_utf8(benchmark)
.expect("benchmark name is always a valid string!")
);
Some(vec![$crate::BenchmarkResult {
components: selected_components.clone(),
.. Default::default()
}])
}
};
if let Some(final_results) = final_results {
$batches.push($crate::BenchmarkBatch {
pallet: name_string.to_vec(),
instance: instance_string.to_vec(),
benchmark: benchmark.clone(),
results: final_results,
});
}
}
)
}
#[macro_export]
macro_rules! cb_add_benchmarks {
( $params:ident, $batches:ident, [ $name:path, $( $location:tt )* ] ) => {
add_benchmark!( $params, $batches, $name, $( $location )* );
};
( $params:ident, $batches:ident, [ $name:path, $( $location:tt )* ] $([ $names:path, $( $locations:tt )* ])+ ) => {
cb_add_benchmarks!( $params, $batches, [ $name, $( $location )* ] );
cb_add_benchmarks!( $params, $batches, $([ $names, $( $locations )* ])+ );
}
}
#[macro_export]
macro_rules! list_benchmark {
( $list:ident, $extra:ident, $name:path, $( $location:tt )* ) => (
let pallet_string = stringify!($name).as_bytes();
let instance_string = stringify!( $( $location )* ).as_bytes();
let benchmarks = $( $location )*::Benchmark::benchmarks($extra);
let pallet_benchmarks = $crate::BenchmarkList {
pallet: pallet_string.to_vec(),
instance: instance_string.to_vec(),
benchmarks: benchmarks.to_vec(),
};
$list.push(pallet_benchmarks)
)
}
#[macro_export]
macro_rules! cb_list_benchmarks {
( $list:ident, $extra:ident, [ $name:path, $( $location:tt )* ] ) => {
list_benchmark!( $list, $extra, $name, $( $location )* );
};
( $list:ident, $extra:ident, [ $name:path, $( $location:tt )* ] $([ $names:path, $( $locations:tt )* ])+ ) => {
cb_list_benchmarks!( $list, $extra, [ $name, $( $location )* ] );
cb_list_benchmarks!( $list, $extra, $([ $names, $( $locations )* ])+ );
}
}
#[macro_export]
macro_rules! define_benchmarks {
( $([ $names:path, $( $locations:tt )* ])* ) => {
#[macro_export]
macro_rules! list_benchmarks {
( $list:ident, $extra:ident ) => {
cb_list_benchmarks!( $list, $extra, $([ $names, $( $locations )* ])+ );
}
}
#[macro_export]
macro_rules! add_benchmarks {
( $params:ident, $batches:ident ) => {
cb_add_benchmarks!( $params, $batches, $([ $names, $( $locations )* ])+ );
}
}
}
}