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

benchmarks for pallets #27

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions Cargo.lock

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

27 changes: 18 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
[workspace.package]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT summary of c2a02c - 76c07f:

  • Added new [workspace.package] section with authors, edition, homepage, repository, and license fields.
  • Added 'pallets/example-basic' to the [workspace] members.
  • Added 'panic = 'unwind'' to the [profile.release] section.

authors = ["Hashed <https://github.com/hashed-io"]
edition = "2021"
homepage = "https://hashed.io"
repository = "https://github.com/hashed-io/hashed-pallets"
license = "MIT"

[workspace]
members = [
'pallets/bitcoin-vaults',
'pallets/confidential-docs',
'pallets/rbac',
'pallets/fruniques',
'pallets/gated-marketplace',
'pallets/mapped-assets',
'pallets/afloat',
'pallets/fund-admin',
'pallets/fund-admin-records',
'pallets/bitcoin-vaults',
'pallets/confidential-docs',
'pallets/rbac',
'pallets/fruniques',
'pallets/gated-marketplace',
'pallets/mapped-assets',
'pallets/afloat',
'pallets/fund-admin',
'pallets/fund-admin-records',
'pallets/example-basic'
]

[profile.release]
panic = 'unwind'
14 changes: 7 additions & 7 deletions pallets/afloat/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT summary of a36bc6 - db42c4:

  • Updated version to 4.0.0-dev
  • Set authors, homepage, edition, license, repository, readme to workspace
  • Updated metadata docs.rs targets to x86_64-unknown-linux-gnu

