From 9fae525b21f85421a02f3566069274c4b57a6204 Mon Sep 17 00:00:00 2001 From: Raid5594 <52794079+Raid5594@users.noreply.github.com> Date: Thu, 23 Nov 2023 19:27:49 +0100 Subject: [PATCH] benchmarking & weights for ddc-nodes (#143) 1. add benchmarking 2. add weights --- Cargo.lock | 1 + pallets/ddc-clusters/src/mock.rs | 1 + pallets/ddc-nodes/Cargo.toml | 9 +++ pallets/ddc-nodes/src/benchmarking.rs | 55 +++++++++++++++++ pallets/ddc-nodes/src/lib.rs | 15 ++++- pallets/ddc-nodes/src/mock.rs | 1 + pallets/ddc-nodes/src/testing_utils.rs | 31 ++++++++++ pallets/ddc-nodes/src/weights.rs | 83 ++++++++++++++++++++++++++ runtime/cere-dev/Cargo.toml | 2 + runtime/cere-dev/src/lib.rs | 2 + 10 files changed, 197 insertions(+), 3 deletions(-) create mode 100644 pallets/ddc-nodes/src/benchmarking.rs create mode 100644 pallets/ddc-nodes/src/testing_utils.rs create mode 100644 pallets/ddc-nodes/src/weights.rs diff --git a/Cargo.lock b/Cargo.lock index 23c3fb341..6b341a929 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4791,6 +4791,7 @@ version = "4.8.1" dependencies = [ "ddc-primitives", "ddc-traits", + "frame-benchmarking", "frame-support", "frame-system", "pallet-balances", diff --git a/pallets/ddc-clusters/src/mock.rs b/pallets/ddc-clusters/src/mock.rs index a69fc350c..efbea68fb 100644 --- a/pallets/ddc-clusters/src/mock.rs +++ b/pallets/ddc-clusters/src/mock.rs @@ -181,6 +181,7 @@ impl pallet_timestamp::Config for Test { impl pallet_ddc_nodes::Config for Test { type RuntimeEvent = RuntimeEvent; type StakingVisitor = TestStakingVisitor; + type WeightInfo = (); } impl crate::pallet::Config for Test { diff --git a/pallets/ddc-nodes/Cargo.toml b/pallets/ddc-nodes/Cargo.toml index ed3d6de36..25afc226f 100644 --- a/pallets/ddc-nodes/Cargo.toml +++ b/pallets/ddc-nodes/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"] } ddc-primitives = { version = "0.1.0", default-features = false, path = "../../primitives" } ddc-traits = { version = "0.1.0", default-features = false, path = "../../traits" } +frame-benchmarking = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.31", optional = true } frame-support = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.31" } frame-system = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.31" } scale-info = { version = "2.1.2", default-features = false, features = ["derive"] } @@ -26,6 +27,7 @@ default = ["std"] std = [ "codec/std", "ddc-primitives/std", + "frame-benchmarking/std", "frame-support/std", "frame-system/std", "scale-info/std", @@ -33,3 +35,10 @@ std = [ "sp-std/std", "sp-core/std", ] +runtime-benchmarks = [ + "ddc-primitives/runtime-benchmarks", + "frame-benchmarking/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", +] diff --git a/pallets/ddc-nodes/src/benchmarking.rs b/pallets/ddc-nodes/src/benchmarking.rs new file mode 100644 index 000000000..2a9b2d9e9 --- /dev/null +++ b/pallets/ddc-nodes/src/benchmarking.rs @@ -0,0 +1,55 @@ +//! DdcStaking pallet benchmarking. + +use super::*; +use crate::{cdn_node::CDNNodeProps, Pallet as DdcNodes}; +use ddc_primitives::CDNNodePubKey; +use testing_utils::*; + +use sp_std::prelude::*; + +pub use frame_benchmarking::{ + account, benchmarks, impl_benchmark_test_suite, whitelist_account, whitelisted_caller, +}; +use frame_system::RawOrigin; + +const USER_SEED: u32 = 999666; + +benchmarks! { + create_node { + let (user, node, cdn_node_params, _) = create_user_and_config::("user", USER_SEED); + + whitelist_account!(user); + }: _(RawOrigin::Signed(user.clone()), node, cdn_node_params) + verify { + assert!(CDNNodes::::contains_key(CDNNodePubKey::new([0; 32]))); + } + + delete_node { + let (user, node, cdn_node_params, _) = create_user_and_config::("user", USER_SEED); + + DdcNodes::::create_node(RawOrigin::Signed(user.clone()).into(),node.clone(), cdn_node_params)?; + + whitelist_account!(user); + }: _(RawOrigin::Signed(user.clone()), node) + verify { + assert!(!CDNNodes::::contains_key(CDNNodePubKey::new([0; 32]))); + } + + set_node_params { + let (user, node, cdn_node_params, cdn_node_params_new) = create_user_and_config::("user", USER_SEED); + + DdcNodes::::create_node(RawOrigin::Signed(user.clone()).into(),node.clone(), cdn_node_params)?; + + whitelist_account!(user); + }: _(RawOrigin::Signed(user.clone()), node, cdn_node_params_new) + verify { + assert_eq!(CDNNodes::::try_get( + CDNNodePubKey::new([0; 32])).unwrap().props, + CDNNodeProps { + host: vec![2u8, 255].try_into().unwrap(), + http_port: 45000u16, + grpc_port: 55000u16, + p2p_port: 65000u16, + }); + } +} diff --git a/pallets/ddc-nodes/src/lib.rs b/pallets/ddc-nodes/src/lib.rs index 4cea30ea7..59b40e8ed 100644 --- a/pallets/ddc-nodes/src/lib.rs +++ b/pallets/ddc-nodes/src/lib.rs @@ -19,6 +19,14 @@ pub(crate) mod mock; #[cfg(test)] mod tests; +pub mod weights; +use crate::weights::WeightInfo; + +#[cfg(feature = "runtime-benchmarks")] +pub mod benchmarking; +#[cfg(any(feature = "runtime-benchmarks", test))] +pub mod testing_utils; + use ddc_primitives::{CDNNodePubKey, ClusterId, NodePubKey, StorageNodePubKey}; use ddc_traits::{ node::{NodeVisitor, NodeVisitorError}, @@ -53,6 +61,7 @@ pub mod pallet { pub trait Config: frame_system::Config { type RuntimeEvent: From> + IsType<::RuntimeEvent>; type StakingVisitor: StakingVisitor; + type WeightInfo: WeightInfo; } #[pallet::event] @@ -87,7 +96,7 @@ pub mod pallet { #[pallet::call] impl Pallet { - #[pallet::weight(10_000)] + #[pallet::weight(T::WeightInfo::create_node())] pub fn create_node( origin: OriginFor, node_pub_key: NodePubKey, @@ -101,7 +110,7 @@ pub mod pallet { Ok(()) } - #[pallet::weight(10_000)] + #[pallet::weight(T::WeightInfo::delete_node())] pub fn delete_node(origin: OriginFor, node_pub_key: NodePubKey) -> DispatchResult { let caller_id = ensure_signed(origin)?; let node = Self::get(node_pub_key.clone()).map_err(Into::>::into)?; @@ -114,7 +123,7 @@ pub mod pallet { Ok(()) } - #[pallet::weight(10_000)] + #[pallet::weight(T::WeightInfo::set_node_params())] pub fn set_node_params( origin: OriginFor, node_pub_key: NodePubKey, diff --git a/pallets/ddc-nodes/src/mock.rs b/pallets/ddc-nodes/src/mock.rs index 30cf49869..419fab3d0 100644 --- a/pallets/ddc-nodes/src/mock.rs +++ b/pallets/ddc-nodes/src/mock.rs @@ -92,6 +92,7 @@ impl pallet_timestamp::Config for Test { impl crate::pallet::Config for Test { type RuntimeEvent = RuntimeEvent; type StakingVisitor = TestStakingVisitor; + type WeightInfo = (); } pub struct TestStakingVisitor; diff --git a/pallets/ddc-nodes/src/testing_utils.rs b/pallets/ddc-nodes/src/testing_utils.rs new file mode 100644 index 000000000..432107e61 --- /dev/null +++ b/pallets/ddc-nodes/src/testing_utils.rs @@ -0,0 +1,31 @@ +//! Testing utils for ddc-staking. + +use crate::{cdn_node::CDNNodeParams, node::NodeParams, Config, NodePubKey}; +use ddc_primitives::CDNNodePubKey; +use frame_benchmarking::account; +use sp_std::vec; + +const SEED: u32 = 0; + +/// Grab a funded user. +pub fn create_user_and_config( + string: &'static str, + n: u32, +) -> (T::AccountId, NodePubKey, NodeParams, NodeParams) { + let user = account(string, n, SEED); + let node = NodePubKey::CDNPubKey(CDNNodePubKey::new([0; 32])); + let cdn_node_params = NodeParams::CDNParams(CDNNodeParams { + host: vec![1u8, 255], + http_port: 35000u16, + grpc_port: 25000u16, + p2p_port: 15000u16, + }); + + let cdn_node_params_new = NodeParams::CDNParams(CDNNodeParams { + host: vec![2u8, 255], + http_port: 45000u16, + grpc_port: 55000u16, + p2p_port: 65000u16, + }); + (user, node, cdn_node_params, cdn_node_params_new) +} diff --git a/pallets/ddc-nodes/src/weights.rs b/pallets/ddc-nodes/src/weights.rs new file mode 100644 index 000000000..868380f40 --- /dev/null +++ b/pallets/ddc-nodes/src/weights.rs @@ -0,0 +1,83 @@ + +//! Autogenerated weights for `pallet_ddc_nodes` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-11-16, STEPS: `200`, REPEAT: 1000, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! HOSTNAME: `Raids-MBP-2`, CPU: `` +//! EXECUTION: None, WASM-EXECUTION: Interpreted, CHAIN: Some("dev"), DB CACHE: 1024 + +// Executed Command: +// ./target/release/cere +// benchmark +// pallet +// --chain +// dev +// --pallet +// pallet_ddc_nodes +// --extrinsic +// * +// --steps +// 200 +// --repeat +// 1000 +// --output +// pallets/ddc-nodes/src/weights.rs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] + +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use sp_std::marker::PhantomData; + +/// Weight functions needed for pallet_ddc_nodes. +pub trait WeightInfo { + fn create_node() -> Weight; + fn delete_node() -> Weight; + fn set_node_params() -> Weight; +} + +/// Weights for pallet_ddc_nodes. +pub struct SubstrateWeight(PhantomData); +impl WeightInfo for SubstrateWeight { + // Storage: DdcNodes CDNNodes (r:1 w:1) + fn create_node() -> Weight { + Weight::from_ref_time(12_000_000u64) + .saturating_add(T::DbWeight::get().reads(1u64)) + .saturating_add(T::DbWeight::get().writes(1u64)) + } + // Storage: DdcNodes CDNNodes (r:1 w:1) + fn delete_node() -> Weight { + Weight::from_ref_time(14_000_000u64) + .saturating_add(T::DbWeight::get().reads(1u64)) + .saturating_add(T::DbWeight::get().writes(1u64)) + } + // Storage: DdcNodes CDNNodes (r:1 w:1) + fn set_node_params() -> Weight { + Weight::from_ref_time(15_000_000u64) + .saturating_add(T::DbWeight::get().reads(1u64)) + .saturating_add(T::DbWeight::get().writes(1u64)) + } +} + +// For backwards compatibility and tests +impl WeightInfo for () { + // Storage: DdcNodes CDNNodes (r:1 w:1) + fn create_node() -> Weight { + Weight::from_ref_time(12_000_000u64) + .saturating_add(RocksDbWeight::get().reads(1u64)) + .saturating_add(RocksDbWeight::get().writes(1u64)) + } + // Storage: DdcNodes CDNNodes (r:1 w:1) + fn delete_node() -> Weight { + Weight::from_ref_time(14_000_000u64) + .saturating_add(RocksDbWeight::get().reads(1u64)) + .saturating_add(RocksDbWeight::get().writes(1u64)) + } + // Storage: DdcNodes CDNNodes (r:1 w:1) + fn set_node_params() -> Weight { + Weight::from_ref_time(15_000_000u64) + .saturating_add(RocksDbWeight::get().reads(1u64)) + .saturating_add(RocksDbWeight::get().writes(1u64)) + } +} \ No newline at end of file diff --git a/runtime/cere-dev/Cargo.toml b/runtime/cere-dev/Cargo.toml index fda50db6e..d38cefcc1 100644 --- a/runtime/cere-dev/Cargo.toml +++ b/runtime/cere-dev/Cargo.toml @@ -176,6 +176,7 @@ std = [ "pallet-child-bounties/std", "pallet-ddc-metrics-offchain-worker/std", "pallet-ddc-payouts/std", + "pallet-ddc-nodes/std", "pallet-ddc-staking/std", "cere-runtime-common/std", "cere-dev-runtime-constants/std", @@ -215,6 +216,7 @@ runtime-benchmarks = [ "pallet-society/runtime-benchmarks", "pallet-staking/runtime-benchmarks", "pallet-ddc-customers/runtime-benchmarks", + "pallet-ddc-nodes/runtime-benchmarks", "pallet-ddc-staking/runtime-benchmarks", "pallet-timestamp/runtime-benchmarks", "pallet-tips/runtime-benchmarks", diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index b64b9aaf2..8d5ea40bc 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -1350,6 +1350,7 @@ impl pallet_ddc_customers::Config for Runtime { impl pallet_ddc_nodes::Config for Runtime { type RuntimeEvent = RuntimeEvent; type StakingVisitor = pallet_ddc_staking::Pallet; + type WeightInfo = pallet_ddc_nodes::weights::SubstrateWeight; } impl pallet_ddc_clusters::Config for Runtime { @@ -1532,6 +1533,7 @@ mod benches { [pallet_staking, Staking] [pallet_ddc_customers, DdcCustomers] [pallet_ddc_staking, DdcStaking] + [pallet_ddc_nodes, DdcNodes] [frame_system, SystemBench::] [pallet_timestamp, Timestamp] [pallet_tips, Tips]