Skip to content

Commit

Permalink
Improve usability of add+list_benchmark! (paritytech#10592)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
ggwpez authored and grishasobol committed Mar 28, 2022
1 parent 8409dd9 commit 74439b3
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 90 deletions.
33 changes: 19 additions & 14 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,21 @@ pub type Executive = frame_executive::Executive<
AllPalletsWithSystem,
>;

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

#[cfg(feature = "runtime-benchmarks")]
mod benches {
define_benchmarks!(
[frame_benchmarking, BaselineBench::<Runtime>]
[frame_system, SystemBench::<Runtime>]
[pallet_balances, Balances]
[pallet_timestamp, Timestamp]
[pallet_template, TemplateModule]
);
}

impl_runtime_apis! {
impl sp_api::Core<Block> for Runtime {
fn version() -> RuntimeVersion {
Expand Down Expand Up @@ -455,18 +470,13 @@ impl_runtime_apis! {
Vec<frame_benchmarking::BenchmarkList>,
Vec<frame_support::traits::StorageInfo>,
) {
use frame_benchmarking::{list_benchmark, baseline, Benchmarking, BenchmarkList};
use frame_benchmarking::{baseline, Benchmarking, BenchmarkList};
use frame_support::traits::StorageInfoTrait;
use frame_system_benchmarking::Pallet as SystemBench;
use baseline::Pallet as BaselineBench;

let mut list = Vec::<BenchmarkList>::new();

list_benchmark!(list, extra, frame_benchmarking, BaselineBench::<Runtime>);
list_benchmark!(list, extra, frame_system, SystemBench::<Runtime>);
list_benchmark!(list, extra, pallet_balances, Balances);
list_benchmark!(list, extra, pallet_timestamp, Timestamp);
list_benchmark!(list, extra, pallet_template, TemplateModule);
list_benchmarks!(list, extra);

let storage_info = AllPalletsWithSystem::storage_info();

Expand All @@ -476,7 +486,7 @@ impl_runtime_apis! {
fn dispatch_benchmark(
config: frame_benchmarking::BenchmarkConfig
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
use frame_benchmarking::{baseline, Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey};
use frame_benchmarking::{baseline, Benchmarking, BenchmarkBatch, TrackedStorageKey};

use frame_system_benchmarking::Pallet as SystemBench;
use baseline::Pallet as BaselineBench;
Expand All @@ -499,12 +509,7 @@ impl_runtime_apis! {

let mut batches = Vec::<BenchmarkBatch>::new();
let params = (&config, &whitelist);

add_benchmark!(params, batches, frame_benchmarking, BaselineBench::<Runtime>);
add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
add_benchmark!(params, batches, pallet_balances, Balances);
add_benchmark!(params, batches, pallet_timestamp, Timestamp);
add_benchmark!(params, batches, pallet_template, TemplateModule);
add_benchmarks!(params, batches);

Ok(batches)
}
Expand Down
123 changes: 49 additions & 74 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,51 @@ mod mmr {
pub type Hashing = <Runtime as pallet_mmr::Config>::Hashing;
}

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

#[cfg(feature = "runtime-benchmarks")]
mod benches {
define_benchmarks!(
[frame_benchmarking, BaselineBench::<Runtime>]
[pallet_assets, Assets]
[pallet_babe, Babe]
[pallet_bags_list, BagsList]
[pallet_balances, Balances]
[pallet_bounties, Bounties]
[pallet_child_bounties, ChildBounties]
[pallet_collective, Council]
[pallet_contracts, Contracts]
[pallet_democracy, Democracy]
[pallet_election_provider_multi_phase, ElectionProviderMultiPhase]
[pallet_elections_phragmen, Elections]
[pallet_gilt, Gilt]
[pallet_grandpa, Grandpa]
[pallet_identity, Identity]
[pallet_im_online, ImOnline]
[pallet_indices, Indices]
[pallet_lottery, Lottery]
[pallet_membership, TechnicalMembership]
[pallet_mmr, Mmr]
[pallet_multisig, Multisig]
[pallet_offences, OffencesBench::<Runtime>]
[pallet_preimage, Preimage]
[pallet_proxy, Proxy]
[pallet_scheduler, Scheduler]
[pallet_session, SessionBench::<Runtime>]
[pallet_staking, Staking]
[frame_system, SystemBench::<Runtime>]
[pallet_timestamp, Timestamp]
[pallet_tips, Tips]
[pallet_transaction_storage, TransactionStorage]
[pallet_treasury, Treasury]
[pallet_uniques, Uniques]
[pallet_utility, Utility]
[pallet_vesting, Vesting]
);
}

impl_runtime_apis! {
impl sp_api::Core<Block> for Runtime {
fn version() -> RuntimeVersion {
Expand Down Expand Up @@ -1664,7 +1709,7 @@ impl_runtime_apis! {
Vec<frame_benchmarking::BenchmarkList>,
Vec<frame_support::traits::StorageInfo>,
) {
use frame_benchmarking::{list_benchmark, baseline, Benchmarking, BenchmarkList};
use frame_benchmarking::{baseline, Benchmarking, BenchmarkList};
use frame_support::traits::StorageInfoTrait;

// Trying to add benchmarks directly to the Session Pallet caused cyclic dependency
Expand All @@ -1676,42 +1721,7 @@ impl_runtime_apis! {
use baseline::Pallet as BaselineBench;

let mut list = Vec::<BenchmarkList>::new();

list_benchmark!(list, extra, frame_benchmarking, BaselineBench::<Runtime>);
list_benchmark!(list, extra, pallet_assets, Assets);
list_benchmark!(list, extra, pallet_babe, Babe);
list_benchmark!(list, extra, pallet_bags_list, BagsList);
list_benchmark!(list, extra, pallet_balances, Balances);
list_benchmark!(list, extra, pallet_bounties, Bounties);
list_benchmark!(list, extra, pallet_child_bounties, ChildBounties);
list_benchmark!(list, extra, pallet_collective, Council);
list_benchmark!(list, extra, pallet_contracts, Contracts);
list_benchmark!(list, extra, pallet_democracy, Democracy);
list_benchmark!(list, extra, pallet_election_provider_multi_phase, ElectionProviderMultiPhase);
list_benchmark!(list, extra, pallet_elections_phragmen, Elections);
list_benchmark!(list, extra, pallet_gilt, Gilt);
list_benchmark!(list, extra, pallet_grandpa, Grandpa);
list_benchmark!(list, extra, pallet_identity, Identity);
list_benchmark!(list, extra, pallet_im_online, ImOnline);
list_benchmark!(list, extra, pallet_indices, Indices);
list_benchmark!(list, extra, pallet_lottery, Lottery);
list_benchmark!(list, extra, pallet_membership, TechnicalMembership);
list_benchmark!(list, extra, pallet_mmr, Mmr);
list_benchmark!(list, extra, pallet_multisig, Multisig);
list_benchmark!(list, extra, pallet_offences, OffencesBench::<Runtime>);
list_benchmark!(list, extra, pallet_preimage, Preimage);
list_benchmark!(list, extra, pallet_proxy, Proxy);
list_benchmark!(list, extra, pallet_scheduler, Scheduler);
list_benchmark!(list, extra, pallet_session, SessionBench::<Runtime>);
list_benchmark!(list, extra, pallet_staking, Staking);
list_benchmark!(list, extra, frame_system, SystemBench::<Runtime>);
list_benchmark!(list, extra, pallet_timestamp, Timestamp);
list_benchmark!(list, extra, pallet_tips, Tips);
list_benchmark!(list, extra, pallet_transaction_storage, TransactionStorage);
list_benchmark!(list, extra, pallet_treasury, Treasury);
list_benchmark!(list, extra, pallet_uniques, Uniques);
list_benchmark!(list, extra, pallet_utility, Utility);
list_benchmark!(list, extra, pallet_vesting, Vesting);
list_benchmarks!(list, extra);

let storage_info = AllPalletsWithSystem::storage_info();

Expand All @@ -1721,7 +1731,7 @@ impl_runtime_apis! {
fn dispatch_benchmark(
config: frame_benchmarking::BenchmarkConfig
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
use frame_benchmarking::{baseline, Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey};
use frame_benchmarking::{baseline, Benchmarking, BenchmarkBatch, TrackedStorageKey};

// Trying to add benchmarks directly to the Session Pallet caused cyclic dependency
// issues. To get around that, we separated the Session benchmarks into its own crate,
Expand Down Expand Up @@ -1755,42 +1765,7 @@ impl_runtime_apis! {

let mut batches = Vec::<BenchmarkBatch>::new();
let params = (&config, &whitelist);

add_benchmark!(params, batches, frame_benchmarking, BaselineBench::<Runtime>);
add_benchmark!(params, batches, pallet_assets, Assets);
add_benchmark!(params, batches, pallet_babe, Babe);
add_benchmark!(params, batches, pallet_balances, Balances);
add_benchmark!(params, batches, pallet_bags_list, BagsList);
add_benchmark!(params, batches, pallet_bounties, Bounties);
add_benchmark!(params, batches, pallet_child_bounties, ChildBounties);
add_benchmark!(params, batches, pallet_collective, Council);
add_benchmark!(params, batches, pallet_contracts, Contracts);
add_benchmark!(params, batches, pallet_democracy, Democracy);
add_benchmark!(params, batches, pallet_election_provider_multi_phase, ElectionProviderMultiPhase);
add_benchmark!(params, batches, pallet_elections_phragmen, Elections);
add_benchmark!(params, batches, pallet_gilt, Gilt);
add_benchmark!(params, batches, pallet_grandpa, Grandpa);
add_benchmark!(params, batches, pallet_identity, Identity);
add_benchmark!(params, batches, pallet_im_online, ImOnline);
add_benchmark!(params, batches, pallet_indices, Indices);
add_benchmark!(params, batches, pallet_lottery, Lottery);
add_benchmark!(params, batches, pallet_membership, TechnicalMembership);
add_benchmark!(params, batches, pallet_mmr, Mmr);
add_benchmark!(params, batches, pallet_multisig, Multisig);
add_benchmark!(params, batches, pallet_offences, OffencesBench::<Runtime>);
add_benchmark!(params, batches, pallet_preimage, Preimage);
add_benchmark!(params, batches, pallet_proxy, Proxy);
add_benchmark!(params, batches, pallet_scheduler, Scheduler);
add_benchmark!(params, batches, pallet_session, SessionBench::<Runtime>);
add_benchmark!(params, batches, pallet_staking, Staking);
add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
add_benchmark!(params, batches, pallet_timestamp, Timestamp);
add_benchmark!(params, batches, pallet_tips, Tips);
add_benchmark!(params, batches, pallet_transaction_storage, TransactionStorage);
add_benchmark!(params, batches, pallet_treasury, Treasury);
add_benchmark!(params, batches, pallet_uniques, Uniques);
add_benchmark!(params, batches, pallet_utility, Utility);
add_benchmark!(params, batches, pallet_vesting, Vesting);
add_benchmarks!(params, batches);

Ok(batches)
}
Expand Down
67 changes: 65 additions & 2 deletions frame/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1701,7 +1701,6 @@ pub fn show_benchmark_debug_info(
/// type Council2 = TechnicalCommittee;
/// add_benchmark!(params, batches, pallet_collective, Council2); // pallet_collective_council_2.rs
/// ```
#[macro_export]
macro_rules! add_benchmark {
( $params:ident, $batches:ident, $name:path, $( $location:tt )* ) => (
Expand Down Expand Up @@ -1770,6 +1769,20 @@ macro_rules! add_benchmark {
)
}

/// Callback for `define_benchmarks` to call `add_benchmark`.
#[macro_export]
macro_rules! cb_add_benchmarks {
// anchor
( $params:ident, $batches:ident, [ $name:path, $( $location:tt )* ] ) => {
add_benchmark!( $params, $batches, $name, $( $location )* );
};
// recursion tail
( $params:ident, $batches:ident, [ $name:path, $( $location:tt )* ] $([ $names:path, $( $locations:tt )* ])+ ) => {
cb_add_benchmarks!( $params, $batches, [ $name, $( $location )* ] );
cb_add_benchmarks!( $params, $batches, $([ $names, $( $locations )* ])+ );
}
}

/// This macro allows users to easily generate a list of benchmarks for the pallets configured
/// in the runtime.
///
Expand All @@ -1789,7 +1802,6 @@ macro_rules! add_benchmark {
/// ```
///
/// This should match what exists with the `add_benchmark!` macro.
#[macro_export]
macro_rules! list_benchmark {
( $list:ident, $extra:ident, $name:path, $( $location:tt )* ) => (
Expand All @@ -1804,3 +1816,54 @@ macro_rules! list_benchmark {
$list.push(pallet_benchmarks)
)
}

/// Callback for `define_benchmarks` to call `list_benchmark`.
#[macro_export]
macro_rules! cb_list_benchmarks {
// anchor
( $list:ident, $extra:ident, [ $name:path, $( $location:tt )* ] ) => {
list_benchmark!( $list, $extra, $name, $( $location )* );
};
// recursion tail
( $list:ident, $extra:ident, [ $name:path, $( $location:tt )* ] $([ $names:path, $( $locations:tt )* ])+ ) => {
cb_list_benchmarks!( $list, $extra, [ $name, $( $location )* ] );
cb_list_benchmarks!( $list, $extra, $([ $names, $( $locations )* ])+ );
}
}

/// Defines pallet configs that `add_benchmarks` and `list_benchmarks` use.
/// Should be preferred instead of having a repetitive list of configs
/// in `add_benchmark` and `list_benchmark`.
#[macro_export]
macro_rules! define_benchmarks {
( $([ $names:path, $( $locations:tt )* ])* ) => {
/// Calls `list_benchmark` with all configs from `define_benchmarks`
/// and passes the first two parameters on.
///
/// Use as:
/// ```ignore
/// list_benchmarks!(list, extra);
/// ```
#[macro_export]
macro_rules! list_benchmarks {
( $list:ident, $extra:ident ) => {
cb_list_benchmarks!( $list, $extra, $([ $names, $( $locations )* ])+ );
}
}

/// Calls `add_benchmark` with all configs from `define_benchmarks`
/// and passes the first two parameters on.
///
/// Use as:
/// ```ignore
/// add_benchmarks!(params, batches);
/// ```
#[macro_export]
macro_rules! add_benchmarks {
( $params:ident, $batches:ident ) => {
cb_add_benchmarks!( $params, $batches, $([ $names, $( $locations )* ])+ );
}
}
}
}

0 comments on commit 74439b3

Please sign in to comment.