Skip to content

Commit

Permalink
Benchmark entire on_container_author_noted
Browse files Browse the repository at this point in the history
  • Loading branch information
girazoki committed Jan 25, 2024
1 parent 207f6b1 commit d68f8ca
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
24 changes: 24 additions & 0 deletions pallets/services-payment/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use {
frame_system::RawOrigin,
sp_runtime::Saturating,
sp_std::prelude::*,
tp_traits::AuthorNotingHook,
};

// Build genesis storage according to the mock runtime.
Expand Down Expand Up @@ -121,5 +122,28 @@ mod benchmarks {
assert!(crate::GivenFreeCredits::<T>::get(&para_id).is_some());
}

#[benchmark]
fn on_container_author_noted() {
let para_id = 1001u32;
let block_cost = T::ProvideBlockProductionCost::block_cost(&para_id.into()).0;
let max_credit_stored = T::MaxCreditsStored::get();
let balance_to_purchase = block_cost.saturating_mul(max_credit_stored.into());
let caller = create_funded_user::<T>("caller", 1, 1_000_000_000u32);
let existential_deposit = <T::Currency>::minimum_balance();
assert_ok!(Pallet::<T>::purchase_credits(
RawOrigin::Signed(caller.clone()).into(),
para_id.into(),
balance_to_purchase + existential_deposit
));
#[block]
{
<Pallet<T> as AuthorNotingHook<T::AccountId>>::on_container_author_noted(
&caller,
0,
para_id.into(),
);
}
}

impl_benchmark_test_suite!(Pallet, crate::benchmarks::new_test_ext(), crate::mock::Test);
}
9 changes: 3 additions & 6 deletions pallets/services-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,8 @@ impl<T: Config> AuthorNotingHook<T::AccountId> for Pallet<T> {
_block_number: BlockNumber,
para_id: ParaId,
) -> Weight {
let total_weight = T::DbWeight::get().reads_writes(1, 1);

if Pallet::<T>::burn_credit_for_para(&para_id).is_err() {
let (amount_to_charge, weight) = T::ProvideBlockProductionCost::block_cost(&para_id);
let (amount_to_charge, _weight) = T::ProvideBlockProductionCost::block_cost(&para_id);
match T::Currency::withdraw(
&Self::parachain_tank(para_id),
amount_to_charge,
Expand All @@ -309,17 +307,16 @@ impl<T: Config> AuthorNotingHook<T::AccountId> for Pallet<T> {
T::OnChargeForBlock::on_unbalanced(imbalance);
}
}
total_weight.saturating_add(weight);
}

total_weight
T::WeightInfo::on_container_author_noted()
}
}

impl<T: Config> Pallet<T> {
/// Derive a derivative account ID from the paraId.
pub fn parachain_tank(para_id: ParaId) -> T::AccountId {
let entropy = (b"modlpy/serpayment", para_id).using_encoded(blake2_256);
let entropy = code..using_encoded(blake2_256);
Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref()))
.expect("infinite length input; no invalid inputs for type; qed")
}
Expand Down
4 changes: 4 additions & 0 deletions pallets/services-payment/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,5 +264,9 @@ fn not_having_enough_tokens_in_tank_should_not_error() {
Balances::balance(&crate::Pallet::<Test>::parachain_tank(1.into())),
1u128
);
println!(
"address is {:?}",
crate::Pallet::<Test>::parachain_tank(2000.into())
)
});
}
28 changes: 28 additions & 0 deletions pallets/services-payment/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub trait WeightInfo {
fn purchase_credits() -> Weight;
fn set_credits() -> Weight;
fn set_given_free_credits() -> Weight;
fn on_container_author_noted() -> Weight;
}

/// Weights for pallet_services_payment using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -93,6 +94,20 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
Weight::from_parts(2_691_000, 0)
.saturating_add(T::DbWeight::get().writes(1_u64))
}

/// Storage: `ServicesPayment::BlockProductionCredits` (r:1 w:0)
/// Proof: `ServicesPayment::BlockProductionCredits` (`max_values`: None, `max_size`: Some(24), added: 2499, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
fn on_container_author_noted() -> Weight {
// Proof Size summary in bytes:
// Measured: `224`
// Estimated: `3593`
// Minimum execution time: 19_043_000 picoseconds.
Weight::from_parts(19_499_000, 3593)
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}

// For backwards compatibility and tests
Expand Down Expand Up @@ -130,4 +145,17 @@ impl WeightInfo for () {
Weight::from_parts(2_691_000, 0)
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: `ServicesPayment::BlockProductionCredits` (r:1 w:0)
/// Proof: `ServicesPayment::BlockProductionCredits` (`max_values`: None, `max_size`: Some(24), added: 2499, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
fn on_container_author_noted() -> Weight {
// Proof Size summary in bytes:
// Measured: `224`
// Estimated: `3593`
// Minimum execution time: 19_043_000 picoseconds.
Weight::from_parts(19_499_000, 3593)
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
}

0 comments on commit d68f8ca

Please sign in to comment.