Przeglądaj źródła

benchmark: proposals/discussion and engine: add weight functions

conectado 4 lat temu
rodzic
commit
c5e76e28a6

+ 51 - 1
runtime-modules/proposals/codex/src/tests/mock/mod.rs

@@ -1,7 +1,7 @@
 #![cfg(test)]
 
 use frame_support::traits::LockIdentifier;
-use frame_support::{impl_outer_dispatch, impl_outer_origin, parameter_types};
+use frame_support::{impl_outer_dispatch, impl_outer_origin, parameter_types, weights::Weight};
 pub use frame_system;
 use sp_core::H256;
 use sp_runtime::curve::PiecewiseLinear;
@@ -86,6 +86,8 @@ parameter_types! {
     pub const LockId: LockIdentifier = [2; 8];
 }
 
+pub struct MockEngineWeight;
+
 impl proposals_engine::Trait for Test {
     type Event = ();
     type ProposerOriginValidator = ();
@@ -100,6 +102,37 @@ impl proposals_engine::Trait for Test {
     type MaxActiveProposalLimit = MaxActiveProposalLimit;
     type DispatchableCallCode = crate::Call<Test>;
     type ProposalObserver = crate::Module<Test>;
+    type WeightInfo = MockEngineWeight;
+}
+
+impl proposals_engine::WeightInfo for MockEngineWeight {
+    fn vote(_: u32) -> Weight {
+        0
+    }
+
+    fn cancel_proposal(_: u32) -> Weight {
+        0
+    }
+
+    fn veto_proposal() -> Weight {
+        0
+    }
+
+    fn on_initialize_immediate_execution_decode_fails(_: u32) -> Weight {
+        0
+    }
+
+    fn on_initialize_approved_pending_constitutionality(_: u32) -> Weight {
+        0
+    }
+
+    fn on_initialize_rejected(_: u32) -> Weight {
+        0
+    }
+
+    fn on_initialize_slashed(_: u32) -> Weight {
+        0
+    }
 }
 
 impl Default for crate::Call<Test> {
@@ -132,6 +165,8 @@ parameter_types! {
     pub const MaxWhiteListSize: u32 = 20;
 }
 
+pub struct MockProposalWeight;
+
 impl proposals_discussion::Trait for Test {
     type Event = ();
     type AuthorOriginValidator = ();
@@ -139,6 +174,21 @@ impl proposals_discussion::Trait for Test {
     type ThreadId = u64;
     type PostId = u64;
     type MaxWhiteListSize = MaxWhiteListSize;
+    type WeightInfo = MockProposalWeight;
+}
+
+impl proposals_discussion::WeightInfo for MockProposalWeight {
+    fn add_post(_: u32, _: u32) -> Weight {
+        0
+    }
+
+    fn update_post() -> Weight {
+        0
+    }
+
+    fn change_thread_mode(_: u32) -> Weight {
+        0
+    }
 }
 
 pub struct MockVotersParameters;

+ 1 - 1
runtime-modules/proposals/discussion/src/benchmarking.rs

@@ -1,13 +1,13 @@
 #![cfg(feature = "runtime-benchmarks")]
 use super::*;
 use crate::Module as ProposalsDiscussion;
-use core::convert::TryInto;
 use frame_benchmarking::{account, benchmarks};
 use frame_system::EventRecord;
 use frame_system::Module as System;
 use frame_system::RawOrigin;
 use membership::Module as Membership;
 use sp_std::cmp::min;
+use sp_std::convert::TryInto;
 use sp_std::prelude::*;
 
 const SEED: u32 = 0;

+ 26 - 4
runtime-modules/proposals/discussion/src/lib.rs

@@ -54,8 +54,11 @@ mod types;
 use frame_support::dispatch::{DispatchError, DispatchResult};
 use frame_support::sp_runtime::SaturatedConversion;
 use frame_support::traits::Get;
-use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure, Parameter};
+use frame_support::{
+    decl_error, decl_event, decl_module, decl_storage, ensure, weights::Weight, Parameter,
+};
 use sp_std::clone::Clone;
+use sp_std::convert::TryInto;
 use sp_std::vec::Vec;
 
 use common::origin::ActorOriginValidator;
@@ -65,6 +68,12 @@ pub use types::ThreadMode;
 
 type MemberId<T> = <T as membership::Trait>::MemberId;
 
+pub trait WeightInfo {
+    fn add_post(i: u32, j: u32) -> Weight;
+    fn update_post() -> Weight; // Note: since parameter doesn't affect weight it's discarded
+    fn change_thread_mode(i: u32) -> Weight;
+}
+
 decl_event!(
     /// Proposals engine events
     pub enum Event<T>
@@ -112,6 +121,9 @@ pub trait Trait: frame_system::Trait + membership::Trait {
 
     /// Defines author list size limit for the Closed discussion.
     type MaxWhiteListSize: Get<u32>;
+
+    /// Weight information for extrinsics in this pallet.
+    type WeightInfo: WeightInfo;
 }
 
 decl_error! {
@@ -170,7 +182,10 @@ decl_module! {
         fn deposit_event() = default;
 
         /// Adds a post with author origin check.
-        #[weight = 10_000_000] // TODO: adjust weight
+        #[weight = <T as Trait>::WeightInfo::add_post(
+            T::MaxWhiteListSize::get(),
+            _text.len().try_into().unwrap()
+        )]
         pub fn add_post(
             origin,
             post_author_id: MemberId<T>,
@@ -202,7 +217,7 @@ decl_module! {
        }
 
         /// Updates a post with author origin check. Update attempts number is limited.
-        #[weight = 10_000_000] // TODO: adjust weight
+        #[weight = <T as Trait>::WeightInfo::update_post()]
         pub fn update_post(
             origin,
             post_author_id: MemberId<T>,
@@ -228,7 +243,13 @@ decl_module! {
        }
 
         /// Changes thread permission mode.
-        #[weight = 10_000_000] // TODO: adjust weight
+        #[weight = <T as Trait>::WeightInfo::change_thread_mode(
+            if let ThreadMode::Closed(ref list) = mode {
+                list.len().try_into().unwrap()
+            } else {
+                0
+            }
+        )]
         pub fn change_thread_mode(
             origin,
             member_id: MemberId<T>,
@@ -260,6 +281,7 @@ decl_module! {
             <ThreadById<T>>::mutate(thread_id, |thread| {
                 thread.mode = mode.clone();
             });
+
             Self::deposit_event(RawEvent::ThreadModeChanged(thread_id, mode));
        }
     }

+ 17 - 1
runtime-modules/proposals/discussion/src/tests/mock.rs

@@ -3,7 +3,7 @@
 pub use frame_system;
 
 use frame_support::traits::{OnFinalize, OnInitialize};
-use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
+use frame_support::{impl_outer_event, impl_outer_origin, parameter_types, weights::Weight};
 use sp_core::H256;
 use sp_runtime::{
     testing::Header,
@@ -12,6 +12,7 @@ use sp_runtime::{
 };
 
 use crate::ActorOriginValidator;
+use crate::WeightInfo;
 
 impl_outer_origin! {
     pub enum Origin for Test {}
@@ -87,6 +88,21 @@ impl crate::Trait for Test {
     type ThreadId = u64;
     type PostId = u64;
     type MaxWhiteListSize = MaxWhiteListSize;
+    type WeightInfo = ();
+}
+
+impl WeightInfo for () {
+    fn add_post(_: u32, _: u32) -> Weight {
+        0
+    }
+
+    fn update_post() -> Weight {
+        0
+    }
+
+    fn change_thread_mode(_: u32) -> Weight {
+        0
+    }
 }
 
 impl ActorOriginValidator<Origin, u64, u64> for () {

+ 8 - 6
runtime-modules/proposals/engine/src/benchmarking.rs

@@ -330,12 +330,14 @@ benchmarks! {
             );
         }
 
-        for proposal_id in proposals.iter() {
-            assert_in_events::<T>(
-                RawEvent::ProposalExecuted(
-                    proposal_id.clone(),
-                    ExecutionStatus::failed_execution("Not enough data to fill buffer")).into()
-            );
+        if cfg!(test) {
+            for proposal_id in proposals.iter() {
+                assert_in_events::<T>(
+                    RawEvent::ProposalExecuted(
+                        proposal_id.clone(),
+                        ExecutionStatus::failed_execution("Not enough data to fill buffer")).into()
+                );
+            }
         }
     }
 

+ 32 - 4
runtime-modules/proposals/engine/src/lib.rs

@@ -134,11 +134,23 @@ use frame_support::{
 };
 use frame_system::{ensure_root, RawOrigin};
 use sp_arithmetic::traits::{Saturating, Zero};
+use sp_std::convert::TryInto;
 use sp_std::vec::Vec;
 
 use common::origin::ActorOriginValidator;
 use membership::staking_handler::StakingHandler;
 
+// proposals_engine
+pub trait WeightInfo {
+    fn vote(i: u32) -> Weight;
+    fn cancel_proposal(i: u32) -> Weight;
+    fn veto_proposal() -> Weight;
+    fn on_initialize_immediate_execution_decode_fails(i: u32) -> Weight;
+    fn on_initialize_approved_pending_constitutionality(i: u32) -> Weight;
+    fn on_initialize_rejected(i: u32) -> Weight;
+    fn on_initialize_slashed(i: u32) -> Weight;
+}
+
 /// Proposals engine trait.
 pub trait Trait:
     frame_system::Trait + pallet_timestamp::Trait + membership::Trait + balances::Trait
@@ -188,6 +200,9 @@ pub trait Trait:
 
     /// Proposal state change observer.
     type ProposalObserver: ProposalObserver<Self>;
+
+    /// Weight information for extrinsics in this pallet.
+    type WeightInfo: WeightInfo;
 }
 
 /// Proposal state change observer.
@@ -357,12 +372,25 @@ decl_module! {
         /// Block Initialization. Perform voting period check, vote result tally, approved proposals
         /// grace period checks, and proposal execution.
         fn on_initialize() -> Weight {
-            10_000_000 // TODO: adjust weight
+            let max_active_proposals = T::MaxActiveProposalLimit::get();
+
+            <T as Trait>::WeightInfo::on_initialize_immediate_execution_decode_fails(max_active_proposals)
+                .max(
+                    <T as Trait>
+                        ::WeightInfo
+                        ::on_initialize_approved_pending_constitutionality(max_active_proposals)
+                )
+                .max(
+                    <T as Trait>::WeightInfo::on_initialize_rejected(max_active_proposals)
+                )
+                .max(
+                    <T as Trait>::WeightInfo::on_initialize_slashed(max_active_proposals)
+                )
                 .saturating_add(Self::process_proposals())
         }
 
         /// Vote extrinsic. Conditions:  origin must allow votes.
-        #[weight = 10_000_000] // TODO: adjust weight
+        #[weight = <T as Trait>::WeightInfo::vote(_rationale.len().try_into().unwrap())]
         pub fn vote(
             origin,
             voter_id: MemberId<T>,
@@ -394,7 +422,7 @@ decl_module! {
         }
 
         /// Cancel a proposal by its original proposer.
-        #[weight = 10_000_000] // TODO: adjust weight
+        #[weight = <T as Trait>::WeightInfo::cancel_proposal(T::MaxLocks::get())]
         pub fn cancel_proposal(origin, proposer_id: MemberId<T>, proposal_id: T::ProposalId) {
             T::ProposerOriginValidator::ensure_actor_origin(origin, proposer_id)?;
 
@@ -411,7 +439,7 @@ decl_module! {
         }
 
         /// Veto a proposal. Must be root.
-        #[weight = 10_000_000] // TODO: adjust weight
+        #[weight = <T as Trait>::WeightInfo::veto_proposal()]
         pub fn veto_proposal(origin, proposal_id: T::ProposalId) {
             ensure_root(origin)?;
 

+ 32 - 1
runtime-modules/proposals/engine/src/tests/mock/mod.rs

@@ -7,7 +7,7 @@
 #![cfg(test)]
 
 use frame_support::traits::LockIdentifier;
-use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
+use frame_support::{impl_outer_event, impl_outer_origin, parameter_types, weights::Weight};
 pub use frame_system;
 use sp_core::H256;
 use sp_runtime::{
@@ -103,6 +103,37 @@ impl crate::Trait for Test {
     type MaxActiveProposalLimit = MaxActiveProposalLimit;
     type DispatchableCallCode = proposals::Call<Test>;
     type ProposalObserver = ();
+    type WeightInfo = ();
+}
+
+impl crate::WeightInfo for () {
+    fn vote(_: u32) -> Weight {
+        0
+    }
+
+    fn cancel_proposal(_: u32) -> Weight {
+        0
+    }
+
+    fn veto_proposal() -> Weight {
+        0
+    }
+
+    fn on_initialize_immediate_execution_decode_fails(_: u32) -> Weight {
+        0
+    }
+
+    fn on_initialize_approved_pending_constitutionality(_: u32) -> Weight {
+        0
+    }
+
+    fn on_initialize_rejected(_: u32) -> Weight {
+        0
+    }
+
+    fn on_initialize_slashed(_: u32) -> Weight {
+        0
+    }
 }
 
 impl ProposalObserver<Test> for () {

+ 2 - 0
runtime/src/lib.rs

@@ -606,6 +606,7 @@ impl proposals_engine::Trait for Runtime {
     type MaxActiveProposalLimit = ProposalMaxActiveProposalLimit;
     type DispatchableCallCode = Call;
     type ProposalObserver = ProposalsCodex;
+    type WeightInfo = weights::proposals_engine::WeightInfo;
 }
 
 impl Default for Call {
@@ -625,6 +626,7 @@ impl proposals_discussion::Trait for Runtime {
     type ThreadId = ThreadId;
     type PostId = PostId;
     type MaxWhiteListSize = MaxWhiteListSize;
+    type WeightInfo = weights::proposals_discussion::WeightInfo;
 }
 
 parameter_types! {

+ 3 - 0
runtime/src/weights/mod.rs

@@ -22,3 +22,6 @@ pub mod pallet_session;
 pub mod pallet_staking;
 pub mod pallet_timestamp;
 pub mod pallet_utility;
+
+pub mod proposals_discussion;
+pub mod proposals_engine;

+ 27 - 0
runtime/src/weights/proposals_discussion.rs

@@ -0,0 +1,27 @@
+//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0
+
+#![allow(unused_parens)]
+#![allow(unused_imports)]
+
+use frame_support::weights::{constants::RocksDbWeight as DbWeight, Weight};
+
+pub struct WeightInfo;
+impl proposals_discussion::WeightInfo for WeightInfo {
+    fn add_post(i: u32, j: u32) -> Weight {
+        (10_881_426_000 as Weight)
+            .saturating_add((66_823_000 as Weight).saturating_mul(i as Weight))
+            .saturating_add((1_000 as Weight).saturating_mul(j as Weight))
+            .saturating_add(DbWeight::get().reads(4 as Weight))
+            .saturating_add(DbWeight::get().writes(2 as Weight))
+    }
+    // WARNING! Some components were not used: ["j"]
+    fn update_post() -> Weight {
+        (7_759_302_000 as Weight).saturating_add(DbWeight::get().reads(3 as Weight))
+    }
+    fn change_thread_mode(i: u32) -> Weight {
+        (11_577_573_000 as Weight)
+            .saturating_add((69_266_000 as Weight).saturating_mul(i as Weight))
+            .saturating_add(DbWeight::get().reads(3 as Weight))
+            .saturating_add(DbWeight::get().writes(1 as Weight))
+    }
+}

+ 58 - 0
runtime/src/weights/proposals_engine.rs

@@ -0,0 +1,58 @@
+//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0
+
+#![allow(unused_parens)]
+#![allow(unused_imports)]
+
+use frame_support::weights::{constants::RocksDbWeight as DbWeight, Weight};
+
+pub struct WeightInfo;
+impl proposals_engine::WeightInfo for WeightInfo {
+    fn vote(i: u32) -> Weight {
+        (10_707_315_000 as Weight)
+            .saturating_add((1_075_000 as Weight).saturating_mul(i as Weight))
+            .saturating_add(DbWeight::get().reads(4 as Weight))
+            .saturating_add(DbWeight::get().writes(2 as Weight))
+    }
+    fn cancel_proposal(i: u32) -> Weight {
+        (27_737_659_000 as Weight)
+            .saturating_add((38_762_000 as Weight).saturating_mul(i as Weight))
+            .saturating_add(DbWeight::get().reads(5 as Weight))
+            .saturating_add(DbWeight::get().writes(8 as Weight))
+    }
+    fn veto_proposal() -> Weight {
+        (13_460_881_000 as Weight)
+            .saturating_add(DbWeight::get().reads(4 as Weight))
+            .saturating_add(DbWeight::get().writes(8 as Weight))
+    }
+    fn on_initialize_immediate_execution_decode_fails(i: u32) -> Weight {
+        (845_226_000 as Weight)
+            .saturating_add((18_757_457_000 as Weight).saturating_mul(i as Weight))
+            .saturating_add(DbWeight::get().reads(3 as Weight))
+            .saturating_add(DbWeight::get().reads((4 as Weight).saturating_mul(i as Weight)))
+            .saturating_add(DbWeight::get().writes(2 as Weight))
+            .saturating_add(DbWeight::get().writes((7 as Weight).saturating_mul(i as Weight)))
+    }
+    fn on_initialize_approved_pending_constitutionality(i: u32) -> Weight {
+        (1_194_100_000 as Weight)
+            .saturating_add((8_103_583_000 as Weight).saturating_mul(i as Weight))
+            .saturating_add(DbWeight::get().reads(2 as Weight))
+            .saturating_add(DbWeight::get().reads((1 as Weight).saturating_mul(i as Weight)))
+            .saturating_add(DbWeight::get().writes((1 as Weight).saturating_mul(i as Weight)))
+    }
+    fn on_initialize_rejected(i: u32) -> Weight {
+        (751_079_000 as Weight)
+            .saturating_add((27_189_652_000 as Weight).saturating_mul(i as Weight))
+            .saturating_add(DbWeight::get().reads(3 as Weight))
+            .saturating_add(DbWeight::get().reads((3 as Weight).saturating_mul(i as Weight)))
+            .saturating_add(DbWeight::get().writes(2 as Weight))
+            .saturating_add(DbWeight::get().writes((7 as Weight).saturating_mul(i as Weight)))
+    }
+    fn on_initialize_slashed(i: u32) -> Weight {
+        (1_297_364_000 as Weight)
+            .saturating_add((20_346_415_000 as Weight).saturating_mul(i as Weight))
+            .saturating_add(DbWeight::get().reads(3 as Weight))
+            .saturating_add(DbWeight::get().reads((3 as Weight).saturating_mul(i as Weight)))
+            .saturating_add(DbWeight::get().writes(2 as Weight))
+            .saturating_add(DbWeight::get().writes((7 as Weight).saturating_mul(i as Weight)))
+    }
+}

+ 14 - 0
scripts/generate-weights.sh

@@ -0,0 +1,14 @@
+#!/usr/bin/env bash
+
+# Executes and replaces all benchmarks with the new weights
+
+echo "Benchmarking proposals_discussion..."
+./target/debug/joystream-node benchmark --pallet=proposals_discussion --extrinsic=* --chain=dev --steps=50 --repeat=20 --execution=wasm --output=. > /dev/null
+mv proposals_discussion.rs runtime/src/weights/
+echo "proposals_discussion benchmarked"
+
+
+echo "Benchmarking proposals_engine..."
+./target/debug/joystream-node benchmark --pallet=proposals_engine --extrinsic=* --chain=dev --steps=50 --repeat=20 --execution=wasm --output=. > /dev/null
+mv proposals_engine.rs runtime/src/weights/
+echo "proposals_engine benchmarked"