Skip to content

Commit

Permalink
benchmarks and weights
Browse files Browse the repository at this point in the history
  • Loading branch information
dudo50 committed Nov 6, 2024
1 parent ab932b9 commit eb12016
Show file tree
Hide file tree
Showing 19 changed files with 628 additions and 261 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions templates/parachain/pallets/xcnft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ scale-info = { features = [
"derive",
], workspace = true }
log = { workspace = true }
enumflags2 = { workspace = true }


# Substrate
Expand Down
189 changes: 129 additions & 60 deletions templates/parachain/pallets/xcnft/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,99 +1,168 @@
#![cfg(feature = "runtime-benchmarks")]
use super::*;

#[allow(unused)]
use crate::Pallet as Template;
use super::*;

use cumulus_primitives_core::ParaId;
use frame_benchmarking::v2::*;
use frame_system::RawOrigin;
use sp_runtime::traits::Hash;
use sp_runtime::traits::{Bounded, StaticLookup};
use pallet_nfts::{
CollectionSetting, CollectionSettings, CollectionConfig,
CollectionConfigFor, MintSettings, BenchmarkHelper, DepositBalanceOf,
};
use enumflags2::{BitFlag, BitFlags};
use frame_support::{traits::Currency, assert_ok, BoundedVec};
use sp_std::vec;

fn make_collection_config<T: Config<I>, I: 'static>(
disable_settings: BitFlags<CollectionSetting>,
) -> CollectionConfigFor<T, I> {
CollectionConfig {
settings: CollectionSettings::from_disabled(disable_settings),
max_supply: None,
mint_settings: MintSettings::default(),
}
}

fn default_collection_config<T: Config<I>, I: 'static>() -> CollectionConfigFor<T, I> {
make_collection_config::<T, I>(CollectionSetting::empty())
}

#[benchmarks]

#[instance_benchmarks]
mod benchmarks {
use super::*;

// Benchmark tries the first scenario of collection_x_transfer (transfering empty collection)
#[benchmark]
fn transfer_collection_empty<T: Config<I>, I: 'static>() {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
let collection = T::Helper::collection(0);

T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());

assert_ok!(pallet_nfts::Pallet::<T, I>::create(RawOrigin::Signed(caller.clone()).into(), caller_lookup.clone() ,default_collection_config::<T, I>()));

#[extrinsic_call]
collection_x_transfer(RawOrigin::Signed(caller.into()),collection.clone() , None, 1000.into(), None);
}

// Benchmark tries the second scenario of collection_x_transfer (transfering collection with items same owner)
#[benchmark]
fn mint_collection_large() {
fn transfer_collection_same_owner<T: Config<I>, I: 'static>() {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
let collection = T::Helper::collection(0);
let item = T::Helper::item(0);

T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());

assert_ok!(pallet_nfts::Pallet::<T, I>::create(RawOrigin::Signed(caller.clone()).into(), caller_lookup.clone() ,default_collection_config::<T, I>()));
assert_ok!(pallet_nfts::Pallet::<T, I>::mint(RawOrigin::Signed(caller.clone()).into(), collection.clone(), item.clone(), caller_lookup.clone(), None));


#[extrinsic_call]
mint_collection(RawOrigin::Signed(caller),b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap());
collection_x_transfer(RawOrigin::Signed(caller.into()),collection.clone() , None, 1000.into(), None);
}

// Benchmark tries the third scenario of collection_x_transfer (transfering collection with items different owners)
#[benchmark]
fn mint_non_fungible_large() {
fn transfer_collection_other_owners<T: Config<I>, I: 'static>() {
let caller: T::AccountId = whitelisted_caller();
let caller2: T::AccountId = account("caller2", 1, 1);
let caller_lookup = T::Lookup::unlookup(caller.clone());
let caller_lookup2 = T::Lookup::unlookup(caller2.clone());
let collection = T::Helper::collection(0);
let item = T::Helper::item(0);
let item2 = T::Helper::item(1);

let collection: pallet::Collection<T> = Collection {
owner: caller.clone(),
collection_name: b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(),
collection_description: b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(),
collection_origin_parachain_id: 0.into(),
};

let collection_hash = T::Hashing::hash_of(&collection);
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());

