git.delta.rocks / unique-network / refs/commits / 4e1bd65c37e5

difftreelog

feat(nonfungible) benchmarking

Yaroslav Bolyukin2021-10-22parent: #54a17a4.patch.diff
in: master

5 files changed

modifiedpallets/nonfungible/Cargo.tomldiffbeforeafterboth
--- a/pallets/nonfungible/Cargo.toml
+++ b/pallets/nonfungible/Cargo.toml
@@ -20,6 +20,7 @@
 evm-coder = { default-features = false, path = '../../crates/evm-coder' }
 pallet-evm-coder-substrate = { default-features = false, path = '../evm-coder-substrate' }
 ethereum = { default-features = false, version = "0.9.0" }
+frame-benchmarking = { default-features = false, optional = true, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
 
 [features]
 default = ["std"]
@@ -33,5 +34,10 @@
     "evm-coder/std",
     "ethereum/std",
     "pallet-evm-coder-substrate/std",
+    'frame-benchmarking/std',
+]
+runtime-benchmarks = [
+    'frame-benchmarking',
+    'frame-support/runtime-benchmarks',
+    'frame-system/runtime-benchmarks',
 ]
-runtime-benchmarks = []
modifiedpallets/nonfungible/src/benchmarking.rsdiffbeforeafterboth
1use super::*;
2use crate::{Pallet, Config, NonfungibleHandle};
3
4use sp_std::prelude::*;
5use pallet_common::benchmarking::{create_collection_raw, create_data};
6use frame_benchmarking::{benchmarks, account};
7use nft_data_structs::{CollectionMode, MAX_ITEMS_PER_BATCH};
8use pallet_common::bench_init;
9use core::convert::TryInto;
10
11const SEED: u32 = 1;
12
13fn create_max_item_data<T: Config>(owner: T::CrossAccountId) -> CreateItemData<T> {
14 let const_data = create_data(CUSTOM_DATA_LIMIT as usize).try_into().unwrap();
15 let variable_data = create_data(CUSTOM_DATA_LIMIT as usize).try_into().unwrap();
16 CreateItemData {
17 const_data,
18 variable_data,
19 owner,
20 }
21}
22fn create_max_item<T: Config>(
23 collection: &NonfungibleHandle<T>,
24 sender: &T::CrossAccountId,
25 owner: T::CrossAccountId,
26) -> Result<TokenId, DispatchError> {
27 <Pallet<T>>::create_item(&collection, sender, create_max_item_data(owner))?;
28 Ok(TokenId(<TokensMinted<T>>::get(&collection.id)))
29}
30
31fn create_collection<T: Config>(
32 owner: T::AccountId,
33) -> Result<NonfungibleHandle<T>, DispatchError> {
34 create_collection_raw(
35 owner,
36 CollectionMode::NFT,
37 <Pallet<T>>::init_collection,
38 NonfungibleHandle::cast,
39 )
40}
41
1#![cfg(feature = "runtime-benchmarking")]42benchmarks! {
43 create_item {
44 bench_init!{
45 owner: sub; collection: collection(owner);
46 sender: cross_from_sub(owner); to: cross_sub;
47 };
48 }: {create_max_item(&collection, &sender, to.clone())?}
49
50 create_multiple_items {
51 let b in 0..MAX_ITEMS_PER_BATCH;
52 bench_init!{
53 owner: sub; collection: collection(owner);
54 sender: cross_from_sub(owner); to: cross_sub;
55 };
56 let data = (0..b).map(|_| create_max_item_data(to.clone())).collect();
57 }: {<Pallet<T>>::create_multiple_items(&collection, &sender, data)?}
58
59 burn_item {
60 bench_init!{
61 owner: sub; collection: collection(owner);
62 sender: cross_from_sub(owner); burner: cross_sub;
63 };
64 let item = create_max_item(&collection, &sender, burner.clone())?;
65 }: {<Pallet<T>>::burn(&collection, &burner, item)?}
66
67 transfer {
68 bench_init!{
69 owner: sub; collection: collection(owner);
70 owner: cross_from_sub; sender: cross_sub; receiver: cross_sub;
71 };
72 let item = create_max_item(&collection, &owner, sender.clone())?;
73 }: {<Pallet<T>>::transfer(&collection, &sender, &receiver, item)?}
74
75 approve {
76 bench_init!{
77 owner: sub; collection: collection(owner);
78 owner: cross_from_sub; sender: cross_sub; spender: cross_sub;
79 };
80 let item = create_max_item(&collection, &owner, sender.clone())?;
81 }: {<Pallet<T>>::set_allowance(&collection, &sender, item, Some(&spender))?}
82
83 transfer_from {
84 bench_init!{
85 owner: sub; collection: collection(owner);
86 owner: cross_from_sub; sender: cross_sub; spender: cross_sub; receiver: cross_sub;
87 };
88 let item = create_max_item(&collection, &owner, sender.clone())?;
89 <Pallet<T>>::set_allowance(&collection, &sender, item, Some(&spender))?;
90 }: {<Pallet<T>>::transfer_from(&collection, &spender, &sender, &receiver, item)?}
91
92 set_variable_metadata {
93 let b in 0..CUSTOM_DATA_LIMIT;
94 bench_init!{
95 owner: sub; collection: collection(owner);
96 owner: cross_from_sub; sender: cross_sub;
97 };
98 let item = create_max_item(&collection, &owner, sender.clone())?;
99 let data = create_data(b as usize);
100 }: {<Pallet<T>>::set_variable_metadata(&collection, &sender, item, data)?}
101}
2102
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -9,8 +9,8 @@
 use sp_std::vec::Vec;
 
 use crate::{
-	AccountBalance, Allowance, Config, CreateItemData, DataKind, Error, NonfungibleHandle, Owned,
-	Owner, Pallet, SelfWeightOf, TokenData, weights::WeightInfo,
+	AccountBalance, Allowance, Config, CreateItemData, Error, NonfungibleHandle, Owned, Pallet,
+	SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,
 };
 
 pub struct CommonWeights<T: Config>(PhantomData<T>);
