referrerpolicy=no-referrer-when-downgrade

Macro frame_benchmarking::add_benchmark

source ·
macro_rules! add_benchmark {
    ( $params:ident, $batches:ident, $name:path, $location:ty ) => { ... };
}
Expand description

This macro adds pallet benchmarks to a Vec<BenchmarkBatch> object.

First create an object that holds in the input parameters for the benchmark:

let params = (&config, &whitelist);

The whitelist is a parameter you pass to control the DB read/write tracking. We use a vector of TrackedStorageKey, which is a simple struct used to set if a key has been read or written to.

For values that should be skipped entirely, we can just pass key.into(). For example:

use sp_storage::TrackedStorageKey;
let whitelist: Vec<TrackedStorageKey> = vec![
	// Block Number
	array_bytes::hex_into_unchecked("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac"),
	// Total Issuance
	array_bytes::hex_into_unchecked("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80"),
	// Execution Phase
	array_bytes::hex_into_unchecked("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a"),
	// Event Count
	array_bytes::hex_into_unchecked("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850"),
];

Then define a mutable local variable to hold your BenchmarkBatch object:

let mut batches = Vec::<BenchmarkBatch>::new();

Then add the pallets you want to benchmark to this object, using their crate name and generated module struct:

add_benchmark!(params, batches, pallet_balances, Balances);
add_benchmark!(params, batches, pallet_session, SessionBench::<Runtime>);
add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
...

At the end of dispatch_benchmark, you should return this batches object.

In the case where you have multiple instances of a pallet that you need to separately benchmark, the name of your module struct will be used as a suffix to your outputted weight file. For example:

add_benchmark!(params, batches, pallet_balances, Balances); // pallet_balances.rs
add_benchmark!(params, batches, pallet_collective, Council); // pallet_collective_council.rs
add_benchmark!(params, batches, pallet_collective, TechnicalCommittee); // pallet_collective_technical_committee.rs

You can manipulate this suffixed string by using a type alias if needed. For example:

type Council2 = TechnicalCommittee;
add_benchmark!(params, batches, pallet_collective, Council2); // pallet_collective_council_2.rs