Fix benchmarks! macro for non dispatchable code (#5100)

* Init allowing non dispatchable closure to be benchmarked.

* Remove example, add it in timestamp

* Fix nits.

* Move test to example.

* Update frame/example/src/lib.rs

Co-Authored-By: Shawn Tabrizi <shawntabrizi@gmail.com>

* Update frame/example/src/lib.rs

Co-Authored-By: Shawn Tabrizi <shawntabrizi@gmail.com>

* Apply review suggestion: move test to benchmarking crate.

* Update frame/benchmarking/src/lib.rs

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

* Remove unused imports.

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Benjamin Kampmann <ben.kampmann@googlemail.com>
This commit is contained in:
Marcio Diaz
2020-03-04 10:13:14 +01:00
committed by GitHub
parent 619f64efe9
commit f39e705523
7 changed files with 352 additions and 130 deletions
+40 -2
View File
@@ -258,10 +258,12 @@ use frame_support::{
dispatch::DispatchResult, decl_module, decl_storage, decl_event,
weights::{SimpleDispatchInfo, DispatchInfo, DispatchClass, ClassifyDispatch, WeighData, Weight, PaysFee},
};
use frame_system::{self as system, ensure_signed, ensure_root};
use sp_std::prelude::*;
use frame_benchmarking::{benchmarks, account};
use frame_system::{self as system, ensure_signed, ensure_root, RawOrigin};
use codec::{Encode, Decode};
use sp_runtime::{
traits::{SignedExtension, Bounded, SaturatedConversion},
traits::{SignedExtension, Bounded, SaturatedConversion, Dispatchable},
transaction_validity::{
ValidTransaction, TransactionValidityError, InvalidTransaction, TransactionValidity,
},
@@ -642,6 +644,42 @@ impl<T: Trait + Send + Sync> SignedExtension for WatchDummy<T> {
}
}
benchmarks!{
_ {
// Define a common range for `b`.
let b in 1 .. 1000 => ();
}
// This will measure the execution time of `accumulate_dummy` for b in [1..1000] range.
accumulate_dummy {
let b in ...;
let caller = account("caller", 0, 0);
}: _ (RawOrigin::Signed(caller), b.into())
// This will measure the execution time of `set_dummy` for b in [1..1000] range.
set_dummy {
let b in ...;
let caller = account("caller", 0, 0);
}: set_dummy (RawOrigin::Signed(caller), b.into())
// This will measure the execution time of `set_dummy` for b in [1..10] range.
another_set_dummy {
let b in 1 .. 10;
let caller = account("caller", 0, 0);
}: set_dummy (RawOrigin::Signed(caller), b.into())
// This will measure the execution time of sorting a vector.
sort_vector {
let x in 0 .. 10000;
let mut m = Vec::<u32>::new();
for i in 0..x {
m.push(i);
}
}: {
m.sort();
}
}
#[cfg(test)]
mod tests {
use super::*;