@@ -39,8 +39,8 @@
 		<SelfWeightOf<T>>::transfer_from()
 	}
 
-	fn set_variable_metadata(_bytes: u32) -> Weight {
-		<SelfWeightOf<T>>::set_variable_metadata()
+	fn set_variable_metadata(bytes: u32) -> Weight {
+		<SelfWeightOf<T>>::set_variable_metadata(bytes)
 	}
 }
 
@@ -67,7 +67,7 @@
 	) -> DispatchResultWithPostInfo {
 		with_weight(
 			<Pallet<T>>::create_item(self, &sender, map_create_data(data, &to)?),
-			<SelfWeightOf<T>>::create_item(),
+			<CommonWeights<T>>::create_item(),
 		)
 	}
 
@@ -85,7 +85,7 @@
 		let amount = data.len();
 		with_weight(
 			<Pallet<T>>::create_multiple_items(self, &sender, data),
-			<SelfWeightOf<T>>::create_multiple_items(amount as u32),
+			<CommonWeights<T>>::create_multiple_items(amount as u32),
 		)
 	}
 
@@ -99,7 +99,7 @@
 		if amount == 1 {
 			with_weight(
 				<Pallet<T>>::burn(&self, &sender, token),
-				<SelfWeightOf<T>>::burn_item(),
+				<CommonWeights<T>>::burn_item(),
 			)
 		} else {
 			Ok(().into())
@@ -117,7 +117,7 @@
 		if amount == 1 {
 			with_weight(
 				<Pallet<T>>::transfer(&self, &from, &to, token),
-				<SelfWeightOf<T>>::transfer(),
+				<CommonWeights<T>>::transfer(),
 			)
 		} else {
 			Ok(().into())
@@ -139,7 +139,7 @@
 			} else {
 				<Pallet<T>>::set_allowance(&self, &sender, token, None)
 			},
-			<SelfWeightOf<T>>::approve(),
+			<CommonWeights<T>>::approve(),
 		)
 	}
 
