Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark pallet asset manager #991

Merged
merged 14 commits into from
Nov 18, 2021
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[alias]
moonbase = " build --release -p moonbeam --no-default-features --features moonbase-native"
moonbase-benchmarks = " build --release -p moonbeam --no-default-features --features moonbase-runtime-benchmarks"
moonbase-rococo = " build --release -p moonbeam --no-default-features --features moonbase-native,rococo-native"
moonbase-tracing = " build --release -p moonbeam --features moonbase-native-tracing"
moonriver = " build --release -p moonbeam --no-default-features --features moonriver-native"
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ runtime-benchmarks = [
"pallet-xcm/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
]

moonbase-runtime-benchmarks = [
"moonbeam-cli/moonbase-runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
]
2 changes: 2 additions & 0 deletions node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ moonbase-native = ["service/moonbase-native", "westend-native"]
moonriver-native = ["service/moonriver-native"]
moonbeam-native = ["service/moonbeam-native"]

moonbase-runtime-benchmarks = ["service/moonbase-runtime-benchmarks"]

wasmtime = ["sc-cli/wasmtime"]
runtime-benchmarks = ["service/runtime-benchmarks"]
try-runtime = [
Expand Down
5 changes: 5 additions & 0 deletions node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,11 @@ pub fn run() -> Result<()> {
#[cfg(not(feature = "moonbase-native"))]
_ => panic!("invalid chain spec"),
}
} else if cfg!(feature = "moonbase-runtime-benchmarks") {
let runner = cli.create_runner(cmd)?;
return runner.sync_run(|config| {
cmd.run::<service::moonbase_runtime::Block, service::MoonbaseExecutor>(config)
});
} else {
Err("Benchmarking wasn't enabled when building the node. \
You can enable it with `--features runtime-benchmarks`."
Expand Down
9 changes: 9 additions & 0 deletions node/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ runtime-benchmarks = [
"moonbeam-runtime/runtime-benchmarks",
"pallet-ethereum/runtime-benchmarks"
]


moonbase-runtime-benchmarks = [
"moonbase-native",
"moonbase-runtime/runtime-benchmarks",
"moonbase-runtime/moonbase-runtime-benchmarks",
"pallet-ethereum/runtime-benchmarks"
]

try-runtime = [
"moonbase-runtime", "moonbase-runtime/try-runtime",
]
2 changes: 0 additions & 2 deletions node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,6 @@ where
prometheus_registry.as_ref(),
telemetry.as_ref().map(|x| x.handle()),
);

let commands_stream: Box<dyn Stream<Item = EngineCommand<H256>> + Send + Sync + Unpin> =
match sealing {
cli_opt::Sealing::Instant => {
Expand Down Expand Up @@ -847,7 +846,6 @@ where
}),
);
}

