Skip to content

Commit

Permalink
add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseAbram committed Aug 15, 2024
1 parent dcf8152 commit 5b8d80f
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 8 deletions.
33 changes: 33 additions & 0 deletions pallets/oracle/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.

// //! Benchmarking setup for pallet-oracle

use super::*;

#[allow(unused)]
use crate::Pallet as Oracle;
use frame_benchmarking::benchmarks;

benchmarks! {
on_initialize {
}: {
Oracle::<T>::on_initialize(50u32.into());
} verify {
assert_eq!(OracleData::<T>::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);
}
9 changes: 7 additions & 2 deletions pallets/oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
}

Expand Down
10 changes: 5 additions & 5 deletions pallets/oracle/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Runtime>;
type Block = frame_system::mocking::MockBlock<Test>;

construct_runtime!(
pub enum Runtime
pub enum Test
{
System: frame_system,
Oracle: pallet_oracle,
Expand All @@ -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::<Runtime>::default().build_storage().unwrap().into()
frame_system::GenesisConfig::<Test>::default().build_storage().unwrap().into()
}
89 changes: 89 additions & 0 deletions pallets/oracle/src/weights.rs
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.


//! 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: `<UNKNOWN>`
//! 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<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
/// 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))
}

}
3 changes: 2 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Runtime>;
}

construct_runtime!(
Expand Down Expand Up @@ -1634,6 +1634,7 @@ mod benches {
[pallet_nomination_pools, NominationPoolsBench::<Runtime>]
[pallet_multisig, Multisig]
[pallet_offences, OffencesBench::<Runtime>]
[pallet_oracle, Oracle]
[pallet_preimage, Preimage]
[pallet_parameters, Parameters]
[pallet_proxy, Proxy]
Expand Down
59 changes: 59 additions & 0 deletions runtime/src/weights/pallet_oracle.rs
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.

//! 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: `<UNKNOWN>`
//! 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<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_oracle::WeightInfo for WeightInfo<T> {
/// 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))
}
}

0 comments on commit 5b8d80f

Please sign in to comment.