Skip to content

Commit

Permalink
ADD: digital twin benchmarks, FIX: pallet version
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuanyshbaev committed Oct 6, 2024
1 parent 492b059 commit 2c05380
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 2 deletions.
10 changes: 9 additions & 1 deletion frame/digital-twin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "pallet-robonomics-digital-twin"
description = "Robonomics Network digital twin runtime module"
version = "0.3.0"
version = "3.1.0"
authors = ["Airalab <[email protected]>"]
edition = "2021"

Expand All @@ -13,6 +13,7 @@ sp-core = { workspace = true }
sp-runtime = { workspace = true }
frame-system = { workspace = true }
frame-support = { workspace = true }
frame-benchmarking = { workspace = true, optional = true }

[dev-dependencies]
sp-io = { workspace = true }
Expand All @@ -26,5 +27,12 @@ std = [
"sp-runtime/std",
"frame-system/std",
"frame-support/std",
"frame-benchmarking/std",
"scale-info/std",
]

runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
112 changes: 112 additions & 0 deletions frame/digital-twin/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright 2018-2024 Robonomics Network <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
// Benchmarks for Digital Twin Pallet

use super::{Pallet as DigitalTwin, *};
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
use frame_system::RawOrigin;
use sp_core::H256;
use sp_std::prelude::*;

const SEED: u32 = 0;

benchmarks! {

create {
let caller: T::AccountId = account("caller", 1, SEED);
// for _ in 0..1000 {
// DigitalTwin::<T>::create(RawOrigin::Signed(caller.clone()).into())?;
// }
DigitalTwin::<T>::create(RawOrigin::Signed(caller.clone()).into())?;
}: _(RawOrigin::Signed(caller))

set_source {
let caller: T::AccountId = account("caller", 1, SEED);
let id: u32 = 0;
let topic: H256 = Default::default();
let source: T::AccountId = account("caller", 2, SEED);

DigitalTwin::<T>::create(RawOrigin::Signed(caller.clone()).into())?;

DigitalTwin::<T>::set_source(
RawOrigin::Signed(caller.clone()).into(),
id,
topic,
source.clone(),
)?;

// for _ in 0..1000 {
// DigitalTwin::<T>::set_source(
// RawOrigin::Signed(caller.clone()).into(),
// id,
// topic,
// source.clone(),
// )?;
// }
}: _(RawOrigin::Signed(caller), id, topic, source)

remove_source {
let caller: T::AccountId = account("caller", 1, SEED);
let id: u32 = 0;
let topic: H256 = Default::default();
let source: T::AccountId = account("caller", 2, SEED);

DigitalTwin::<T>::create(RawOrigin::Signed(caller.clone()).into())?;

DigitalTwin::<T>::set_source(
RawOrigin::Signed(caller.clone()).into(),
id,
topic,
source.clone(),
)?;

DigitalTwin::<T>::remove_source(
RawOrigin::Signed(caller.clone()).into(),
id,
topic,
source.clone(),
)?;

// for _ in 0..1000 {
// DigitalTwin::<T>::set_source(
// RawOrigin::Signed(caller.clone()).into(),
// id,
// topic,
// source.clone(),
// )?;
// }
//
// for _ in 0..1000 {
// DigitalTwin::<T>::remove_source(
// RawOrigin::Signed(caller.clone()).into(),
// id,
// topic,
// source.clone(),
// )?;
// }
}: _(RawOrigin::Signed(caller), id, topic, source)

verify {
}
}

impl_benchmark_test_suite!(
DigitalTwin,
crate::tests::new_test_ext(),
crate::tests::Runtime,
);
66 changes: 65 additions & 1 deletion frame/digital-twin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;
// pub use weights::WeightInfo;

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

