|
| 1 | +pub use crate::*; |
| 2 | + |
| 3 | +use frame_benchmarking::v2::*; |
| 4 | +use frame_support::assert_ok; |
| 5 | +use frame_system::{EventRecord, RawOrigin}; |
| 6 | + |
| 7 | +/// Helper trait for benchmarking. |
| 8 | +pub trait BenchmarkHelper<BlockNumber, AccountId, Balance> { |
| 9 | + fn setup_bid() -> Option<(AccountId, Balance)>; |
| 10 | + fn setup_on_finalize(rand: u32) -> Option<BlockNumber>; |
| 11 | +} |
| 12 | + |
| 13 | +impl<BlockNumber, AccountId, Balance> BenchmarkHelper<BlockNumber, AccountId, Balance> for () { |
| 14 | + fn setup_bid() -> Option<(AccountId, Balance)> { |
| 15 | + None |
| 16 | + } |
| 17 | + fn setup_on_finalize(_rand: u32) -> Option<BlockNumber> { |
| 18 | + None |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +pub struct BaseBenchmarkHelper<T>(sp_std::marker::PhantomData<T>); |
| 23 | + |
| 24 | +impl<T: Config> BenchmarkHelper<BlockNumberFor<T>, T::AccountId, T::Balance> for BaseBenchmarkHelper<T> { |
| 25 | + fn setup_bid() -> Option<(T::AccountId, T::Balance)> { |
| 26 | + let end_block: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number() + 10u32.into(); |
| 27 | + assert_ok!(Pallet::<T>::new_auction( |
| 28 | + frame_system::Pallet::<T>::block_number(), |
| 29 | + Some(end_block), |
| 30 | + )); |
| 31 | + |
| 32 | + let auction_id: T::AuctionId = 0u32.into(); |
| 33 | + let pre_bidder: T::AccountId = account("pre_bidder", 0, 0); |
| 34 | + let pre_bid_price: T::Balance = 10_000u32.into(); |
| 35 | + |
| 36 | + assert_ok!(Pallet::<T>::bid( |
| 37 | + RawOrigin::Signed(pre_bidder).into(), |
| 38 | + auction_id, |
| 39 | + pre_bid_price |
| 40 | + )); |
| 41 | + |
| 42 | + let bidder: T::AccountId = account("bidder", 0, 0); |
| 43 | + let bid_price: T::Balance = 20_000u32.into(); |
| 44 | + |
| 45 | + Some((bidder, bid_price)) |
| 46 | + } |
| 47 | + |
| 48 | + fn setup_on_finalize(rand: u32) -> Option<BlockNumberFor<T>> { |
| 49 | + let end_block: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number() + 10u32.into(); |
| 50 | + |
| 51 | + for _auction_id in 0..rand { |
| 52 | + assert_ok!(Pallet::<T>::new_auction( |
| 53 | + frame_system::Pallet::<T>::block_number(), |
| 54 | + Some(end_block), |
| 55 | + )); |
| 56 | + } |
| 57 | + Some(end_block) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +fn assert_last_event<T: Config>(generic_event: <T as frame_system::Config>::RuntimeEvent) { |
| 62 | + let events = frame_system::Pallet::<T>::events(); |
| 63 | + let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into(); |
| 64 | + // compare to the last event record |
| 65 | + let EventRecord { event, .. } = &events[events.len() - 1]; |
| 66 | + assert_eq!(event, &system_event); |
| 67 | +} |
| 68 | + |
| 69 | +#[benchmarks] |
| 70 | +mod benchmarks { |
| 71 | + use super::*; |
| 72 | + |
| 73 | + // `bid` a collateral auction, worst cases: |
| 74 | + // there's bidder before and bid price will exceed target amount |
| 75 | + #[benchmark] |
| 76 | + fn bid() { |
| 77 | + let auction_id: T::AuctionId = 0u32.into(); |
| 78 | + let (bidder, bid_price) = T::BenchmarkHelper::setup_bid().unwrap(); |
| 79 | + |
| 80 | + #[extrinsic_call] |
| 81 | + _(RawOrigin::Signed(bidder.clone()), auction_id, bid_price); |
| 82 | + |
| 83 | + assert_last_event::<T>( |
| 84 | + Event::Bid { |
| 85 | + auction_id, |
| 86 | + bidder: bidder, |
| 87 | + amount: bid_price, |
| 88 | + } |
| 89 | + .into(), |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + #[benchmark] |
| 94 | + fn on_finalize(c: Liner<1, 100>) { |
| 95 | + let end_block = T::BenchmarkHelper::setup_on_finalize(c).unwrap(); |
| 96 | + |
| 97 | + #[block] |
| 98 | + { |
| 99 | + Pallet::<T>::on_finalize(end_block); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + impl_benchmark_test_suite! { |
| 104 | + Pallet, |
| 105 | + crate::mock::ExtBuilder::default().build(), |
| 106 | + crate::mock::Runtime, |
| 107 | + } |
| 108 | +} |
0 commit comments