name = "pallet-afloat"
version = "4.0.0-dev"
description = ""
authors = ["Hashed <https://github.com/hashed-io"]
homepage = "https://hashed.io"
edition = "2021"
license = "MIT"
publish = false
repository = "https://github.com/hashed-io/hashed-pallets"
version = "4.0.0-dev"
authors.workspace = true
homepage.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
Expand Down
8 changes: 4 additions & 4 deletions pallets/afloat/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,22 +969,22 @@ impl<T: Config> Pallet<T> {
ensure!(is_admin_or_owner || offer.creator_id == who, Error::<T>::Unauthorized);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT summary of 6a0c05 - f30747:

  • Changed the casing of OfferStatus::CREATED to OfferStatus::Created
  • Changed the casing of OfferStatus::TF_PENDING_SIGNATURE to OfferStatus::PendingTFSignature
  • Added a cancellation_date field to both OfferStatus::Created and OfferStatus::PendingTFSignature
  • Changed the OfferStatus of both OfferStatus::Created and OfferStatus::PendingTFSignature to OfferStatus::Cancelled


match offer.status {
OfferStatus::CREATED => {
OfferStatus::Created => {
<AfloatOffers<T>>::try_mutate(order_id, |offer| -> DispatchResult {
let offer = offer.as_mut().ok_or(Error::<T>::OfferNotFound)?;
offer.cancellation_date =
pallet_gated_marketplace::Pallet::<T>::get_timestamp_in_milliseconds();
offer.status = OfferStatus::CANCELLED;
offer.status = OfferStatus::Cancelled;
Ok(())
})?;
Ok(())
},
OfferStatus::TF_PENDING_SIGNATURE => {
OfferStatus::PendingTFSignature => {
<AfloatOffers<T>>::try_mutate(order_id, |offer| -> DispatchResult {
let offer = offer.as_mut().ok_or(Error::<T>::OfferNotFound)?;
offer.cancellation_date =
pallet_gated_marketplace::Pallet::<T>::get_timestamp_in_milliseconds();
offer.status = OfferStatus::CANCELLED;
offer.status = OfferStatus::Cancelled;
Ok(())
})?;
Ok(())
Expand Down
3 changes: 1 addition & 2 deletions pallets/afloat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ pub mod types;
pub mod pallet {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT summary of 7bbf98 - 5e522f:

  • Removed Time trait from frame_support::traits
  • Added RawOrigin from frame_system
  • Removed Scale trait from sp_runtime::traits
  • Updated StorageVersion to StorageVersion::new(0)

use frame_support::{
pallet_prelude::*,
traits::{Currency, Time},
traits::Currency,
};
use frame_system::{pallet_prelude::*, RawOrigin};
use pallet_fruniques::types::{Attributes, CollectionDescription, FruniqueRole, ParentInfo};
use pallet_gated_marketplace::types::*;
use sp_runtime::traits::Scale;
const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);

use crate::types::*;
Expand Down
1 change: 0 additions & 1 deletion pallets/afloat/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT summary of b3edb9 - 0295d5:

  • Removed type alias for UncheckedExtrinsic
  • Added type alias for Block
  • Imported EnsureRoot and EnsureSigned from system module

BuildStorage,
};
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
use frame_system::EnsureRoot;
use system::EnsureSigned;
Expand Down
22 changes: 11 additions & 11 deletions pallets/afloat/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ pub enum SignUpArgs {
Encode, Decode, Clone, Eq, PartialEq, RuntimeDebugNoBound, MaxEncodedLen, TypeInfo, Copy,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT summary of 4b5dc0 - e8b575:

  • Renamed CREATED, MATCHED, TF_FILLED, TF_PENDING_SIGNATURE, TF_SIGNED, TF_SUBMITTED, TF_APPROVED, APPROVED, FILLED, CANCELLED to Created, Matched, FilledByTF, PendingTFSignature, SignedTF, SubmittedTF, ApprovedTF, Approved, Filled, Cancelled respectively.

)]
pub enum OfferStatus {
CREATED,
MATCHED,
TF_FILLED,
TF_PENDING_SIGNATURE,
TF_SIGNED,
TF_SUBMITTED,
TF_APPROVED,
APPROVED,
FILLED,
CANCELLED,
Created,
Matched,
FilledByTF,
PendingTFSignature,
SignedTF,
SubmittedTF,
ApprovedTF,
Approved,
Filled,
Cancelled,
}

impl Default for OfferStatus {
fn default() -> Self {
OfferStatus::CREATED
OfferStatus::Created
}
}

Expand Down
14 changes: 7 additions & 7 deletions pallets/bitcoin-vaults/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT summary of ff18ca - f4345f:

  • Updated version from "4.0.0-dev" to "4.0.0-dev"
  • Added authors.workspace, homepage.workspace, edition.workspace, license.workspace, repository.workspace, and readme attributes
  • Updated targets from ["x86_64-unknown-linux-gnu"]

name = "pallet-bitcoin-vaults"
version = "4.0.0-dev"
description = ""
authors = ["Hashed <https://github.com/hashed-io"]
homepage = "https://hashed.io"
edition = "2021"
license = "MIT"
publish = false
repository = "https://github.com/hashed-io/hashed-pallets"
version = "4.0.0-dev"
authors.workspace = true
homepage.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
Expand Down
14 changes: 7 additions & 7 deletions pallets/confidential-docs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT summary of c0dc16 - c5e4d5:

  • Changed version, authors, homepage, edition, license, publish, and repository fields
  • Added authors.workspace, homepage.workspace, edition.workspace, license.workspace, repository.workspace, and readme fields
  • Updated docs.rs targets to include x86_64-unknown-linux-gnu

name = "pallet-confidential-docs"
version = "4.0.0-dev"
description = "Provides backend services for the confidentials docs solution"
authors = ["Hashed <https://github.com/hashed-io"]
homepage = "https://hashed.io"
edition = "2021"
license = "Unlicense"
publish = false
repository = "https://github.com/hashed-io/hashed-pallets"
version = "4.0.0-dev"
authors.workspace = true
homepage.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
Expand Down
59 changes: 59 additions & 0 deletions pallets/example-basic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[package]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT summary of None - b5683a:

  • Added name, description, version, authors, homepage, edition, license, repository, and readme to [package] section
  • Added targets to [package.metadata.docs.rs] section
  • Added dependencies to [dependencies] section
  • Added dev-dependencies to [dev-dependencies] section
  • Added default, std, runtime-benchmarks, and try-runtime features to [features] section

name = "pallet-example-basic"
description = "FRAME example pallet"
version = "4.0.0-dev"
authors.workspace = true
homepage.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false }
log = { version = "0.4.17", default-features = false }
scale-info = { version = "2.10.0", default-features = false, features = [
"derive"
] }
frame-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk", tag = "v1.3.0-rc1", default-features = false, optional = true }
frame-support = { git = "https://github.com/paritytech/polkadot-sdk", tag = "v1.3.0-rc1", default-features = false }
frame-system = { git = "https://github.com/paritytech/polkadot-sdk", tag = "v1.3.0-rc1", default-features = false }
pallet-balances = { git = "https://github.com/paritytech/polkadot-sdk", tag = "v1.3.0-rc1", default-features = false }
sp-io = { git = "https://github.com/paritytech/polkadot-sdk", tag = "v1.3.0-rc1", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk", tag = "v1.3.0-rc1", default-features = false }
sp-std = { git = "https://github.com/paritytech/polkadot-sdk", tag = "v1.3.0-rc1", default-features = false }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/polkadot-sdk", tag = "v1.3.0-rc1", default-features = false }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-balances/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-balances/try-runtime",
"sp-runtime/try-runtime",
]
File renamed without changes.
118 changes: 118 additions & 0 deletions pallets/example-basic/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// This file is part of Substrate.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT summary of None - 4b2ebb:

  • Added benchmarking for pallet-example-basic
  • Benchmarked the execution time of set_dummy and accumulate_dummy
  • Benchmarked the execution time of sorting a vector


// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.

//! Benchmarking for `pallet-example-basic`.

// Only enable this module for benchmarking.
#![cfg(feature = "runtime-benchmarks")]

use crate::*;
use frame_benchmarking::v2::*;
use frame_system::RawOrigin;

// To actually run this benchmark on pallet-example-basic, we need to put this pallet into the
// runtime and compile it with `runtime-benchmarks` feature. The detail procedures are
// documented at:
// https://docs.substrate.io/reference/how-to-guides/weights/add-benchmarks/
//
// The auto-generated weight estimate of this pallet is copied over to the `weights.rs` file.
// The exact command of how the estimate generated is printed at the top of the file.

// Details on using the benchmarks macro can be seen at:
// https://paritytech.github.io/substrate/master/frame_benchmarking/trait.Benchmarking.html#tymethod.benchmarks
#[benchmarks]
mod benchmarks {
use super::*;

// This will measure the execution time of `set_dummy`.
#[benchmark]
fn set_dummy_benchmark() {
// This is the benchmark setup phase.
// `set_dummy` is a constant time function, hence we hard-code some random value here.
let value = 1000u32.into();
#[extrinsic_call]
set_dummy(RawOrigin::Root, value); // The execution phase is just running `set_dummy` extrinsic call

// This is the optional benchmark verification phase, asserting certain states.
assert_eq!(Pallet::<T>::dummy(), Some(value))
}

// An example method that returns a Result that can be called within a benchmark
fn example_result_method() -> Result<(), BenchmarkError> {
Ok(())
}

// This will measure the execution time of `accumulate_dummy`.
// The benchmark execution phase is shorthanded. When the name of the benchmark case is the same
// as the extrinsic call. `_(...)` is used to represent the extrinsic name.
// The benchmark verification phase is omitted.
#[benchmark]
fn accumulate_dummy() -> Result<(), BenchmarkError> {
let value = 1000u32.into();
// The caller account is whitelisted for DB reads/write by the benchmarking macro.
let caller: T::AccountId = whitelisted_caller();

// an example of calling something result-based within a benchmark using the ? operator
// this necessitates specifying the `Result<(), BenchmarkError>` return type
example_result_method()?;

// You can use `_` if the name of the Call matches the benchmark name.
#[extrinsic_call]
_(RawOrigin::Signed(caller), value);

// need this to be compatible with the return type
Ok(())
}

/// You can write helper functions in here since its a normal Rust module.
fn setup_vector(len: u32) -> Vec<u32> {
let mut vector = Vec::<u32>::new();
for i in (0..len).rev() {
vector.push(i);
}
vector
}

// This will measure the execution time of sorting a vector.
//
// Define `x` as a linear component with range `[0, =10_000]`. This means that the benchmarking
// will assume that the weight grows at a linear rate depending on `x`.
#[benchmark]
fn sort_vector(x: Linear<0, 10_000>) {
let mut vector = setup_vector(x);

// The benchmark execution phase could also be a closure with custom code:
#[block]
{
vector.sort();
}

// Check that it was sorted correctly. This will not be benchmarked and is just for
// verification.
vector.windows(2).for_each(|w| assert!(w[0] <= w[1]));
}

// 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::tests::new_test_ext(), crate::tests::Test);
}
Loading
Loading