let collection_with_hash: pallet::CollectionWithHash<T> = CollectionWithHash {
owner: caller.clone(),
collection_name: b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(),
collection_description: b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(),
collection_origin_parachain_id: 0.into(),
collection_hash: collection_hash,
};
assert_ok!(pallet_nfts::Pallet::<T, I>::create(RawOrigin::Signed(caller.clone()).into(), caller_lookup.clone() ,default_collection_config::<T, I>()));
assert_ok!(pallet_nfts::Pallet::<T, I>::mint(RawOrigin::Signed(caller.clone()).into(), collection.clone(), item.clone(), caller_lookup.clone(), None));
assert_ok!(pallet_nfts::Pallet::<T, I>::mint(RawOrigin::Signed(caller.clone()).into(), collection.clone(), item2.clone(), caller_lookup2.clone(), None));

pallet::Collections::<T>::insert(collection_hash, collection_with_hash);
pallet::CollectionSize::<T>::insert(collection_hash, 0);

#[extrinsic_call]
mint_non_fungible(RawOrigin::Signed(caller),b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(),collection_hash);
collection_x_transfer(RawOrigin::Signed(caller.into()),collection.clone() , None, 1000.into(), None);
}

//Benchmark tries nft transfer
#[benchmark]
fn mint_collection_received_large() {
fn transfer_nft<T: Config<I>, I: 'static>() {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
let collection = T::Helper::collection(0);
let item = T::Helper::item(0);

T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());

assert_ok!(pallet_nfts::Pallet::<T, I>::create(RawOrigin::Signed(caller.clone()).into(), caller_lookup.clone() ,default_collection_config::<T, I>()));
assert_ok!(pallet_nfts::Pallet::<T, I>::mint(RawOrigin::Signed(caller.clone()).into(), collection.clone(), item.clone(), caller_lookup.clone(), None));

#[extrinsic_call]
mint_collection_received(RawOrigin::Signed(caller),b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(),1000.into(), caller.clone());
nft_x_transfer(RawOrigin::Signed(caller.into()),collection.clone() , item.clone(), 1000.into(), collection.clone() , item.clone());
}

//Benchmark tries collection empty parse
#[benchmark]
fn parse_empty_col<T: Config<I>, I: 'static>() {
let caller: T::AccountId = whitelisted_caller();
let collection = T::Helper::collection(0);

T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());

#[extrinsic_call]
parse_collection_empty(RawOrigin::Signed(caller.into()),collection.clone() , None, BoundedVec::new(), None);
}

//Benchmark tries collection parse with items
#[benchmark]
fn parse_same_owner_col<T: Config<I>, I: 'static>() {
let caller: T::AccountId = whitelisted_caller();
let collection = T::Helper::collection(0);
let item = T::Helper::item(0);
let nfts = vec![(item.clone(), BoundedVec::new())];

T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());


#[extrinsic_call]
parse_collection_same_owner(RawOrigin::Signed(caller.into()),None, BoundedVec::new(), nfts.clone() ,1000.into(), collection.clone(), None);
}

//Benchmark tries collection parse with items and different owners
#[benchmark]
fn parse_diff_owner_col<T: Config<I>, I: 'static>() {
let caller: T::AccountId = whitelisted_caller();
let caller2: T::AccountId = account("caller2", 1, 1);
let caller_lookup2 = T::Lookup::unlookup(caller2.clone());
let collection = T::Helper::collection(0);
let item = T::Helper::item(0);
let nfts = vec![(item.clone(), caller_lookup2, BoundedVec::new())];

T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());

