mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-11 21:11:07 +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:
Generated
+1
@@ -5293,6 +5293,7 @@ dependencies = [
|
||||
name = "pallet-template"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"frame-benchmarking",
|
||||
"frame-support",
|
||||
"frame-system",
|
||||
"parity-scale-codec",
|
||||
|
||||
@@ -25,6 +25,12 @@ default-features = false
|
||||
version = "3.0.0"
|
||||
path = "../../../../frame/system"
|
||||
|
||||
[dependencies.frame-benchmarking]
|
||||
default-features = false
|
||||
version = "3.1.0"
|
||||
path = "../../../../frame/benchmarking"
|
||||
optional = true
|
||||
|
||||
[dev-dependencies]
|
||||
serde = { version = "1.0.101" }
|
||||
|
||||
@@ -43,12 +49,13 @@ default-features = false
|
||||
version = "3.0.0"
|
||||
path = "../../../../primitives/runtime"
|
||||
|
||||
|
||||
[features]
|
||||
default = ['std']
|
||||
std = [
|
||||
'codec/std',
|
||||
'frame-support/std',
|
||||
'frame-system/std'
|
||||
'frame-system/std',
|
||||
'frame-benchmarking/std',
|
||||
]
|
||||
runtime-benchmarks = ["frame-benchmarking"]
|
||||
try-runtime = ["frame-support/try-runtime"]
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
//! Benchmarking setup for pallet-template
|
||||
|
||||
use super::*;
|
||||
|
||||
use frame_system::RawOrigin;
|
||||
use frame_benchmarking::{benchmarks, whitelisted_caller, impl_benchmark_test_suite};
|
||||
#[allow(unused)]
|
||||
use crate::Module as Template;
|
||||
|
||||
benchmarks! {
|
||||
do_something {
|
||||
let s in 0 .. 100;
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
}: _(RawOrigin::Signed(caller), s)
|
||||
verify {
|
||||
assert_eq!(Something::<T>::get(), Some(s));
|
||||
}
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite!(
|
||||
Template,
|
||||
crate::mock::new_test_ext(),
|
||||
crate::mock::Test,
|
||||
);
|
||||
@@ -12,6 +12,9 @@ mod mock;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
mod benchmarking;
|
||||
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*};
|
||||
@@ -46,7 +49,7 @@ pub mod pallet {
|
||||
/// parameters. [something, who]
|
||||
SomethingStored(u32, T::AccountId),
|
||||
}
|
||||
|
||||
|
||||
// Errors inform users that something went wrong.
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
|
||||
@@ -89,4 +89,5 @@ runtime-benchmarks = [
|
||||
"frame-system/runtime-benchmarks",
|
||||
"pallet-balances/runtime-benchmarks",
|
||||
"pallet-timestamp/runtime-benchmarks",
|
||||
"template/runtime-benchmarks",
|
||||
]
|
||||
|
||||
@@ -475,6 +475,7 @@ impl_runtime_apis! {
|
||||
add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
|
||||
add_benchmark!(params, batches, pallet_balances, Balances);
|
||||
add_benchmark!(params, batches, pallet_timestamp, Timestamp);
|
||||
add_benchmark!(params, batches, template, TemplateModule);
|
||||
|
||||
if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
|
||||
Ok(batches)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
use crate::*;
|
||||
use frame_support::traits::OnInitialize;
|
||||
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};
|
||||
use sp_std::prelude::*;
|
||||
|
||||
benchmarks! {
|
||||
on_initialize {
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#![cfg(feature = "runtime-benchmarks")]
|
||||
|
||||
use super::*;
|
||||
use sp_std::prelude::*;
|
||||
use frame_system::RawOrigin;
|
||||
use frame_support::{ensure, traits::OnFinalize};
|
||||
use frame_benchmarking::{benchmarks, TrackedStorageKey, impl_benchmark_test_suite};
|
||||
|
||||
Reference in New Issue
Block a user