Skip to content

Commit

Permalink
benchmarking & weights for ddc-nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Raid5594 committed Nov 16, 2023
1 parent 4906924 commit 36d18bf
Show file tree
Hide file tree
Showing 7 changed files with 154 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.

4 changes: 4 additions & 0 deletions pallets/ddc-nodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ 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 = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30", optional = true }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }

scale-info = { version = "2.1.2", default-features = false, features = ["derive"] }
sp-runtime = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
sp-std = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
Expand All @@ -23,10 +25,12 @@ 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 = ["frame-benchmarking/runtime-benchmarks"]
74 changes: 74 additions & 0 deletions pallets/ddc-nodes/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//! DdcStaking pallet benchmarking.

use super::*;
use crate::{cdn_node::CDNNodeParams, node::NodeParams, Pallet as DdcNodes};
use ddc_primitives::CDNNodePubKey;

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: T::AccountId = account("user", USER_SEED, 0u32);
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,
});

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: T::AccountId = account("user", USER_SEED, 0u32);
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,
});

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: T::AccountId = account("user", USER_SEED, 0u32);
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,
});
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 {
}
}
13 changes: 10 additions & 3 deletions pallets/ddc-nodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![recursion_limit = "256"]

pub mod weights;
use crate::weights::WeightInfo;

#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;

use ddc_primitives::{CDNNodePubKey, ClusterId, NodePubKey, StorageNodePubKey};
use ddc_traits::node::{NodeVisitor, NodeVisitorError};
use frame_support::pallet_prelude::*;
Expand Down Expand Up @@ -43,6 +49,7 @@ pub mod pallet {
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type WeightInfo: WeightInfo;
}

#[pallet::event]
Expand Down Expand Up @@ -76,7 +83,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 @@ -90,7 +97,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 @@ -101,7 +108,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
61 changes: 61 additions & 0 deletions pallets/ddc-nodes/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

//! 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};
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))
}
}
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 = [
"sp-io/std",
"pallet-child-bounties/std",
"pallet-ddc-metrics-offchain-worker/std",
"pallet-ddc-nodes/std",
"pallet-ddc-staking/std",
"cere-runtime-common/std",
"cere-dev-runtime-constants/std",
Expand Down Expand Up @@ -214,6 +215,7 @@ runtime-benchmarks = [
"pallet-session-benchmarking/runtime-benchmarks",
"pallet-society/runtime-benchmarks",
"pallet-staking/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 @@ -1338,6 +1338,7 @@ impl pallet_ddc_customers::Config for Runtime {

impl pallet_ddc_nodes::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_ddc_nodes::weights::SubstrateWeight<Runtime>;
}

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

0 comments on commit 36d18bf

Please sign in to comment.