Skip to content

Commit 08545c0

Browse files
committed
add tokens benchmarks
1 parent 7312d2a commit 08545c0

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

tokens/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ log = { workspace = true }
1313
scale-info = { workspace = true }
1414
serde = { workspace = true, optional = true }
1515

16+
frame-benchmarking = { workspace = true, optional = true }
1617
frame-support = { workspace = true }
1718
frame-system = { workspace = true }
1819
sp-arithmetic = { workspace = true }
@@ -31,6 +32,7 @@ sp-staking = { workspace = true, features = ["std"] }
3132
[features]
3233
default = [ "std" ]
3334
std = [
35+
"frame-benchmarking?/std",
3436
"frame-support/std",
3537
"frame-system/std",
3638
"log/std",
@@ -43,9 +45,11 @@ std = [
4345
"sp-std/std",
4446
]
4547
runtime-benchmarks = [
48+
"frame-benchmarking/runtime-benchmarks",
4649
"frame-support/runtime-benchmarks",
4750
"frame-system/runtime-benchmarks",
4851
"sp-runtime/runtime-benchmarks",
52+
"pallet-treasury/runtime-benchmarks",
4953
]
5054
try-runtime = [
5155
"frame-support/try-runtime",

tokens/src/benchmarking.rs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
pub use crate::*;
2+
3+
use frame_benchmarking::v2::*;
4+
use frame_support::assert_ok;
5+
use frame_system::RawOrigin;
6+
use sp_runtime::traits::SaturatedConversion;
7+
8+
/// Helper trait for benchmarking.
9+
pub trait BenchmarkHelper<CurrencyId> {
10+
/// Returns a currency id to be used for benchmarking.
11+
fn get_currency_id() -> Option<CurrencyId>;
12+
}
13+
14+
impl<CurrencyId> BenchmarkHelper<CurrencyId> for () {
15+
fn get_currency_id() -> Option<CurrencyId> {
16+
None
17+
}
18+
}
19+
20+
const AMOUNT: u32 = 1_000_000_000;
21+
22+
#[benchmarks]
23+
mod benchmarks {
24+
use super::*;
25+
26+
#[benchmark]
27+
fn transfer() {
28+
let from: T::AccountId = account("from", 0, 0);
29+
let amount: T::Balance = AMOUNT.into();
30+
31+
let currency_id: T::CurrencyId = T::BenchmarkHelper::get_currency_id().unwrap();
32+
33+
assert_ok!(<Pallet::<T> as MultiCurrencyExtended<_>>::update_balance(
34+
currency_id,
35+
&from,
36+
amount.saturated_into()
37+
));
38+
39+
let to: T::AccountId = account("to", 0, 0);
40+
let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone());
41+
42+
#[extrinsic_call]
43+
_(RawOrigin::Signed(from), to_lookup, currency_id, amount);
44+
45+
assert_eq!(Pallet::<T>::total_balance(currency_id, &to), amount);
46+
}
47+
48+
#[benchmark]
49+
fn transfer_all() {
50+
let from: T::AccountId = account("from", 0, 0);
51+
let amount: T::Balance = AMOUNT.into();
52+
53+
let currency_id: T::CurrencyId = T::BenchmarkHelper::get_currency_id().unwrap();
54+
55+
assert_ok!(<Pallet::<T> as MultiCurrencyExtended<_>>::update_balance(
56+
currency_id,
57+
&from,
58+
amount.saturated_into()
59+
));
60+
61+
let to: T::AccountId = account("to", 0, 0);
62+
let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone());
63+
64+
#[extrinsic_call]
65+
_(RawOrigin::Signed(from.clone()), to_lookup, currency_id, false);
66+
67+
assert_eq!(
68+
<Pallet::<T> as MultiCurrency<_>>::total_balance(currency_id, &from),
69+
0u32.into()
70+
);
71+
}
72+
73+
#[benchmark]
74+
fn transfer_keep_alive() {
75+
let from: T::AccountId = account("from", 0, 0);
76+
let amount: T::Balance = AMOUNT.into();
77+
78+
let currency_id: T::CurrencyId = T::BenchmarkHelper::get_currency_id().unwrap();
79+
80+
assert_ok!(<Pallet::<T> as MultiCurrencyExtended<_>>::update_balance(
81+
currency_id,
82+
&from,
83+
amount.saturating_mul(2u32.into()).saturated_into()
84+
));
85+
86+
let to: T::AccountId = account("to", 0, 0);
87+
let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone());
88+
89+
#[extrinsic_call]
90+
_(RawOrigin::Signed(from), to_lookup, currency_id, amount);
91+
92+
assert_eq!(
93+
<Pallet::<T> as MultiCurrency<_>>::total_balance(currency_id, &to),
94+
amount
95+
);
96+
}
97+
98+
#[benchmark]
99+
fn force_transfer() {
100+
let from: T::AccountId = account("from", 0, 0);
101+
let from_lookup = <T as frame_system::Config>::Lookup::unlookup(from.clone());
102+
let amount: T::Balance = AMOUNT.into();
103+
104+
let currency_id: T::CurrencyId = T::BenchmarkHelper::get_currency_id().unwrap();
105+
106+
assert_ok!(<Pallet::<T> as MultiCurrencyExtended<_>>::update_balance(
107+
currency_id,
108+
&from,
109+
amount.saturated_into()
110+
));
111+
112+
let to: T::AccountId = account("to", 0, 0);
113+
let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone());
114+
115+
#[extrinsic_call]
116+
_(RawOrigin::Root, from_lookup, to_lookup, currency_id, amount);
117+
118+
assert_eq!(
119+
<Pallet::<T> as MultiCurrency<_>>::total_balance(currency_id, &to),
120+
amount
121+
);
122+
}
123+
124+
#[benchmark]
125+
fn set_balance() {
126+
let who: T::AccountId = account("who", 0, 0);
127+
let who_lookup = <T as frame_system::Config>::Lookup::unlookup(who.clone());
128+
let amount: T::Balance = AMOUNT.into();
129+
130+
let currency_id: T::CurrencyId = T::BenchmarkHelper::get_currency_id().unwrap();
131+
132+
#[extrinsic_call]
133+
_(RawOrigin::Root, who_lookup, currency_id, amount, amount);
134+
135+
assert_eq!(
136+
<Pallet::<T> as MultiCurrency<_>>::total_balance(currency_id, &who),
137+
amount.saturating_mul(2u32.into())
138+
);
139+
}
140+
141+
impl_benchmark_test_suite! {
142+
Pallet,
143+
crate::mock::ExtBuilder::default().build(),
144+
crate::mock::Runtime,
145+
}
146+
}

