Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Improve usability of add+list_benchmark! #10592

Merged
merged 8 commits into from
Jan 19, 2022

Conversation

ggwpez
Copy link
Member

@ggwpez ggwpez commented Jan 5, 2022

Problem

The usage of the add_benchmark and list_benchmark macros is error prone since they require code duplication.
Related paritytech/polkadot-sdk#397

Current Syntax

impl Benchmark<Block> for Runtime {
	fn benchmark_metadata()  {
		add_benchmark!(list, extra, path1, location1);
		...
		add_benchmark!(list, extra, pathN, locationN);
	}

	fn dispatch_benchmark()  {
		list_benchmark!(params, batches, path1, location1);
		// error prone repetition
		list_benchmark!(params, batches, pathN, locationN);
	}
}

Possible Solution

This MR contains a non-destructive way of extending the syntax to eliminate the repetition.
Added:

  • define_benchmarks!: takes a list of bench configs that was formerly passed to add_+list_benchmark!
  • list_benchmarks!: lists all benchmarks defined in define_benchmarks!
  • add_benchmarks!: adds all benchmarks defined in define_benchmarks!

New Syntax

define_benchmarks!(
	[path1, location1]
	...
	[pathN, locationN]
);

impl_runtime_apis! {
	impl Benchmark<Block> for Runtime {
		fn benchmark_metadata()  {
			add_benchmarks!(list, extra);
		}

		fn dispatch_benchmark()  {
			list_benchmarks!(params, batches);
		}
	}
}

@ reviewers

  • I tried to label the MR, D-* label is currently missing.
  • There is a bit of boilerplate code before the define_benchmarks macro call, which could be moved into it but I am not sure if this is good style.
  • Please take a look at the syntax and naming. I was not sure if the [path, location] … is the best syntax.
  • I checked that the benchmarks to run are the same as on master via: cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --pallet='*' --extrinsic='*' --list and the same for the node-template.

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
@ggwpez ggwpez added B7-runtimenoteworthy C1-low PR touches the given topic and has a low impact on builders. labels Jan 7, 2022
@shawntabrizi shawntabrizi added the D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit label Jan 7, 2022
@shawntabrizi
Copy link
Member

This looks good in general, but I would prefer a backwards compatible PR, rather than forcing everyone into this way of things. That probably means just keeping the existing macro as is, and changing the name of the new macro you created to something like auto_list_benchmark!. Otherwise, seems good to me, and something I would def use in Substrate / Polkadot.

@ggwpez
Copy link
Member Author

ggwpez commented Jan 12, 2022

This looks good in general, but I would prefer a backwards compatible PR, rather than forcing everyone into this way of things.

It is backwards-compatible 😄 but the naming is a bit sneaky.
add_benchmark vs add_benchmarks: note the suffix s. Should I still rename it to auto_list_benchmark?

Also what do you think about the boilerplate code, should I put it inside the macro or not? This specifically:

#[macro_use]
extern crate frame_benchmarking;

#[cfg(feature = "runtime-benchmarks")]
mod benches {
}

@shawntabrizi

@ggwpez ggwpez marked this pull request as ready for review January 12, 2022 16:22
@ggwpez ggwpez added the A0-please_review Pull request needs code review. label Jan 12, 2022
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
This reverts commit 82ea52f.
@@ -1385,6 +1385,51 @@ mod mmr {
pub type Hashing = <Runtime as pallet_mmr::Config>::Hashing;
}

#[cfg(feature = "runtime-benchmarks")]
#[macro_use]
extern crate frame_benchmarking;
Copy link
Contributor

Choose a reason for hiding this comment

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

I thought this syntax (extern crate) was not needed anymore?

Copy link
Member Author

Choose a reason for hiding this comment

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

I could do this, but then we would have to import the "hidden" cb_add_benchmarks and cb_list_benchmarks macros, which is more confusing I think since the user does not directly need them.

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

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

LGTM, just have a few small questions.

@kianenigma
Copy link
Contributor

This looks good in general, but I would prefer a backwards compatible PR, rather than forcing everyone into this way of things.

It is backwards-compatible 😄 but the naming is a bit sneaky. add_benchmark vs add_benchmarks: note the suffix s. Should I still rename it to auto_list_benchmark?

Also what do you think about the boilerplate code, should I put it inside the macro or not? This specifically:

#[macro_use]
extern crate frame_benchmarking;

#[cfg(feature = "runtime-benchmarks")]
mod benches {
}

@shawntabrizi

More a fan of having it inside the impl_runtime_api macro to keep all the benchmark related stuff under one roof.

@ggwpez ggwpez added A3-in_progress Pull request is in progress. No review needed at this stage. and removed A0-please_review Pull request needs code review. labels Jan 18, 2022
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
@ggwpez
Copy link
Member Author

ggwpez commented Jan 19, 2022

More a fan of having it inside the impl_runtime_api macro to keep all the benchmark related stuff under one roof.

Yes I tried this. The impl_runtime_apis! only accept impl ... syntax.
So either I also change the impl_runtime_apis macro or it wont work.

This MR is only a temporary solution until we have automatic bench-case extraction from the pallet metadata, so I think it is ok.

@ggwpez ggwpez added A0-please_review Pull request needs code review. and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Jan 19, 2022
Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

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

Okay, still LGTM!

@ggwpez
Copy link
Member Author

ggwpez commented Jan 19, 2022

bot merge

@paritytech-processbot paritytech-processbot bot merged commit bea8f32 into paritytech:master Jan 19, 2022
@shawntabrizi
Copy link
Member

@ggwpez please do the same in Polkadot and Cumulus?