@@ -156,7 +156,7 @@
 		if amount == 1 {
 			with_weight(
 				<Pallet<T>>::transfer_from(&self, &sender, &from, &to, token),
-				<SelfWeightOf<T>>::transfer_from(),
+				<CommonWeights<T>>::transfer_from(),
 			)
 		} else {
 			Ok(().into())
@@ -169,9 +169,10 @@
 		token: TokenId,
 		data: Vec<u8>,
 	) -> DispatchResultWithPostInfo {
+		let len = data.len();
 		with_weight(
 			<Pallet<T>>::set_variable_metadata(&self, &sender, token, data),
-			<SelfWeightOf<T>>::set_variable_metadata(),
+			<CommonWeights<T>>::set_variable_metadata(len as u32),
 		)
 	}
 
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -16,6 +16,7 @@
 use codec::{Encode, Decode};
 
 pub use pallet::*;
+#[cfg(feature = "runtime-benchmarks")]
 pub mod benchmarking;
 pub mod common;
 pub mod erc;
modifiedpallets/nonfungible/src/weights.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/weights.rs
+++ b/pallets/nonfungible/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_nonfungible
+//!
+//! 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-nonfungible
+// --wasm-execution
+// compiled
+// --extrinsic
+// *
+// --template
+// .maintain/frame-weight-template.hbs
+// --steps=50
+// --repeat=20
+// --output=./pallets/nonfungible/src/weights.rs
+
+
 #![cfg_attr(rustfmt, rustfmt_skip)]
 #![allow(unused_parens)]
 #![allow(unused_imports)]
@@ -5,33 +29,144 @@
 use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
 use sp_std::marker::PhantomData;
 
+/// Weight functions needed for pallet_nonfungible.
 pub trait WeightInfo {
 	fn create_item() -> Weight;
-	fn create_multiple_items(b: u32) -> Weight;
+	fn create_multiple_items(b: u32, ) -> Weight;
 	fn burn_item() -> Weight;
 	fn transfer() -> Weight;
 	fn approve() -> Weight;
 	fn transfer_from() -> Weight;
-	fn set_variable_metadata() -> Weight;
+	fn set_variable_metadata(b: u32, ) -> Weight;
 }
 
+/// Weights for pallet_nonfungible using the Substrate node and recommended hardware.
 pub struct SubstrateWeight<T>(PhantomData<T>);
 impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
