Generate Unit Tests for Benchmarks (#5527)

* Update to latest staking

* generate tests for benchmarking

* add tests, fix warnings

* starting on democracy

* impl_benchmark_tests

* Way more readable

* add test feature flag (does this work?)

* Fix `successful_origin` impl

* democracry benchmark tests

* Fix example benchmarks, add tests

* identity benchmark tests

* Update im-online benchmark tests

* try to add session benchmarking tests (problem with mock)

* staking and timestamp

* add test for treasury, issue with dynamic contains

* utility

* Vesting

* test instead of check

* hide until we figure out what is wrong

* add docs

* close code

* Create custom mock for session-pallet-benchmarking

* Use refcell pattern

* make un-pub

* test-linux-stable includes `runtime-benchmarks` feature

* Revert "test-linux-stable includes `runtime-benchmarks` feature"

This reverts commit a2dab38abd18ac3eb8a6220e4a00e687740bd38c.

* run tests in `--release`

* undo balance change

* build wasm
This commit is contained in:
Shawn Tabrizi
2020-04-07 11:35:00 +02:00
committed by GitHub
parent 3e9e5e1bac
commit d3cc051419
28 changed files with 667 additions and 116 deletions
+27 -4
View File
@@ -22,7 +22,9 @@ use super::*;
use codec::Decode;
use sp_std::prelude::*;
use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::{H256, Header}};
use frame_support::{dispatch::DispatchResult, decl_module, impl_outer_origin};
use frame_support::{
dispatch::DispatchResult, decl_module, impl_outer_origin, assert_ok, assert_err, ensure
};
use frame_system::{RawOrigin, ensure_signed, ensure_none};
decl_module! {
@@ -107,13 +109,24 @@ benchmarks!{
}: other_dummy (RawOrigin::Signed(caller), b.into())
sort_vector {
let x in 0 .. 10000;
let x in 1 .. 10000;
let mut m = Vec::<u32>::new();
for i in 0..x {
for i in (0..x).rev() {
m.push(i);
}
}: {
m.sort();
ensure!(m[0] == 0, "You forgot to sort!")
}
broken_benchmark {
let x in 1 .. 10000;
let mut m = Vec::<u32>::new();
for i in (0..x).rev() {
m.push(i);
}
}: {
ensure!(m[0] == 0, "You forgot to sort!")
}
}
@@ -157,7 +170,7 @@ fn benchmarks_macro_works_for_non_dispatchable() {
let selected_benchmark = SelectedBenchmark::sort_vector;
let components = <SelectedBenchmark as BenchmarkingSetup<Test>>::components(&selected_benchmark);
assert_eq!(components, vec![(BenchmarkParameter::x, 0, 10000)]);
assert_eq!(components, vec![(BenchmarkParameter::x, 1, 10000)]);
let closure = <SelectedBenchmark as BenchmarkingSetup<Test>>::instance(
&selected_benchmark,
@@ -166,3 +179,13 @@ fn benchmarks_macro_works_for_non_dispatchable() {
assert_eq!(closure(), Ok(()));
}
#[test]
fn benchmarks_generate_unit_tests() {
new_test_ext().execute_with(|| {
assert_ok!(test_benchmark_dummy::<Test>());
assert_err!(test_benchmark_other_name::<Test>(), "Bad origin");
assert_ok!(test_benchmark_sort_vector::<Test>());
assert_err!(test_benchmark_broken_benchmark::<Test>(), "You forgot to sort!");
});
}