From 5b8d80f9f745e09c08f445b903001a17cc541b23 Mon Sep 17 00:00:00 2001 From: Jesse Abramowitz Date: Thu, 15 Aug 2024 16:31:49 -0400 Subject: [PATCH] add benchmarks --- pallets/oracle/src/benchmarking.rs | 33 +++++++++++ pallets/oracle/src/lib.rs | 9 ++- pallets/oracle/src/mock.rs | 10 ++-- pallets/oracle/src/weights.rs | 89 ++++++++++++++++++++++++++++ runtime/src/lib.rs | 3 +- runtime/src/weights/pallet_oracle.rs | 59 ++++++++++++++++++ 6 files changed, 195 insertions(+), 8 deletions(-) create mode 100644 pallets/oracle/src/benchmarking.rs create mode 100644 pallets/oracle/src/weights.rs create mode 100644 runtime/src/weights/pallet_oracle.rs diff --git a/pallets/oracle/src/benchmarking.rs b/pallets/oracle/src/benchmarking.rs new file mode 100644 index 000000000..3f3713c6b --- /dev/null +++ b/pallets/oracle/src/benchmarking.rs @@ -0,0 +1,33 @@ +// Copyright (C) 2023 Entropy Cryptography Inc. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +// //! Benchmarking setup for pallet-oracle + +use super::*; + +#[allow(unused)] +use crate::Pallet as Oracle; +use frame_benchmarking::benchmarks; + +benchmarks! { + on_initialize { + }: { + Oracle::::on_initialize(50u32.into()); + } verify { + assert_eq!(OracleData::::get(BoundedVec::try_from("block_number_entropy".encode()).unwrap()).unwrap()[0], 50); + } + + impl_benchmark_test_suite!(Oracle, crate::mock::new_test_ext(), crate::mock::Test); +} diff --git a/pallets/oracle/src/lib.rs b/pallets/oracle/src/lib.rs index a58e88fd5..ec1898f03 100644 --- a/pallets/oracle/src/lib.rs +++ b/pallets/oracle/src/lib.rs @@ -25,19 +25,24 @@ #![allow(clippy::unused_unit)] use frame_support::pallet_prelude::*; -use frame_system::{pallet_prelude::*, WeightInfo}; +use frame_system::pallet_prelude::*; #[cfg(test)] mod mock; #[cfg(test)] mod tests; +pub mod weights; + +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; pub use module::*; #[frame_support::pallet] pub mod module { use super::*; + pub use crate::weights::WeightInfo; #[pallet::config] pub trait Config: frame_system::Config { @@ -90,7 +95,7 @@ pub mod module { BoundedVec::try_from(block_number.encode()) .expect("Block number fits in bounded vec; qed"), ); - T::DbWeight::get().writes(1) + T::WeightInfo::on_initialize() } } diff --git a/pallets/oracle/src/mock.rs b/pallets/oracle/src/mock.rs index 7fc597aad..81745833f 100644 --- a/pallets/oracle/src/mock.rs +++ b/pallets/oracle/src/mock.rs @@ -31,7 +31,7 @@ pub type AccountId = u128; use crate as pallet_oracle; #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] -impl frame_system::Config for Runtime { +impl frame_system::Config for Test { type AccountData = (); type AccountId = AccountId; type BaseCallFilter = Everything; @@ -62,17 +62,17 @@ parameter_types! { pub const MaxOracleValueLength: u32 = 100; } -impl Config for Runtime { +impl Config for Test { type RuntimeEvent = RuntimeEvent; type MaxOracleKeyLength = MaxOracleKeyLength; type MaxOracleValueLength = MaxOracleValueLength; type WeightInfo = (); } -type Block = frame_system::mocking::MockBlock; +type Block = frame_system::mocking::MockBlock; construct_runtime!( - pub enum Runtime + pub enum Test { System: frame_system, Oracle: pallet_oracle, @@ -81,5 +81,5 @@ construct_runtime!( // Build genesis storage according to the mock runtime. pub fn new_test_ext() -> sp_io::TestExternalities { - frame_system::GenesisConfig::::default().build_storage().unwrap().into() + frame_system::GenesisConfig::::default().build_storage().unwrap().into() } diff --git a/pallets/oracle/src/weights.rs b/pallets/oracle/src/weights.rs new file mode 100644 index 000000000..c8f5e7990 --- /dev/null +++ b/pallets/oracle/src/weights.rs @@ -0,0 +1,89 @@ +// Copyright (C) 2023 Entropy Cryptography Inc. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +//! Autogenerated weights for pallet_oracle +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-11-02, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `hcastano`, CPU: `` +//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 + +// Executed Command: +// ./target/release/entropy +// benchmark +// pallet +// --chain +// dev +// --wasm-execution=compiled +// --pallet +// pallet_oracle +// --extrinsic +// * +// --steps +// 5 +// --repeat +// 2 +// --template +// .maintain/frame-weight-template.hbs +// --output +// pallets/oracle/src/weights.rs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] +#![allow(missing_docs)] + +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use core::marker::PhantomData; + +/// Weight functions needed for pallet_oracle. +pub trait WeightInfo { + fn on_initialize() -> Weight; +} + +/// Weights for pallet_oracle using the Substrate node and recommended hardware. +pub struct SubstrateWeight(PhantomData); +impl WeightInfo for SubstrateWeight { + /// Storage: `Oracle::OracleData` (r:0 w:1) + /// Proof: `Oracle::OracleData` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn on_initialize() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_000_000 picoseconds. + Weight::from_parts(2_000_000, 0) + .saturating_add(Weight::from_parts(0, 0)) + .saturating_add(T::DbWeight::get().writes(1)) + } + +} + +// For backwards compatibility and tests +impl WeightInfo for () { + /// Storage: `Oracle::OracleData` (r:0 w:1) + /// Proof: `Oracle::OracleData` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn on_initialize() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_000_000 picoseconds. + Weight::from_parts(2_000_000, 0) + .saturating_add(Weight::from_parts(0, 0)) + .saturating_add(RocksDbWeight::get().writes(1)) + } + +} diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index aa90f54bb..5cb70651c 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -1502,7 +1502,7 @@ impl pallet_oracle::Config for Runtime { type RuntimeEvent = RuntimeEvent; type MaxOracleKeyLength = MaxOracleKeyLength; type MaxOracleValueLength = MaxOracleValueLength; - type WeightInfo = (); + type WeightInfo = weights::pallet_oracle::WeightInfo; } construct_runtime!( @@ -1634,6 +1634,7 @@ mod benches { [pallet_nomination_pools, NominationPoolsBench::] [pallet_multisig, Multisig] [pallet_offences, OffencesBench::] + [pallet_oracle, Oracle] [pallet_preimage, Preimage] [pallet_parameters, Parameters] [pallet_proxy, Proxy] diff --git a/runtime/src/weights/pallet_oracle.rs b/runtime/src/weights/pallet_oracle.rs new file mode 100644 index 000000000..24cd1c8db --- /dev/null +++ b/runtime/src/weights/pallet_oracle.rs @@ -0,0 +1,59 @@ +// Copyright (C) 2023 Entropy Cryptography Inc. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +//! Autogenerated weights for `pallet_oracle` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 33.0.0 +//! DATE: 2024-08-15, STEPS: `5`, REPEAT: `2`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `Jesses-MacBook-Pro.local`, CPU: `` +//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: 1024 + +// Executed Command: +// ./target/release/entropy +// benchmark +// pallet +// --chain +// dev +// --pallet=pallet_oracle +// --extrinsic=* +// --steps=5 +// --repeat=2 +// --header=.maintain/AGPL-3.0-header.txt +// --output=./runtime/src/weights/ + +#![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 `pallet_oracle`. +pub struct WeightInfo(PhantomData); +impl pallet_oracle::WeightInfo for WeightInfo { + /// Storage: `Oracle::OracleData` (r:0 w:1) + /// Proof: `Oracle::OracleData` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn on_initialize() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_000_000 picoseconds. + Weight::from_parts(2_000_000, 0) + .saturating_add(Weight::from_parts(0, 0)) + .saturating_add(T::DbWeight::get().writes(1)) + } +}