Skip to content

Commit

Permalink
Rich benchmarkings (#396)
Browse files Browse the repository at this point in the history
* orml-currencies benchmarks added

* orml-tokens benchmarks added
  • Loading branch information
herryho committed Nov 9, 2021
1 parent fdf1f2c commit d845a25
Show file tree
Hide file tree
Showing 18 changed files with 750 additions and 28 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,4 @@ orml-xtokens = { git = "https://github.com/open-web3-stack/open-runtime-module-l
orml-unknown-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "5ba42f532b72d71a28aaa18421855de003b9ca71" }
orml-xcm = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "5ba42f532b72d71a28aaa18421855de003b9ca71" }
orml-xcm-support = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "5ba42f532b72d71a28aaa18421855de003b9ca71" }
orml-benchmarking = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "5ba42f532b72d71a28aaa18421855de003b9ca71" }
3 changes: 3 additions & 0 deletions runtime/asgard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ orml-xtokens = { version = "0.4.1-dev", default-features = false }
orml-unknown-tokens = { version = "0.4.1-dev", default-features = false }
orml-xcm = { version = "0.4.1-dev", default-features = false }
orml-xcm-support = { version = "0.4.1-dev", default-features = false }
orml-benchmarking = { version = "0.4.1-dev", default-features = false, optional = true }

zenlink-protocol = { version = "*", default-features = false }
zenlink-protocol-runtime-api = { version = "*", default-features = false }
Expand Down Expand Up @@ -206,6 +207,7 @@ std = [
"orml-unknown-tokens/std",
"orml-xcm/std",
"orml-xcm-support/std",
"orml-benchmarking/std",
"zenlink-protocol/std",
"zenlink-protocol-runtime-api/std",
]
Expand All @@ -229,6 +231,7 @@ runtime-benchmarks = [
"bifrost-token-issuer/runtime-benchmarks",
"bifrost-lightening-redeem/runtime-benchmarks",
"bifrost-call-switchgear/runtime-benchmarks",
"orml-benchmarking"
]

