Skip to content

Commit

Permalink
benchmarking & weights for ddc-nodes (#143)
Browse files Browse the repository at this point in the history
1. add benchmarking
2. add weights
  • Loading branch information
Raid5594 authored Nov 23, 2023
1 parent 85ac950 commit 9fae525
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 3 deletions.
1 change: 1 addition & 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 pallets/ddc-clusters/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions pallets/ddc-nodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -26,10 +27,18 @@ default = ["std"]
std = [
"codec/std",
"ddc-primitives/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-runtime/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",
]
55 changes: 55 additions & 0 deletions pallets/ddc-nodes/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -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::<T>("user", USER_SEED);

whitelist_account!(user);
}: _(RawOrigin::Signed(user.clone()), node, cdn_node_params)
verify {
assert!(CDNNodes::<T>::contains_key(CDNNodePubKey::new([0; 32])));
}

delete_node {
let (user, node, cdn_node_params, _) = create_user_and_config::<T>("user", USER_SEED);

DdcNodes::<T>::create_node(RawOrigin::Signed(user.clone()).into(),node.clone(), cdn_node_params)?;

whitelist_account!(user);
}: _(RawOrigin::Signed(user.clone()), node)
verify {
assert!(!CDNNodes::<T>::contains_key(CDNNodePubKey::new([0; 32])));
}

set_node_params {
let (user, node, cdn_node_params, cdn_node_params_new) = create_user_and_config::<T>("user", USER_SEED);

DdcNodes::<T>::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::<T>::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,
});
}
}
15 changes: 12 additions & 3 deletions pallets/ddc-nodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -53,6 +61,7 @@ pub mod pallet {
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type StakingVisitor: StakingVisitor<Self>;
type WeightInfo: WeightInfo;
}

#[pallet::event]
Expand Down Expand Up @@ -87,7 +96,7 @@ pub mod pallet {

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(10_000)]
#[pallet::weight(T::WeightInfo::create_node())]
pub fn create_node(
origin: OriginFor<T>,
node_pub_key: NodePubKey,
Expand All @@ -101,7 +110,7 @@ pub mod pallet {
Ok(())
}

#[pallet::weight(10_000)]
#[pallet::weight(T::WeightInfo::delete_node())]
pub fn delete_node(origin: OriginFor<T>, node_pub_key: NodePubKey) -> DispatchResult {
let caller_id = ensure_signed(origin)?;
let node = Self::get(node_pub_key.clone()).map_err(Into::<Error<T>>::into)?;
Expand All @@ -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<T>,
node_pub_key: NodePubKey,
Expand Down
1 change: 1 addition & 0 deletions pallets/ddc-nodes/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions pallets/ddc-nodes/src/testing_utils.rs
Original file line number Diff line number Diff line change
@@ -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<T: 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)
}
83 changes: 83 additions & 0 deletions pallets/ddc-nodes/src/weights.rs
Original file line number Diff line number Diff line change
@@ -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: `<UNKNOWN>`
//! 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<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// 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))
}
}
2 changes: 2 additions & 0 deletions runtime/cere-dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions runtime/cere-dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Runtime>;
type WeightInfo = pallet_ddc_nodes::weights::SubstrateWeight<Runtime>;
}

impl pallet_ddc_clusters::Config for Runtime {
Expand Down Expand Up @@ -1532,6 +1533,7 @@ mod benches {
[pallet_staking, Staking]
[pallet_ddc_customers, DdcCustomers]
[pallet_ddc_staking, DdcStaking]
[pallet_ddc_nodes, DdcNodes]
[frame_system, SystemBench::<Runtime>]
[pallet_timestamp, Timestamp]
[pallet_tips, Tips]
Expand Down

0 comments on commit 9fae525

Please sign in to comment.