grishasobol pushed a commit to gear-tech/substrate that referenced this pull request Mar 28, 2022
* Improve usability of add_benchmark and list_benchmark.

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

* node-template: use new define_benchmarks syntax

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

* make CI happy

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

* remove old imports

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

* fix TryBuild tests

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

* Revert "fix TryBuild tests"

This reverts commit 82ea52f.

* review: remove blank lines

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
wischli added a commit to KILTprotocol/kilt-node that referenced this pull request Dec 6, 2022
## fixes KILTProtocol/ticket#2289 and KILTProtocol/ticket#2296
* Upgrades from Polkadot v0.9.29 to v0.9.32
* Adds missing feature implementations for all tomls (checked via
`subalfred check features` in all crates)
* Actually necessary for application of
paritytech/substrate#10592 (see runtime changes
in
[dd81eac](dd81eac))
* Migrates Democracy, Preimage and Scheduler pallets to use bounded
Calls, see below

## Summary of changes (Polkadot v0.9.30-0.9.32)

* Weights v2 are not fully there yet, but the struct now includes the
[second field for storage
size](paritytech/substrate#12277) (tracking
issue: https://github.com/paritytech/substrate/issues/12176)

### Breaking Changes
* Breaking: Outer enums
(paritytech/substrate#11981)
  * `Origin` --> `RuntimeOrigin`
  * `Call` --> `RuntimeCall`
  * `Event` --> `RuntimeEvent`
* ~Convention seems to be to keep `Event`, `Call`, `Origin` for inner
pallet usage, e.g. `Did::Origin`~ Update: We use `Runtime` prefix
internally as well

### Noteworthy PRs
* paritytech/substrate#12109
* paritytech/substrate#12328
* paritytech/cumulus#1585
* Following the effort of decoupling collators and full relay nodes,
this PR adds the possibility of pointing the collator to an “external”
(non in-process) relay node. This is still considered experimental, and
the **relay node is suggested to run on the same machine than the
collator for the moment**.
* To specify the relay full node rpc: `polkadot-parachain --alice
--collator --relay-chain-rpc-url <rpc-websocket-url>`
* paritytech/substrate#12486
* Before this change only the interpreted WASM executor was included in
per default compilations. Making the compiled executor opt-in, now,
compiled WASM executor is set by default and an opt-out instead. This
could lead to **big performance difference** between using these two, as
more recent versions of the interpreter see a regression in performance.

### Scheduler, Preimage, Democracy Migration

* paritytech/substrate#11649
* Referenda, Democracy, Scheduler and Preimage pallets are all now
bounded in storage access footprint
* Removed the concept of a "hard deadline" or weight-override priority
and no longer guarantees that at least one scheduled item will be
executed per block (since these are both dangerous to parachains which
have a strict need of weight limits). This means you must ensure that
scheduled items are below the MaximumWeight or they will not be
executed.
* Interesting comment:
paritytech/substrate#11649 (comment)

> There is migration code which moves existing proposals and referenda
over to the new format. However IT DOES NOT MIGRATE EVERYTHING:
> 
> * Preimages are **NOT** migrated. Any registered preimages in
Democracy at the time of migration are dropped. Their balance is **NOT
UNRESERVED**.
> * The re-dispatcher used in the old Democracy implementation is
removed. Any proposals scheduled for dispatch by Democracy **WILL NOT
EXECUTE**.
>
> This means you SHOULD ensure that:
> 
> * **the preimage for the runtime upgrade is placed as an imminent
preimage, not with a deposit;**
> * **no other preimages are in place at the time of upgrade;**
> * **there are no other proposals scheduled for dispatch by Democracy
at the time of upgrade.**
> 
> The Democracy pallet will be marked as deprecated immediately once
Referenda is considered production-ready. **ALL TEAMS ARE RECOMMENDED TO
SWITCH SWAY FROM DEMOCRACY PALLET TO REFERENDA/CONVICTION-VOTING PALLETS
ASAP**

#### Result of `try-runtime` against Spiritnet on Friday Nov 18, 2022:
```
2022-11-18 09:27:23.917  INFO                 main runtime::preimage::migration::v1: Migrating 0 images
2022-11-18 09:27:23.917  INFO                 main runtime::scheduler::migration: Trying to migrate 0 agendas...
2022-11-18 09:27:23.917  INFO                 main runtime::scheduler::migration: Migrated 0 agendas.
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: 0 public proposals will be migrated.
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: 25 referenda will be migrated.
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #7
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #20
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #13
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #5
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #8
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #1
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #19
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #9
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #16
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #14
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #21
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #15
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #24
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #22
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #2
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #10
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #0
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #6
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #11
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #3
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #17
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #18
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #23
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #4
2022-11-18 09:27:23.917  INFO                 main runtime::democracy::migration::v1: migrating referendum #25
2022-11-18 09:27:23.918  INFO                 main runtime::democracy::migration::v1: 0 public proposals migrated, 25 referenda migrated
```

## Checklist:

- [x] I have verified that the code works
- [x] No panics! (checked arithmetic ops, no indexing `array[3]` use
`get(3)`, ...)
- [x] I have verified that the code is easy to understand
  - [ ] If not, I have left a well-balanced amount of inline comments
- [x] I have [left the code in a better
state](https://deviq.com/principles/boy-scout-rule)
- [x] I have documented the changes (where applicable)
ark0f pushed a commit to gear-tech/substrate that referenced this pull request Feb 27, 2023
* Improve usability of add_benchmark and list_benchmark.

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

* node-template: use new define_benchmarks syntax

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

* make CI happy

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

* remove old imports

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

* fix TryBuild tests

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

* Revert "fix TryBuild tests"

This reverts commit 82ea52f.

* review: remove blank lines

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants