I am currently writing code for the hooks on_initialize
and on_idle
. They share functionality in the form of 2 methods, each using these methods multiple times. I would like to accurately benchmark these 2 methods to use when calculating and returning the weight in the mentioned hooks.
Is this the correct method? or should i be benchmarking the hooks themselves with some worst case scenario. In essence i am trying to dynamically set the weight which feels especially like gas metering. To the best of my knowledge this is not supported in standard extrinsic calls, except it seems possible in the hooks.
If it is the correct method how can i benchmark code that is not an extrinsic (not annotated with #[pallet::call]
).
Preferably i would like to avoid db::reads_writes(x, y)
to manually tally weights, is this best practice?
Here is some sample code to describe what i am trying to do:
fn on_initialize(_b: T::BlockNumber) -> Weight {
let mut weight = Weight::default();
let mut details = Details::<T>::get();
Self::do_something_with_details(details);
weight.saturating_add(T::WeightInfo::do_something_with_details());
Self::mutate_details(details, c);
weight.saturating_add(T::WeightInfo::mutate_details());
weight
}