Skip to content

Commit 4799169

Browse files
authored
Add auction benchmarks (#1040)
* add auction benchmarks * fix * update * remove unused files
1 parent 5e26401 commit 4799169

File tree

8 files changed

+132
-63
lines changed

8 files changed

+132
-63
lines changed

auction/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ parity-scale-codec = { workspace = true }
1212
scale-info = { workspace = true }
1313
serde = { workspace = true, optional = true }
1414

15+
frame-benchmarking = { workspace = true, optional = true }
1516
frame-support = { workspace = true }
1617
frame-system = { workspace = true }
1718
sp-runtime = { workspace = true }
@@ -26,6 +27,7 @@ sp-io = { workspace = true, features = ["std"] }
2627
[features]
2728
default = [ "std" ]
2829
std = [
30+
"frame-benchmarking?/std",
2931
"frame-support/std",
3032
"frame-system/std",
3133
"orml-traits/std",
@@ -35,6 +37,12 @@ std = [
3537
"sp-runtime/std",
3638
"sp-std/std",
3739
]
40+
runtime-benchmarks = [
41+
"frame-benchmarking/runtime-benchmarks",
42+
"frame-support/runtime-benchmarks",
43+
"frame-system/runtime-benchmarks",
44+
"sp-runtime/runtime-benchmarks",
45+
]
3846
try-runtime = [
3947
"frame-support/try-runtime",
4048
"frame-system/try-runtime",

auction/src/benchmarking.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}

auction/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ use sp_runtime::{
2222
DispatchError, DispatchResult,
2323
};
2424

25+
#[cfg(feature = "runtime-benchmarks")]
26+
mod benchmarking;
2527
mod mock;
2628
mod tests;
2729
mod weights;
2830

31+
#[cfg(feature = "runtime-benchmarks")]
32+
pub use benchmarking::{BaseBenchmarkHelper, BenchmarkHelper};
2933
pub use module::*;
3034
pub use weights::WeightInfo;
3135

@@ -61,6 +65,9 @@ pub mod module {
6165

6266
/// Weight information for extrinsics in this module.
6367
type WeightInfo: WeightInfo;
68+
69+
#[cfg(feature = "runtime-benchmarks")]
70+
type BenchmarkHelper: BenchmarkHelper<BlockNumberFor<Self>, Self::AccountId, Self::Balance>;
6471
}
6572

6673
#[pallet::error]
@@ -130,7 +137,7 @@ pub mod module {
130137
/// The dispatch origin for this call must be `Signed` by the
131138
/// transactor.
132139
#[pallet::call_index(0)]
133-
#[pallet::weight(T::WeightInfo::bid_collateral_auction())]
140+
#[pallet::weight(T::WeightInfo::bid())]
134141
pub fn bid(origin: OriginFor<T>, id: T::AuctionId, #[pallet::compact] value: T::Balance) -> DispatchResult {
135142
let from = ensure_signed(origin)?;
136143

auction/src/mock.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ impl AuctionHandler<AccountId, Balance, BlockNumber, AuctionId> for Handler {
2929
now: BlockNumber,
3030
_id: AuctionId,
3131
new_bid: (AccountId, Balance),
32-
_last_bid: Option<(AccountId, Balance)>,
32+
last_bid: Option<(AccountId, Balance)>,
3333
) -> OnNewBidResult<BlockNumber> {
34-
if new_bid.0 == ALICE {
34+
if last_bid.is_none() || last_bid.unwrap().0 != new_bid.0 {
3535
OnNewBidResult {
3636
accept_bid: true,
3737
auction_end_change: Change::NewValue(Some(now + BID_EXTEND_BLOCK)),
@@ -52,6 +52,8 @@ impl Config for Runtime {
5252
type AuctionId = AuctionId;
5353
type Handler = Handler;
5454
type WeightInfo = ();
55+
#[cfg(feature = "runtime-benchmarks")]
56+
type BenchmarkHelper = BaseBenchmarkHelper<Runtime>;
5557
}
5658

5759
type Block = frame_system::mocking::MockBlock<Runtime>;
@@ -64,7 +66,6 @@ construct_runtime!(
6466
);
6567

6668
pub const ALICE: AccountId = 1;
67-
pub const BOB: AccountId = 2;
6869
pub const BID_EXTEND_BLOCK: BlockNumber = 10;
6970

7071
pub struct ExtBuilder;

auction/src/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ fn bid_should_fail() {
9393
AuctionModule::bid(RuntimeOrigin::signed(ALICE), 0, 20),
9494
Error::<Runtime>::AuctionNotStarted
9595
);
96+
assert_ok!(AuctionModule::bid(RuntimeOrigin::signed(ALICE), 1, 20));
9697
assert_noop!(
97-
AuctionModule::bid(RuntimeOrigin::signed(BOB), 1, 20),
98+
AuctionModule::bid(RuntimeOrigin::signed(ALICE), 1, 30),
9899
Error::<Runtime>::BidNotAccepted,
99100
);
100101
assert_noop!(

auction/src/weights.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ use sp_std::marker::PhantomData;
3030

3131
/// Weight functions needed for orml_auction.
3232
pub trait WeightInfo {
33-
fn bid_collateral_auction() -> Weight;
33+
fn bid() -> Weight;
3434
fn on_finalize(c: u32, ) -> Weight;
3535
}
3636

3737
/// Default weights.
3838
impl WeightInfo for () {
39-
fn bid_collateral_auction() -> Weight {
39+
fn bid() -> Weight {
4040
Weight::from_parts(108_000_000, 0)
4141
.saturating_add(RocksDbWeight::get().reads(8 as u64))
4242
.saturating_add(RocksDbWeight::get().writes(9 as u64))

currencies/src/default_weight.rs

Lines changed: 0 additions & 29 deletions
This file was deleted.

vesting/src/default_weight.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)