#[extrinsic_call]
parse_collection_diff_owners(RawOrigin::Signed(caller.into()),None, BoundedVec::new(), nfts.clone() ,1000.into(), collection.clone(), None);
}

//Benchmark tries collection parse item
#[benchmark]
fn mint_non_fungible_received_large() {
fn parse_item<T: Config<I>, I: 'static>() {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
let collection = T::Helper::collection(0);
let item = T::Helper::item(0);

T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());

let collection: pallet::Collection<T> = Collection {
owner: caller.clone(),
collection_name: b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(),
collection_description: b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(),
collection_origin_parachain_id: 0.into(),
};

let collection_hash = T::Hashing::hash_of(&collection);

let collection_with_hash: pallet::CollectionWithHash<T> = CollectionWithHash {
owner: caller.clone(),
collection_name: b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(),
collection_description: b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(),
collection_origin_parachain_id: 0.into(),
collection_hash: collection_hash,
};

let parachain_id: ParaId = 0.into();
let _ = ReceivedCollections::<T>::mutate(parachain_id, |x| -> Result<(), ()> {
if let Some(x) = x {
x.try_push(collection_with_hash).map_err(|_| ())?;
Ok(())
} else {
*x = Some(vec![collection_with_hash].try_into().map_err(|_| ())?);
Ok(())
}
});

pallet::CollectionSize::<T>::insert(collection_hash, 0);
assert_ok!(pallet_nfts::Pallet::<T, I>::create(RawOrigin::Signed(caller.clone()).into(), caller_lookup.clone() ,default_collection_config::<T, I>()));

#[extrinsic_call]
mint_non_fungible_received(RawOrigin::Signed(caller),b"pHgJCOTaAPedH0mtvRVVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(), b"pHgJCOTaAPEceLC69VVtYyApzDcR0WebyTdJw1sSIRTozxXKFI3cA91Yv3ZzFk5ZH00J2SC7a3aFDrlt5rPIpwGO5UE6jSplFqdu7AhoEf8t7D6aD5CDBGOL8AZnllhAIBKBLgspdsSGoacIWx0CLFpPF2ALtm1iitrDo4B39sZC2ne9PGFIe4C1PazXLnWwbF0Kea3akaoNv6HRqKWgFNv4VTxdhCoWDsAzbzzP3GSyBYuSwBlhXP".to_vec().try_into().unwrap(),collection_hash, 0.into(), caller.clone());
parse_nft_transfer(RawOrigin::Signed(caller.into()),collection.clone(), item.clone(), BoundedVec::new(), collection.clone(), item.clone() ,1000.into());
}

impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test);
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
}
2 changes: 1 addition & 1 deletion templates/parachain/pallets/xcnft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub mod tests;

pub mod weights;

#[cfg(feature = "runtime-benchmarks")]
#[cfg_attr(feature = "runtime-benchmarks", recursion_limit = "256")]
mod benchmarking;

#[frame_support::pallet]
Expand Down
2 changes: 2 additions & 0 deletions templates/parachain/pallets/xcnft/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ impl pallet_nfts::Config for Test {
type Features = NftFeatures;
type OffchainSignature = MultiSignature;
type OffchainPublic = AccountPublic;
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
type WeightInfo = ();
}

Expand Down
2 changes: 2 additions & 0 deletions templates/parachain/pallets/xcnft/src/tests/testpara.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ impl pallet_nfts::Config for Runtime {
type Features = NftFeatures;
type OffchainSignature = MultiSignature;
type OffchainPublic = AccountPublic;
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
type WeightInfo = ();
}

Expand Down
2 changes: 2 additions & 0 deletions templates/parachain/pallets/xcnft/src/tests/testrelay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ impl pallet_nfts::Config for Runtime {
type Features = NftFeatures;
type OffchainSignature = MultiSignature;
type OffchainPublic = AccountPublic;
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
type WeightInfo = ();
}

Expand Down
Loading

0 comments on commit eb12016

Please sign in to comment.