mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 17:01:09 +00:00
Add benchmark to node-template pallet-template (#8239)
* Add benchmark to node-template pallet-template * export sp_std to avoid missing dep when using macro * fix more `sp_std` deps * remove unused * Update bin/node-template/pallets/template/src/benchmarking.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * Update bin/node-template/pallets/template/Cargo.toml Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
@@ -34,6 +34,8 @@ pub use sp_runtime::traits::Zero;
|
||||
#[doc(hidden)]
|
||||
pub use frame_support;
|
||||
#[doc(hidden)]
|
||||
pub use sp_std::{self, vec, prelude::Vec, boxed::Box};
|
||||
#[doc(hidden)]
|
||||
pub use paste;
|
||||
#[doc(hidden)]
|
||||
pub use sp_storage::TrackedStorageKey;
|
||||
@@ -566,8 +568,8 @@ macro_rules! benchmark_backend {
|
||||
$crate::BenchmarkingSetup<T $(, $instance)? > for $name
|
||||
where $( $where_clause )*
|
||||
{
|
||||
fn components(&self) -> Vec<($crate::BenchmarkParameter, u32, u32)> {
|
||||
vec! [
|
||||
fn components(&self) -> $crate::Vec<($crate::BenchmarkParameter, u32, u32)> {
|
||||
$crate::vec! [
|
||||
$(
|
||||
($crate::BenchmarkParameter::$param, $param_from, $param_to)
|
||||
),*
|
||||
@@ -578,7 +580,7 @@ macro_rules! benchmark_backend {
|
||||
&self,
|
||||
components: &[($crate::BenchmarkParameter, u32)],
|
||||
verify: bool
|
||||
) -> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str> {
|
||||
) -> Result<$crate::Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str> {
|
||||
$(
|
||||
// Prepare instance
|
||||
let $param = components.iter()
|
||||
@@ -592,7 +594,7 @@ macro_rules! benchmark_backend {
|
||||
$( $param_instancer ; )*
|
||||
$( $post )*
|
||||
|
||||
Ok(Box::new(move || -> Result<(), &'static str> {
|
||||
Ok($crate::Box::new(move || -> Result<(), &'static str> {
|
||||
$eval;
|
||||
if verify {
|
||||
$postcode;
|
||||
@@ -637,7 +639,7 @@ macro_rules! selected_benchmark {
|
||||
$crate::BenchmarkingSetup<T $(, $instance )? > for SelectedBenchmark
|
||||
where $( $where_clause )*
|
||||
{
|
||||
fn components(&self) -> Vec<($crate::BenchmarkParameter, u32, u32)> {
|
||||
fn components(&self) -> $crate::Vec<($crate::BenchmarkParameter, u32, u32)> {
|
||||
match self {
|
||||
$(
|
||||
Self::$bench => <
|
||||
@@ -651,7 +653,7 @@ macro_rules! selected_benchmark {
|
||||
&self,
|
||||
components: &[($crate::BenchmarkParameter, u32)],
|
||||
verify: bool
|
||||
) -> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str> {
|
||||
) -> Result<$crate::Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str> {
|
||||
match self {
|
||||
$(
|
||||
Self::$bench => <
|
||||
@@ -677,8 +679,8 @@ macro_rules! impl_benchmark {
|
||||
$crate::Benchmarking<$crate::BenchmarkResults> for Module<T $(, $instance)? >
|
||||
where T: frame_system::Config, $( $where_clause )*
|
||||
{
|
||||
fn benchmarks(extra: bool) -> Vec<&'static [u8]> {
|
||||
let mut all = vec![ $( stringify!($name).as_ref() ),* ];
|
||||
fn benchmarks(extra: bool) -> $crate::Vec<&'static [u8]> {
|
||||
let mut all = $crate::vec![ $( stringify!($name).as_ref() ),* ];
|
||||
if !extra {
|
||||
let extra = [ $( stringify!($name_extra).as_ref() ),* ];
|
||||
all.retain(|x| !extra.contains(x));
|
||||
@@ -694,15 +696,15 @@ macro_rules! impl_benchmark {
|
||||
repeat: u32,
|
||||
whitelist: &[$crate::TrackedStorageKey],
|
||||
verify: bool,
|
||||
) -> Result<Vec<$crate::BenchmarkResults>, &'static str> {
|
||||
) -> Result<$crate::Vec<$crate::BenchmarkResults>, &'static str> {
|
||||
// Map the input to the selected benchmark.
|
||||
let extrinsic = sp_std::str::from_utf8(extrinsic)
|
||||
let extrinsic = $crate::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."),
|
||||
};
|
||||
let mut results: Vec<$crate::BenchmarkResults> = Vec::new();
|
||||
let mut results: $crate::Vec<$crate::BenchmarkResults> = $crate::Vec::new();
|
||||
if repeat == 0 {
|
||||
return Ok(results);
|
||||
}
|
||||
@@ -710,7 +712,7 @@ macro_rules! impl_benchmark {
|
||||
// Add whitelist to DB including whitelisted caller
|
||||
let mut whitelist = whitelist.to_vec();
|
||||
let whitelisted_caller_key =
|
||||
<frame_system::Account::<T> as frame_support::storage::StorageMap<_,_>>::hashed_key_for(
|
||||
<frame_system::Account::<T> as $crate::frame_support::storage::StorageMap<_,_>>::hashed_key_for(
|
||||
$crate::whitelisted_caller::<T::AccountId>()
|
||||
);
|
||||
whitelist.push(whitelisted_caller_key.into());
|
||||
@@ -730,7 +732,7 @@ macro_rules! impl_benchmark {
|
||||
let repeat_benchmark = |
|
||||
repeat: u32,
|
||||
c: &[($crate::BenchmarkParameter, u32)],
|
||||
results: &mut Vec<$crate::BenchmarkResults>,
|
||||
results: &mut $crate::Vec<$crate::BenchmarkResults>,
|
||||
verify: bool,
|
||||
| -> Result<(), &'static str> {
|
||||
// Run the benchmark `repeat` times.
|
||||
@@ -807,7 +809,7 @@ macro_rules! impl_benchmark {
|
||||
if components.is_empty() {
|
||||
if verify {
|
||||
// If `--verify` is used, run the benchmark once to verify it would complete.
|
||||
repeat_benchmark(1, Default::default(), &mut Vec::new(), true)?;
|
||||
repeat_benchmark(1, Default::default(), &mut $crate::Vec::new(), true)?;
|
||||
}
|
||||
repeat_benchmark(repeat, Default::default(), &mut results, false)?;
|
||||
} else {
|
||||
@@ -834,7 +836,7 @@ macro_rules! impl_benchmark {
|
||||
let component_value = lowest + step_size * s;
|
||||
|
||||
// Select the max value for all the other components.
|
||||
let c: Vec<($crate::BenchmarkParameter, u32)> = components.iter()
|
||||
let c: $crate::Vec<($crate::BenchmarkParameter, u32)> = components.iter()
|
||||
.enumerate()
|
||||
.map(|(idx, (n, _, h))|
|
||||
if n == name {
|
||||
@@ -847,7 +849,7 @@ macro_rules! impl_benchmark {
|
||||
|
||||
if verify {
|
||||
// If `--verify` is used, run the benchmark once to verify it would complete.
|
||||
repeat_benchmark(1, &c, &mut Vec::new(), true)?;
|
||||
repeat_benchmark(1, &c, &mut $crate::Vec::new(), true)?;
|
||||
}
|
||||
repeat_benchmark(repeat, &c, &mut results, false)?;
|
||||
}
|
||||
@@ -872,7 +874,7 @@ macro_rules! impl_benchmark {
|
||||
where
|
||||
T: Config + frame_system::Config, $( $where_clause )*
|
||||
{
|
||||
let name = sp_std::str::from_utf8(name)
|
||||
let name = $crate::sp_std::str::from_utf8(name)
|
||||
.map_err(|_| "`name` is not a valid utf8 string!")?;
|
||||
match name {
|
||||
$( stringify!($name) => {
|
||||
@@ -905,7 +907,7 @@ macro_rules! impl_benchmark_test {
|
||||
>::components(&selected_benchmark);
|
||||
|
||||
let execute_benchmark = |
|
||||
c: Vec<($crate::BenchmarkParameter, u32)>
|
||||
c: $crate::Vec<($crate::BenchmarkParameter, u32)>
|
||||
| -> Result<(), &'static str> {
|
||||
// Set up the benchmark, return execution + verification function.
|
||||
let closure_to_verify = <
|
||||
@@ -931,9 +933,9 @@ macro_rules! impl_benchmark_test {
|
||||
} else {
|
||||
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] {
|
||||
for component_value in $crate::vec![low, high] {
|
||||
// Select the max value for all the other components.
|
||||
let c: Vec<($crate::BenchmarkParameter, u32)> = components.iter()
|
||||
let c: $crate::Vec<($crate::BenchmarkParameter, u32)> = components.iter()
|
||||
.enumerate()
|
||||
.map(|(_, (n, _, h))|
|
||||
if n == name {
|
||||
|
||||
Reference in New Issue
Block a user