Skip to content
Snippets Groups Projects

Quota pallet benchmark

All threads resolved!
Compare and
5 files
+ 275
66
Compare changes
  • Side-by-side
  • Inline
Files
5
@@ -19,46 +19,108 @@
@@ -19,46 +19,108 @@
use super::*;
use super::*;
use frame_benchmarking::{account, benchmarks};
use frame_benchmarking::{account, benchmarks};
// FIXME this is a naïve implementation of benchmarks:
fn assert_has_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
// - without properly prepare data
frame_system::Pallet::<T>::assert_has_event(generic_event.into());
// - without "verify" blocks
}
// - without thinking about worst case scenario
// - without writing complexity in the term of refund queue length
// It's there as a seed for benchmark implementation and to use WeightInfo where needed.
benchmarks! {
benchmarks! {
where_clause {
where_clause {
where
where
IdtyId<T>: From<u32>,
IdtyId<T>: From<u32>,
BalanceOf<T>: From<u64>,
BalanceOf<T>: From<u64>,
 
T::AccountId: From<[u8; 32]>,
}
}
queue_refund {
queue_refund {
let account: T::AccountId = account("Alice", 1, 1);
let account: T::AccountId = account("Alice", 1, 1);
 
let dummy_refund = Refund {
 
account: account.clone(),
 
identity: 0u32.into(),
 
amount: 20u64.into(),
 
};
let refund = Refund {
let refund = Refund {
account,
account,
identity: 1u32.into(),
identity: 1u32.into(),
amount: 10u64.into(),
amount: 10u64.into(),
};
};
}: { Pallet::<T>::queue_refund(refund) }
// Complexity is bound to MAX_QUEUD_REFUNDS where an insertion is O(n-1)
 
for i in 0..MAX_QUEUED_REFUNDS-1 {
 
Pallet::<T>::queue_refund(dummy_refund.clone())
 
}
 
}: { Pallet::<T>::queue_refund(refund.clone()) }
 
verify {
 
assert_eq!(RefundQueue::<T>::get().last(), Some(refund).as_ref());
 
assert_eq!(RefundQueue::<T>::get().len() as u32, MAX_QUEUED_REFUNDS);
 
}
spend_quota {
spend_quota {
let idty_id = 1u32;
let idty_id: IdtyId<T> = 1u32.into();
let amount = 1u64;
let amount = 2u64;
 
IdtyQuota::<T>::insert(
 
idty_id,
 
Quota {
 
last_use: T::BlockNumber::zero(),
 
amount: 10u64.into(),
 
},
 
);
}: { Pallet::<T>::spend_quota(idty_id.into(), amount.into()) }
}: { Pallet::<T>::spend_quota(idty_id.into(), amount.into()) }
 
verify {
 
// Initially 10, updated to 11, minus 2
 
assert_eq!(IdtyQuota::<T>::get(idty_id).unwrap().amount, 9u64.into());
 
}
try_refund {
try_refund {
let account: T::AccountId = account("Alice", 1, 1);
let account: T::AccountId = account("Alice", 1, 1);
 
let idty_id: IdtyId<T> = 1u32.into();
 
IdtyQuota::<T>::insert(
 
idty_id,
 
Quota {
 
last_use: T::BlockNumber::zero(),
 
amount: 10u64.into(),
 
},
 
);
 
let _ = CurrencyOf::<T>:: make_free_balance_be(
 
&T::RefundAccount::get(),u32::MAX.into(),
 
);
 
// The worst-case scenario is when the refund fails
 
// and can only be triggered if the account is dead,
 
// in this case by having no balance in the account.
let refund = Refund {
let refund = Refund {
account,
account: account.clone(),
identity: 1u32.into(),
identity: 1u32.into(),
amount: 10u64.into(),
amount: 10u64.into(),
};
};
}: { Pallet::<T>::try_refund(refund) }
}: { Pallet::<T>::try_refund(refund) }
 
verify {
 
assert_has_event::<T>(Event::<T>::RefundFailed ( account ).into());
 
}
do_refund {
do_refund {
let account: T::AccountId = account("Alice", 1, 1);
let account: T::AccountId = account("Alice", 1, 1);
 
let _ = CurrencyOf::<T>:: make_free_balance_be(
 
&T::RefundAccount::get(),u32::MAX.into(),
 
);
 
// The worst-case scenario is when the refund fails
 
// and can only be triggered if the account is dead,
 
// in this case by having no balance in the account.
let refund = Refund {
let refund = Refund {
account,
account: account.clone(),
identity: 1u32.into(),
identity: 1u32.into(),
amount: 10u64.into(),
amount: 10u64.into(),
};
};
let amount = 5u64.into();
}: { Pallet::<T>::try_refund(refund) }
}: { Pallet::<T>::do_refund(refund, amount) }
verify {
 
assert_has_event::<T>(Event::<T>::RefundFailed ( account ).into());
 
}
 
process_refund_queue {
 
let account: T::AccountId = account("Alice", 1, 1);
 
let dummy_refund = Refund {
 
account: account.clone(),
 
identity: 0u32.into(),
 
amount: 20u64.into(),
 
};
 
// Worst case scenario: the queue is full, and the available weight is maximal.
 
for i in 0 .. MAX_QUEUED_REFUNDS {
 
Pallet::<T>::queue_refund(dummy_refund.clone())
 
}
 
}: { Pallet::<T>::process_refund_queue(Weight::MAX) }
 
verify {
 
assert_eq!(RefundQueue::<T>::get().len(), 0);
 
}
}
}
Loading