Skip to content

Commit

Permalink
correct benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Jun 2, 2023
1 parent fa71840 commit 9c6e7eb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
23 changes: 8 additions & 15 deletions pallets/loans-ref/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,14 @@ where
fn initialize_active_state(n: u32) -> PoolIdOf<T> {
let pool_id = Self::prepare_benchmark();

// TODO: ideally should this be computed by a parameter?
for i in 1..MaxRateCountOf::<T>::get() {
// First `i` (i=0) used by the loan's interest rate.
let rate = T::Rate::saturating_from_rational(i + 1, 5000);
T::InterestAccrual::reference_rate(rate).unwrap();
}

// TODO: ideally should this be computed by a parameter?
for i in 0..MaxCollectionSizeOf::<T>::get() {
let price_id = i.into();
// This account is different in each iteration because of how oracles works.
Expand All @@ -248,15 +250,6 @@ where

pool_id
}

fn max_benchmark_loans() -> u32 {
// Maximum loans used for benchmaks.
// This can be smaller than `MaxActiveLoansPerPool`,
// to avoid calculate weights during hours if the loan numbers is too
// large. It should be large enough to be able to represent a consistent weight
// function for any number of loans.
T::MaxActiveLoansPerPool::get().min(50)
}
}

benchmarks! {
Expand All @@ -283,7 +276,7 @@ benchmarks! {
}: _(RawOrigin::Signed(borrower), pool_id, loan_info)

borrow {
let n in 1..Helper::<T>::max_benchmark_loans() - 1;
let n in 1..T::MaxActiveLoansPerPool::get() - 1;

let borrower = account("borrower", 0, 0);
let pool_id = Helper::<T>::initialize_active_state(n);
Expand All @@ -292,7 +285,7 @@ benchmarks! {
}: _(RawOrigin::Signed(borrower), pool_id, loan_id, 10.into())

repay {
let n in 1..Helper::<T>::max_benchmark_loans() - 1;
let n in 1..T::MaxActiveLoansPerPool::get() - 1;

let borrower = account("borrower", 0, 0);
let pool_id = Helper::<T>::initialize_active_state(n);
Expand All @@ -302,7 +295,7 @@ benchmarks! {
}: _(RawOrigin::Signed(borrower), pool_id, loan_id, 10.into(), 0.into())

write_off {
let n in 1..Helper::<T>::max_benchmark_loans() - 1;
let n in 1..T::MaxActiveLoansPerPool::get() - 1;

let borrower = account("borrower", 0, 0);
let pool_id = Helper::<T>::initialize_active_state(n);
Expand All @@ -314,7 +307,7 @@ benchmarks! {
}: _(RawOrigin::Signed(borrower), pool_id, loan_id)

admin_write_off {
let n in 1..Helper::<T>::max_benchmark_loans() - 1;
let n in 1..T::MaxActiveLoansPerPool::get() - 1;

let loan_admin = account("loan_admin", 0, 0);
let pool_id = Helper::<T>::initialize_active_state(n);
Expand All @@ -325,7 +318,7 @@ benchmarks! {
}: _(RawOrigin::Signed(loan_admin), pool_id, loan_id, T::Rate::zero(), T::Rate::zero())

close {
let n in 1..Helper::<T>::max_benchmark_loans() - 1;
let n in 1..T::MaxActiveLoansPerPool::get() - 1;

let borrower = account("borrower", 0, 0);
let pool_id = Helper::<T>::initialize_active_state(n);
Expand All @@ -343,7 +336,7 @@ benchmarks! {
}: _(RawOrigin::Signed(pool_admin), pool_id, policy)

update_portfolio_valuation {
let n in 1..Helper::<T>::max_benchmark_loans();
let n in 1..T::MaxActiveLoansPerPool::get();

let borrower = account("borrower", 0, 0);
let pool_id = Helper::<T>::initialize_active_state(n);
Expand Down
11 changes: 11 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ pub mod account_conversion;
pub mod apis;
pub mod evm;

#[macro_export]
macro_rules! production_or_benchmark {
($production:expr, $benchmark:expr) => {{
if cfg!(feature = "runtime-benchmarks") {
$benchmark
} else {
$production
}
}};
}

pub mod xcm_fees {
use cfg_primitives::{constants::currency_decimals, types::Balance};
use frame_support::weights::constants::{ExtrinsicBaseWeight, WEIGHT_REF_TIME_PER_SECOND};
Expand Down
42 changes: 23 additions & 19 deletions runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,16 +1296,20 @@ impl pallet_xcm_transactor::Config for Runtime {

parameter_types! {
pub const MaxActiveLoansPerPool: u32 = 1000;
/// We do not need so many iterations for benchmarking,
/// which also increase A LOT the time of calculating weights
pub const MaxActiveLoansPerPoolBenchmark: u32 = production_or_benchmark!(
MaxActiveLoansPerPool::get(),
50
);
pub const MaxWriteOffPolicySize: u32 = 10;
pub const MaxPriceOracleMembers: u32 = 10;
pub const MaxHasDispatchedSize: u32 = if cfg!(feature = "runtime-benchmarks") {
// This can be removed once
// <https://github.com/open-web3-stack/open-runtime-module-library/issues/920> be merged.
// The benchmark distintion can be removed once
// <https://github.com/open-web3-stack/open-runtime-module-library/issues/920> be merged.
pub const MaxHasDispatchedSize: u32 = production_or_benchmark!(
MaxPriceOracleMembers::get(),
MaxActiveLoansPerPool::get()
} else {
MaxPriceOracleMembers::get()
};

);
pub const MaxPoolsWithExternalPrices: u32 = 50;
pub RootOperatorOraclePrice: AccountId = PRICE_ORACLE_PALLET_ID.into_account_truncating();
}
Expand Down Expand Up @@ -1351,14 +1355,25 @@ impl pallet_data_collector::Config for Runtime {
type Moment = Moment;
}

impl pallet_interest_accrual::Config for Runtime {
type Balance = Balance;
type InterestRate = Rate;
// TODO: This is a stopgap value until we can calculate it correctly with
// updated benchmarks. See #1024
type MaxRateCount = MaxActiveLoansPerPool;
type RuntimeEvent = RuntimeEvent;
type Time = Timestamp;
type Weights = ();
}

impl pallet_loans_ref::Config for Runtime {
type Balance = Balance;
type CollectionId = CollectionId;
type CurrencyId = CurrencyId;
type InterestAccrual = InterestAccrual;
type ItemId = ItemId;
type LoanId = LoanId;
type MaxActiveLoansPerPool = MaxActiveLoansPerPool;
type MaxActiveLoansPerPool = MaxActiveLoansPerPoolBenchmark;
type MaxWriteOffPolicySize = MaxWriteOffPolicySize;
type NonFungible = Uniques;
type Permissions = Permissions;
Expand Down Expand Up @@ -1546,17 +1561,6 @@ impl orml_asset_registry::Config for Runtime {
type WeightInfo = ();
}

impl pallet_interest_accrual::Config for Runtime {
type Balance = Balance;
type InterestRate = Rate;
// TODO: This is a stopgap value until we can calculate it correctly with
// updated benchmarks. See #1024
type MaxRateCount = MaxActiveLoansPerPool;
type RuntimeEvent = RuntimeEvent;
type Time = Timestamp;
type Weights = ();
}

impl pallet_connectors::Config for Runtime {
type AccountConverter = AccountConverter<Runtime>;
type AdminOrigin = EnsureRoot<AccountId>;
Expand Down

0 comments on commit 9c6e7eb

Please sign in to comment.