Skip to content

Commit

Permalink
Always wipe DB state after benchmark (paritytech#12025)
Browse files Browse the repository at this point in the history
* Add test

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Always wipe the DB state

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Silence Clippy

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
  • Loading branch information
ggwpez authored and ark0f committed Feb 27, 2023
1 parent d6be15c commit 5686d29
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frame/benchmarking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ frame-support = { version = "4.0.0-dev", default-features = false, path = "../su
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api" }
sp-application-crypto = { version = "6.0.0", default-features = false, path = "../../primitives/application-crypto" }
sp-core = { version = "6.0.0", default-features = false, path = "../../primitives/core" }
sp-io = { version = "6.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "6.0.0", default-features = false, path = "../../primitives/runtime" }
sp-runtime-interface = { version = "6.0.0", default-features = false, path = "../../primitives/runtime-interface" }
Expand All @@ -45,6 +46,7 @@ std = [
"serde",
"sp-api/std",
"sp-application-crypto/std",
"sp-core/std",
"sp-io/std",
"sp-runtime-interface/std",
"sp-runtime/std",
Expand Down
18 changes: 9 additions & 9 deletions frame/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub use log;
#[doc(hidden)]
pub use paste;
#[doc(hidden)]
pub use sp_core::defer;
#[doc(hidden)]
pub use sp_io::storage::root as storage_root;
#[doc(hidden)]
pub use sp_runtime::traits::Zero;
Expand Down Expand Up @@ -1033,6 +1035,9 @@ macro_rules! impl_benchmark {

// Always do at least one internal repeat...
for _ in 0 .. internal_repeats.max(1) {
// Always reset the state after the benchmark.
$crate::defer!($crate::benchmarking::wipe_db());

// Set up the externalities environment for the setup we want to
// benchmark.
let closure_to_benchmark = <
Expand Down Expand Up @@ -1110,9 +1115,6 @@ macro_rules! impl_benchmark {
proof_size: diff_pov,
keys: read_and_written_keys,
});

// Wipe the DB back to the genesis state.
$crate::benchmarking::wipe_db();
}

return Ok(results);
Expand Down Expand Up @@ -1175,6 +1177,9 @@ macro_rules! impl_benchmark_test {
let execute_benchmark = |
c: $crate::Vec<($crate::BenchmarkParameter, u32)>
| -> Result<(), $crate::BenchmarkError> {
// Always reset the state after the benchmark.
$crate::defer!($crate::benchmarking::wipe_db());

// Set up the benchmark, return execution + verification function.
let closure_to_verify = <
SelectedBenchmark as $crate::BenchmarkingSetup<T, _>
Expand All @@ -1186,12 +1191,7 @@ macro_rules! impl_benchmark_test {
}

// Run execution + verification
closure_to_verify()?;

// Reset the state
$crate::benchmarking::wipe_db();

Ok(())
closure_to_verify()
};

if components.is_empty() {
Expand Down
44 changes: 44 additions & 0 deletions frame/benchmarking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ fn new_test_ext() -> sp_io::TestExternalities {
GenesisConfig::default().build_storage().unwrap().into()
}

// NOTE: This attribute is only needed for the `modify_in_` functions.
#[allow(unreachable_code)]
mod benchmarks {
use super::{new_test_ext, pallet_test::Value, Test};
use crate::{account, BenchmarkError, BenchmarkParameter, BenchmarkResult, BenchmarkingSetup};
Expand Down Expand Up @@ -227,6 +229,24 @@ mod benchmarks {
// This should never be reached.
assert!(value > 100);
}

modify_in_setup_then_error {
Value::<T>::set(Some(123));
return Err(BenchmarkError::Stop("Should error"));
}: { }

modify_in_call_then_error {
}: {
Value::<T>::set(Some(123));
return Err(BenchmarkError::Stop("Should error"));
}

modify_in_verify_then_error {
}: {
} verify {
Value::<T>::set(Some(123));
return Err(BenchmarkError::Stop("Should error"));
}
}

#[test]
Expand Down Expand Up @@ -350,4 +370,28 @@ mod benchmarks {
assert_eq!(Pallet::<Test>::test_benchmark_skip_benchmark(), Err(BenchmarkError::Skip),);
});
}

/// An error return of a benchmark test function still causes the db to be wiped.
#[test]
fn benchmark_error_wipes_storage() {
new_test_ext().execute_with(|| {
// It resets when the error happens in the setup:
assert_err!(
Pallet::<Test>::test_benchmark_modify_in_setup_then_error(),
"Should error"
);
assert_eq!(Value::<Test>::get(), None);

// It resets when the error happens in the call:
assert_err!(Pallet::<Test>::test_benchmark_modify_in_call_then_error(), "Should error");
assert_eq!(Value::<Test>::get(), None);

// It resets when the error happens in the verify:
assert_err!(
Pallet::<Test>::test_benchmark_modify_in_verify_then_error(),
"Should error"
);
assert_eq!(Value::<Test>::get(), None);
});
}
}

0 comments on commit 5686d29

Please sign in to comment.