Skip to content

Commit

Permalink
[CU-2326a8t] Update benchmarks of pallet-dutch-auction (#512)
Browse files Browse the repository at this point in the history
* Update benchmarks of pallet-dutch-auction

* Update benchmarks of pallet-dutch-auction
  • Loading branch information
andor0 authored Jan 20, 2022
1 parent f20f18b commit 678b6b9
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 61 deletions.
51 changes: 25 additions & 26 deletions frame/dutch-auction/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use super::*;
use crate::{mock::currency::CurrencyId, Pallet as DutchAuction};
use codec::{Decode, Encode};
use crate::Pallet as DutchAuction;
use codec::Decode;
use composable_traits::defi::{CurrencyPair, DeFiComposableConfig, Ratio, Sell, Take};
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_support::traits::{fungibles::Mutate, Hooks};
use frame_support::traits::{fungibles::Mutate, Currency, Get, Hooks};
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
use sp_runtime::FixedPointNumber;
use sp_runtime::{
traits::{AccountIdConversion, Saturating},
FixedPointNumber,
};
use sp_std::prelude::*;

// meaningless sell of 1 to 1
Expand Down Expand Up @@ -36,6 +39,19 @@ where
)
}

fn mint_native_tokens<T>(account_id: &T::AccountId)
where
T: Config,
<T as Config>::MultiCurrency:
Mutate<T::AccountId, Balance = T::Balance, AssetId = T::MayBeAssetId>,
{
let treasury = &T::PalletId::get().into_account();
let native_token_amount = <T as pallet::Config>::NativeCurrency::minimum_balance()
.saturating_mul(1_000_000_000u32.into());
<T as pallet::Config>::NativeCurrency::make_free_balance_be(&treasury, native_token_amount);
<T as pallet::Config>::NativeCurrency::make_free_balance_be(account_id, native_token_amount);
}

