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
+2
View File
@@ -11,6 +11,7 @@ description = "FRAME example pallet"
[dependencies]
serde = { version = "1.0.101", optional = true }
codec = { package = "parity-scale-codec", version = "1.2.0", default-features = false }
frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../benchmarking" }
frame-support = { version = "2.0.0-alpha.2", default-features = false, path = "../support" }
frame-system = { version = "2.0.0-alpha.2", default-features = false, path = "../system" }
pallet-balances = { version = "2.0.0-alpha.2", default-features = false, path = "../balances" }
@@ -27,6 +28,7 @@ std = [
"serde",
"codec/std",
"sp-runtime/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"pallet-balances/std",
+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::*;