Skip to content

Commit 9615fe4

Browse files
authored
add tokens benchmarks (#1037)
* update oracle * add tokens benchmarks * updates * fix * fix * fix * fix * update
1 parent e1713bd commit 9615fe4

File tree

16 files changed

+210
-23
lines changed

16 files changed

+210
-23
lines changed

asset-registry/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ std = [
7878
runtime-benchmarks = [
7979
"frame-support/runtime-benchmarks",
8080
"frame-system/runtime-benchmarks",
81+
"orml-tokens/runtime-benchmarks",
8182
"pallet-xcm/runtime-benchmarks",
8283
"polkadot-runtime-common/runtime-benchmarks",
8384
"sp-runtime/runtime-benchmarks",

asset-registry/src/mock/para.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ impl orml_tokens::Config for Runtime {
7777
type MaxReserves = ();
7878
type MaxLocks = ConstU32<50>;
7979
type DustRemovalWhitelist = Nothing;
80+
#[cfg(feature = "runtime-benchmarks")]
81+
type BenchmarkHelper = ();
8082
}
8183

8284
#[derive(scale_info::TypeInfo, Encode, Decode, Clone, Eq, PartialEq, Debug, MaxEncodedLen, DecodeWithMemTracking)]

currencies/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ orml-utilities = { path = "../utilities", version = "1.5.0", default-features =
2525
pallet-balances = { workspace = true, features = ["std"] }
2626
sp-core = { workspace = true, features = ["std"] }
2727

28-
orml_tokens = { package = "orml-tokens", path = "../tokens" }
28+
orml-tokens = { path = "../tokens", features = ["std"] }
2929

3030
[features]
3131
default = [ "std" ]
@@ -41,6 +41,12 @@ std = [
4141
"sp-runtime/std",
4242
"sp-std/std",
4343
]
44+
runtime-benchmarks = [
45+
"frame-support/runtime-benchmarks",
46+
"frame-system/runtime-benchmarks",
47+
"orml-tokens/runtime-benchmarks",
48+
"sp-runtime/runtime-benchmarks",
49+
]
4450
try-runtime = [
4551
"frame-support/try-runtime",
4652
"frame-system/try-runtime",

currencies/src/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ impl orml_tokens::Config for Runtime {
8585
type MaxReserves = ConstU32<100_000>;
8686
type ReserveIdentifier = ReserveIdentifier;
8787
type DustRemovalWhitelist = Nothing;
88+
#[cfg(feature = "runtime-benchmarks")]
89+
type BenchmarkHelper = ();
8890
}
8991

9092
pub const NATIVE_CURRENCY_ID: CurrencyId = 1;

oracle/src/benchmarking.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
pub use crate::*;
22

3-
use frame_benchmarking::v2::{impl_test_function, instance_benchmarks, whitelisted_caller};
4-
3+
use frame_benchmarking::v2::*;
54
use frame_support::assert_ok;
65
use frame_system::{Pallet as System, RawOrigin};
76

7+
/// Helper trait for benchmarking.
8+
pub trait BenchmarkHelper<OracleKey, OracleValue, L: Get<u32>> {
9+
/// Returns a list of `(oracle_key, oracle_value)` pairs to be used for
10+
/// benchmarking.
11+
///
12+
/// NOTE: User should ensure to at least submit two values, otherwise the
13+
/// benchmark linear analysis might fail.
14+
fn get_currency_id_value_pairs() -> BoundedVec<(OracleKey, OracleValue), L>;
15+
}
16+
17+
impl<OracleKey, OracleValue, L: Get<u32>> BenchmarkHelper<OracleKey, OracleValue, L> for () {
18+
fn get_currency_id_value_pairs() -> BoundedVec<(OracleKey, OracleValue), L> {
19+
BoundedVec::default()
20+
}
21+
}
22+
823
#[instance_benchmarks]
924
mod benchmarks {
1025
use super::*;

oracle/src/lib.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,18 @@ use sp_std::{prelude::*, vec};
4343
pub use crate::default_combine_data::DefaultCombineData;
4444

4545
#[cfg(feature = "runtime-benchmarks")]
46-
pub mod benchmarking;
46+
mod benchmarking;
4747

4848
mod default_combine_data;
4949
mod mock;
5050
mod tests;
5151
mod weights;
5252

53+
#[cfg(feature = "runtime-benchmarks")]
54+
pub use benchmarking::BenchmarkHelper;
5355
pub use module::*;
5456
pub use weights::WeightInfo;
5557

56-
#[cfg(feature = "runtime-benchmarks")]
57-
/// Helper trait for benchmarking.
58-
pub trait BenchmarkHelper<OracleKey, OracleValue, L: Get<u32>> {
59-
/// Returns a list of `(oracle_key, oracle_value)` pairs to be used for
60-
/// benchmarking.
61-
///
62-
/// NOTE: User should ensure to at least submit two values, otherwise the
63-
/// benchmark linear analysis might fail.
64-
fn get_currency_id_value_pairs() -> BoundedVec<(OracleKey, OracleValue), L>;
65-
}
66-
67-
#[cfg(feature = "runtime-benchmarks")]
68-
impl<OracleKey, OracleValue, L: Get<u32>> BenchmarkHelper<OracleKey, OracleValue, L> for () {
69-
fn get_currency_id_value_pairs() -> BoundedVec<(OracleKey, OracleValue), L> {
70-
BoundedVec::default()
71-
}
72-
}
73-
7458
#[frame_support::pallet]
7559
pub mod module {
7660
use super::*;

payments/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ orml-traits = {path = "../traits", version = "1.5.0", default-features = false }
2626
sp-core = { workspace = true }
2727
sp-io = { workspace = true }
2828

29-
orml-tokens = { path = "../tokens" }
29+
orml-tokens = { path = "../tokens", features = ["std"] }
3030

3131
[features]
3232
default = [ 'std' ]
@@ -41,6 +41,12 @@ std = [
4141
'sp-runtime/std',
4242
'sp-std/std',
4343
]
44+
runtime-benchmarks = [
45+
"frame-support/runtime-benchmarks",
46+
"frame-system/runtime-benchmarks",
47+
"orml-tokens/runtime-benchmarks",
48+
"sp-runtime/runtime-benchmarks",
49+
]
4450
try-runtime = [
4551
"frame-support/try-runtime",
4652
"frame-system/try-runtime",

payments/src/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ impl orml_tokens::Config for Test {
7474
type DustRemovalWhitelist = MockDustRemovalWhitelist;
7575
type MaxReserves = ConstU32<2>;
7676
type ReserveIdentifier = ReserveIdentifier;
77+
#[cfg(feature = "runtime-benchmarks")]
78+
type BenchmarkHelper = ();
7779
}
7880

7981
pub struct MockDisputeResolver;

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,8 +45,10 @@ std = [
4345
"sp-std/std",
4446
]
4547
runtime-benchmarks = [
48+
"frame-benchmarking/runtime-benchmarks",
4649
"frame-support/runtime-benchmarks",
4750
"frame-system/runtime-benchmarks",
51+
"pallet-treasury/runtime-benchmarks",
4852
"sp-runtime/runtime-benchmarks",
4953
]
5054
try-runtime = [

tokens/src/benchmarking.rs

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

0 commit comments

Comments
 (0)