rpc::spawn_essential_tasks(rpc::SpawnTasksParams {
task_manager: &task_manager,
client: client.clone(),
Expand Down
6 changes: 6 additions & 0 deletions pallets/asset-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ scale-info = { version = "1.0", default-features = false, features = ["derive"]
frame-support = { git = "https://github.com/purestake/substrate", default-features = false, branch = "moonbeam-polkadot-v0.9.12" }
frame-system = { git = "https://github.com/purestake/substrate", default-features = false, branch = "moonbeam-polkadot-v0.9.12" }

frame-benchmarking = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.12", default-features = false, optional = true }

[dev-dependencies]
pallet-balances = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.12" }
sp-core = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.12" }
Expand All @@ -35,3 +37,7 @@ std = [
"xcm-primitives/std",
"scale-info/std",
]

runtime-benchmarks = [
"frame-benchmarking",
]
67 changes: 67 additions & 0 deletions pallets/asset-manager/src/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2019-2021 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam 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.

// Moonbeam 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 Moonbeam. If not, see <http://www.gnu.org/licenses/>.

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

use crate::{Call, Config, Pallet};
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};
use frame_system::RawOrigin;

benchmarks! {
register_asset {
// does not really matter what we register
let asset_type = T::AssetType::default();
let metadata = T::AssetRegistrarMetadata::default();
let amount = 1u32.into();
let asset_id: T::AssetId = asset_type.clone().into();

}: _(RawOrigin::Root, asset_type.clone(), metadata, amount)
verify {
assert_eq!(Pallet::<T>::asset_id_type(asset_id), Some(asset_type));
}

set_asset_units_per_second {
// does not really matter what we register
let asset_type = T::AssetType::default();
let metadata = T::AssetRegistrarMetadata::default();
let amount = 1u32.into();
let asset_id: T::AssetId = asset_type.clone().into();
Pallet::<T>::register_asset(RawOrigin::Root.into(), asset_type.clone(), metadata, amount)?;

}: _(RawOrigin::Root, asset_id, 1)
verify {
assert_eq!(Pallet::<T>::asset_id_units_per_second(asset_id), Some(1));
}
}

#[cfg(test)]
mod tests {
use crate::mock::Test;
use sp_io::TestExternalities;

pub fn new_test_ext() -> TestExternalities {
let t = frame_system::GenesisConfig::default()
.build_storage::<Test>()
.unwrap();
TestExternalities::new(t)
}
}

impl_benchmark_test_suite!(
Pallet,
crate::benchmarks::tests::new_test_ext(),
crate::mock::Test
);
12 changes: 9 additions & 3 deletions pallets/asset-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@

use frame_support::pallet;
pub use pallet::*;
#[cfg(any(test, feature = "runtime-benchmarks"))]
mod benchmarks;
#[cfg(test)]
pub mod mock;
#[cfg(test)]
pub mod tests;
pub mod weights;

#[pallet]
pub mod pallet {

use crate::weights::WeightInfo;
use frame_support::{pallet_prelude::*, PalletId};
use frame_system::pallet_prelude::*;
use parity_scale_codec::HasCompact;
Expand Down Expand Up @@ -83,7 +87,7 @@ pub mod pallet {
type AssetId: Member + Parameter + Default + Copy + HasCompact + MaxEncodedLen;

/// The Asset Metadata we want to store
type AssetRegistrarMetadata: Member + Parameter;
type AssetRegistrarMetadata: Member + Parameter + Default;

/// The Asset Kind.
type AssetType: Parameter + Member + Ord + PartialOrd + Into<Self::AssetId> + Default;
Expand All @@ -96,6 +100,8 @@ pub mod pallet {

/// Origin that is allowed to create and modify asset information
type AssetModifierOrigin: EnsureOrigin<Self::Origin>;

type WeightInfo: WeightInfo;
}

/// An error that can occur while executing the mapping pallet's logic.
Expand Down Expand Up @@ -127,7 +133,7 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Register new asset with the asset manager
#[pallet::weight(0)]
#[pallet::weight(T::WeightInfo::register_asset())]
pub fn register_asset(
origin: OriginFor<T>,
asset: T::AssetType,
Expand All @@ -151,7 +157,7 @@ pub mod pallet {
}

/// Change the amount of units we are charging per execution second for a given AssetId
#[pallet::weight(0)]
#[pallet::weight(T::WeightInfo::set_asset_units_per_second())]
pub fn set_asset_units_per_second(
origin: OriginFor<T>,
asset_id: T::AssetId,
Expand Down
1 change: 1 addition & 0 deletions pallets/asset-manager/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ impl Config for Test {
type AssetType = MockAssetType;
type AssetRegistrar = MockAssetPalletRegistrar;
type AssetModifierOrigin = EnsureRoot<u64>;
type WeightInfo = ();
}

pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
Expand Down
86 changes: 86 additions & 0 deletions pallets/asset-manager/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2019-2021 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam 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.

// Moonbeam 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 Moonbeam. If not, see <http://www.gnu.org/licenses/>.

//! Autogenerated weights for pallet_asset_manager
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2021-11-15, STEPS: `32`, REPEAT: 64, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128

// Executed Command:
// ./target/release/moonbeam
// benchmark
// --chain
// dev
// --execution=wasm
// --wasm-execution=compiled
// --pallet
// pallet_asset_manager
// --extrinsic
// *
// --steps
// 32
// --repeat
// 64
// --raw
// --template=./benchmarking/frame-weight-template.hbs
// --output
// /tmp/
// --record-proof

#![allow(unused_parens)]
#![allow(unused_imports)]

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

/// Weight functions needed for pallet_asset_manager.
pub trait WeightInfo {
fn register_asset() -> Weight;
fn set_asset_units_per_second() -> Weight;
}

/// Weights for pallet_asset_manager using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
fn register_asset() -> Weight {
(52_572_000 as Weight)
.saturating_add(T::DbWeight::get().reads(7 as Weight))
.saturating_add(T::DbWeight::get().writes(5 as Weight))
}
fn set_asset_units_per_second() -> Weight {
(22_860_000 as Weight)
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
}

// For backwards compatibility and tests
impl WeightInfo for () {
fn register_asset() -> Weight {
(52_572_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(7 as Weight))
.saturating_add(RocksDbWeight::get().writes(5 as Weight))
}
fn set_asset_units_per_second() -> Weight {
(22_860_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(5 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
}
2 changes: 2 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sp-std = { git = "https://github.com/purestake/substrate", branch = "moonbeam-po
frame-support = { git = "https://github.com/purestake/substrate", default-features = false, branch = "moonbeam-polkadot-v0.9.12" }
pallet-author-mapping = { path = "../../pallets/author-mapping", default-features = false }
parachain-staking = { path = "../../pallets/parachain-staking", default-features = false }
pallet-asset-manager = { path = "../../pallets/asset-manager", default-features = false, optional = true }
pallet-collective = { git = "https://github.com/purestake/substrate", default-features = false, branch = "moonbeam-polkadot-v0.9.12" }
frame-system = { git = "https://github.com/purestake/substrate", default-features = false, branch = "moonbeam-polkadot-v0.9.12" }
log = "0.4"
Expand All @@ -26,4 +27,5 @@ std = [
"frame-support/std",
"pallet-author-mapping/std",
"parachain-staking/std",
"pallet-asset-manager/std"
]
15 changes: 15 additions & 0 deletions runtime/common/src/apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,17 @@ macro_rules! impl_runtime_apis_plus_common {
use pallet_crowdloan_rewards::Pallet as PalletCrowdloanRewardsBench;
use parachain_staking::Pallet as ParachainStakingBench;
use pallet_author_mapping::Pallet as PalletAuthorMappingBench;
#[cfg(feature = "moonbase-runtime-benchmarks")]
use pallet_asset_manager::Pallet as PalletAssetManagerBench;

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

list_benchmark!(list, extra, frame_system, SystemBench::<Runtime>);
list_benchmark!(list, extra, parachain_staking, ParachainStakingBench::<Runtime>);
list_benchmark!(list, extra, pallet_crowdloan_rewards, PalletCrowdloanRewardsBench::<Runtime>);
list_benchmark!(list, extra, pallet_author_mapping, PalletAuthorMappingBench::<Runtime>);
#[cfg(feature = "moonbase-runtime-benchmarks")]
list_benchmark!(list, extra, pallet_asset_manager, PalletAssetManagerBench::<Runtime>);

let storage_info = AllPalletsWithSystem::storage_info();

Expand All @@ -426,6 +430,10 @@ macro_rules! impl_runtime_apis_plus_common {
use pallet_crowdloan_rewards::Pallet as PalletCrowdloanRewardsBench;
use parachain_staking::Pallet as ParachainStakingBench;
use pallet_author_mapping::Pallet as PalletAuthorMappingBench;
#[cfg(feature = "moonbase-runtime-benchmarks")]
use pallet_asset_manager::Pallet as PalletAssetManagerBench;


let whitelist: Vec<TrackedStorageKey> = vec![];

let mut batches = Vec::<BenchmarkBatch>::new();
Expand All @@ -450,6 +458,13 @@ macro_rules! impl_runtime_apis_plus_common {
PalletAuthorMappingBench::<Runtime>
);
add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
#[cfg(feature = "moonbase-runtime-benchmarks")]
add_benchmark!(
params,
batches,
pallet_asset_manager,
PalletAssetManagerBench::<Runtime>
);

if batches.is_empty() {
return Err("Benchmark not found for this pallet.".into());
Expand Down
2 changes: 2 additions & 0 deletions runtime/moonbase/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ runtime-benchmarks = [
"xcm-builder/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"pallet-ethereum/runtime-benchmarks",
"pallet-asset-manager/runtime-benchmarks",
]
moonbase-runtime-benchmarks = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this imply runtime-benchmarks so you don't need multiple feature flags to use it? You would probably need to do that in a lot of pallets or create an alias in .cargo/config.toml, so maybe not...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try

Copy link
Collaborator Author

@girazoki girazoki Nov 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main issue here is that I need to propagate the flag at least till runtime-common, which is the common place where we put our benchmark apis. And since runtime-common is common for all runtimes, I need to have a special flag to incorportate the benchmarks for pallets belonging to moonbase only.

The way I designed it currently is that if you build with moonbase-runtime-bechmarks, it will build with runtime-benchmarks & moonbase-runtime-benchmarks just the moonbase runtime. From here there are two possibilities:

  • Leave it as is, where building with features moonbase-runtime-bechmarks compile the moonbase runtime with both runtime-benchmarks and moonbase-runtime-benchmarks
  • Make it so that we only have one flag in moonbase called moonbase-runtime-benchmarks, and do something like moonbase-runtime-benchmarks = ["frame-support/runtime-benchmarks", "pallet-society/runtime-benchmarks", etc..]"

Quite honestly I'd rather not be too invasive with this PR, and not change much how things worked. I think this approach is the lesser invasive as things worked as before, but we have a new flag with which we can run the just moonbase benchmarks

try-runtime = [
"frame-try-runtime",
"frame-executive/try-runtime",
Expand Down
Loading