Skip to content

Commit

Permalink
Merge branch 'benchmarking'
Browse files Browse the repository at this point in the history
  • Loading branch information
yxf committed Dec 10, 2020
2 parents 7b35305 + d6f6c0b commit 55e9020
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 7 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ pallet-staking-rpc = { path = "../pallets/staking/rpc" }

[build-dependencies]
substrate-build-script-utils = { version = "2.0.0" }
# frame-benchmarking-cli = { version = '2.0.0', optional = true }


[features]
default = []
runtime-benchmarks = [
"uart-runtime/runtime-benchmarks",
]
15 changes: 14 additions & 1 deletion pallets/rewards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,17 @@ sp-core = { version = "2.0.0", default-features = false }
sp-runtime = { version = "2.0.0", default-features = false }
pallet-aura = { version = "2.0.0", default-features = false }
pallet-session = { version = "2.0.0", default-features = false }
sp-consensus-aura = { version = "0.8.0", default-features = false }
sp-consensus-aura = { version = "0.8.0", default-features = false }
frame-benchmarking = { version = "2.0.0", default-features = false, optional = true }

[features]
default = ["std"]
std = [
"codec/std",
"sp-std/std",
"sp-runtime/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
]
runtime-benchmarks = ["frame-benchmarking"]
58 changes: 58 additions & 0 deletions pallets/rewards/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! Rewards pallet benchmarking.
#![cfg(feature = "runtime-benchmarks")]

use super::*;
use sp_std::prelude::*;
use frame_system::RawOrigin;
use frame_support::{traits::OnFinalize};
use frame_benchmarking::benchmarks;
use crate::Module as Rewards;
use sp_runtime::generic::DigestItem;
use codec::{Encode};

const MAX_TIME: u32 = 100;

benchmarks! {
_ { }

claim {
let validators = pallet_session::Module::<T>::validators();
// let validators_size: usize = validators.len();
// let i in 0..validators_size;
let validator = validators[0].clone();
let miner = T::AccountIdOf::convert(validator).unwrap();
// let rewards = Rewards::<T>::current_reward_per_block();
let rewards = T::RewardThreshold::get();
Rewards::<T>::set_rewards(miner.clone(), rewards, Zero::zero());
Rewards::<T>::set_rewards(miner.clone(), rewards, Zero::zero());
}: claim(RawOrigin::Signed(miner.clone()))
verify {
// assert_eq!(Rewards::<T>::immature_rewards(&miner).0, rewards);
assert_eq!(Rewards::<T>::mature_rewards(&miner), Zero::zero());
}

on_finalize {
let _ = Rewards::<T>::set_start_block(RawOrigin::Root.into(), Zero::zero());
let t in 1 .. MAX_TIME;

let slot = 1u64;
let log: DigestItem<T::Hash> = DigestItem::PreRuntime(
sp_consensus_aura::AURA_ENGINE_ID,
slot.encode()
);
<frame_system::Module<T>>::deposit_log(log.into());

let prev_mined_rewards = Rewards::<T>::mined_rewards();
}: { Rewards::<T>::on_finalize(t.into()); }
verify {
let rewards_per_block = Rewards::<T>::current_reward_per_block();
assert_eq!(Rewards::<T>::mined_rewards(), prev_mined_rewards + rewards_per_block);
}
}

#[cfg(test)]
mod tests {

}

12 changes: 9 additions & 3 deletions pallets/rewards/src/default_weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};

