Skip to content

Commit

Permalink
Improve receive_messages_proof benchmarks accuracy (#2176)
Browse files Browse the repository at this point in the history
* Adjust messages pallet benchmarks

* Address comment
  • Loading branch information
serban300 authored and bkontur committed May 17, 2024
1 parent 9a140d3 commit 0011396
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 125 deletions.
24 changes: 13 additions & 11 deletions bridges/modules/messages/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ use codec::Decode;
use frame_benchmarking::{account, v2::*};
use frame_support::weights::Weight;
use frame_system::RawOrigin;
use sp_runtime::{traits::TrailingZeroInput, BoundedVec};
use sp_runtime::{
traits::{Get, TrailingZeroInput},
BoundedVec,
};
use sp_std::{ops::RangeInclusive, prelude::*};

const SEED: u32 = 0;
Expand Down Expand Up @@ -186,14 +189,17 @@ mod benchmarks {
// Benchmarks that are used directly by the runtime calls weight formulae.
//

fn max_msgs<T: Config<I>, I: 'static>() -> u32 {
T::MaxUnconfirmedMessagesAtInboundLane::get() as u32 -
ReceiveMessagesProofSetup::<T, I>::LATEST_RECEIVED_NONCE as u32
}

// Benchmark `receive_messages_proof` extrinsic with single minimal-weight message and following
// conditions:
// * proof does not include outbound lane state proof;
// * inbound lane already has state, so it needs to be read and decoded;
// * message is dispatched (reminder: dispatch weight should be minimal);
// * message requires all heavy checks done by dispatcher.
//
// This is base benchmark for all other message delivery benchmarks.
#[benchmark]
fn receive_single_message_proof() {
// setup code
Expand All @@ -219,21 +225,16 @@ mod benchmarks {
setup.check_last_nonce();
}

// Benchmark `receive_messages_proof` extrinsic with two minimal-weight messages and following
// Benchmark `receive_messages_proof` extrinsic with `n` minimal-weight messages and following
// conditions:
// * proof does not include outbound lane state proof;
// * inbound lane already has state, so it needs to be read and decoded;
// * message is dispatched (reminder: dispatch weight should be minimal);
// * message requires all heavy checks done by dispatcher.
//
// The weight of single message delivery could be approximated as
// `weight(receive_two_messages_proof) - weight(receive_single_message_proof)`.
// This won't be super-accurate if message has non-zero dispatch weight, but estimation should
// be close enough to real weight.
#[benchmark]
fn receive_two_messages_proof() {
fn receive_n_messages_proof(n: Linear<1, { max_msgs::<T, I>() }>) {
// setup code
let setup = ReceiveMessagesProofSetup::<T, I>::new(2);
let setup = ReceiveMessagesProofSetup::<T, I>::new(n);
let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams {
lane: T::bench_lane_id(),
message_nonces: setup.nonces(),
Expand Down Expand Up @@ -489,6 +490,7 @@ mod benchmarks {
// * inbound lane already has state, so it needs to be read and decoded;
// * message is **SUCCESSFULLY** dispatched;
// * message requires all heavy checks done by dispatcher.
// #[benchmark(extra)]
#[benchmark]
fn receive_single_message_n_bytes_proof_with_dispatch(
/// Proof size in bytes
Expand Down
162 changes: 87 additions & 75 deletions bridges/modules/messages/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use sp_std::marker::PhantomData;
/// Weight functions needed for pallet_bridge_messages.
pub trait WeightInfo {
fn receive_single_message_proof() -> Weight;
fn receive_two_messages_proof() -> Weight;
fn receive_n_messages_proof(n: u32) -> Weight;
fn receive_single_message_proof_with_outbound_lane_state() -> Weight;
fn receive_single_message_n_kb_proof(n: u32) -> Weight;
fn receive_delivery_proof_for_single_message() -> Weight;
Expand Down Expand Up @@ -81,33 +81,39 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
/// 51655, mode: MaxEncodedLen)
fn receive_single_message_proof() -> Weight {
// Proof Size summary in bytes:
// Measured: `428`
// Measured: `490`
// Estimated: `52645`
// Minimum execution time: 32_573 nanoseconds.
Weight::from_parts(35_227_000, 52645)
// Minimum execution time: 34_644 nanoseconds.
Weight::from_parts(36_135_000, 52645)
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
/// Storage: BridgeUnknownMessages PalletOperatingMode (r:1 w:0)
/// Storage: BridgeRialtoMessages PalletOperatingMode (r:1 w:0)
///
/// Proof: BridgeUnknownMessages PalletOperatingMode (max_values: Some(1), max_size: Some(2),
/// Proof: BridgeRialtoMessages PalletOperatingMode (max_values: Some(1), max_size: Some(2),
/// added: 497, mode: MaxEncodedLen)
///
/// Storage: BridgeUnknownGrandpa ImportedHeaders (r:1 w:0)
/// Storage: BridgeRialtoGrandpa ImportedHeaders (r:1 w:0)
///
/// Proof: BridgeUnknownGrandpa ImportedHeaders (max_values: Some(14400), max_size: Some(68),
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: Some(14400), max_size: Some(68),
/// added: 2048, mode: MaxEncodedLen)
///
/// Storage: BridgeUnknownMessages InboundLanes (r:1 w:1)
/// Storage: BridgeRialtoMessages InboundLanes (r:1 w:1)
///
/// Proof: BridgeUnknownMessages InboundLanes (max_values: None, max_size: Some(49180), added:
/// Proof: BridgeRialtoMessages InboundLanes (max_values: None, max_size: Some(49180), added:
/// 51655, mode: MaxEncodedLen)
fn receive_two_messages_proof() -> Weight {
///
/// The range of component `n` is `[1, 1004]`.
///
/// The range of component `n` is `[1, 1004]`.
fn receive_n_messages_proof(n: u32) -> Weight {
// Proof Size summary in bytes:
// Measured: `428`
// Measured: `490`
// Estimated: `52645`
// Minimum execution time: 43_726 nanoseconds.
Weight::from_parts(47_518_000, 52645)
// Minimum execution time: 35_330 nanoseconds.
Weight::from_parts(27_526_047, 52645)
// Standard Error: 2_681
.saturating_add(Weight::from_parts(7_412_923, 0).saturating_mul(n.into()))
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
Expand All @@ -127,10 +133,10 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
/// 51655, mode: MaxEncodedLen)
fn receive_single_message_proof_with_outbound_lane_state() -> Weight {
// Proof Size summary in bytes:
// Measured: `428`
// Measured: `490`
// Estimated: `52645`
// Minimum execution time: 40_091 nanoseconds.
Weight::from_parts(43_639_000, 52645)
// Minimum execution time: 41_123 nanoseconds.
Weight::from_parts(43_023_000, 52645)
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
Expand All @@ -146,20 +152,20 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
///
/// Storage: BridgeUnknownMessages InboundLanes (r:1 w:1)
///
/// Proof: BridgeRialtoParachainMessages InboundLanes (max_values: None, max_size: Some(49180),
/// added: 51655, mode: MaxEncodedLen)
/// Proof: BridgeUnknownMessages InboundLanes (max_values: None, max_size: Some(49180), added:
/// 51655, mode: MaxEncodedLen)
///
/// The range of component `n` is `[1, 16]`.
///
/// The range of component `n` is `[1, 16]`.
fn receive_single_message_n_kb_proof(n: u32) -> Weight {
// Proof Size summary in bytes:
// Measured: `428`
// Measured: `490`
// Estimated: `52645`
// Minimum execution time: 34_578 nanoseconds.
Weight::from_parts(36_723_095, 52645)
// Standard Error: 8_197
.saturating_add(Weight::from_parts(1_317_904, 0).saturating_mul(n.into()))
// Minimum execution time: 36_301 nanoseconds.
Weight::from_parts(37_103_459, 52645)
// Standard Error: 4_645
.saturating_add(Weight::from_parts(1_172_720, 0).saturating_mul(n.into()))
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
Expand All @@ -184,10 +190,10 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
/// mode: MaxEncodedLen)
fn receive_delivery_proof_for_single_message() -> Weight {
// Proof Size summary in bytes:
// Measured: `453`
// Measured: `515`
// Estimated: `3530`
// Minimum execution time: 32_635 nanoseconds.
Weight::from_parts(33_711_000, 3530)
// Minimum execution time: 33_941 nanoseconds.
Weight::from_parts(35_252_000, 3530)
.saturating_add(T::DbWeight::get().reads(4_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
Expand All @@ -212,10 +218,10 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
/// mode: MaxEncodedLen)
fn receive_delivery_proof_for_two_messages_by_single_relayer() -> Weight {
// Proof Size summary in bytes:
// Measured: `470`
// Measured: `532`
// Estimated: `3530`
// Minimum execution time: 31_808 nanoseconds.
Weight::from_parts(33_071_000, 3530)
// Minimum execution time: 33_259 nanoseconds.
Weight::from_parts(34_558_000, 3530)
.saturating_add(T::DbWeight::get().reads(4_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
Expand All @@ -240,10 +246,10 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
/// mode: MaxEncodedLen)
fn receive_delivery_proof_for_two_messages_by_two_relayers() -> Weight {
// Proof Size summary in bytes:
// Measured: `470`
// Measured: `532`
// Estimated: `6070`
// Minimum execution time: 34_171 nanoseconds.
Weight::from_parts(35_591_000, 6070)
// Minimum execution time: 35_199 nanoseconds.
Weight::from_parts(36_989_000, 6070)
.saturating_add(T::DbWeight::get().reads(5_u64))
.saturating_add(T::DbWeight::get().writes(3_u64))
}
Expand All @@ -265,12 +271,12 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
/// The range of component `n` is `[128, 2048]`.
fn receive_single_message_n_bytes_proof_with_dispatch(n: u32) -> Weight {
// Proof Size summary in bytes:
// Measured: `428`
// Measured: `490`
// Estimated: `52645`
// Minimum execution time: 76_504 nanoseconds.
Weight::from_parts(75_331_522, 52645)
// Standard Error: 1_440
.saturating_add(Weight::from_parts(302_158, 0).saturating_mul(n.into()))
// Minimum execution time: 75_228 nanoseconds.
Weight::from_parts(62_255_691, 52645)
// Standard Error: 2_005
.saturating_add(Weight::from_parts(353_141, 0).saturating_mul(n.into()))
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
Expand All @@ -294,33 +300,39 @@ impl WeightInfo for () {
/// 51655, mode: MaxEncodedLen)
fn receive_single_message_proof() -> Weight {
// Proof Size summary in bytes:
// Measured: `428`
// Measured: `490`
// Estimated: `52645`
// Minimum execution time: 32_573 nanoseconds.
Weight::from_parts(35_227_000, 52645)
// Minimum execution time: 34_644 nanoseconds.
Weight::from_parts(36_135_000, 52645)
.saturating_add(RocksDbWeight::get().reads(3_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: BridgeUnknownMessages PalletOperatingMode (r:1 w:0)
/// Storage: BridgeRialtoMessages PalletOperatingMode (r:1 w:0)
///
/// Proof: BridgeUnknownMessages PalletOperatingMode (max_values: Some(1), max_size: Some(2),
/// Proof: BridgeRialtoMessages PalletOperatingMode (max_values: Some(1), max_size: Some(2),
/// added: 497, mode: MaxEncodedLen)
///
/// Storage: BridgeUnknownGrandpa ImportedHeaders (r:1 w:0)
/// Storage: BridgeRialtoGrandpa ImportedHeaders (r:1 w:0)
///
/// Proof: BridgeUnknownGrandpa ImportedHeaders (max_values: Some(14400), max_size: Some(68),
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: Some(14400), max_size: Some(68),
/// added: 2048, mode: MaxEncodedLen)
///
/// Storage: BridgeUnknownMessages InboundLanes (r:1 w:1)
/// Storage: BridgeRialtoMessages InboundLanes (r:1 w:1)
///
/// Proof: BridgeUnknownMessages InboundLanes (max_values: None, max_size: Some(49180), added:
/// Proof: BridgeRialtoMessages InboundLanes (max_values: None, max_size: Some(49180), added:
/// 51655, mode: MaxEncodedLen)
fn receive_two_messages_proof() -> Weight {
///
/// The range of component `n` is `[1, 1004]`.
///
/// The range of component `n` is `[1, 1004]`.
fn receive_n_messages_proof(n: u32) -> Weight {
// Proof Size summary in bytes:
// Measured: `428`
// Measured: `490`
// Estimated: `52645`
// Minimum execution time: 43_726 nanoseconds.
Weight::from_parts(47_518_000, 52645)
// Minimum execution time: 35_330 nanoseconds.
Weight::from_parts(27_526_047, 52645)
// Standard Error: 2_681
.saturating_add(Weight::from_parts(7_412_923, 0).saturating_mul(n.into()))
.saturating_add(RocksDbWeight::get().reads(3_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand All @@ -340,10 +352,10 @@ impl WeightInfo for () {
/// 51655, mode: MaxEncodedLen)
fn receive_single_message_proof_with_outbound_lane_state() -> Weight {
// Proof Size summary in bytes:
// Measured: `428`
// Measured: `490`
// Estimated: `52645`
// Minimum execution time: 40_091 nanoseconds.
Weight::from_parts(43_639_000, 52645)
// Minimum execution time: 41_123 nanoseconds.
Weight::from_parts(43_023_000, 52645)
.saturating_add(RocksDbWeight::get().reads(3_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand All @@ -359,20 +371,20 @@ impl WeightInfo for () {
///
/// Storage: BridgeUnknownMessages InboundLanes (r:1 w:1)
///
/// Proof: BridgeRialtoParachainMessages InboundLanes (max_values: None, max_size: Some(49180),
/// added: 51655, mode: MaxEncodedLen)
/// Proof: BridgeRialtoMessages InboundLanes (max_values: None, max_size: Some(49180), added:
/// 51655, mode: MaxEncodedLen)
///
/// The range of component `n` is `[1, 16]`.
///
/// The range of component `n` is `[1, 16]`.
fn receive_single_message_n_kb_proof(n: u32) -> Weight {
// Proof Size summary in bytes:
// Measured: `428`
// Measured: `490`
// Estimated: `52645`
// Minimum execution time: 34_578 nanoseconds.
Weight::from_parts(36_723_095, 52645)
// Standard Error: 8_197
.saturating_add(Weight::from_parts(1_317_904, 0).saturating_mul(n.into()))
// Minimum execution time: 36_301 nanoseconds.
Weight::from_parts(37_103_459, 52645)
// Standard Error: 4_645
.saturating_add(Weight::from_parts(1_172_720, 0).saturating_mul(n.into()))
.saturating_add(RocksDbWeight::get().reads(3_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand All @@ -397,10 +409,10 @@ impl WeightInfo for () {
/// mode: MaxEncodedLen)
fn receive_delivery_proof_for_single_message() -> Weight {
// Proof Size summary in bytes:
// Measured: `453`
// Measured: `515`
// Estimated: `3530`
// Minimum execution time: 32_635 nanoseconds.
Weight::from_parts(33_711_000, 3530)
// Minimum execution time: 33_941 nanoseconds.
Weight::from_parts(35_252_000, 3530)
.saturating_add(RocksDbWeight::get().reads(4_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
Expand All @@ -425,10 +437,10 @@ impl WeightInfo for () {
/// mode: MaxEncodedLen)
fn receive_delivery_proof_for_two_messages_by_single_relayer() -> Weight {
// Proof Size summary in bytes:
// Measured: `470`
// Measured: `532`
// Estimated: `3530`
// Minimum execution time: 31_808 nanoseconds.
Weight::from_parts(33_071_000, 3530)
// Minimum execution time: 33_259 nanoseconds.
Weight::from_parts(34_558_000, 3530)
.saturating_add(RocksDbWeight::get().reads(4_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
Expand All @@ -453,10 +465,10 @@ impl WeightInfo for () {
/// mode: MaxEncodedLen)
fn receive_delivery_proof_for_two_messages_by_two_relayers() -> Weight {
// Proof Size summary in bytes:
// Measured: `470`
// Measured: `532`
// Estimated: `6070`
// Minimum execution time: 34_171 nanoseconds.
Weight::from_parts(35_591_000, 6070)
// Minimum execution time: 35_199 nanoseconds.
Weight::from_parts(36_989_000, 6070)
.saturating_add(RocksDbWeight::get().reads(5_u64))
.saturating_add(RocksDbWeight::get().writes(3_u64))
}
Expand All @@ -478,12 +490,12 @@ impl WeightInfo for () {
/// The range of component `n` is `[128, 2048]`.
fn receive_single_message_n_bytes_proof_with_dispatch(n: u32) -> Weight {
// Proof Size summary in bytes:
// Measured: `428`
// Measured: `490`
// Estimated: `52645`
// Minimum execution time: 76_504 nanoseconds.
Weight::from_parts(75_331_522, 52645)
// Standard Error: 1_440
.saturating_add(Weight::from_parts(302_158, 0).saturating_mul(n.into()))
// Minimum execution time: 75_228 nanoseconds.
Weight::from_parts(62_255_691, 52645)
// Standard Error: 2_005
.saturating_add(Weight::from_parts(353_141, 0).saturating_mul(n.into()))
.saturating_add(RocksDbWeight::get().reads(3_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand Down
Loading

0 comments on commit 0011396

Please sign in to comment.