Skip to content

Commit

Permalink
feat(benchmark): pallet tft price [1/4] (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
renauter authored May 22, 2023
1 parent a33dafb commit 2eb3860
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 139 deletions.
121 changes: 62 additions & 59 deletions substrate-node/.maintain/frame-weight-template.hbs
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
// This file is part of Substrate.

// Copyright (C) 2022 Threefold Tech
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

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

{{header}}
//! Autogenerated weights for {{pallet}}
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION {{version}}
//! DATE: {{date}}, STEPS: {{cmd.steps}}, REPEAT: {{cmd.repeat}}, LOW RANGE: {{cmd.lowest_range_values}}, HIGH RANGE: {{cmd.highest_range_values}}
//! DATE: {{date}}, STEPS: `{{cmd.steps}}`, REPEAT: {{cmd.repeat}}, LOW RANGE: `{{cmd.lowest_range_values}}`, HIGH RANGE: `{{cmd.highest_range_values}}`
//! HOSTNAME: `{{hostname}}`, CPU: `{{cpuname}}`
//! EXECUTION: {{cmd.execution}}, WASM-EXECUTION: {{cmd.wasm_execution}}, CHAIN: {{cmd.chain}}, DB CACHE: {{cmd.db_cache}}

// Executed Command:
{{#each args as |arg|~}}
{{#each args as |arg|}}
// {{arg}}
{{/each}}

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

Expand All @@ -35,70 +20,88 @@ use sp_std::marker::PhantomData;

/// Weight functions needed for {{pallet}}.
pub trait WeightInfo {
{{~#each benchmarks as |benchmark|}}
{{#each benchmarks as |benchmark|}}
fn {{benchmark.name~}}
(
{{~#each benchmark.components as |c| ~}}
{{c.name}}: u32, {{/each~}}
) -> Weight;
{{~/each}}
{{/each}}
}

/// Weights for {{pallet}} using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
{{#if (eq pallet "frame_system")}}
impl<T: crate::Config> WeightInfo for SubstrateWeight<T> {
{{else}}
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
{{~#each benchmarks as |benchmark|}}
{{/if}}
{{#each benchmarks as |benchmark|}}
{{#each benchmark.comments as |comment|}}
// {{comment}}
{{/each}}
{{#each benchmark.component_ranges as |range|}}
/// The range of component `{{range.name}}` is `[{{range.min}}, {{range.max}}]`.
{{/each}}
fn {{benchmark.name~}}
(
{{~#each benchmark.components as |c| ~}}
{{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}
) -> Weight {
({{underscore benchmark.base_weight}} as Weight)
{{~#each benchmark.component_weight as |cw|}}
// Minimum execution time: {{underscore benchmark.min_execution_time}} nanoseconds.
Weight::from_ref_time({{underscore benchmark.base_weight}})
{{#each benchmark.component_weight as |cw|}}
// Standard Error: {{underscore cw.error}}
.saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))
{{~/each}}
{{~#if (ne benchmark.base_reads "0")}}
.saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight))
{{~/if}}
{{~#each benchmark.component_reads as |cr|}}
.saturating_add(T::DbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight)))
{{~/each}}
{{~#if (ne benchmark.base_writes "0")}}
.saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}} as Weight))
{{~/if}}
{{~#each benchmark.component_writes as |cw|}}
.saturating_add(T::DbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)))
{{~/each}}
.saturating_add(Weight::from_ref_time({{underscore cw.slope}}).saturating_mul({{cw.name}}.into()))
{{/each}}
{{#if (ne benchmark.base_reads "0")}}
.saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}}))
{{/if}}
{{#each benchmark.component_reads as |cr|}}
.saturating_add(T::DbWeight::get().reads(({{cr.slope}}_u64).saturating_mul({{cr.name}}.into())))
{{/each}}
{{#if (ne benchmark.base_writes "0")}}
.saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}}))
{{/if}}
{{#each benchmark.component_writes as |cw|}}
.saturating_add(T::DbWeight::get().writes(({{cw.slope}}_u64).saturating_mul({{cw.name}}.into())))
{{/each}}
}
{{~/each}}
{{/each}}
}

// For backwards compatibility and tests
impl WeightInfo for () {
{{~#each benchmarks as |benchmark|}}
{{#each benchmarks as |benchmark|}}
{{#each benchmark.comments as |comment|}}
// {{comment}}
{{/each}}
{{#each benchmark.component_ranges as |range|}}
/// The range of component `{{range.name}}` is `[{{range.min}}, {{range.max}}]`.
{{/each}}
fn {{benchmark.name~}}
(
{{~#each benchmark.components as |c| ~}}
{{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}
) -> Weight {
({{underscore benchmark.base_weight}} as Weight)
{{~#each benchmark.component_weight as |cw|}}
// Minimum execution time: {{underscore benchmark.min_execution_time}} nanoseconds.
Weight::from_ref_time({{underscore benchmark.base_weight}})
{{#each benchmark.component_weight as |cw|}}
// Standard Error: {{underscore cw.error}}
.saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))
{{~/each}}
{{~#if (ne benchmark.base_reads "0")}}
.saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}} as Weight))
{{~/if}}
{{~#each benchmark.component_reads as |cr|}}
.saturating_add(RocksDbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight)))
{{~/each}}
{{~#if (ne benchmark.base_writes "0")}}
.saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}} as Weight))
{{~/if}}
{{~#each benchmark.component_writes as |cw|}}
.saturating_add(RocksDbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)))
{{~/each}}
.saturating_add(Weight::from_ref_time({{underscore cw.slope}}).saturating_mul({{cw.name}}.into()))
{{/each}}
{{#if (ne benchmark.base_reads "0")}}
.saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}}))
{{/if}}
{{#each benchmark.component_reads as |cr|}}
.saturating_add(RocksDbWeight::get().reads(({{cr.slope}}_u64).saturating_mul({{cr.name}}.into())))
{{/each}}
{{#if (ne benchmark.base_writes "0")}}
.saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}}))
{{/if}}
{{#each benchmark.component_writes as |cw|}}
.saturating_add(RocksDbWeight::get().writes(({{cw.slope}}_u64).saturating_mul({{cw.name}}.into())))
{{/each}}
}
{{~/each}}
}
{{/each}}
}
78 changes: 41 additions & 37 deletions substrate-node/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,51 @@ clap = { version = "4.0.9", features = ["derive"] }
serde = { version = "1.0.119", features = ["derive"] }
serde_json = "1.0.64"

frame-benchmarking = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36" }
frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36" }
frame-benchmarking-cli = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36" }
pallet-transaction-payment-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-basic-authorship = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-cli = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-executor = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-service = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-consensus = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-consensus-aura = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-finality-grandpa-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-keystore = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-network = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-rpc-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-telemetry = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-transaction-pool = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-transaction-pool-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-block-builder = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-consensus-aura = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-transaction-pool = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
frame-rpc-system = { package = "substrate-frame-rpc-system", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
frame-benchmarking = {git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36"}
frame-system = {git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36"}
frame-benchmarking-cli = {git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36"}
pallet-transaction-payment-rpc = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
pallet-transaction-payment = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-basic-authorship = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-cli = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sc-client-api = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-client-db = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-executor = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-service = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-consensus = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-consensus-aura = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-finality-grandpa = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-finality-grandpa-rpc = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-keystore = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sp-keyring = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-network = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-rpc = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-rpc-api = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-telemetry = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-transaction-pool = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sc-transaction-pool-api = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sp-api = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sp-block-builder = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sp-blockchain = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sp-consensus = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sp-consensus-aura = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sp-core = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sp-finality-grandpa = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sp-inherents = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sp-runtime = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sp-io = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sp-timestamp = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
sp-transaction-pool = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
frame-rpc-system = {package = "substrate-frame-rpc-system", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"}
frame-try-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36", optional = true }
try-runtime-cli = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36", optional = true }

tfchain-runtime = { path = '../runtime' }

[features]
default = []
runtime-benchmarks = ["tfchain-runtime/runtime-benchmarks"]
try-runtime = ["tfchain-runtime/try-runtime", "try-runtime-cli/try-runtime"]
runtime-benchmarks = [
"tfchain-runtime/runtime-benchmarks",
"sc-client-db/runtime-benchmarks",
]
try-runtime = [ "tfchain-runtime/try-runtime", "try-runtime-cli/try-runtime"]
2 changes: 1 addition & 1 deletion substrate-node/pallets/pallet-dao/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ pub(crate) fn get_relay_input(relay_input: &[u8]) -> RelayInput {
}

pub(crate) fn get_public_key_input(pk_input: &[u8]) -> PkInput {
Some(BoundedVec::try_from(pk_input.to_vec()).expect("Invalid document hash input."))
Some(BoundedVec::try_from(pk_input.to_vec()).expect("Invalid public key input."))
}

pub(crate) fn get_public_ip_ip_input(ip_input: &[u8]) -> Ip4Input {
Expand Down
38 changes: 14 additions & 24 deletions substrate-node/pallets/pallet-smart-contract/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::*;

use crate::Pallet as SmartContractModule;
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_benchmarking::{account, benchmarks, whitelisted_caller};
use frame_support::assert_ok;
use frame_system::RawOrigin;
use sp_std::{
Expand All @@ -15,7 +15,7 @@ use sp_std::{
use pallet_tfgrid::{
types::{self as pallet_tfgrid_types, LocationInput},
CityNameInput, CountryNameInput, DocumentHashInput, DocumentLinkInput, LatitudeInput,
LongitudeInput, ResourcesInput, TwinIpInput,
LongitudeInput, PkInput, RelayInput, ResourcesInput,
};
use tfchain_support::{resources::Resources, types::IP4};
const GIGABYTE: u64 = 1024 * 1024 * 1024;
Expand Down Expand Up @@ -103,7 +103,7 @@ benchmarks! {
let stamp: u64 = 1628082000 * 1000 * 10 * 6000;
pallet_timestamp::Pallet::<T>::set_timestamp(stamp.try_into().unwrap());
// run_to_block::<T>(10);
}: _ (RawOrigin::Signed(a1.clone()), 1)
}: _ (RawOrigin::Signed(a1.clone()), 0, 1) // Update here
verify {
let contract = SmartContractModule::<T>::contracts(1).unwrap();
assert_eq!(
Expand All @@ -112,34 +112,20 @@ benchmarks! {
}
}

impl_benchmark_test_suite! {Pallet, crate::tests::new_test_ext(), crate::tests::Test}

#[cfg(test)]
mod benchmarktests {
use super::*;
use crate::mock::{new_test_ext, TestRuntime};
use frame_support::assert_ok;

#[test]
fn test_benchmarks() {
new_test_ext().execute_with(|| {
assert_ok!(test_benchmark_create_node_contract::<TestRuntime>());
assert_ok!(test_benchmark_add_nru_reports::<TestRuntime>());
});
}
}

pub fn create_twin<T: Config>(source: T::AccountId) {
assert_ok!(pallet_tfgrid::Pallet::<T>::user_accept_tc(
RawOrigin::Signed(source.clone()).into(),
get_document_link_input(b"some_link"),
get_document_hash_input(b"some_hash"),
));

let ip = get_twin_ip_input(b"::1");
let relay = get_relay_input(b"somerelay.io");
let pk =
get_public_key_input(b"0x6c8fd181adc178cea218e168e8549f0b0ff30627c879db9eac4318927e87c901");
assert_ok!(pallet_tfgrid::Pallet::<T>::create_twin(
RawOrigin::Signed(source).into(),
ip
relay,
pk
));
}

Expand Down Expand Up @@ -291,6 +277,10 @@ pub(crate) fn get_document_hash_input(document_hash_input: &[u8]) -> DocumentHas
BoundedVec::try_from(document_hash_input.to_vec()).expect("Invalid document hash input.")
}

pub(crate) fn get_twin_ip_input(twin_ip_input: &[u8]) -> TwinIpInput {
BoundedVec::try_from(twin_ip_input.to_vec()).expect("Invalid twin ip input.")
pub(crate) fn get_relay_input(relay_input: &[u8]) -> RelayInput {
Some(BoundedVec::try_from(relay_input.to_vec()).expect("Invalid relay input."))
}

pub(crate) fn get_public_key_input(pk_input: &[u8]) -> PkInput {
Some(BoundedVec::try_from(pk_input.to_vec()).expect("Invalid public key input."))
}
1 change: 1 addition & 0 deletions substrate-node/pallets/pallet-smart-contract/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ impl pallet_tft_price::Config for TestRuntime {
type AuthorityId = pallet_tft_price::AuthId;
type Call = RuntimeCall;
type RestrictedOrigin = EnsureRoot<Self::AccountId>;
type WeightInfo = pallet_tft_price::weights::SubstrateWeight<TestRuntime>;
}

impl pallet_timestamp::Config for TestRuntime {
Expand Down
7 changes: 7 additions & 0 deletions substrate-node/pallets/pallet-tft-price/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v
pallet-authorship = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }
pallet-session = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }

# Benchmarking
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false, optional = true }

[dev-dependencies]
tfchain-support = { path = "../../support", default-features = false }
substrate-validator-set = { path = '../substrate-validator-set' }
Expand All @@ -45,6 +48,7 @@ std = [
'codec/std',
'frame-support/std',
'frame-system/std',
'frame-benchmarking/std',
'lite-json/std',
'sp-io/std',
'sp-runtime/std',
Expand All @@ -57,6 +61,9 @@ std = [
'pallet-authorship/std',
'pallet-session/std'
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
]
Loading

0 comments on commit 2eb3860

Please sign in to comment.