benchmarks! {
where_clause {
where
Expand All @@ -47,31 +63,25 @@ benchmarks! {
let account_id : T::AccountId = whitelisted_caller();
let caller = RawOrigin::Signed(account_id.clone());
let amount: T::Balance = 1_000_000_000_000_u64.into();
mint_native_tokens::<T>(&account_id);
<T as pallet::Config>::MultiCurrency::mint_into(sell.pair.base, &account_id, amount).unwrap();
}: _(
caller,
sell,
<_>::default()
)
take {
let x in 1..2^16;
let sell = sell_identity::<T>();
let account_id : T::AccountId = whitelisted_caller();
let caller = RawOrigin::Signed(account_id.clone());
let amount: T::Balance = 1_000_000_000_000_u64.into();

let encoded = CurrencyId::PICA.encode();
let native_asset_id = T::MayBeAssetId::decode(&mut &encoded[..]).unwrap();
<T as pallet::Config>::MultiCurrency::mint_into(native_asset_id, &account_id, amount).unwrap();

mint_native_tokens::<T>(&account_id);
<T as pallet::Config>::MultiCurrency::mint_into(sell.pair.base, &account_id, amount).unwrap();
<T as pallet::Config>::MultiCurrency::mint_into(sell.pair.quote, &account_id, amount).unwrap();
DutchAuction::<T>::ask(caller.clone().into(), sell, <_>::default()).unwrap();
let order_id = OrdersIndex::<T>::get();
let take_order = take_identity::<T>();
for i in 0..x {
DutchAuction::<T>::take(caller.clone().into(), order_id, take_order.clone()).unwrap();
}
DutchAuction::<T>::take(caller.clone().into(), order_id, take_order.clone()).unwrap();
}: _(
caller,
order_id,
Expand All @@ -82,12 +92,7 @@ benchmarks! {
let account_id : T::AccountId = whitelisted_caller();
let caller = RawOrigin::Signed(account_id.clone());
let amount: T::Balance = 1_000_000_000_000_u64.into();

let encoded = CurrencyId::PICA.encode();
let native_asset_id = T::MayBeAssetId::decode(&mut &encoded[..]).unwrap();
<T as pallet::Config>::MultiCurrency::mint_into(native_asset_id, &account_id, amount).unwrap();


mint_native_tokens::<T>(&account_id);
<T as pallet::Config>::MultiCurrency::mint_into(sell.pair.base, &account_id, amount).unwrap();
DutchAuction::<T>::ask(caller.clone().into(), sell, <_>::default()).unwrap();
let order_id = OrdersIndex::<T>::get();
Expand All @@ -100,12 +105,7 @@ benchmarks! {
let account_id : T::AccountId = whitelisted_caller();
let caller = RawOrigin::Signed(account_id.clone());
let amount: T::Balance = 1_000_000_000_000_u64.into();

let encoded = CurrencyId::PICA.encode();
let native_asset_id = T::MayBeAssetId::decode(&mut &encoded[..]).unwrap();
<T as pallet::Config>::MultiCurrency::mint_into(native_asset_id, &account_id, amount).unwrap();


mint_native_tokens::<T>(&account_id);
<T as pallet::Config>::MultiCurrency::mint_into(sell.pair.base, &account_id, amount).unwrap();
<T as pallet::Config>::MultiCurrency::mint_into(sell.pair.quote, &account_id, amount).unwrap();
DutchAuction::<T>::ask(caller.clone().into(), sell, <_>::default()).unwrap();
Expand All @@ -115,7 +115,6 @@ benchmarks! {
} : {
<DutchAuction::<T> as Hooks<BlockNumberFor<T>>>::on_finalize(T::BlockNumber::default())
}

}

impl_benchmark_test_suite!(
Expand Down
37 changes: 34 additions & 3 deletions frame/dutch-auction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub mod pallet {
math::WrappingNext,
time::{TimeReleaseFunction, Timestamp},
};
#[cfg(feature = "runtime-benchmarks")]
use frame_support::traits::Currency;
use frame_support::{
pallet_prelude::*,
traits::{tokens::fungible::Transfer as NativeTransfer, IsType, UnixTime},
Expand All @@ -99,6 +101,7 @@ pub mod pallet {

#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
#[cfg(not(feature = "runtime-benchmarks"))]
pub trait Config: DeFiComposableConfig + frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type UnixTime: UnixTime;
Expand All @@ -119,6 +122,27 @@ pub mod pallet {
/// Convert a weight value into a deductible fee based on the currency type.
type WeightToFee: WeightToFeePolynomial<Balance = Self::Balance>;
}
#[cfg(feature = "runtime-benchmarks")]
pub trait Config: DeFiComposableConfig + frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type UnixTime: UnixTime;
type OrderId: OrderIdLike + WrappingNext + Zero;
type MultiCurrency: MultiCurrency<
Self::AccountId,
CurrencyId = Self::MayBeAssetId,
Balance = <Self as DeFiComposableConfig>::Balance,
> + MultiReservableCurrency<
Self::AccountId,
CurrencyId = Self::MayBeAssetId,
Balance = <Self as DeFiComposableConfig>::Balance,
>;
type WeightInfo: WeightInfo;
type PalletId: Get<PalletId>;
type NativeCurrency: NativeTransfer<Self::AccountId, Balance = Self::Balance>
+ Currency<Self::AccountId>;
/// Convert a weight value into a deductible fee based on the currency type.
type WeightToFee: WeightToFeePolynomial<Balance = Self::Balance>;
}

#[derive(Encode, Decode, Default, TypeInfo, Clone, Debug, PartialEq)]
pub struct SellOrder<AssetId, Balance, AccountId, Context> {
Expand Down Expand Up @@ -225,7 +249,7 @@ pub mod pallet {
}

/// adds take to list, does not execute take immediately
#[pallet::weight(T::WeightInfo::take(42))] // FIXME: need to update benchmark and weight for this extrinsic
#[pallet::weight(T::WeightInfo::take())]
pub fn take(
origin: OriginFor<T>,
order_id: T::OrderId,
Expand All @@ -249,7 +273,12 @@ pub mod pallet {
// pollute account system
let treasury = &T::PalletId::get().into_account();
T::MultiCurrency::unreserve(order.order.pair.base, &who, order.order.take.amount);
T::NativeCurrency::transfer(treasury, &order.from_to, order.context.deposit, true)?;
<T::NativeCurrency as NativeTransfer<T::AccountId>>::transfer(
treasury,
&order.from_to,
order.context.deposit,
true,
)?;

<SellOrders<T>>::remove(order_id);
Self::deposit_event(Event::OrderRemoved { order_id });
Expand All @@ -273,7 +302,9 @@ pub mod pallet {
});
let treasury = &T::PalletId::get().into_account();
let deposit = T::WeightToFee::calc(&T::WeightInfo::liquidate());
T::NativeCurrency::transfer(from_to, treasury, deposit, true)?;
<T::NativeCurrency as NativeTransfer<T::AccountId>>::transfer(
from_to, treasury, deposit, true,
)?;
let now = T::UnixTime::now().as_secs();
let order = SellOf::<T> {
from_to: from_to.clone(),
Expand Down
43 changes: 24 additions & 19 deletions frame/dutch-auction/src/weights.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#![allow(unused_parens, unused_imports, clippy::unnecessary_cast)]

use frame_support::{pallet_prelude::Weight, traits::Get, weights::constants::RocksDbWeight};
use sp_std::marker::PhantomData;

pub trait WeightInfo {
fn ask() -> Weight;
fn take(_x: u32) -> Weight;
fn take() -> Weight;
fn liquidate() -> Weight;
fn known_overhead_for_on_finalize() -> Weight;
}
Expand All @@ -13,71 +14,75 @@ pub trait WeightInfo {
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: DutchAuction OrdersIndex (r:1 w:1)
// Storage: System Account (r:1 w:1)
// Storage: Timestamp Now (r:1 w:0)
// Storage: Tokens Accounts (r:1 w:1)
// Storage: DutchAuction SellOrders (r:0 w:1)
fn ask() -> Weight {
(164_004_000 as Weight)
.saturating_add(T::DbWeight::get().reads(3 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
(185_212_000 as Weight)
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().writes(4 as Weight))
}
// Storage: DutchAuction SellOrders (r:1 w:0)
// Storage: Timestamp Now (r:1 w:0)
// Storage: Tokens Accounts (r:1 w:1)
// Storage: DutchAuction Takes (r:1 w:1)
fn take(_x: u32) -> Weight {
(158_860_000 as Weight)
fn take() -> Weight {
(140_074_000 as Weight)
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: DutchAuction SellOrders (r:1 w:1)
// Storage: Tokens Accounts (r:1 w:1)
// Storage: System Account (r:1 w:1)
fn liquidate() -> Weight {
(105_812_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
(150_867_000 as Weight)
.saturating_add(T::DbWeight::get().reads(3 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
// Storage: DutchAuction Takes (r:2 w:1)
// Storage: DutchAuction SellOrders (r:1 w:1)
// Storage: Tokens Accounts (r:2 w:2)
fn known_overhead_for_on_finalize() -> Weight {
(287_359_000 as Weight)
(195_022_000 as Weight)
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().writes(4 as Weight))
}
}

impl WeightInfo for () {
// Storage: DutchAuction OrdersIndex (r:1 w:1)
// Storage: System Account (r:1 w:1)
// Storage: Timestamp Now (r:1 w:0)
// Storage: Tokens Accounts (r:1 w:1)
// Storage: DutchAuction SellOrders (r:0 w:1)
fn ask() -> Weight {
(164_004_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(3 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
(185_212_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(4 as Weight))
.saturating_add(RocksDbWeight::get().writes(4 as Weight))
}
// Storage: DutchAuction SellOrders (r:1 w:0)
// Storage: Timestamp Now (r:1 w:0)
// Storage: Tokens Accounts (r:1 w:1)
// Storage: DutchAuction Takes (r:1 w:1)
fn take(_x: u32) -> Weight {
(158_860_000 as Weight)
fn take() -> Weight {
(140_074_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(4 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
// Storage: DutchAuction SellOrders (r:1 w:1)
// Storage: Tokens Accounts (r:1 w:1)
// Storage: System Account (r:1 w:1)
fn liquidate() -> Weight {
(105_812_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
(150_867_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(3 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
// Storage: DutchAuction Takes (r:2 w:1)
// Storage: DutchAuction SellOrders (r:1 w:1)
// Storage: Tokens Accounts (r:2 w:2)
fn known_overhead_for_on_finalize() -> Weight {
(287_359_000 as Weight)
(195_022_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(5 as Weight))
.saturating_add(RocksDbWeight::get().writes(4 as Weight))
}
Expand Down
2 changes: 1 addition & 1 deletion frame/lending/src/mocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl pallet_dutch_auction::weights::WeightInfo for DutchAuctionsMocks {
0
}

fn take(_x: u32) -> frame_support::dispatch::Weight {
fn take() -> frame_support::dispatch::Weight {
0
}

Expand Down
27 changes: 15 additions & 12 deletions runtime/dali/src/weights/dutch_auction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

//! Autogenerated weights for `dutch_auction`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2022-01-11, STEPS: `5`, REPEAT: 2, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2022-01-18, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dali-dev"), DB CACHE: 128

// Executed Command:
Expand All @@ -12,8 +13,8 @@
// --wasm-execution=compiled
// --pallet=dutch_auction
// --extrinsic=*
// --steps=5
// --repeat=2
// --steps=50
// --repeat=20
// --raw
// --output=./runtime/dali/src/weights

Expand All @@ -28,35 +29,37 @@ use frame_support::{traits::Get, weights::Weight};
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> dutch_auction::WeightInfo for WeightInfo<T> {
// Storage: DutchAuction OrdersIndex (r:1 w:1)
// Storage: System Account (r:1 w:1)
// Storage: Timestamp Now (r:1 w:0)
// Storage: Tokens Accounts (r:1 w:1)
// Storage: DutchAuction SellOrders (r:0 w:1)
fn ask() -> Weight {
(164_004_000 as Weight)
.saturating_add(T::DbWeight::get().reads(3 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
(185_212_000 as Weight)
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().writes(4 as Weight))
}
// Storage: DutchAuction SellOrders (r:1 w:0)
// Storage: Timestamp Now (r:1 w:0)
// Storage: Tokens Accounts (r:1 w:1)
// Storage: DutchAuction Takes (r:1 w:1)
fn take(_x: u32, ) -> Weight {
(158_860_000 as Weight)
fn take() -> Weight {
(140_074_000 as Weight)
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: DutchAuction SellOrders (r:1 w:1)
// Storage: Tokens Accounts (r:1 w:1)
// Storage: System Account (r:1 w:1)
fn liquidate() -> Weight {
(105_812_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
(150_867_000 as Weight)
.saturating_add(T::DbWeight::get().reads(3 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
// Storage: DutchAuction Takes (r:2 w:1)
// Storage: DutchAuction SellOrders (r:1 w:1)
// Storage: Tokens Accounts (r:2 w:2)
fn known_overhead_for_on_finalize() -> Weight {
(287_359_000 as Weight)
(195_022_000 as Weight)
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().writes(4 as Weight))
}
Expand Down

0 comments on commit 678b6b9

Please sign in to comment.