benchmarks! macro: factorize instance usage. (#6750)

* factorize benchmark!

* fix types

* fix types
This commit is contained in:
Guillaume Thiolliere
2020-07-29 14:33:54 +02:00
committed by GitHub
parent 6e6568167a
commit 1eeb4b0e3f
2 changed files with 97 additions and 419 deletions
+93 -406
View File
@@ -184,7 +184,7 @@ macro_rules! benchmarks {
$( $rest:tt )*
) => {
$crate::benchmarks_iter!(
NO_INSTANCE
{ }
{ $( $( $where_ty: $where_bound ),* )? }
{ $( { $common , $common_from , $common_to , $common_instancer } )* }
( )
@@ -206,7 +206,7 @@ macro_rules! benchmarks_instance {
$( $rest:tt )*
) => {
$crate::benchmarks_iter!(
INSTANCE
{ I }
{ $( $( $where_ty: $where_bound ),* )? }
{ $( { $common , $common_from , $common_to , $common_instancer } )* }
( )
@@ -220,16 +220,16 @@ macro_rules! benchmarks_instance {
macro_rules! benchmarks_iter {
// mutation arm:
(
$instance:ident
{ $( $instance:ident )? }
{ $( $where_clause:tt )* }
{ $( $common:tt )* }
( $( $names:ident )* )
( $( $names:tt )* ) // This contains $( $( { $instance } )? $name:ident )*
$name:ident { $( $code:tt )* }: _ ( $origin:expr $( , $arg:expr )* )
verify $postcode:block
$( $rest:tt )*
) => {
$crate::benchmarks_iter! {
$instance
{ $( $instance)? }
{ $( $where_clause )* }
{ $( $common )* }
( $( $names )* )
@@ -238,49 +238,27 @@ macro_rules! benchmarks_iter {
$( $rest )*
}
};
// no instance mutation arm:
// mutation arm:
(
NO_INSTANCE
{ $( $instance:ident )? }
{ $( $where_clause:tt )* }
{ $( $common:tt )* }
( $( $names:ident )* )
( $( $names:tt )* )
$name:ident { $( $code:tt )* }: $dispatch:ident ( $origin:expr $( , $arg:expr )* )
verify $postcode:block
$( $rest:tt )*
) => {
$crate::benchmarks_iter! {
NO_INSTANCE
{ $( $instance)? }
{ $( $where_clause )* }
{ $( $common )* }
( $( $names )* )
$name { $( $code )* }: {
<
Call<T> as $crate::frame_support::traits::UnfilteredDispatchable
>::dispatch_bypass_filter(Call::<T>::$dispatch($($arg),*), $origin.into())?;
}
verify $postcode
$( $rest )*
}
};
// instance mutation arm:
(
INSTANCE
{ $( $where_clause:tt )* }
{ $( $common:tt )* }
( $( $names:ident )* )
$name:ident { $( $code:tt )* }: $dispatch:ident ( $origin:expr $( , $arg:expr )* )
verify $postcode:block
$( $rest:tt )*
) => {
$crate::benchmarks_iter! {
INSTANCE
{ $( $where_clause )* }
{ $( $common )* }
( $( $names )* )
$name { $( $code )* }: {
<
Call<T, I> as $crate::frame_support::traits::UnfilteredDispatchable
>::dispatch_bypass_filter(Call::<T, I>::$dispatch($($arg),*), $origin.into())?;
Call<T $(, $instance)? > as $crate::frame_support::traits::UnfilteredDispatchable
>::dispatch_bypass_filter(
Call::<T $(, $instance)? >::$dispatch($($arg),*), $origin.into()
)?;
}
verify $postcode
$( $rest )*
@@ -288,16 +266,16 @@ macro_rules! benchmarks_iter {
};
// iteration arm:
(
$instance:ident
{ $( $instance:ident )? }
{ $( $where_clause:tt )* }
{ $( $common:tt )* }
( $( $names:ident )* )
( $( $names:tt )* )
$name:ident { $( $code:tt )* }: $eval:block
verify $postcode:block
$( $rest:tt )*
) => {
$crate::benchmark_backend! {
$instance
{ $( $instance)? }
$name
{ $( $where_clause )* }
{ $( $common )* }
@@ -308,32 +286,41 @@ macro_rules! benchmarks_iter {
}
#[cfg(test)]
$crate::impl_benchmark_test!( { $( $where_clause )* } $instance $name );
$crate::impl_benchmark_test!(
{ $( $where_clause )* }
{ $( $instance)? }
$name
);
$crate::benchmarks_iter!(
$instance
{ $( $instance)? }
{ $( $where_clause )* }
{ $( $common )* }
( $( $names )* $name )
( $( $names )* { $( $instance )? } $name )
$( $rest )*
);
};
// iteration-exit arm
( $instance:ident { $( $where_clause:tt )* } { $( $common:tt )* } ( $( $names:ident )* ) ) => {
$crate::selected_benchmark!( { $( $where_clause)* } $instance $( $names ),* );
$crate::impl_benchmark!( { $( $where_clause )* } $instance $( $names ),* );
(
{ $( $instance:ident )? }
{ $( $where_clause:tt )* }
{ $( $common:tt )* }
( $( $names:tt )* )
) => {
$crate::selected_benchmark!( { $( $where_clause)* } { $( $instance)? } $( $names )* );
$crate::impl_benchmark!( { $( $where_clause )* } { $( $instance)? } $( $names )* );
};
// add verify block to _() format
(
$instance:ident
{ $( $instance:ident )? }
{ $( $where_clause:tt )* }
{ $( $common:tt )* }
( $( $names:ident )* )
( $( $names:tt )* )
$name:ident { $( $code:tt )* }: _ ( $origin:expr $( , $arg:expr )* )
$( $rest:tt )*
) => {
$crate::benchmarks_iter! {
$instance
{ $( $instance)? }
{ $( $where_clause )* }
{ $( $common )* }
( $( $names )* )
@@ -344,15 +331,15 @@ macro_rules! benchmarks_iter {
};
// add verify block to name() format
(
$instance:ident
{ $( $instance:ident )? }
{ $( $where_clause:tt )* }
{ $( $common:tt )* }
( $( $names:ident )* )
( $( $names:tt )* )
$name:ident { $( $code:tt )* }: $dispatch:ident ( $origin:expr $( , $arg:expr )* )
$( $rest:tt )*
) => {
$crate::benchmarks_iter! {
$instance
{ $( $instance)? }
{ $( $where_clause )* }
{ $( $common )* }
( $( $names )* )
@@ -363,15 +350,15 @@ macro_rules! benchmarks_iter {
};
// add verify block to {} format
(
$instance:ident
{ $( $instance:ident )? }
{ $( $where_clause:tt )* }
{ $( $common:tt )* }
( $( $names:ident )* )
( $( $names:tt )* )
$name:ident { $( $code:tt )* }: $eval:block
$( $rest:tt )*
) => {
$crate::benchmarks_iter!(
$instance
{ $( $instance)? }
{ $( $where_clause )* }
{ $( $common )* }
( $( $names )* )
@@ -386,7 +373,7 @@ macro_rules! benchmarks_iter {
#[doc(hidden)]
macro_rules! benchmark_backend {
// parsing arms
($instance:ident $name:ident {
( { $( $instance:ident )? } $name:ident {
$( $where_clause:tt )*
} {
$( $common:tt )*
@@ -397,13 +384,13 @@ macro_rules! benchmark_backend {
$( $rest:tt )*
} $postcode:block) => {
$crate::benchmark_backend! {
$instance $name { $( $where_clause )* } { $( $common )* } {
{ $( $instance)? } $name { $( $where_clause )* } { $( $common )* } {
$( PRE { $( $pre_parsed )* } )*
PRE { $pre_id , $pre_ty , $pre_ex }
} { $eval } { $( $rest )* } $postcode
}
};
($instance:ident $name:ident {
( { $( $instance:ident )? } $name:ident {
$( $where_clause:tt )*
} {
$( $common:tt )*
@@ -414,14 +401,14 @@ macro_rules! benchmark_backend {
$( $rest:tt )*
} $postcode:block) => {
$crate::benchmark_backend! {
$instance $name { $( $where_clause )* } { $( $common )* } {
{ $( $instance)? } $name { $( $where_clause )* } { $( $common )* } {
$( $parsed )*
PARAM { $param , $param_from , $param_to , $param_instancer }
} { $eval } { $( $rest )* } $postcode
}
};
// mutation arm to look after defaulting to a common param
($instance:ident $name:ident {
( { $( $instance:ident )? } $name:ident {
$( $where_clause:tt )*
} {
$( { $common:ident , $common_from:tt , $common_to:expr , $common_instancer:expr } )*
@@ -432,7 +419,7 @@ macro_rules! benchmark_backend {
$( $rest:tt )*
} $postcode:block) => {
$crate::benchmark_backend! {
$instance $name { $( $where_clause )* } {
{ $( $instance)? } $name { $( $where_clause )* } {
$( { $common , $common_from , $common_to , $common_instancer } )*
} {
$( $parsed )*
@@ -446,7 +433,7 @@ macro_rules! benchmark_backend {
}
};
// mutation arm to look after defaulting only the range to common param
($instance:ident $name:ident {
( { $( $instance:ident )? } $name:ident {
$( $where_clause:tt )*
} {
$( { $common:ident , $common_from:tt , $common_to:expr , $common_instancer:expr } )*
@@ -457,7 +444,7 @@ macro_rules! benchmark_backend {
$( $rest:tt )*
} $postcode:block) => {
$crate::benchmark_backend! {
$instance $name { $( $where_clause )* } {
{ $( $instance)? } $name { $( $where_clause )* } {
$( { $common , $common_from , $common_to , $common_instancer } )*
} {
$( $parsed )*
@@ -471,7 +458,7 @@ macro_rules! benchmark_backend {
}
};
// mutation arm to look after a single tt for param_from.
($instance:ident $name:ident {
( { $( $instance:ident )? } $name:ident {
$( $where_clause:tt )*
} {
$( $common:tt )*
@@ -482,14 +469,15 @@ macro_rules! benchmark_backend {
$( $rest:tt )*
} $postcode:block) => {
$crate::benchmark_backend! {
$instance $name { $( $where_clause )* } { $( $common )* } { $( $parsed )* } { $eval } {
{ $( $instance)? }
$name { $( $where_clause )* } { $( $common )* } { $( $parsed )* } { $eval } {
let $param in ( $param_from ) .. $param_to => $param_instancer;
$( $rest )*
} $postcode
}
};
// mutation arm to look after the default tail of `=> ()`
($instance:ident $name:ident {
( { $( $instance:ident )? } $name:ident {
$( $where_clause:tt )*
} {
$( $common:tt )*
@@ -500,14 +488,15 @@ macro_rules! benchmark_backend {
$( $rest:tt )*
} $postcode:block) => {
$crate::benchmark_backend! {
$instance $name { $( $where_clause )* } { $( $common )* } { $( $parsed )* } { $eval } {
{ $( $instance)? }
$name { $( $where_clause )* } { $( $common )* } { $( $parsed )* } { $eval } {
let $param in $param_from .. $param_to => ();
$( $rest )*
} $postcode
}
};
// mutation arm to look after `let _ =`
($instance:ident $name:ident {
( { $( $instance:ident )? } $name:ident {
$( $where_clause:tt )*
} {
$( $common:tt )*
@@ -518,14 +507,15 @@ macro_rules! benchmark_backend {
$( $rest:tt )*
} $postcode:block) => {
$crate::benchmark_backend! {
$instance $name { $( $where_clause )* } { $( $common )* } { $( $parsed )* } { $eval } {
{ $( $instance)? }
$name { $( $where_clause )* } { $( $common )* } { $( $parsed )* } { $eval } {
let $pre_id : _ = $pre_ex;
$( $rest )*
} $postcode
}
};
// no instance actioning arm
(NO_INSTANCE $name:ident {
// actioning arm
( { $( $instance:ident )? } $name:ident {
$( $where_clause:tt )*
} {
$( { $common:ident , $common_from:tt , $common_to:expr , $common_instancer:expr } )*
@@ -536,7 +526,8 @@ macro_rules! benchmark_backend {
#[allow(non_camel_case_types)]
struct $name;
#[allow(unused_variables)]
impl<T: Trait> $crate::BenchmarkingSetup<T> for $name
impl<T: Trait $( <$instance>, I: Instance)? >
$crate::BenchmarkingSetup<T $(, $instance)? > for $name
where $( $where_clause )*
{
fn components(&self) -> Vec<($crate::BenchmarkParameter, u32, u32)> {
@@ -590,72 +581,6 @@ macro_rules! benchmark_backend {
}
}
};
// instance actioning arm
(INSTANCE $name:ident {
$( $where_clause:tt )*
} {
$( { $common:ident , $common_from:tt , $common_to:expr , $common_instancer:expr } )*
} {
$( 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<T: Trait<I>, I: Instance> $crate::BenchmarkingSetupInstance<T, I> for $name
where $( $where_clause )*
{
fn components(&self) -> Vec<($crate::BenchmarkParameter, u32, u32)> {
vec! [
$(
($crate::BenchmarkParameter::$param, $param_from, $param_to)
),*
]
}
fn instance(&self, components: &[($crate::BenchmarkParameter, u32)])
-> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str>
{
$(
let $common = $common_from;
)*
$(
// Prepare instance
let $param = components.iter()
.find(|&c| c.0 == $crate::BenchmarkParameter::$param)
.unwrap().1;
)*
$(
let $pre_id : $pre_ty = $pre_ex;
)*
$( $param_instancer ; )*
$( $post )*
Ok(Box::new(move || -> Result<(), &'static str> { $eval; Ok(()) }))
}
fn verify(&self, components: &[($crate::BenchmarkParameter, u32)])
-> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str>
{
$(
let $common = $common_from;
)*
$(
// Prepare instance
let $param = components.iter()
.find(|&c| c.0 == $crate::BenchmarkParameter::$param)
.unwrap().1;
)*
$(
let $pre_id : $pre_ty = $pre_ex;
)*
$( $param_instancer ; )*
$( $post )*
Ok(Box::new(move || -> Result<(), &'static str> { $eval; $postcode; Ok(()) }))
}
}
}
}
// Creates a `SelectedBenchmark` enum implementing `BenchmarkingSetup`.
@@ -670,14 +595,15 @@ macro_rules! benchmark_backend {
// struct SetBalance;
// impl BenchmarkingSetup for SetBalance { ... }
//
// selected_benchmark!(Transfer, SetBalance);
// selected_benchmark!({} Transfer {} SetBalance);
// ```
#[macro_export]
#[doc(hidden)]
macro_rules! selected_benchmark {
(
{ $( $where_clause:tt )* }
NO_INSTANCE $( $bench:ident ),*
{ $( $instance:ident )? }
$( { $( $bench_inst:ident )? } $bench:ident )*
) => {
// The list of available benchmarks for this pallet.
#[allow(non_camel_case_types)]
@@ -686,12 +612,17 @@ macro_rules! selected_benchmark {
}
// Allow us to select a benchmark from the list of available benchmarks.
impl<T: Trait> $crate::BenchmarkingSetup<T> for SelectedBenchmark
impl<T: Trait $( <$instance>, I: Instance )? >
$crate::BenchmarkingSetup<T $(, $instance )? > for SelectedBenchmark
where $( $where_clause )*
{
fn components(&self) -> Vec<($crate::BenchmarkParameter, u32, u32)> {
match self {
$( Self::$bench => <$bench as $crate::BenchmarkingSetup<T>>::components(&$bench), )*
$(
Self::$bench => <
$bench as $crate::BenchmarkingSetup<T $(, $bench_inst)? >
>::components(&$bench),
)*
}
}
@@ -699,7 +630,11 @@ macro_rules! selected_benchmark {
-> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str>
{
match self {
$( Self::$bench => <$bench as $crate::BenchmarkingSetup<T>>::instance(&$bench, components), )*
$(
Self::$bench => <
$bench as $crate::BenchmarkingSetup<T $(, $bench_inst)? >
>::instance(&$bench, components),
)*
}
}
@@ -707,48 +642,15 @@ macro_rules! selected_benchmark {
-> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str>
{
match self {
$( Self::$bench => <$bench as $crate::BenchmarkingSetup<T>>::verify(&$bench, components), )*
$(
Self::$bench => <
$bench as $crate::BenchmarkingSetup<T $(, $bench_inst)? >
>::verify(&$bench, components),
)*
}
}
}
};
(
{ $( $where_clause:tt )* }
INSTANCE $( $bench:ident ),*
) => {
// The list of available benchmarks for this pallet.
#[allow(non_camel_case_types)]
enum SelectedBenchmark {
$( $bench, )*
}
// Allow us to select a benchmark from the list of available benchmarks.
impl<T: Trait<I>, I: Instance> $crate::BenchmarkingSetupInstance<T, I> for SelectedBenchmark
where $( $where_clause )*
{
fn components(&self) -> Vec<($crate::BenchmarkParameter, u32, u32)> {
match self {
$( Self::$bench => <$bench as $crate::BenchmarkingSetupInstance<T, I>>::components(&$bench), )*
}
}
fn instance(&self, components: &[($crate::BenchmarkParameter, u32)])
-> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str>
{
match self {
$( Self::$bench => <$bench as $crate::BenchmarkingSetupInstance<T, I>>::instance(&$bench, components), )*
}
}
fn verify(&self, components: &[($crate::BenchmarkParameter, u32)])
-> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str>
{
match self {
$( Self::$bench => <$bench as $crate::BenchmarkingSetupInstance<T, I>>::verify(&$bench, components), )*
}
}
}
}
}
#[macro_export]
@@ -756,9 +658,11 @@ macro_rules! selected_benchmark {
macro_rules! impl_benchmark {
(
{ $( $where_clause:tt )* }
NO_INSTANCE $( $name:ident ),*
{ $( $instance:ident )? }
$( { $( $name_inst:ident )? } $name:ident )*
) => {
impl<T: Trait> $crate::Benchmarking<$crate::BenchmarkResults> for Module<T>
impl<T: Trait $(<$instance>, I: Instance)? >
$crate::Benchmarking<$crate::BenchmarkResults> for Module<T $(, $instance)? >
where T: frame_system::Trait, $( $where_clause )*
{
fn benchmarks() -> Vec<&'static [u8]> {
@@ -788,7 +692,9 @@ macro_rules! impl_benchmark {
$crate::benchmarking::commit_db();
$crate::benchmarking::wipe_db();
let components = <SelectedBenchmark as $crate::BenchmarkingSetup<T>>::components(&selected_benchmark);
let components = <
SelectedBenchmark as $crate::BenchmarkingSetup<T $(, $instance)?>
>::components(&selected_benchmark);
let mut results: Vec<$crate::BenchmarkResults> = Vec::new();
// Default number of steps for a component.
@@ -804,7 +710,7 @@ macro_rules! impl_benchmark {
// Set up the externalities environment for the setup we want to
// benchmark.
let closure_to_benchmark = <
SelectedBenchmark as $crate::BenchmarkingSetup<T>
SelectedBenchmark as $crate::BenchmarkingSetup<T $(, $instance)?>
>::instance(&selected_benchmark, &c)?;
// Set the block number to at least 1 so events are deposited.
@@ -909,167 +815,6 @@ macro_rules! impl_benchmark {
}
}
};
(
{ $( $where_clause:tt )* }
INSTANCE $( $name:ident ),*
) => {
impl<T: Trait<I>, I: Instance> $crate::Benchmarking<$crate::BenchmarkResults>
for Module<T, I>
where T: frame_system::Trait, $( $where_clause )*
{
fn benchmarks() -> Vec<&'static [u8]> {
vec![ $( stringify!($name).as_ref() ),* ]
}
fn run_benchmark(
extrinsic: &[u8],
lowest_range_values: &[u32],
highest_range_values: &[u32],
steps: &[u32],
repeat: u32,
whitelist: &[Vec<u8>]
) -> Result<Vec<$crate::BenchmarkResults>, &'static str> {
// Map the input to the selected benchmark.
let extrinsic = sp_std::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."),
};
// Add whitelist to DB
$crate::benchmarking::set_whitelist(whitelist.to_vec());
// Warm up the DB
$crate::benchmarking::commit_db();
$crate::benchmarking::wipe_db();
let components = <
SelectedBenchmark as $crate::BenchmarkingSetupInstance<T, I>
>::components(&selected_benchmark);
let mut results: Vec<$crate::BenchmarkResults> = Vec::new();
// Default number of steps for a component.
let mut prev_steps = 10;
let repeat_benchmark = |
repeat: u32,
c: Vec<($crate::BenchmarkParameter, u32)>,
results: &mut Vec<$crate::BenchmarkResults>,
| -> Result<(), &'static str> {
// Run the benchmark `repeat` times.
for _ in 0..repeat {
// Set up the externalities environment for the setup we want to
// benchmark.
let closure_to_benchmark = <
SelectedBenchmark as $crate::BenchmarkingSetupInstance<T, I>
>::instance(&selected_benchmark, &c)?;
// Set the block number to at least 1 so events are deposited.
if $crate::Zero::is_zero(&frame_system::Module::<T>::block_number()) {
frame_system::Module::<T>::set_block_number(1.into());
}
// Commit the externalities to the database, flushing the DB cache.
// This will enable worst case scenario for reading from the database.
$crate::benchmarking::commit_db();
// Reset the read/write counter so we don't count operations in the setup process.
$crate::benchmarking::reset_read_write_count();
// Time the extrinsic logic.
frame_support::debug::trace!(
target: "benchmark",
"Start Benchmark: {:?}",
c,
);
let start_extrinsic = $crate::benchmarking::current_time();
closure_to_benchmark()?;
let finish_extrinsic = $crate::benchmarking::current_time();
let elapsed_extrinsic = finish_extrinsic - start_extrinsic;
// Commit the changes to get proper write count
$crate::benchmarking::commit_db();
frame_support::debug::trace!(
target: "benchmark",
"End Benchmark: {} ns",
elapsed_extrinsic,
);
let read_write_count = $crate::benchmarking::read_write_count();
frame_support::debug::trace!(
target: "benchmark",
"Read/Write Count {:?}",
read_write_count,
);
// Time the storage root recalculation.
let start_storage_root = $crate::benchmarking::current_time();
$crate::storage_root();
let finish_storage_root = $crate::benchmarking::current_time();
let elapsed_storage_root = finish_storage_root - start_storage_root;
results.push($crate::BenchmarkResults {
components: c.clone(),
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,
});
// Wipe the DB back to the genesis state.
$crate::benchmarking::wipe_db();
}
Ok(())
};
if components.is_empty() {
repeat_benchmark(repeat, Default::default(), &mut results)?;
} else {
// Select the component we will be benchmarking. Each component will be benchmarked.
for (idx, (name, low, high)) in components.iter().enumerate() {
// Get the number of steps for this component.
let steps = steps.get(idx).cloned().unwrap_or(prev_steps);
prev_steps = steps;
// Skip this loop if steps is zero
if steps == 0 { continue }
let lowest = lowest_range_values.get(idx).cloned().unwrap_or(*low);
let highest = highest_range_values.get(idx).cloned().unwrap_or(*high);
let diff = highest - lowest;
// Create up to `STEPS` steps for that component between high and low.
let step_size = (diff / steps).max(1);
let num_of_steps = diff / step_size + 1;
for s in 0..num_of_steps {
// This is the value we will be testing for component `name`
let component_value = lowest + step_size * s;
// Select the max value for all the other components.
let c: Vec<($crate::BenchmarkParameter, u32)> = components.iter()
.enumerate()
.map(|(idx, (n, _, h))|
if n == name {
(*n, component_value)
} else {
(*n, *highest_range_values.get(idx).unwrap_or(h))
}
)
.collect();
repeat_benchmark(repeat, c, &mut results)?;
}
}
}
return Ok(results);
}
}
}
}
// This creates a unit test for one benchmark of the main benchmark macro.
@@ -1080,16 +825,16 @@ macro_rules! impl_benchmark {
macro_rules! impl_benchmark_test {
(
{ $( $where_clause:tt )* }
NO_INSTANCE
{ $( $instance:ident )? }
$name:ident
) => {
$crate::paste::item! {
fn [<test_benchmark_ $name>] <T: Trait> () -> Result<(), &'static str>
fn [<test_benchmark_ $name>] <T: Trait > () -> Result<(), &'static str>
where T: frame_system::Trait, $( $where_clause )*
{
let selected_benchmark = SelectedBenchmark::$name;
let components = <
SelectedBenchmark as $crate::BenchmarkingSetup<T>
SelectedBenchmark as $crate::BenchmarkingSetup<T, _>
>::components(&selected_benchmark);
let execute_benchmark = |
@@ -1097,7 +842,7 @@ macro_rules! impl_benchmark_test {
| -> Result<(), &'static str> {
// Set up the verification state
let closure_to_verify = <
SelectedBenchmark as $crate::BenchmarkingSetup<T>
SelectedBenchmark as $crate::BenchmarkingSetup<T, _>
>::verify(&selected_benchmark, &c)?;
// Set the block number to at least 1 so events are deposited.
@@ -1140,64 +885,6 @@ macro_rules! impl_benchmark_test {
}
}
};
(
{ $( $where_clause:tt )* }
INSTANCE
$name:ident
) => {
$crate::paste::item! {
fn [<test_benchmark_ $name>] <T: Trait> () -> Result<(), &'static str>
where T: frame_system::Trait, $( $where_clause )*
{
let selected_benchmark = SelectedBenchmark::$name;
let components = <
SelectedBenchmark as $crate::BenchmarkingSetupInstance<T, _>
>::components(&selected_benchmark);
let execute_benchmark = |
c: Vec<($crate::BenchmarkParameter, u32)>
| -> Result<(), &'static str> {
// Set up the verification state
let closure_to_verify = <
SelectedBenchmark as $crate::BenchmarkingSetupInstance<T, _>
>::verify(&selected_benchmark, &c)?;
// Set the block number to at least 1 so events are deposited.
if $crate::Zero::is_zero(&frame_system::Module::<T>::block_number()) {
frame_system::Module::<T>::set_block_number(1.into());
}
// Run verification
closure_to_verify()?;
// Reset the state
$crate::benchmarking::wipe_db();
Ok(())
};
for (_, (name, low, high)) in components.iter().enumerate() {
// Test only the low and high value, assuming values in the middle won't break
for component_value in vec![low, high] {
// Select the max value for all the other components.
let c: Vec<($crate::BenchmarkParameter, u32)> = components.iter()
.enumerate()
.map(|(_, (n, _, h))|
if n == name {
(*n, *component_value)
} else {
(*n, *h)
}
)
.collect();
execute_benchmark(c)?;
}
}
Ok(())
}
}
};
}
+4 -13
View File
@@ -134,19 +134,10 @@ pub trait Benchmarking<T> {
}
/// The required setup for creating a benchmark.
pub trait BenchmarkingSetup<T> {
/// Return the components and their ranges which should be tested in this benchmark.
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)>;
/// Set up the storage, and prepare a closure to run the benchmark.
fn instance(&self, components: &[(BenchmarkParameter, u32)]) -> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str>;
/// Set up the storage, and prepare a closure to test and verify the benchmark
fn verify(&self, components: &[(BenchmarkParameter, u32)]) -> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str>;
}
/// The required setup for creating a benchmark.
pub trait BenchmarkingSetupInstance<T, I> {
///
/// Instance generic parameter is optional and can be used in order to capture unused generics for
/// instantiable pallets.
pub trait BenchmarkingSetup<T, I = ()> {
/// Return the components and their ranges which should be tested in this benchmark.
fn components(&self) -> Vec<(BenchmarkParameter, u32, u32)>;