#[frame_support::pallet]
pub mod pallet {
Expand Down Expand Up @@ -70,6 +75,8 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Create new digital twin.
// #[pallet::weight(T::WeightInfo::create())]
// #[pallet::call_index(0)]
#[pallet::weight(50_000)]

Check warning on line 80 in frame/digital-twin/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this let-binding has unit value

warning: this let-binding has unit value --> frame/digital-twin/src/lib.rs:80:26 | 80 | #[pallet::weight(50_000)] | ^^^^^^ help: omit the `let` binding: `50_000;` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value

Check warning on line 80 in frame/digital-twin/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

use of deprecated constant `pallet::warnings::ConstantWeight_0::_w`: It is deprecated to use hard-coded constant as call weight. Please instead benchmark all calls or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/13798>

warning: use of deprecated constant `pallet::warnings::ConstantWeight_0::_w`: It is deprecated to use hard-coded constant as call weight. Please instead benchmark all calls or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/13798> --> frame/digital-twin/src/lib.rs:80:26 | 80 | #[pallet::weight(50_000)] | ^^^^^^
pub fn create(origin: OriginFor<T>) -> DispatchResultWithPostInfo {

Check warning on line 81 in frame/digital-twin/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

use of deprecated constant `pallet::warnings::ImplicitCallIndex_0::_w`: It is deprecated to use implicit call indices. Please instead ensure that all calls have a `pallet::call_index` attribute or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/12891> <https://github.com/paritytech/substrate/pull/11381>

warning: use of deprecated constant `pallet::warnings::ImplicitCallIndex_0::_w`: It is deprecated to use implicit call indices. Please instead ensure that all calls have a `pallet::call_index` attribute or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/12891> <https://github.com/paritytech/substrate/pull/11381> --> frame/digital-twin/src/lib.rs:81:16 | 81 | pub fn create(origin: OriginFor<T>) -> DispatchResultWithPostInfo { | ^^^^^^
let sender = ensure_signed(origin)?;
Expand All @@ -81,6 +88,8 @@ pub mod pallet {
}

/// Set data source account for difital twin.
// #[pallet::weight(T::WeightInfo::set_source())]
// #[pallet::call_index(1)]
#[pallet::weight(50_000)]

Check warning on line 93 in frame/digital-twin/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this let-binding has unit value

warning: this let-binding has unit value --> frame/digital-twin/src/lib.rs:93:26 | 93 | #[pallet::weight(50_000)] | ^^^^^^ help: omit the `let` binding: `50_000;` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value

Check warning on line 93 in frame/digital-twin/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

use of deprecated constant `pallet::warnings::ConstantWeight_1::_w`: It is deprecated to use hard-coded constant as call weight. Please instead benchmark all calls or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/13798>

warning: use of deprecated constant `pallet::warnings::ConstantWeight_1::_w`: It is deprecated to use hard-coded constant as call weight. Please instead benchmark all calls or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/13798> --> frame/digital-twin/src/lib.rs:93:26 | 93 | #[pallet::weight(50_000)] | ^^^^^^
pub fn set_source(

Check warning on line 94 in frame/digital-twin/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this let-binding has unit value

warning: this let-binding has unit value --> frame/digital-twin/src/lib.rs:94:16 | 94 | pub fn set_source( | ^^^^^^^^^^ help: omit the `let` binding: `set_source;` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value

Check warning on line 94 in frame/digital-twin/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

use of deprecated constant `pallet::warnings::ImplicitCallIndex_1::_w`: It is deprecated to use implicit call indices. Please instead ensure that all calls have a `pallet::call_index` attribute or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/12891> <https://github.com/paritytech/substrate/pull/11381>

warning: use of deprecated constant `pallet::warnings::ImplicitCallIndex_1::_w`: It is deprecated to use implicit call indices. Please instead ensure that all calls have a `pallet::call_index` attribute or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/12891> <https://github.com/paritytech/substrate/pull/11381> --> frame/digital-twin/src/lib.rs:94:16 | 94 | pub fn set_source( | ^^^^^^^^^^
origin: OriginFor<T>,
Expand All @@ -106,6 +115,30 @@ pub mod pallet {
});
Ok(().into())
}

/// Remove data source account for difital twin.
// #[pallet::weight(T::WeightInfo::remove_source())]
// #[pallet::call_index(2)]
#[pallet::weight(50_000)]

Check warning on line 122 in frame/digital-twin/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this let-binding has unit value

warning: this let-binding has unit value --> frame/digital-twin/src/lib.rs:122:26 | 122 | #[pallet::weight(50_000)] | ^^^^^^ help: omit the `let` binding: `50_000;` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value

Check warning on line 122 in frame/digital-twin/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

use of deprecated constant `pallet::warnings::ConstantWeight_2::_w`: It is deprecated to use hard-coded constant as call weight. Please instead benchmark all calls or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/13798>

warning: use of deprecated constant `pallet::warnings::ConstantWeight_2::_w`: It is deprecated to use hard-coded constant as call weight. Please instead benchmark all calls or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/13798> --> frame/digital-twin/src/lib.rs:122:26 | 122 | #[pallet::weight(50_000)] | ^^^^^^
pub fn remove_source(

Check warning on line 123 in frame/digital-twin/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this let-binding has unit value

warning: this let-binding has unit value --> frame/digital-twin/src/lib.rs:123:16 | 123 | pub fn remove_source( | ^^^^^^^^^^^^^ help: omit the `let` binding: `remove_source;` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value

Check warning on line 123 in frame/digital-twin/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

use of deprecated constant `pallet::warnings::ImplicitCallIndex_2::_w`: It is deprecated to use implicit call indices. Please instead ensure that all calls have a `pallet::call_index` attribute or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/12891> <https://github.com/paritytech/substrate/pull/11381>

warning: use of deprecated constant `pallet::warnings::ImplicitCallIndex_2::_w`: It is deprecated to use implicit call indices. Please instead ensure that all calls have a `pallet::call_index` attribute or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/12891> <https://github.com/paritytech/substrate/pull/11381> --> frame/digital-twin/src/lib.rs:123:16 | 123 | pub fn remove_source( | ^^^^^^^^^^^^^
origin: OriginFor<T>,
id: u32,
topic: H256,
source: T::AccountId,
) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;
ensure!(
<Owner<T>>::get(id) == Some(sender.clone()),
"sender should be a twin owner"
);
Self::deposit_event(Event::TopicChanged(sender, id, topic, source.clone()));
<DigitalTwin<T>>::mutate(id, |m| {
if let Some(map) = m {
map.remove(&topic);
}
});
Ok(().into())
}
}
}

