Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated benchmarking of buy-back from v1 to v2. #1539

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 79 additions & 22 deletions pallets/buy-back/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

#![cfg(feature = "runtime-benchmarks")]

use crate::{BalanceOf, Call, Config, Pallet, Pallet as BuyBack, *};
use crate::{BalanceOf, Call, Config, Pallet as BuyBack, *};
use bifrost_primitives::VDOT;
use frame_benchmarking::v1::{account, benchmarks, BenchmarkError};
use frame_benchmarking::v2::*;
use frame_support::{
assert_ok,
traits::{EnsureOrigin, Hooks},
Expand All @@ -31,49 +31,106 @@ use frame_system::RawOrigin;
use orml_traits::MultiCurrency;
use sp_runtime::traits::UniqueSaturatedFrom;

benchmarks! {
set_vtoken {
let origin = T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
}: _<T::RuntimeOrigin>(origin,VDOT,1_000_000u32.into(),Permill::from_percent(2),1000u32.into(),1000u32.into(),true,Some(Permill::from_percent(2)),Permill::from_percent(2))
#[benchmarks]
mod benchmarks {
use super::*;

charge {
let test_account: T::AccountId = account("seed",1,1);
#[benchmark]
fn set_vtoken() -> Result<(), BenchmarkError> {
let origin =
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;

T::MultiCurrency::deposit(VDOT, &test_account, BalanceOf::<T>::unique_saturated_from(1_000_000_000_000_000u128))?;
}: _(RawOrigin::Signed(test_account),VDOT,BalanceOf::<T>::unique_saturated_from(9_000_000_000_000u128))
#[extrinsic_call]
_(
origin as <T as frame_system::Config>::RuntimeOrigin,
VDOT,
1_000_000u32.into(),
Permill::from_percent(2),
1000u32.into(),
1000u32.into(),
true,
Some(Permill::from_percent(2)),
Permill::from_percent(2),
);

Ok(())
}

#[benchmark]
fn charge() -> Result<(), BenchmarkError> {
let test_account: T::AccountId = account("seed", 1, 1);

T::MultiCurrency::deposit(
VDOT,
&test_account,
BalanceOf::<T>::unique_saturated_from(1_000_000_000_000_000u128),
)?;

#[extrinsic_call]
_(
RawOrigin::Signed(test_account),
VDOT,
BalanceOf::<T>::unique_saturated_from(9_000_000_000_000u128),
);

Ok(())
}

#[benchmark]
fn remove_vtoken() -> Result<(), BenchmarkError> {
let origin =
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;

remove_vtoken {
let origin = T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
assert_ok!(BuyBack::<T>::set_vtoken(
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
origin.clone() as <T as frame_system::Config>::RuntimeOrigin,
VDOT,
1_000_000u32.into(),
Permill::from_percent(2),
1000u32.into(),
1000u32.into(),
true,
Some(Permill::from_percent(2)),
Permill::from_percent(2)
Permill::from_percent(2),
));
}: _<T::RuntimeOrigin>(origin,VDOT)

#[extrinsic_call]
_(origin as <T as frame_system::Config>::RuntimeOrigin, VDOT);

Ok(())
}

#[benchmark]
fn on_initialize() -> Result<(), BenchmarkError> {
let origin =
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;

on_initialize {
let origin = T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
assert_ok!(BuyBack::<T>::set_vtoken(
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
origin as <T as frame_system::Config>::RuntimeOrigin,
VDOT,
1_000_000u32.into(),
Permill::from_percent(2),
1000u32.into(),
1000u32.into(),
true,
Some(Permill::from_percent(2)),
Permill::from_percent(2)
Permill::from_percent(2),
));
}: {
BuyBack::<T>::on_initialize(BlockNumberFor::<T>::from(0u32));

#[block]
{
BuyBack::<T>::on_initialize(BlockNumberFor::<T>::from(0u32));
}

Ok(())
}

impl_benchmark_test_suite!(BuyBack,crate::mock::ExtBuilder::default().build(),crate::mock::Runtime);
// This line generates test cases for benchmarking, and could be run by:
// `cargo test -p pallet-example-basic --all-features`, you will see one line per case:
// `test benchmarking::bench_sort_vector ... ok`
// `test benchmarking::bench_accumulate_dummy ... ok`
// `test benchmarking::bench_set_dummy_benchmark ... ok` in the result.
//
// The line generates three steps per benchmark, with repeat=1 and the three steps are
// [low, mid, high] of the range.
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext_benchmark(), crate::mock::Runtime);
}
2 changes: 1 addition & 1 deletion pallets/buy-back/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ pub mod pallet {
_ => (),
}
}
T::WeightInfo::on_idle()
T::WeightInfo::on_initialize()
}
}

Expand Down
5 changes: 5 additions & 0 deletions pallets/buy-back/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,8 @@ impl ExtBuilder {
t.into()
}
}

#[cfg(feature = "runtime-benchmarks")]
pub fn new_test_ext_benchmark() -> sp_io::TestExternalities {
ExtBuilder::default().build()
}
117 changes: 58 additions & 59 deletions pallets/buy-back/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,54 @@

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// 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.

//! Autogenerated weights for `bifrost_buy_back`
//! Autogenerated weights for bifrost_buy_back
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 32.0.0
//! DATE: 2024-06-05, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `yml`, CPU: `AMD Ryzen 9 3950X 16-Core Processor`
//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("bifrost-polkadot-local")`, DB CACHE: 1024
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 42.0.1
//! DATE: 2024-12-04, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! HOSTNAME: `mjl-legion`, CPU: `12th Gen Intel(R) Core(TM) i9-12900H`
//! WASM-EXECUTION: Compiled, CHAIN: Some("bifrost-polkadot-local"), DB CACHE: 1024

// Executed Command:
// ./target/release/bifrost
// target/release/bifrost
// benchmark
// pallet
// --chain=bifrost-polkadot-local
// --pallet=bifrost-buy-back
// --extrinsic=*
// --steps=50
// --repeat=20
// --pallet=bifrost_buy-back
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --header=./HEADER-GPL3
// --output=./bifrost-buy-back.rs
// --output=./pallets/buy-back/src/weights.rs
// --template=./weight-template/pallet-weight-template.hbs

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

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

/// Weight functions for `bifrost_buy_back`.
/// Weight functions needed for bifrost_buy_back.
pub trait WeightInfo {
fn set_vtoken() -> Weight;
fn charge() -> Weight;
fn remove_vtoken() -> Weight;
fn on_initialize() -> Weight;
}

// For backwards compatibility and tests
impl WeightInfo for () {
/// Storage: `AssetRegistry::CurrencyMetadatas` (r:1 w:0)
/// Proof: `AssetRegistry::CurrencyMetadatas` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `System::Number` (r:1 w:0)
/// Proof: `System::Number` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `System::ExecutionPhase` (r:1 w:0)
Expand All @@ -60,14 +75,15 @@ impl WeightInfo for () {
/// Proof: `BuyBack::Infos` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn set_vtoken() -> Weight {
// Proof Size summary in bytes:
// Measured: `25`
// Estimated: `1510`
// Minimum execution time: 11_582_000 picoseconds.
Weight::from_parts(11_753_000, 0)
.saturating_add(Weight::from_parts(0, 1510))
.saturating_add(RocksDbWeight::get().reads(4))
.saturating_add(RocksDbWeight::get().writes(3))
// Measured: `667`
// Estimated: `4132`
// Minimum execution time: 10_542_000 picoseconds.
Weight::from_parts(11_314_000, 4132)
.saturating_add(RocksDbWeight::get().reads(5_u64))
.saturating_add(RocksDbWeight::get().writes(3_u64))
}
/// Storage: `AssetRegistry::CurrencyMetadatas` (r:1 w:0)
/// Proof: `AssetRegistry::CurrencyMetadatas` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Tokens::Accounts` (r:2 w:2)
/// Proof: `Tokens::Accounts` (`max_values`: None, `max_size`: Some(118), added: 2593, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:2 w:1)
Expand All @@ -82,14 +98,15 @@ impl WeightInfo for () {
/// Proof: `System::Events` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn charge() -> Weight {
// Proof Size summary in bytes:
// Measured: `508`
// Measured: `1441`
// Estimated: `6196`
// Minimum execution time: 43_842_000 picoseconds.
Weight::from_parts(44_144_000, 0)
.saturating_add(Weight::from_parts(0, 6196))
.saturating_add(RocksDbWeight::get().reads(8))
.saturating_add(RocksDbWeight::get().writes(5))
// Minimum execution time: 32_777_000 picoseconds.
Weight::from_parts(33_824_000, 6196)
.saturating_add(RocksDbWeight::get().reads(9_u64))
.saturating_add(RocksDbWeight::get().writes(5_u64))
}
/// Storage: `BuyBack::Infos` (r:1 w:1)
/// Proof: `BuyBack::Infos` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `System::Number` (r:1 w:0)
/// Proof: `System::Number` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `System::ExecutionPhase` (r:1 w:0)
Expand All @@ -98,41 +115,23 @@ impl WeightInfo for () {
/// Proof: `System::EventCount` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `System::Events` (r:1 w:1)
/// Proof: `System::Events` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `BuyBack::Infos` (r:0 w:1)
/// Proof: `BuyBack::Infos` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn remove_vtoken() -> Weight {
// Proof Size summary in bytes:
// Measured: `25`
// Estimated: `1510`
// Minimum execution time: 10_931_000 picoseconds.
Weight::from_parts(11_161_000, 0)
.saturating_add(Weight::from_parts(0, 1510))
.saturating_add(RocksDbWeight::get().reads(4))
.saturating_add(RocksDbWeight::get().writes(3))
// Measured: `266`
// Estimated: `3731`
// Minimum execution time: 8_183_000 picoseconds.
Weight::from_parts(8_648_000, 3731)
.saturating_add(RocksDbWeight::get().reads(5_u64))
.saturating_add(RocksDbWeight::get().writes(3_u64))
}
/// Storage: `BuyBack::Infos` (r:2 w:1)
/// Storage: `BuyBack::Infos` (r:2 w:0)
/// Proof: `BuyBack::Infos` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `ParachainInfo::ParachainId` (r:1 w:0)
/// Proof: `ParachainInfo::ParachainId` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `Tokens::Accounts` (r:2 w:0)
/// Proof: `Tokens::Accounts` (`max_values`: None, `max_size`: Some(118), added: 2593, mode: `MaxEncodedLen`)
fn on_idle() -> Weight {
fn on_initialize() -> Weight {
// Proof Size summary in bytes:
// Measured: `502`
// Estimated: `6442`
// Minimum execution time: 32_792_000 picoseconds.
Weight::from_parts(33_293_000, 0)
.saturating_add(Weight::from_parts(0, 6442))
.saturating_add(RocksDbWeight::get().reads(5))
.saturating_add(RocksDbWeight::get().writes(1))
// Measured: `303`
// Estimated: `6243`
// Minimum execution time: 5_610_000 picoseconds.
Weight::from_parts(5_932_000, 6243)
.saturating_add(RocksDbWeight::get().reads(2_u64))
}
}


/// Weight functions needed for bifrost_buy_back.
pub trait WeightInfo {
fn set_vtoken() -> Weight;
fn charge() -> Weight;
fn remove_vtoken() -> Weight;
fn on_idle() -> Weight;
}
2 changes: 1 addition & 1 deletion runtime/bifrost-polkadot/src/weights/bifrost_buy_back.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<T: frame_system::Config> bifrost_buy_back::WeightInfo for BifrostWeight<T>
/// Proof: `ParachainInfo::ParachainId` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `Tokens::Accounts` (r:2 w:0)
/// Proof: `Tokens::Accounts` (`max_values`: None, `max_size`: Some(118), added: 2593, mode: `MaxEncodedLen`)
fn on_idle() -> Weight {
fn on_initialize() -> Weight {
// Proof Size summary in bytes:
// Measured: `502`
// Estimated: `6442`
Expand Down