impl crate::WeightInfo for () {
fn claim() -> Weight {
(65949000 as Weight)
.saturating_add(DbWeight::get().reads(2 as Weight))
(50_000_000 as Weight)
.saturating_add(DbWeight::get().reads(3 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
}
// WARNING! Some components were not used: ["t"]
fn on_finalize() -> Weight {
(29_157_000 as Weight)
.saturating_add(DbWeight::get().reads(7 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
}
3 changes: 2 additions & 1 deletion pallets/rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ use sp_runtime::{
};

use codec::Codec;
use sp_std::prelude::*;

pub mod default_weights;
mod benchmarking;

pub trait WeightInfo {
fn claim() -> Weight;
fn on_finalize() -> Weight;
}


Expand Down
22 changes: 22 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ pallet-trade = { path = "../pallets/trade", default-features = false }
# pallet-lotteries = { path = "../pallets/lotteries", default-features = false }
primitives = { package = "uniarts-primitives", path = "../primitives", default-features = false }


# Used for runtime benchmarking
frame-benchmarking = { version = "2.0.0", default-features = false, optional = true }
frame-system-benchmarking = { version = "2.0.0", default-features = false, optional = true }
hex-literal = { version = "0.3.1", optional = true }


[build-dependencies]
wasm-builder-runner = { version = "1.0.5", package = "substrate-wasm-builder-runner" }

Expand Down Expand Up @@ -104,6 +111,7 @@ std = [
"pallet-token/std",
"pallet-trade/std",
"pallet-staking/std",
"pallet-rewards/std",
"pallet-staking-rpc-runtime-api/std",
"pallet-multisig/std",
"pallet-proxy/std",
Expand All @@ -127,3 +135,17 @@ std = [
"frame-system-rpc-runtime-api/std",
"primitives/std",
]

runtime-benchmarks = [
"sp-runtime/runtime-benchmarks",
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system-benchmarking",
"hex-literal",
"frame-system/runtime-benchmarks",
"pallet-collective/runtime-benchmarks",
"pallet-society/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-rewards/runtime-benchmarks",
]
38 changes: 37 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl pallet_rewards::Trait for Runtime {
type BlocksPerYear = BlocksPerYear;
type MiningCap = MiningCap;
type Event = Event;
type WeightInfo = ();
type WeightInfo = weights::pallet_rewards::WeightInfo<Runtime>;
}


Expand Down Expand Up @@ -1170,4 +1170,40 @@ impl_runtime_apis! {
Staking::pending_rewards(pool_id, account_id)
}
}

#[cfg(feature = "runtime-benchmarks")]
impl frame_benchmarking::Benchmark<Block> for Runtime {
fn dispatch_benchmark(
config: frame_benchmarking::BenchmarkConfig
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey};

use frame_system_benchmarking::Module as SystemBench;
impl frame_system_benchmarking::Trait for Runtime {}

let whitelist: Vec<TrackedStorageKey> = vec![
// Block Number
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec().into(),
// Total Issuance
hex_literal::hex!("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80").to_vec().into(),
// Execution Phase
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a").to_vec().into(),
// Event Count
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850").to_vec().into(),
// System Events
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7").to_vec().into(),
];

let mut batches = Vec::<BenchmarkBatch>::new();
let params = (&config, &whitelist);

add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
add_benchmark!(params, batches, pallet_balances, Balances);
add_benchmark!(params, batches, pallet_timestamp, Timestamp);
add_benchmark!(params, batches, pallet_rewards, Rewards);

if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
Ok(batches)
}
}
}
3 changes: 2 additions & 1 deletion runtime/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pub mod pallet_timestamp;
pub mod pallet_balances;
pub mod pallet_elections_phragmen;
pub mod pallet_treasury;
pub mod pallet_identity;
pub mod pallet_identity;
pub mod pallet_rewards;
22 changes: 22 additions & 0 deletions runtime/src/weights/pallet_rewards.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0
#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};
use sp_std::marker::PhantomData;

pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Trait> pallet_rewards::WeightInfo for WeightInfo<T> {
fn claim() -> Weight {
(50_000_000 as Weight)
.saturating_add(DbWeight::get().reads(3 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
// WARNING! Some components were not used: ["t"]
fn on_finalize() -> Weight {
(29_157_000 as Weight)
.saturating_add(DbWeight::get().reads(7 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
}

0 comments on commit 55e9020

Please sign in to comment.