Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[benchmarking] Reset to genesis storage after each run #5655

Merged
merged 22 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions prdoc/pr_5655.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
title: '[benchmarking] Reset to genesis storage after each run'
doc:
- audience: Runtime Dev
description: |-
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but these changes are reset by the runtime after the first repetition

You mean here that this was overridden and not reset?


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.
crates:
- name: frame-benchmarking-cli
bump: minor
- name: frame-benchmarking-pallet-pov
bump: minor
bkontur marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 35 additions & 0 deletions substrate/bin/node/cli/tests/benchmark_pallet_works.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ fn benchmark_pallet_works() {
benchmark_pallet(20, 50, true);
}

#[test]
fn benchmark_pallet_args_work() {
benchmark_pallet_args(&["--list", "--pallet=pallet_balances"], true);
benchmark_pallet_args(&["--list", "--pallet=pallet_balances"], true);
benchmark_pallet_args(
&["--list", "--pallet=pallet_balances", "--genesis-builder=spec-genesis"],
true,
);
benchmark_pallet_args(
&["--list", "--pallet=pallet_balances", "--chain=dev", "--genesis-builder=spec-genesis"],
true,
);

// Error because the genesis runtime does not have any presets in it:
benchmark_pallet_args(
&["--list", "--pallet=pallet_balances", "--chain=dev", "--genesis-builder=spec-runtime"],
false,
);
// Error because no runtime is provided:
benchmark_pallet_args(
&["--list", "--pallet=pallet_balances", "--chain=dev", "--genesis-builder=runtime"],
false,
);
}

fn benchmark_pallet(steps: u32, repeat: u32, should_work: bool) {
let status = Command::new(cargo_bin("substrate-node"))
.args(["benchmark", "pallet", "--dev"])
Expand All @@ -51,3 +76,13 @@ fn benchmark_pallet(steps: u32, repeat: u32, should_work: bool) {

assert_eq!(status.success(), should_work);
}

fn benchmark_pallet_args(args: &[&str], should_work: bool) {
let status = Command::new(cargo_bin("substrate-node"))
.args(["benchmark", "pallet"])
.args(args)
.status()
.unwrap();

assert_eq!(status.success(), should_work);
}
30 changes: 30 additions & 0 deletions substrate/frame/benchmarking/pov/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ use frame_support::traits::UnfilteredDispatchable;
use frame_system::{Pallet as System, RawOrigin};
use sp_runtime::traits::Hash;

#[cfg(feature = "std")]
frame_support::parameter_types! {
pub static StorageRootHash: Option<alloc::vec::Vec<u8>> = None;
}

#[benchmarks]
mod benchmarks {
use super::*;
Expand Down Expand Up @@ -392,6 +397,31 @@ mod benchmarks {
}
}

#[benchmark]
fn storage_root_is_the_same_every_time(i: Linear<0, 10>) {
let root = sp_io::storage::root(sp_runtime::StateVersion::V1);

#[cfg(feature = "std")]
match (i, StorageRootHash::get()) {
(0, Some(_)) => panic!("StorageRootHash should be None initially"),
(0, None) => StorageRootHash::set(Some(root)),
(_, Some(r)) if r == root => {},
(_, Some(r)) =>
panic!("StorageRootHash should be the same every time: {:?} vs {:?}", r, root),
(_, None) => panic!("StorageRootHash should be Some after the first iteration"),
}

// Also test that everything is reset correctly:
sp_io::storage::set(b"key1", b"value");

#[block]
{
sp_io::storage::set(b"key2", b"value");
}

sp_io::storage::set(b"key3", b"value");
}

impl_benchmark_test_suite!(Pallet, super::mock::new_test_ext(), super::mock::Test,);
}

Expand Down
Loading
Loading