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