Add steps setting to benchmarking CLI (#5033)

* Add steps setting to CLI, use max value to hit worst case.

* Bump impl_version.

* Apply review suggestion.
This commit is contained in:
Marcio Diaz
2020-02-25 15:51:25 +01:00
committed by GitHub
parent 36770979bb
commit d024364503
4 changed files with 20 additions and 13 deletions
+2 -2
View File
@@ -82,7 +82,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// implementation changes and behavior does not, then leave spec_version as // implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version. // is and increment impl_version.
spec_version: 225, spec_version: 225,
impl_version: 0, impl_version: 1,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
}; };
@@ -819,7 +819,7 @@ impl_runtime_apis! {
fn dispatch_benchmark( fn dispatch_benchmark(
module: Vec<u8>, module: Vec<u8>,
extrinsic: Vec<u8>, extrinsic: Vec<u8>,
steps: u32, steps: Vec<u32>,
repeat: u32, repeat: u32,
) -> Option<Vec<frame_benchmarking::BenchmarkResults>> { ) -> Option<Vec<frame_benchmarking::BenchmarkResults>> {
use frame_benchmarking::Benchmarking; use frame_benchmarking::Benchmarking;
+12 -6
View File
@@ -140,7 +140,7 @@ macro_rules! impl_benchmark {
$( $name:ident ),* $( $name:ident ),*
) => { ) => {
impl<T: Trait> $crate::Benchmarking<$crate::BenchmarkResults> for Module<T> { impl<T: Trait> $crate::Benchmarking<$crate::BenchmarkResults> for Module<T> {
fn run_benchmark(extrinsic: Vec<u8>, steps: u32, repeat: u32) -> Result<Vec<$crate::BenchmarkResults>, &'static str> { fn run_benchmark(extrinsic: Vec<u8>, steps: Vec<u32>, repeat: u32) -> Result<Vec<$crate::BenchmarkResults>, &'static str> {
// Map the input to the selected benchmark. // Map the input to the selected benchmark.
let extrinsic = sp_std::str::from_utf8(extrinsic.as_slice()) let extrinsic = sp_std::str::from_utf8(extrinsic.as_slice())
.map_err(|_| "Could not find extrinsic")?; .map_err(|_| "Could not find extrinsic")?;
@@ -153,15 +153,21 @@ macro_rules! impl_benchmark {
$crate::benchmarking::commit_db(); $crate::benchmarking::commit_db();
$crate::benchmarking::wipe_db(); $crate::benchmarking::wipe_db();
// first one is set_identity.
let components = <SelectedBenchmark as $crate::BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::components(&selected_benchmark); let components = <SelectedBenchmark as $crate::BenchmarkingSetup<T, crate::Call<T>, RawOrigin<T::AccountId>>>::components(&selected_benchmark);
// results go here
let mut results: Vec<$crate::BenchmarkResults> = Vec::new(); let mut results: Vec<$crate::BenchmarkResults> = Vec::new();
// Default number of steps for a component.
let mut prev_steps = &10;
// Select the component we will be benchmarking. Each component will be benchmarked. // Select the component we will be benchmarking. Each component will be benchmarked.
for (name, low, high) in components.iter() { for (idx, (name, low, high)) in components.iter().enumerate() {
// Get the number of steps for this component.
let steps = steps.get(idx).unwrap_or(&prev_steps);
prev_steps = steps;
// Create up to `STEPS` steps for that component between high and low. // Create up to `STEPS` steps for that component between high and low.
let step_size = ((high - low) / steps).max(1); let step_size = ((high - low) / steps).max(1);
let num_of_steps = (high - low) / step_size; let num_of_steps = (high - low) / step_size + 1;
for s in 0..num_of_steps { for s in 0..num_of_steps {
// This is the value we will be testing for component `name` // This is the value we will be testing for component `name`
let component_value = low + step_size * s; let component_value = low + step_size * s;
@@ -169,7 +175,7 @@ macro_rules! impl_benchmark {
// Select the mid value for all the other components. // Select the mid value for all the other components.
let c: Vec<($crate::BenchmarkParameter, u32)> = components.iter() let c: Vec<($crate::BenchmarkParameter, u32)> = components.iter()
.map(|(n, l, h)| .map(|(n, l, h)|
(*n, if n == name { component_value } else { (h - l) / 2 + l }) (*n, if n == name { component_value } else { *h })
).collect(); ).collect();
// Run the benchmark `repeat` times. // Run the benchmark `repeat` times.
+2 -2
View File
@@ -40,7 +40,7 @@ sp_api::decl_runtime_apis! {
fn dispatch_benchmark( fn dispatch_benchmark(
module: Vec<u8>, module: Vec<u8>,
extrinsic: Vec<u8>, extrinsic: Vec<u8>,
steps: u32, steps: Vec<u32>,
repeat: u32, repeat: u32,
) -> Option<Vec<BenchmarkResults>>; ) -> Option<Vec<BenchmarkResults>>;
} }
@@ -78,7 +78,7 @@ pub trait Benchmarking<T> {
/// - `extrinsic`: The name of extrinsic function you want to benchmark encoded as bytes. /// - `extrinsic`: The name of extrinsic function you want to benchmark encoded as bytes.
/// - `steps`: The number of sample points you want to take across the range of parameters. /// - `steps`: The number of sample points you want to take across the range of parameters.
/// - `repeat`: The number of times you want to repeat a benchmark. /// - `repeat`: The number of times you want to repeat a benchmark.
fn run_benchmark(extrinsic: Vec<u8>, steps: u32, repeat: u32) -> Result<Vec<T>, &'static str>; fn run_benchmark(extrinsic: Vec<u8>, steps: Vec<u32>, repeat: u32) -> Result<Vec<T>, &'static str>;
} }
/// The required setup for creating a benchmark. /// The required setup for creating a benchmark.
@@ -36,8 +36,8 @@ pub struct BenchmarkCmd {
pub extrinsic: String, pub extrinsic: String,
/// Select how many samples we should take across the variable components. /// Select how many samples we should take across the variable components.
#[structopt(short, long, default_value = "1")] #[structopt(short, long, use_delimiter = true)]
pub steps: u32, pub steps: Vec<u32>,
/// Select how many repetitions of this benchmark should run. /// Select how many repetitions of this benchmark should run.
#[structopt(short, long, default_value = "1")] #[structopt(short, long, default_value = "1")]
@@ -97,13 +97,14 @@ impl BenchmarkCmd {
wasm_method, wasm_method,
None, // heap pages None, // heap pages
); );
let result = StateMachine::<_, _, NumberFor<BB>, _>::new( let result = StateMachine::<_, _, NumberFor<BB>, _>::new(
&state, &state,
None, None,
&mut changes, &mut changes,
&executor, &executor,
"Benchmark_dispatch_benchmark", "Benchmark_dispatch_benchmark",
&(&self.pallet, &self.extrinsic, self.steps, self.repeat).encode(), &(&self.pallet, &self.extrinsic, self.steps.clone(), self.repeat).encode(),
Default::default(), Default::default(),
) )
.execute(strategy.into()) .execute(strategy.into())