-    fn create_item() -> Weight {0}
-	fn create_multiple_items(_b: u32) -> Weight {0}
-	fn burn_item() -> Weight {0}
-	fn transfer() -> Weight {0}
-	fn approve() -> Weight {0}
-	fn transfer_from() -> Weight {0}
-	fn set_variable_metadata() -> Weight {0}
+	// Storage: Nonfungible TokensMinted (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:1 w:1)
+	// Storage: Nonfungible TokenData (r:0 w:1)
+	// Storage: Nonfungible Owned (r:0 w:1)
+	fn create_item() -> Weight {
+		(16_902_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(2 as Weight))
+			.saturating_add(T::DbWeight::get().writes(4 as Weight))
+	}
+	// Storage: Nonfungible TokensMinted (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:1 w:1)
+	// Storage: Nonfungible TokenData (r:0 w:4)
+	// Storage: Nonfungible Owned (r:0 w:4)
+	fn create_multiple_items(b: u32, ) -> Weight {
+		(15_860_000 as Weight)
+			// Standard Error: 5_000
+			.saturating_add((3_916_000 as Weight).saturating_mul(b as Weight))
+			.saturating_add(T::DbWeight::get().reads(2 as Weight))
+			.saturating_add(T::DbWeight::get().writes(2 as Weight))
+			.saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(b as Weight)))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	// Storage: Nonfungible TokensBurnt (r:1 w:1)
+	// Storage: Nonfungible Allowance (r:1 w:0)
+	// Storage: Nonfungible Owned (r:0 w:1)
+	fn burn_item() -> Weight {
+		(17_966_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(3 as Weight))
+			.saturating_add(T::DbWeight::get().writes(3 as Weight))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:2 w:2)
+	// Storage: Nonfungible Allowance (r:1 w:0)
+	// Storage: Nonfungible Owned (r:0 w:2)
+	fn transfer() -> Weight {
+		(23_886_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(4 as Weight))
+			.saturating_add(T::DbWeight::get().writes(5 as Weight))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:0)
+	// Storage: Nonfungible Allowance (r:1 w:1)
+	fn approve() -> Weight {
+		(14_697_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(2 as Weight))
+			.saturating_add(T::DbWeight::get().writes(1 as Weight))
+	}
+	// Storage: Nonfungible Allowance (r:1 w:1)
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:2 w:2)
+	// Storage: Nonfungible Owned (r:0 w:2)
+	fn transfer_from() -> Weight {
+		(28_001_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(4 as Weight))
+			.saturating_add(T::DbWeight::get().writes(6 as Weight))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	fn set_variable_metadata(_b: u32, ) -> Weight {
+		(6_380_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(1 as Weight))
+			.saturating_add(T::DbWeight::get().writes(1 as Weight))
+	}
 }
 
+// For backwards compatibility and tests
 impl WeightInfo for () {
-    fn create_item() -> Weight {0}
-	fn create_multiple_items(_b: u32) -> Weight {0}
-	fn burn_item() -> Weight {0}
-	fn transfer() -> Weight {0}
-	fn approve() -> Weight {0}
-	fn transfer_from() -> Weight {0}
-	fn set_variable_metadata() -> Weight {0}
+	// Storage: Nonfungible TokensMinted (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:1 w:1)
+	// Storage: Nonfungible TokenData (r:0 w:1)
+	// Storage: Nonfungible Owned (r:0 w:1)
+	fn create_item() -> Weight {
+		(16_902_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(4 as Weight))
+	}
+	// Storage: Nonfungible TokensMinted (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:1 w:1)
+	// Storage: Nonfungible TokenData (r:0 w:4)
+	// Storage: Nonfungible Owned (r:0 w:4)
+	fn create_multiple_items(b: u32, ) -> Weight {
+		(15_860_000 as Weight)
+			// Standard Error: 5_000
+			.saturating_add((3_916_000 as Weight).saturating_mul(b as Weight))
+			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
+			.saturating_add(RocksDbWeight::get().writes((2 as Weight).saturating_mul(b as Weight)))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	// Storage: Nonfungible TokensBurnt (r:1 w:1)
+	// Storage: Nonfungible Allowance (r:1 w:0)
+	// Storage: Nonfungible Owned (r:0 w:1)
+	fn burn_item() -> Weight {
+		(17_966_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(3 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(3 as Weight))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:2 w:2)
+	// Storage: Nonfungible Allowance (r:1 w:0)
+	// Storage: Nonfungible Owned (r:0 w:2)
+	fn transfer() -> Weight {
+		(23_886_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(4 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(5 as Weight))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:0)
+	// Storage: Nonfungible Allowance (r:1 w:1)
+	fn approve() -> Weight {
+		(14_697_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
+	}
+	// Storage: Nonfungible Allowance (r:1 w:1)
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:2 w:2)
+	// Storage: Nonfungible Owned (r:0 w:2)
+	fn transfer_from() -> Weight {
+		(28_001_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(4 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(6 as Weight))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	fn set_variable_metadata(_b: u32, ) -> Weight {
+		(6_380_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
+	}
 }