Skip to content

Commit 2e4e5bf

Browse files
ggwpezbkchrbkontur
authored
[benchmarking] Reset to genesis storage after each run (#5655)
The genesis state is currently partially provided via `OverlayedChanges`, but these changes are reset by the runtime after the first repetition, causing the second repitition to use an invalid genesis state. Changes: - Provide the genesis state as a `Storage` without any `OverlayedChanges` to make it work correctly with repetitions. - Add `--genesis-builder-preset` option to use different genesis preset names. - Improve error messages. --------- Signed-off-by: Oliver Tale-Yazdi <[email protected]> Co-authored-by: ggwpez <[email protected]> Co-authored-by: Bastian Köcher <[email protected]> Co-authored-by: Branislav Kontur <[email protected]>
1 parent 128f6c7 commit 2e4e5bf

File tree

7 files changed

+197
-157
lines changed

7 files changed

+197
-157
lines changed

prdoc/pr_5655.prdoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
title: '[benchmarking] Reset to genesis storage after each run'
2+
doc:
3+
- audience: Runtime Dev
4+
description: |-
5+
The genesis state is currently partially provided via `OverlayedChanges`, but these changes are reset by the runtime after the first repetition, causing the second repitition to use an invalid genesis state.
6+
7+
Changes:
8+
- Provide the genesis state as a `Storage` without any `OverlayedChanges` to make it work correctly with repetitions.
9+
- Add `--genesis-builder-preset` option to use different genesis preset names.
10+
- Improve error messages.
11+
crates:
12+
- name: frame-benchmarking-cli
13+
bump: major
14+
- name: frame-benchmarking-pallet-pov
15+
bump: patch

substrate/bin/node/cli/tests/benchmark_pallet_works.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,31 @@ fn benchmark_pallet_works() {
3333
benchmark_pallet(20, 50, true);
3434
}
3535

36+
#[test]
37+
fn benchmark_pallet_args_work() {
38+
benchmark_pallet_args(&["--list", "--pallet=pallet_balances"], true);
39+
benchmark_pallet_args(&["--list", "--pallet=pallet_balances"], true);
40+
benchmark_pallet_args(
41+
&["--list", "--pallet=pallet_balances", "--genesis-builder=spec-genesis"],
42+
true,
43+
);
44+
benchmark_pallet_args(
45+
&["--list", "--pallet=pallet_balances", "--chain=dev", "--genesis-builder=spec-genesis"],
46+
true,
47+
);
48+
49+
// Error because the genesis runtime does not have any presets in it:
50+
benchmark_pallet_args(
51+
&["--list", "--pallet=pallet_balances", "--chain=dev", "--genesis-builder=spec-runtime"],
52+
false,
53+
);
54+
// Error because no runtime is provided:
55+
benchmark_pallet_args(
56+
&["--list", "--pallet=pallet_balances", "--chain=dev", "--genesis-builder=runtime"],
57+
false,
58+
);
59+
}
60+
3661
fn benchmark_pallet(steps: u32, repeat: u32, should_work: bool) {
3762
let status = Command::new(cargo_bin("substrate-node"))
3863
.args(["benchmark", "pallet", "--dev"])
@@ -51,3 +76,13 @@ fn benchmark_pallet(steps: u32, repeat: u32, should_work: bool) {
5176

5277
assert_eq!(status.success(), should_work);
5378
}
79+
80+
fn benchmark_pallet_args(args: &[&str], should_work: bool) {
81+
let status = Command::new(cargo_bin("substrate-node"))
82+
.args(["benchmark", "pallet"])
83+
.args(args)
84+
.status()
85+
.unwrap();
86+
87+
assert_eq!(status.success(), should_work);
88+
}

substrate/frame/benchmarking/pov/src/benchmarking.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ use frame_support::traits::UnfilteredDispatchable;
2626
use frame_system::{Pallet as System, RawOrigin};
2727
use sp_runtime::traits::Hash;
2828

29+
#[cfg(feature = "std")]
30+
frame_support::parameter_types! {
31+
pub static StorageRootHash: Option<alloc::vec::Vec<u8>> = None;
32+
}
33+
2934
#[benchmarks]
3035
mod benchmarks {
3136
use super::*;
@@ -392,6 +397,32 @@ mod benchmarks {
392397
}
393398
}
394399

400+
#[benchmark]
401+
fn storage_root_is_the_same_every_time(i: Linear<0, 10>) {
402+
#[cfg(feature = "std")]
403+
let root = sp_io::storage::root(sp_runtime::StateVersion::V1);
404+
405+
#[cfg(feature = "std")]
406+
match (i, StorageRootHash::get()) {
407+
(0, Some(_)) => panic!("StorageRootHash should be None initially"),
408+
(0, None) => StorageRootHash::set(Some(root)),
409+
(_, Some(r)) if r == root => {},
410+
(_, Some(r)) =>
411+
panic!("StorageRootHash should be the same every time: {:?} vs {:?}", r, root),
412+
(_, None) => panic!("StorageRootHash should be Some after the first iteration"),
413+
}
414+
415+
// Also test that everything is reset correctly:
416+
sp_io::storage::set(b"key1", b"value");
417+
418+
#[block]
419+
{
420+
sp_io::storage::set(b"key2", b"value");
421+
}
422+
423+
sp_io::storage::set(b"key3", b"value");
424+
}
425+
395426
impl_benchmark_test_suite!(Pallet, super::mock::new_test_ext(), super::mock::Test,);
396427
}
397428

0 commit comments

Comments
 (0)