Custom Benchmark Errors and Override (#9517)

* initial idea

* update benchmark test to frame v2

* fix some errors

* fixes for elec phrag

* fix tests

* update extrinsic time and docs

* fix import

* undo extra changes

* helper function

* wrong way

* Update frame/benchmarking/src/utils.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* doesnt need encode/decode

* fix benchmark return

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Shawn Tabrizi
2021-08-19 14:34:56 +02:00
committed by GitHub
parent 7a32c3504d
commit ccfe485b91
13 changed files with 254 additions and 136 deletions
+57 -9
View File
@@ -18,7 +18,11 @@
//! Interfaces, types and utils for benchmarking a FRAME runtime.
use codec::{Decode, Encode};
use frame_support::traits::StorageInfo;
use frame_support::{
dispatch::{DispatchError, DispatchErrorWithPostInfo},
pallet_prelude::*,
traits::StorageInfo,
};
use sp_io::hashing::blake2_256;
use sp_std::{prelude::Box, vec::Vec};
use sp_storage::TrackedStorageKey;
@@ -73,7 +77,7 @@ pub struct BenchmarkBatch {
/// The extrinsic (or benchmark name) of this benchmark.
pub benchmark: Vec<u8>,
/// The results from this benchmark.
pub results: Vec<BenchmarkResults>,
pub results: Vec<BenchmarkResult>,
}
// TODO: could probably make API cleaner here.
@@ -87,16 +91,16 @@ pub struct BenchmarkBatchSplitResults {
/// The extrinsic (or benchmark name) of this benchmark.
pub benchmark: Vec<u8>,
/// The extrinsic timing results from this benchmark.
pub time_results: Vec<BenchmarkResults>,
pub time_results: Vec<BenchmarkResult>,
/// The db tracking results from this benchmark.
pub db_results: Vec<BenchmarkResults>,
pub db_results: Vec<BenchmarkResult>,
}
/// Results from running benchmarks on a FRAME pallet.
/// Result from running benchmarks on a FRAME pallet.
/// Contains duration of the function call in nanoseconds along with the benchmark parameters
/// used for that benchmark result.
#[derive(Encode, Decode, Default, Clone, PartialEq, Debug)]
pub struct BenchmarkResults {
pub struct BenchmarkResult {
pub components: Vec<(BenchmarkParameter, u32)>,
pub extrinsic_time: u128,
pub storage_root_time: u128,
@@ -108,6 +112,50 @@ pub struct BenchmarkResults {
pub keys: Vec<(Vec<u8>, u32, u32, bool)>,
}
impl BenchmarkResult {
pub fn from_weight(w: Weight) -> Self {
Self { extrinsic_time: (w as u128) / 1_000, ..Default::default() }
}
}
/// Possible errors returned from the benchmarking pipeline.
///
/// * Stop: The benchmarking pipeline should stop and return the inner string.
/// * WeightOverride: The benchmarking pipeline is allowed to fail here, and we should use the
/// included weight instead.
#[derive(Clone, PartialEq, Debug)]
pub enum BenchmarkError {
Stop(&'static str),
Override(BenchmarkResult),
}
impl From<BenchmarkError> for &'static str {
fn from(e: BenchmarkError) -> Self {
match e {
BenchmarkError::Stop(s) => s,
BenchmarkError::Override(_) => "benchmark override",
}
}
}
impl From<&'static str> for BenchmarkError {
fn from(s: &'static str) -> Self {
Self::Stop(s)
}
}
impl From<DispatchErrorWithPostInfo> for BenchmarkError {
fn from(e: DispatchErrorWithPostInfo) -> Self {
Self::Stop(e.into())
}
}
impl From<DispatchError> for BenchmarkError {
fn from(e: DispatchError) -> Self {
Self::Stop(e.into())
}
}
/// Configuration used to setup and run runtime benchmarks.
#[derive(Encode, Decode, Default, Clone, PartialEq, Debug)]
pub struct BenchmarkConfig {
@@ -235,7 +283,7 @@ pub trait Benchmarking {
}
/// The pallet benchmarking trait.
pub trait Benchmarking<T> {
pub trait Benchmarking {
/// Get the benchmarks available for this pallet. Generally there is one benchmark per
/// extrinsic, so these are sometimes just called "extrinsics".
///
@@ -251,7 +299,7 @@ pub trait Benchmarking<T> {
whitelist: &[TrackedStorageKey],
verify: bool,
internal_repeats: u32,
) -> Result<Vec<T>, &'static str>;
) -> Result<Vec<BenchmarkResult>, BenchmarkError>;
}
/// The required setup for creating a benchmark.
@@ -267,7 +315,7 @@ pub trait BenchmarkingSetup<T, I = ()> {
&self,
components: &[(BenchmarkParameter, u32)],
verify: bool,
) -> Result<Box<dyn FnOnce() -> Result<(), &'static str>>, &'static str>;
) -> Result<Box<dyn FnOnce() -> Result<(), BenchmarkError>>, &'static str>;
}
/// Grab an account, seeded by a name and index.