Skip to content
Snippets Groups Projects
Unverified Commit 6048fc08 authored by bgallois's avatar bgallois
Browse files

add missing benchmarks pallet_universal_dividend

parent 8a5844b9
No related branches found
No related tags found
No related merge requests found
Pipeline #34403 passed
This diff is collapsed.
......@@ -18,21 +18,26 @@
use super::*;
use core::num::NonZeroU16;
use frame_benchmarking::{account, benchmarks, whitelisted_caller};
use frame_support::pallet_prelude::IsType;
use frame_support::traits::Get; // OnTimestampSet
use frame_support::traits::StoredMap;
use frame_system::RawOrigin;
use pallet_balances::Pallet as Balances;
use sp_runtime::traits::Bounded;
use sp_runtime::traits::Convert;
use crate::Pallet;
const ED_MULTIPLIER: u32 = 10;
const SEED: u32 = 0;
benchmarks! {
fn assert_has_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
frame_system::Pallet::<T>::assert_has_event(generic_event.into());
}
// TODO write benchmarks for new UD creation hook (on_timestamp_set)
benchmarks! {
// Benchmark `transfer_ud` extrinsic with the worst possible conditions:
// * Transfer will kill the sender account.
......@@ -42,6 +47,20 @@ benchmarks! {
T: pallet_balances::Config, T::Balance: From<u64>,
<T::Currency as Currency<T::AccountId>>::Balance: IsType<T::Balance>
}
claim_uds {
let i in 1..T::MaxPastReeval::get();
let caller: T::AccountId = T::AccountIdOf::convert(1).unwrap();
CurrentUdIndex::<T>::put(2054u16);
T::MembersStorage::insert(&caller, FirstEligibleUd(Some(NonZeroU16::new(CurrentUdIndex::<T>::get() - i as u16).unwrap())))?;
let (_, uds_total) = compute_claim_uds::compute_claim_uds(
CurrentUdIndex::<T>::get(),
CurrentUdIndex::<T>::get() - i as u16,
PastReevals::<T>::get().into_iter(),
);
}: _(RawOrigin::Signed(caller.clone()))
verify {
assert_has_event::<T>(Event::<T>::UdsClaimed {count: i as u16, total: uds_total, who: caller}.into());
}
transfer_ud {
let existential_deposit = T::ExistentialDeposit::get();
let caller = whitelisted_caller();
......@@ -64,7 +83,6 @@ benchmarks! {
// Benchmark `transfer_ud_keep_alive` with the worst possible condition:
// * The recipient account is created.
where_clause { where T: pallet_balances::Config, T::Balance: From<u64>, <T::Currency as Currency<T::AccountId>>::Balance: IsType<T::Balance> }
transfer_ud_keep_alive {
let caller = whitelisted_caller();
let recipient: T::AccountId = account("recipient", 0, SEED);
......
......@@ -90,6 +90,8 @@ pub mod pallet {
type UnitsPerUd: Get<BalanceOf<Self>>;
/// Pallet weights info
type WeightInfo: WeightInfo;
#[cfg(feature = "runtime-benchmarks")]
type AccountIdOf: Convert<u32, Option<Self::AccountId>>;
}
// STORAGE //
......
......@@ -28,6 +28,9 @@ use sp_runtime::{
BuildStorage,
};
#[cfg(feature = "runtime-benchmarks")]
use sp_runtime::traits::ConvertInto;
pub const BLOCK_TIME: u64 = 6_000;
type Balance = u64;
......@@ -64,7 +67,7 @@ impl system::Config for Test {
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type AccountId = u32;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type RuntimeEvent = RuntimeEvent;
......@@ -120,12 +123,12 @@ parameter_types! {
}
pub struct TestMembersStorage;
impl frame_support::traits::StoredMap<u64, FirstEligibleUd> for TestMembersStorage {
fn get(key: &u64) -> FirstEligibleUd {
impl frame_support::traits::StoredMap<u32, FirstEligibleUd> for TestMembersStorage {
fn get(key: &u32) -> FirstEligibleUd {
crate::TestMembers::<Test>::get(key)
}
fn try_mutate_exists<R, E: From<sp_runtime::DispatchError>>(
key: &u64,
key: &u32,
f: impl FnOnce(&mut Option<FirstEligibleUd>) -> Result<R, E>,
) -> Result<R, E> {
let mut value = Some(crate::TestMembers::<Test>::get(key));
......@@ -136,7 +139,7 @@ impl frame_support::traits::StoredMap<u64, FirstEligibleUd> for TestMembersStora
Ok(result)
}
}
pub struct TestMembersStorageIter(frame_support::storage::PrefixIterator<(u64, FirstEligibleUd)>);
pub struct TestMembersStorageIter(frame_support::storage::PrefixIterator<(u32, FirstEligibleUd)>);
impl From<Option<Vec<u8>>> for TestMembersStorageIter {
fn from(maybe_key: Option<Vec<u8>>) -> Self {
let mut iter = crate::TestMembers::<Test>::iter();
......@@ -147,7 +150,7 @@ impl From<Option<Vec<u8>>> for TestMembersStorageIter {
}
}
impl Iterator for TestMembersStorageIter {
type Item = (u64, FirstEligibleUd);
type Item = (u32, FirstEligibleUd);
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
......@@ -167,6 +170,8 @@ impl pallet_universal_dividend::Config for Test {
type UdReevalPeriod = UdReevalPeriod;
type UnitsPerUd = frame_support::traits::ConstU64<1_000>;
type WeightInfo = ();
#[cfg(feature = "runtime-benchmarks")]
type AccountIdOf = ConvertInto;
}
// Build genesis storage according to the mock runtime.
......
......@@ -20,49 +20,17 @@ use frame_support::weights::{constants::RocksDbWeight, Weight};
/// Weight functions needed for pallet_universal_dividend.
pub trait WeightInfo {
fn on_initialize() -> Weight;
fn on_initialize_ud_created() -> Weight;
fn on_initialize_ud_reevalued() -> Weight;
fn claim_uds(n: u32) -> Weight;
fn claim_uds(i: u32) -> Weight;
fn transfer_ud() -> Weight;
fn transfer_ud_keep_alive() -> Weight;
}
// Insecure weights implementation, use it for tests only!
impl WeightInfo for () {
// Storage: (r:0 w:0)
fn on_initialize() -> Weight {
Weight::from_parts(2_260_000, 0)
}
// Storage: Membership CounterForMembership (r:1 w:0)
// Storage: UniversalDividend NextReeval (r:1 w:0)
// Storage: UniversalDividend CurrentUd (r:1 w:0)
// Storage: UniversalDividend MonetaryMass (r:1 w:1)
// Storage: UniversalDividend CurrentUdIndex (r:1 w:1)
fn on_initialize_ud_created() -> Weight {
Weight::from_parts(20_160_000, 0)
.saturating_add(RocksDbWeight::get().reads(5))
.saturating_add(RocksDbWeight::get().writes(2))
}
// Storage: Membership CounterForMembership (r:1 w:0)
// Storage: UniversalDividend NextReeval (r:1 w:1)
// Storage: UniversalDividend CurrentUd (r:1 w:1)
// Storage: UniversalDividend MonetaryMass (r:1 w:1)
// Storage: UniversalDividend PastReevals (r:1 w:1)
// Storage: UniversalDividend CurrentUdIndex (r:1 w:1)
fn on_initialize_ud_reevalued() -> Weight {
Weight::from_parts(32_770_000, 0)
.saturating_add(RocksDbWeight::get().reads(6))
.saturating_add(RocksDbWeight::get().writes(5))
}
// Storage: Identity IdentityIndexOf (r:1 w:0)
// Storage: Identity Identities (r:1 w:1)
// Storage: UniversalDividend CurrentUdIndex (r:1 w:0)
// Storage: UniversalDividend PastReevals (r:1 w:0)
fn claim_uds(n: u32) -> Weight {
fn claim_uds(i: u32) -> Weight {
Weight::from_parts(32_514_000, 0)
// Standard Error: 32_000
.saturating_add(Weight::from_parts(8_000, 0).saturating_mul(n as u64))
.saturating_add(Weight::from_parts(8_000, 0).saturating_mul(i as u64))
.saturating_add(RocksDbWeight::get().reads(4))
.saturating_add(RocksDbWeight::get().writes(1))
}
......
......@@ -448,6 +448,8 @@ macro_rules! pallets_config {
type UdReevalPeriod = UdReevalPeriod;
type UnitsPerUd = frame_support::traits::ConstU64<1_000>;
type WeightInfo = common_runtime::weights::pallet_universal_dividend::WeightInfo<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type AccountIdOf = common_runtime::providers::IdentityAccountIdProvider<Self>;
}
// WEB OF TRUST //
......
......@@ -2,7 +2,7 @@
//! Autogenerated weights for `pallet_universal_dividend`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-11-22, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2023-11-23, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `bgallois-ms7d43`, CPU: `12th Gen Intel(R) Core(TM) i3-12100F`
//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
......@@ -15,7 +15,7 @@
// dev
// --wasm-execution=compiled
// --pallet
// *
// pallet-universal-dividend
// --extrinsic
// *
// --steps
......@@ -35,6 +35,27 @@ use core::marker::PhantomData;
/// Weight functions for `pallet_universal_dividend`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_universal_dividend::WeightInfo for WeightInfo<T> {
/// Storage: Identity IdentityIndexOf (r:1 w:0)
/// Proof Skipped: Identity IdentityIndexOf (max_values: None, max_size: None, mode: Measured)
/// Storage: Identity Identities (r:1 w:1)
/// Proof Skipped: Identity Identities (max_values: None, max_size: None, mode: Measured)
/// Storage: UniversalDividend CurrentUdIndex (r:1 w:0)
/// Proof: UniversalDividend CurrentUdIndex (max_values: Some(1), max_size: Some(2), added: 497, mode: MaxEncodedLen)
/// Storage: UniversalDividend PastReevals (r:1 w:0)
/// Proof: UniversalDividend PastReevals (max_values: Some(1), max_size: Some(1602), added: 2097, mode: MaxEncodedLen)
/// Storage: System Account (r:1 w:1)
/// Proof: System Account (max_values: None, max_size: Some(126), added: 2601, mode: MaxEncodedLen)
/// The range of component `i` is `[1, 160]`.
fn claim_uds(_i: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `644`
// Estimated: `4109`
// Minimum execution time: 16_554_000 picoseconds.
Weight::from_parts(18_469_192, 0)
.saturating_add(Weight::from_parts(0, 4109))
.saturating_add(T::DbWeight::get().reads(5))
.saturating_add(T::DbWeight::get().writes(2))
}
/// Storage: UniversalDividend CurrentUd (r:1 w:0)
/// Proof: UniversalDividend CurrentUd (max_values: Some(1), max_size: Some(8), added: 503, mode: MaxEncodedLen)
/// Storage: System Account (r:1 w:1)
......@@ -45,8 +66,8 @@ impl<T: frame_system::Config> pallet_universal_dividend::WeightInfo for WeightIn
// Proof Size summary in bytes:
// Measured: `93`
// Estimated: `3591`
// Minimum execution time: 23_549_000 picoseconds.
Weight::from_parts(24_378_000, 0)
// Minimum execution time: 23_431_000 picoseconds.
Weight::from_parts(23_871_000, 0)
.saturating_add(Weight::from_parts(0, 3591))
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(2))
......@@ -61,8 +82,8 @@ impl<T: frame_system::Config> pallet_universal_dividend::WeightInfo for WeightIn
// Proof Size summary in bytes:
// Measured: `93`
// Estimated: `3591`
// Minimum execution time: 16_820_000 picoseconds.
Weight::from_parts(17_437_000, 0)
// Minimum execution time: 16_815_000 picoseconds.
Weight::from_parts(17_432_000, 0)
.saturating_add(Weight::from_parts(0, 3591))
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(2))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment