From 54a17a4acbdb9c8babec42aca3f53c5dd56fd905 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Fri, 22 Oct 2021 07:03:53 +0000 Subject: [PATCH] feat(fungible): benchmarking --- --- a/pallets/fungible/Cargo.toml +++ b/pallets/fungible/Cargo.toml @@ -20,6 +20,7 @@ evm-coder = { default-features = false, path = '../../crates/evm-coder' } ethereum = { default-features = false, version = "0.9.0" } pallet-evm-coder-substrate = { default-features = false, path = '../evm-coder-substrate' } +frame-benchmarking = { default-features = false, optional = true, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' } [features] default = ["std"] @@ -32,5 +33,9 @@ "pallet-common/std", "evm-coder/std", "ethereum/std", + 'frame-benchmarking/std', ] -runtime-benchmarks = [] +runtime-benchmarks = [ + 'frame-benchmarking', + 'pallet-common/runtime-benchmarks', +] --- a/pallets/fungible/src/benchmarking.rs +++ b/pallets/fungible/src/benchmarking.rs @@ -1 +1,61 @@ -#![cfg(feature = "runtime-benchmarking")] +use super::*; +use crate::{Pallet, Config, FungibleHandle}; + +use sp_std::prelude::*; +use pallet_common::benchmarking::create_collection_raw; +use frame_benchmarking::{benchmarks, account}; +use nft_data_structs::{CollectionMode}; +use pallet_common::bench_init; + +const SEED: u32 = 1; + +fn create_collection(owner: T::AccountId) -> Result, DispatchError> { + create_collection_raw( + owner, + CollectionMode::Fungible(0), + >::init_collection, + FungibleHandle::cast, + ) +} + +benchmarks! { + create_item { + bench_init!{ + owner: sub; collection: collection(owner); + sender: cross_from_sub(owner); to: cross_sub; + }; + }: {>::create_item(&collection, &sender, (to, 200))?} + + burn_item { + bench_init!{ + owner: sub; collection: collection(owner); + owner: cross_from_sub; burner: cross_sub; + }; + >::create_item(&collection, &owner, (burner.clone(), 200))?; + }: {>::burn(&collection, &burner, 100)?} + + transfer { + bench_init!{ + owner: sub; collection: collection(owner); + owner: cross_from_sub; sender: cross_sub; to: cross_sub; + }; + >::create_item(&collection, &owner, (sender.clone(), 200))?; + }: {>::transfer(&collection, &sender, &to, 200)?} + + approve { + bench_init!{ + owner: sub; collection: collection(owner); + owner: cross_from_sub; sender: cross_sub; spender: cross_sub; + }; + >::create_item(&collection, &owner, (sender.clone(), 200))?; + }: {>::set_allowance(&collection, &sender, &spender, 100)?} + + transfer_from { + bench_init!{ + owner: sub; collection: collection(owner); + owner: cross_from_sub; sender: cross_sub; spender: cross_sub; receiver: cross_sub; + }; + >::create_item(&collection, &owner, (sender.clone(), 200))?; + >::set_allowance(&collection, &sender, &spender, 200)?; + }: {>::transfer_from(&collection, &spender, &sender, &receiver, 100)?} +} --- a/pallets/fungible/src/common.rs +++ b/pallets/fungible/src/common.rs @@ -54,7 +54,7 @@ match data { nft_data_structs::CreateItemData::Fungible(data) => with_weight( >::create_item(self, &sender, (to, data.value)), - >::create_item(), + >::create_item(), ), _ => fail!(>::NotFungibleDataUsedToMintFungibleCollectionToken), } @@ -80,7 +80,7 @@ with_weight( >::create_item(self, &sender, (to, sum)), - >::create_item(), + >::create_item(), ) } @@ -97,7 +97,7 @@ with_weight( >::burn(self, &sender, amount), - >::burn_item(), + >::burn_item(), ) } @@ -115,7 +115,7 @@ with_weight( >::transfer(&self, &from, &to, amount), - >::transfer(), + >::transfer(), ) } @@ -133,7 +133,7 @@ with_weight( >::set_allowance(&self, &sender, &spender, amount), - >::approve(), + >::approve(), ) } @@ -152,7 +152,7 @@ with_weight( >::transfer_from(&self, &sender, &from, &to, amount), - >::transfer_from(), + >::transfer_from(), ) } --- a/pallets/fungible/src/lib.rs +++ b/pallets/fungible/src/lib.rs @@ -13,6 +13,7 @@ pub use pallet::*; use crate::erc::ERC20Events; +#[cfg(feature = "runtime-benchmarks")] pub mod benchmarking; pub mod common; pub mod erc; --- a/pallets/fungible/src/weights.rs +++ b/pallets/fungible/src/weights.rs @@ -1,3 +1,27 @@ +// Template adopted from https://github.com/paritytech/substrate/blob/master/.maintain/frame-weight-template.hbs + +//! Autogenerated weights for pallet_fungible +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2021-10-21, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: None, DB CACHE: 128 + +// Executed Command: +// target/release/nft +// benchmark +// --pallet +// pallet-fungible +// --wasm-execution +// compiled +// --extrinsic +// * +// --template +// .maintain/frame-weight-template.hbs +// --steps=50 +// --repeat=20 +// --output=./pallets/fungible/src/weights.rs + + #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] #![allow(unused_imports)] @@ -5,6 +29,7 @@ use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; use sp_std::marker::PhantomData; +/// Weight functions needed for pallet_fungible. pub trait WeightInfo { fn create_item() -> Weight; fn burn_item() -> Weight; @@ -13,19 +38,79 @@ fn transfer_from() -> Weight; } +/// Weights for pallet_fungible using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { - fn create_item() -> Weight {0} - fn burn_item() -> Weight {0} - fn transfer() -> Weight {0} - fn approve() -> Weight {0} - fn transfer_from() -> Weight {0} + // Storage: Fungible Balance (r:1 w:1) + // Storage: Fungible TotalSupply (r:0 w:1) + fn create_item() -> Weight { + (12_069_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().writes(2 as Weight)) + } + // Storage: Fungible TotalSupply (r:1 w:1) + // Storage: Fungible Balance (r:1 w:1) + fn burn_item() -> Weight { + (14_096_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().writes(2 as Weight)) + } + // Storage: Fungible Balance (r:2 w:2) + fn transfer() -> Weight { + (15_436_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().writes(2 as Weight)) + } + // Storage: Fungible Balance (r:1 w:0) + // Storage: Fungible Allowance (r:0 w:1) + fn approve() -> Weight { + (12_867_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } + // Storage: Fungible Allowance (r:1 w:1) + // Storage: Fungible Balance (r:2 w:2) + fn transfer_from() -> Weight { + (21_462_000 as Weight) + .saturating_add(T::DbWeight::get().reads(3 as Weight)) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) + } } +// For backwards compatibility and tests impl WeightInfo for () { - fn create_item() -> Weight {0} - fn burn_item() -> Weight {0} - fn transfer() -> Weight {0} - fn approve() -> Weight {0} - fn transfer_from() -> Weight {0} + // Storage: Fungible Balance (r:1 w:1) + // Storage: Fungible TotalSupply (r:0 w:1) + fn create_item() -> Weight { + (12_069_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(1 as Weight)) + .saturating_add(RocksDbWeight::get().writes(2 as Weight)) + } + // Storage: Fungible TotalSupply (r:1 w:1) + // Storage: Fungible Balance (r:1 w:1) + fn burn_item() -> Weight { + (14_096_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(2 as Weight)) + .saturating_add(RocksDbWeight::get().writes(2 as Weight)) + } + // Storage: Fungible Balance (r:2 w:2) + fn transfer() -> Weight { + (15_436_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(2 as Weight)) + .saturating_add(RocksDbWeight::get().writes(2 as Weight)) + } + // Storage: Fungible Balance (r:1 w:0) + // Storage: Fungible Allowance (r:0 w:1) + fn approve() -> Weight { + (12_867_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(1 as Weight)) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) + } + // Storage: Fungible Allowance (r:1 w:1) + // Storage: Fungible Balance (r:2 w:2) + fn transfer_from() -> Weight { + (21_462_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(3 as Weight)) + .saturating_add(RocksDbWeight::get().writes(3 as Weight)) + } } -- gitstuff