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

Benchmarking workflow #290

Merged
merged 18 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 16 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
2 changes: 0 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ on:
- "**.md"
env:
CARGO_TERM_COLOR: always
GCP_ZONE: europe-west3-a

jobs:
check_branch:
Expand All @@ -41,7 +40,6 @@ jobs:
image_family: ubuntu-2004-lts
machine_type: e2-highcpu-32
disk_size: 100
machine_zone: ${{ env.GCP_ZONE }}
ephemeral: true

test-features:
Expand Down
7 changes: 0 additions & 7 deletions ci/jobs/build-and-test.sh

This file was deleted.

7 changes: 0 additions & 7 deletions ci/jobs/clippy.sh

This file was deleted.

3 changes: 3 additions & 0 deletions libs/common-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ std = [
'sp-std/std',
'xcm/std',
]

runtime-benchmarks = []

try-runtime = [
"common-traits/try-runtime",
"frame-support/try-runtime",
Expand Down
83 changes: 73 additions & 10 deletions libs/common-types/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,79 @@ pub enum CurrencyId {
ForeignAsset(ForeignAssetId),
}

#[derive(
Clone,
Copy,
PartialOrd,
Ord,
PartialEq,
Eq,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
Serialize,
Deserialize,
)]
pub enum ForeignAssetId {
ETH,
USDT,
}

#[derive(
Clone,
Copy,
PartialOrd,
Ord,
PartialEq,
Eq,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
Serialize,
Deserialize,
)]
/// The foreign owned account describes the chain
pub enum ForeignOwnedAccount {
TRON([u8; 22]),
ETH([u8; 20]),
}

impl ForeignOwnedAccount {
/// Here we can define which currencies per network we support
/// For example when given a TRON account we can use this to see if the account
/// and the currency are compatible.
pub fn ensure_supported_currency(&self, currency: CurrencyId) -> bool {
match currency {
CurrencyId::Native => false,
CurrencyId::KSM => false,
CurrencyId::AUSD => false,
CurrencyId::KAR => false,
CurrencyId::MGX => false,
CurrencyId::ForeignAsset(asset) => match &self {
ForeignOwnedAccount::TRON(_) => match asset {
ForeignAssetId::ETH => false,
ForeignAssetId::USDT => true,
},
ForeignOwnedAccount::ETH(_) => match asset {
ForeignAssetId::ETH => true,
ForeignAssetId::USDT => true,
},
},
}
}
#[cfg(feature = "runtime-benchmarks")]
pub fn get_supported_currency_eoa_combo() -> (ForeignOwnedAccount, CurrencyId) {
(
ForeignOwnedAccount::ETH(Default::default()),
CurrencyId::ForeignAsset(ForeignAssetId::ETH),
)
}
}