try-runtime = [
Expand Down
128 changes: 128 additions & 0 deletions runtime/asgard/src/benchmarking/currencies.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// This file is part of Bifrost.

// Copyright (C) 2019-2021 Liebi Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![cfg(feature = "runtime-benchmarks")]

use frame_benchmarking::{account, whitelisted_caller};
use frame_support::assert_ok;
use frame_system::RawOrigin;
use orml_benchmarking::runtime_benchmarks;
use orml_traits::MultiCurrencyExtended;
use sp_runtime::{
traits::{StaticLookup, UniqueSaturatedInto},
SaturatedConversion,
};
use sp_std::prelude::*;

use crate::{
AccountId, Amount, Balance, Currencies, CurrencyId, ExistentialDeposit, NativeCurrencyId,
Runtime, TokenSymbol,
};

const SEED: u32 = 0;
const NATIVE: CurrencyId = NativeCurrencyId::get();

pub fn set_balance(currency_id: CurrencyId, who: &AccountId, balance: Balance) {
assert_ok!(<Currencies as MultiCurrencyExtended<_>>::update_balance(
currency_id,
who,
balance.saturated_into()
));
}

runtime_benchmarks! {
{ Runtime, orml_currencies }

// `transfer` non-native currency
transfer_non_native_currency {
let currency_id = CurrencyId::Token(TokenSymbol::KSM);
let amount: Balance = 100u32.into();
let from: AccountId = whitelisted_caller();
set_balance(currency_id, &from, amount);

let receiver: AccountId = account("bechmarking_account_1", 0, 0);
let recipient = <Runtime as frame_system::Config>::Lookup::unlookup(receiver.clone());
}: transfer(RawOrigin::Signed(from), recipient, currency_id, amount)


// `transfer` native currency and in worst case
#[extra]
transfer_native_currency_worst_case {
let existential_deposit = ExistentialDeposit::get();
let amount: Balance = existential_deposit.saturating_mul(1000);
let from: AccountId = whitelisted_caller();
set_balance(NATIVE, &from, amount);

let to: AccountId = account("to", 0, SEED);
let to_lookup = <Runtime as frame_system::Config>::Lookup::unlookup(to.clone());

}: transfer(RawOrigin::Signed(from), to_lookup, NATIVE, amount)

// `transfer_native_currency` in worst case
// * will create the `to` account.
// * will kill the `from` account.
transfer_native_currency {
let existential_deposit = ExistentialDeposit::get();
let amount: Balance = existential_deposit.saturating_mul(1000);
let from: AccountId = whitelisted_caller();
set_balance(NATIVE, &from, amount);

let to: AccountId = account("to", 0, SEED);
let to_lookup = <Runtime as frame_system::Config>::Lookup::unlookup(to.clone());
}: _(RawOrigin::Signed(from), to_lookup, amount)

// `update_balance` for non-native currency
update_balance_non_native_currency {
let currency_id = CurrencyId::Token(TokenSymbol::KSM);
let balance: Balance = 1_000_000_000_000u128.into();
let amount: Amount = balance.unique_saturated_into();
let who: AccountId = account("who", 0, SEED);
let who_lookup = <Runtime as frame_system::Config>::Lookup::unlookup(who.clone());
}: update_balance(RawOrigin::Root, who_lookup, currency_id, amount)

// `update_balance` for native currency
// * will create the `who` account.
update_balance_native_currency_creating {
let existential_deposit = ExistentialDeposit::get();
let balance: Balance = existential_deposit.saturating_mul(1000);
let amount: Amount = balance.unique_saturated_into();
let who: AccountId = account("who", 0, SEED);
let who_lookup = <Runtime as frame_system::Config>::Lookup::unlookup(who.clone());
}: update_balance(RawOrigin::Root, who_lookup, NATIVE, amount)

// `update_balance` for native currency
// * will kill the `who` account.
update_balance_native_currency_killing {
let existential_deposit = ExistentialDeposit::get();
let balance: Balance = existential_deposit.saturating_mul(1000);
let amount: Amount = balance.unique_saturated_into();
let who: AccountId = account("who", 0, SEED);
let who_lookup = <Runtime as frame_system::Config>::Lookup::unlookup(who.clone());
set_balance(NATIVE, &who, balance);
}: update_balance(RawOrigin::Root, who_lookup, NATIVE, -amount)
}

#[cfg(test)]
mod tests {
use orml_benchmarking::impl_benchmark_test_suite;

use super::*;
use crate::benchmarking::utils::tests::new_test_ext;

impl_benchmark_test_suite!(new_test_ext(),);
}
20 changes: 20 additions & 0 deletions runtime/asgard/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This file is part of Bifrost.

// Copyright (C) 2019-2021 Liebi Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

pub mod currencies;
pub mod tokens;
105 changes: 105 additions & 0 deletions runtime/asgard/src/benchmarking/tokens.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// This file is part of Bifrost.

// Copyright (C) 2019-2021 Liebi Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![cfg(feature = "runtime-benchmarks")]

use frame_benchmarking::{account, whitelisted_caller};
use frame_support::assert_ok;
use frame_system::RawOrigin;
use orml_benchmarking::runtime_benchmarks;
use orml_traits::MultiCurrencyExtended;
use sp_runtime::{
traits::{StaticLookup, UniqueSaturatedInto},
SaturatedConversion,
};
use sp_std::prelude::*;

use crate::{
AccountId, Amount, Balance, Currencies, CurrencyId, ExistentialDeposit, Runtime,
StableCurrencyId, TokenSymbol,
};

const SEED: u32 = 0;
const STABLECOIN: CurrencyId = StableCurrencyId::get();

pub fn update_balance(currency_id: CurrencyId, who: &AccountId, balance: Balance) {
assert_ok!(<Currencies as MultiCurrencyExtended<_>>::update_balance(
currency_id,
who,
balance.saturated_into()
));
}

runtime_benchmarks! {
{ Runtime, orml_tokens }

transfer {
let amount: Balance = 1_000_000_000_000u128;
let from: AccountId = whitelisted_caller();
update_balance(STABLECOIN, &from, amount);

let to: AccountId = account("to", 0, SEED);
let to_lookup = <Runtime as frame_system::Config>::Lookup::unlookup(to.clone());
}: _(RawOrigin::Signed(from), to_lookup, STABLECOIN, amount)

transfer_all {
let amount: Balance = 1_000_000_000_000u128;

let from: AccountId = whitelisted_caller();
update_balance(STABLECOIN, &from, amount);

let to: AccountId = account("to", 0, SEED);
let to_lookup = <Runtime as frame_system::Config>::Lookup::unlookup(to.clone());
}: _(RawOrigin::Signed(from.clone()), to_lookup, STABLECOIN, false)

transfer_keep_alive {
let balance: Balance = 1_000_000_000_000u128;
let from: AccountId = whitelisted_caller();
update_balance(STABLECOIN, &from, balance * 2);

let to: AccountId = account("to", 0, SEED);
let to_lookup = <Runtime as frame_system::Config>::Lookup::unlookup(to.clone());
}: _(RawOrigin::Signed(from), to_lookup, STABLECOIN, balance)

force_transfer {
let balance: Balance = 1_000_000_000_000u128;
let from: AccountId = account("from", 0, SEED);
let from_lookup = <Runtime as frame_system::Config>::Lookup::unlookup(from.clone());
update_balance(STABLECOIN, &from, 2 * balance);

let to: AccountId = account("to", 0, SEED);
let to_lookup = <Runtime as frame_system::Config>::Lookup::unlookup(to.clone());
}: _(RawOrigin::Root, from_lookup, to_lookup, STABLECOIN, balance)

set_balance {
let balance: Balance = 1_000_000_000_000u128;
let who: AccountId = account("who", 0, SEED);
let who_lookup = <Runtime as frame_system::Config>::Lookup::unlookup(who.clone());

}: _(RawOrigin::Root, who_lookup, STABLECOIN, balance, balance)
}

#[cfg(test)]
mod tests {
use orml_benchmarking::impl_benchmark_test_suite;

use super::*;
use crate::benchmarking::utils::tests::new_test_ext;

impl_benchmark_test_suite!(new_test_ext(),);
}
14 changes: 12 additions & 2 deletions runtime/asgard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ use zenlink_protocol::{
MultiAssetsHandler, PairInfo, ZenlinkMultiAssets,
};

mod benchmarking;
mod weights;

pub type SessionHandlers = ();
Expand Down Expand Up @@ -1106,7 +1107,7 @@ impl orml_currencies::Config for Runtime {
type GetNativeCurrencyId = NativeCurrencyId;
type MultiCurrency = Tokens;
type NativeCurrency = BasicCurrencyAdapter<Runtime, Balances, Amount, BlockNumber>;
type WeightInfo = ();
type WeightInfo = weights::orml_currencies::WeightInfo<Runtime>;
}

orml_traits::parameter_type_with_key! {
Expand Down Expand Up @@ -1151,7 +1152,7 @@ impl orml_tokens::Config for Runtime {
type ExistentialDeposits = ExistentialDeposits;
type MaxLocks = MaxLocks;
type OnDust = orml_tokens::TransferDust<Runtime, BifrostTreasuryAccount>;
type WeightInfo = ();
type WeightInfo = weights::orml_tokens::WeightInfo<Runtime>;
}

parameter_types! {
Expand Down Expand Up @@ -1919,6 +1920,8 @@ impl_runtime_apis! {
) {
use frame_support::traits::StorageInfoTrait;
use frame_benchmarking::{list_benchmark, Benchmarking, BenchmarkList};
use orml_benchmarking::list_benchmark as orml_list_benchmark;

let mut list = Vec::<BenchmarkList>::new();

list_benchmark!(list, extra, bifrost_salp, Salp);
Expand All @@ -1932,6 +1935,9 @@ impl_runtime_apis! {
list_benchmark!(list, extra, bifrost_lightening_redeem, LighteningRedeem);
list_benchmark!(list, extra, bifrost_call_switchgear, CallSwitchgear);

orml_list_benchmark!(list, extra, orml_currencies, benchmarking::currencies);
orml_list_benchmark!(list, extra, orml_tokens, benchmarking::tokens);

let storage_info = AllPalletsWithSystem::storage_info();

return (list, storage_info)
Expand All @@ -1941,6 +1947,7 @@ impl_runtime_apis! {
config: frame_benchmarking::BenchmarkConfig
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, RuntimeString> {
use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey};
use orml_benchmarking::{add_benchmark as orml_add_benchmark};

let whitelist: Vec<TrackedStorageKey> = vec![
// Block Number
Expand Down Expand Up @@ -1972,6 +1979,9 @@ impl_runtime_apis! {
add_benchmark!(params, batches, bifrost_lightening_redeem, LighteningRedeem);
add_benchmark!(params, batches, bifrost_call_switchgear, CallSwitchgear);

orml_add_benchmark!(params, batches, orml_currencies, benchmarking::currencies);
orml_add_benchmark!(params, batches, orml_tokens, benchmarking::tokens);

if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
Ok(batches)
}
Expand Down
2 changes: 2 additions & 0 deletions runtime/asgard/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ pub mod bifrost_salp_lite;
pub mod bifrost_token_issuer;
pub mod bifrost_vsbond_auction;
pub mod bifrost_vtoken_mint;
pub mod orml_currencies;
pub mod orml_tokens;
pub mod pallet_vesting;
Loading

0 comments on commit d845a25

Please sign in to comment.