Expand Down Expand Up @@ -159,7 +192,7 @@ mod tests {
type RuntimeEvent = RuntimeEvent;
}

fn new_test_ext() -> sp_io::TestExternalities {
pub fn new_test_ext() -> sp_io::TestExternalities {
let storage = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap();
Expand Down Expand Up @@ -201,6 +234,37 @@ mod tests {
})
}

#[test]
fn test_remove_source() {
new_test_ext().execute_with(|| {
let sender = 1;
let bad_sender = 2;
let source = 3;
assert_ok!(DigitalTwin::create(RuntimeOrigin::signed(sender)));
assert_ok!(DigitalTwin::set_source(
RuntimeOrigin::signed(sender),
0,
Default::default(),
source
));
assert_err!(
DigitalTwin::remove_source(
RuntimeOrigin::signed(bad_sender),
0,
Default::default(),
source
),
DispatchError::Other("sender should be a twin owner")
);
assert_ok!(DigitalTwin::remove_source(
RuntimeOrigin::signed(sender),
0,
Default::default(),
source
));
})
}

#[test]
fn test_bad_origin() {
new_test_ext().execute_with(|| {
Expand Down
80 changes: 80 additions & 0 deletions frame/digital-twin/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

//! Autogenerated weights for `robonomics_digital_twin`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2024-10-06, STEPS: `20`, REPEAT: `10`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `nano`, CPU: `11th Gen Intel(R) Core(TM) i5-1130G7 @ 1.10GHz`
//! EXECUTION: ``, WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: 1024
// Executed Command:
// ./target/release/robonomics
// benchmark
// pallet
// --chain
// dev
// --pallet
// robonomics_digital_twin
// --extrinsic
// *
// --steps
// 20
// --repeat
// 10
// --output
// /home/denis/weights.rs

#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
#![allow(missing_docs)]

use frame_support::{traits::Get, weights::Weight};
use core::marker::PhantomData;

/// Weight functions for `robonomics_digital_twin`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> robonomics_digital_twin::WeightInfo for WeightInfo<T> {
/// Storage: `DigitalTwin::Total` (r:1 w:1)
/// Proof: `DigitalTwin::Total` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `DigitalTwin::Owner` (r:0 w:1)
/// Proof: `DigitalTwin::Owner` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn create() -> Weight {
// Proof Size summary in bytes:
// Measured: `103`
// Estimated: `1588`
// Minimum execution time: 20_339_000 picoseconds.
Weight::from_parts(21_541_000, 0)
.saturating_add(Weight::from_parts(0, 1588))
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(2))
}
/// Storage: `DigitalTwin::Owner` (r:1 w:0)
/// Proof: `DigitalTwin::Owner` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `DigitalTwin::DigitalTwin` (r:1 w:1)
/// Proof: `DigitalTwin::DigitalTwin` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn set_source() -> Weight {
// Proof Size summary in bytes:
// Measured: `234`
// Estimated: `3699`
// Minimum execution time: 19_339_000 picoseconds.
Weight::from_parts(19_984_000, 0)
.saturating_add(Weight::from_parts(0, 3699))
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(1))
}
/// Storage: `DigitalTwin::Owner` (r:1 w:0)
/// Proof: `DigitalTwin::Owner` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `DigitalTwin::DigitalTwin` (r:1 w:1)
/// Proof: `DigitalTwin::DigitalTwin` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn remove_source() -> Weight {
// Proof Size summary in bytes:
// Measured: `166`
// Estimated: `3631`
// Minimum execution time: 18_591_000 picoseconds.
Weight::from_parts(19_151_000, 0)
.saturating_add(Weight::from_parts(0, 3631))
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(1))
}
}
Loading

0 comments on commit 2c05380

Please sign in to comment.