pub mod currency_decimals {
pub const NATIVE: u32 = 12;
pub const AUSD: u32 = 12;
Expand All @@ -39,16 +112,6 @@ pub mod currency_decimals {
pub const MGX: u32 = 18;
}

// A way to generate different currencies from a number.
// Can be used in tests/benchmarks to generate different currencies.
impl From<u32> for CurrencyId {
fn from(value: u32) -> Self {
CurrencyId::ForeignAsset(value)
}
}

pub type ForeignAssetId = u32;

#[derive(
Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen,
)]
Expand Down
1 change: 1 addition & 0 deletions pallets/briefs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ std = [

runtime-benchmarks = [
"common-runtime/runtime-benchmarks",
"common-types/runtime-benchmarks",
"frame-benchmarking/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"pallet-deposits/runtime-benchmarks",
Expand Down
63 changes: 40 additions & 23 deletions pallets/briefs/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::*;
use crate::test_utils::gen_hash;
use crate::Pallet as Briefs;
use crate::{BoundedBriefOwners, BoundedProposedMilestones};
use common_types::CurrencyId;
use common_types::{CurrencyId, ForeignOwnedAccount};
use frame_benchmarking::v2::*;
use frame_support::{assert_ok, traits::Get};
use frame_system::{EventRecord, RawOrigin};
Expand All @@ -22,14 +22,15 @@ mod benchmarks {

#[benchmark]
fn create_brief() {
let brief_owners = get_max_brief_owners::<T>();
let (eoa, currency_id) = ForeignOwnedAccount::get_supported_currency_eoa_combo();
let brief_owners = get_max_brief_owners::<T>(currency_id);
let caller: T::AccountId = brief_owners[0].clone();
let applicant = create_account_id::<T>("applicant", 1);
let applicant = create_account_id::<T>("applicant", 1, currency_id);
let budget = 10_000u32.into();
let initial_contribution = 5_000u32.into();
let brief_id = gen_hash(1);
let milestones = get_max_milestones::<T>();
// (origin, brief_owners, applicant, budget, initial_contribution, brief_id, currency_id, milestones)
// (origin, brief_owners, applicant, budget, initial_contribution, brief_id, currency_id, milestones, Option<eoa>)

#[extrinsic_call]
create_brief(
Expand All @@ -39,17 +40,20 @@ mod benchmarks {
budget,
initial_contribution,
brief_id,
CurrencyId::Native,
currency_id,
milestones,
Some(eoa),
false,
);
assert_last_event::<T>(Event::<T>::BriefSubmitted(caller, brief_id).into());
}

#[benchmark]
fn contribute_to_brief() {
let brief_owners = get_max_brief_owners::<T>();
let currency_id = CurrencyId::Native;
let brief_owners = get_max_brief_owners::<T>(currency_id);
let caller: T::AccountId = brief_owners[0].clone();
let applicant: T::AccountId = create_account_id::<T>("applicant", 1);
let applicant: T::AccountId = create_account_id::<T>("applicant", 1, currency_id);
let budget = 10_000_000_000_000u128.saturated_into();
let initial_contribution = 5_000_000_000_000u128.saturated_into();
let contribution = 5_000_000_000_000u128.saturated_into();
Expand All @@ -62,8 +66,10 @@ mod benchmarks {
budget,
initial_contribution,
brief_id,
CurrencyId::Native,
milestones
currency_id,
milestones,
None,
false,
));
let brief_owner: T::AccountId = brief_owners[0].clone();
// (brief_owner, brief_id, contribution)
Expand All @@ -78,22 +84,26 @@ mod benchmarks {

#[benchmark]
fn commence_work() {
let brief_owners = get_max_brief_owners::<T>();
let currency_id = CurrencyId::Native;
let brief_owners = get_max_brief_owners::<T>(currency_id);
let caller: T::AccountId = brief_owners[0].clone();
let applicant: T::AccountId = create_account_id::<T>("applicant", 1);
let applicant: T::AccountId = create_account_id::<T>("applicant", 1, currency_id);
let budget = 10_000_000_000_000u128.saturated_into();
let initial_contribution = 5_000_000_000_000u128.saturated_into();
let brief_id = gen_hash(1);
let milestones = get_max_milestones::<T>();

assert_ok!(Briefs::<T>::create_brief(
RawOrigin::Signed(caller).into(),
brief_owners,
applicant.clone(),
budget,
initial_contribution,
brief_id,
CurrencyId::Native,
milestones
currency_id,
milestones,
None,
false,
));
// (origin, brief_id)
#[extrinsic_call]
Expand All @@ -103,9 +113,10 @@ mod benchmarks {

#[benchmark]
fn cancel_brief() {
let brief_owners = get_max_brief_owners::<T>();
let currency_id = CurrencyId::Native;
let brief_owners = get_max_brief_owners::<T>(currency_id);
let caller: T::AccountId = brief_owners[0].clone();
let applicant: T::AccountId = create_account_id::<T>("applicant", 1);
let applicant: T::AccountId = create_account_id::<T>("applicant", 1, currency_id);
let budget = 10_000_000_000_000u128.saturated_into();
let initial_contribution = 5_000_000_000_000u128.saturated_into();
let brief_id = gen_hash(1);
Expand All @@ -117,8 +128,10 @@ mod benchmarks {
budget,
initial_contribution,
brief_id,
CurrencyId::Native,
milestones
currency_id,
milestones,
None,
false,
));
// (origin, brief_id)
#[extrinsic_call]
Expand All @@ -133,11 +146,15 @@ mod benchmarks {
);
}

fn create_account_id<T: Config>(suri: &'static str, n: u32) -> T::AccountId {
fn create_account_id<T: Config>(
suri: &'static str,
n: u32,
currency_id: CurrencyId,
) -> T::AccountId {
let user = account(suri, n, SEED);
let initial_balance = 1_000_000_000_000_000u128;
assert_ok!(T::RMultiCurrency::deposit(
CurrencyId::Native,
currency_id,
&user,
initial_balance.saturated_into()
));
Expand All @@ -152,21 +169,21 @@ fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
assert_eq!(event, &system_event);
}

fn get_brief_owners<T: Config>(mut n: u32) -> BoundedBriefOwners<T> {
fn get_brief_owners<T: Config>(mut n: u32, currency: CurrencyId) -> BoundedBriefOwners<T> {
let max = <T as Config>::MaxBriefOwners::get();
if n > max {
n = max;
}
(0..n)
.map(|i| create_account_id::<T>("brief_owner", i))
.map(|i| create_account_id::<T>("brief_owner", i, currency))
.collect::<Vec<T::AccountId>>()
.try_into()
.expect("qed")
}

fn get_max_brief_owners<T: Config>() -> BoundedBriefOwners<T> {
fn get_max_brief_owners<T: Config>(currency_id: CurrencyId) -> BoundedBriefOwners<T> {
let max_brief_owners: u32 = <T as Config>::MaxBriefOwners::get();
get_brief_owners::<T>(max_brief_owners)
get_brief_owners::<T>(max_brief_owners, currency_id)
}

fn get_milestones<T: Config>(mut n: u32) -> BoundedProposedMilestones<T> {
Expand Down
2 changes: 2 additions & 0 deletions pallets/briefs/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ fn create_proposal_from_brief() {
brief_id,
CurrencyId::Native,
get_milestones(10),
None,
false,
);

assert_ok!(BriefsMod::commence_work(
Expand Down
Loading
Loading