tokens/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ use orml_traits::{
7171
MultiReservableCurrency, NamedMultiReservableCurrency,
7272
};
7373

74+
#[cfg(feature = "runtime-benchmarks")]
75+
mod benchmarking;
7476
mod imbalances;
7577
mod impls;
7678
mod mock;
@@ -82,6 +84,8 @@ mod tests_multicurrency;
8284

8385
mod weights;
8486

87+
#[cfg(feature = "runtime-benchmarks")]
88+
pub use benchmarking::BenchmarkHelper;
8589
pub use impls::*;
8690
pub use weights::WeightInfo;
8791

@@ -226,6 +230,10 @@ pub mod module {
226230
// The whitelist of accounts that will not be reaped even if its total
227231
// is zero or below ED.
228232
type DustRemovalWhitelist: Contains<Self::AccountId>;
233+
234+
/// The benchmarks need a way to create asset ids from u32s.
235+
#[cfg(feature = "runtime-benchmarks")]
236+
type BenchmarkHelper: BenchmarkHelper<Self::CurrencyId>;
229237
}
230238

231239
#[pallet::error]

tokens/src/mock.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,15 @@ where
375375
type OnKilledTokenAccount = TrackKilledAccounts<T>;
376376
}
377377

378+
#[cfg(feature = "runtime-benchmarks")]
379+
pub struct MockBenchmarkHelper;
380+
#[cfg(feature = "runtime-benchmarks")]
381+
impl BenchmarkHelper<CurrencyId> for MockBenchmarkHelper {
382+
fn get_currency_id() -> Option<CurrencyId> {
383+
Some(DOT)
384+
}
385+
}
386+
378387
impl Config for Runtime {
379388
type Balance = Balance;
380389
type Amount = i64;
@@ -386,6 +395,8 @@ impl Config for Runtime {
386395
type MaxReserves = ConstU32<2>;
387396
type ReserveIdentifier = ReserveIdentifier;
388397
type DustRemovalWhitelist = MockDustRemovalWhitelist;
398+
#[cfg(feature = "runtime-benchmarks")]
399+
type BenchmarkHelper = MockBenchmarkHelper;
389400
}
390401
pub type TreasuryCurrencyAdapter = <Runtime as pallet_treasury::Config>::Currency;
391402

0 commit comments

Comments
 (0)