Browse Source

Move complex weight calculations to the separate functions

iorveth 4 years ago
parent
commit
187fa9e4ba

+ 5 - 1
runtime-modules/membership/src/benchmarking.rs

@@ -88,6 +88,8 @@ benchmarks! {
 
         let i in 0 .. MAX_BYTES;
 
+        let j in 0 .. MAX_BYTES;
+
         let member_id = 0;
 
         let account_id = account::<T::AccountId>("member", member_id, SEED);
@@ -102,10 +104,12 @@ benchmarks! {
 
         let fee = Module::<T>::membership_price();
 
+        let name = vec![0u8].repeat(j as usize);
+
         let params = BuyMembershipParameters {
             root_account: account_id.clone(),
             controller_account: account_id.clone(),
-            name: None,
+            name: Some(name),
             handle: Some(handle.clone()),
             avatar_uri: None,
             about: None,

+ 36 - 19
runtime-modules/membership/src/lib.rs

@@ -355,19 +355,7 @@ decl_module! {
         /// - DB:
         ///    - O(W)
         /// # </weight>
-        #[weight = if params.referrer_id.is_some() {
-            WeightInfoMembership::<T>::buy_membership_with_referrer(
-                params.handle.as_ref()
-                    .map(|handle| handle.len().saturated_into())
-                    .unwrap_or_default(),
-            )
-        } else {
-            WeightInfoMembership::<T>::buy_membership_without_referrer(
-                params.handle.as_ref()
-                    .map(|handle| handle.len().saturated_into())
-                    .unwrap_or_default(),
-            )
-        }]
+        #[weight = Module::<T>::calculate_weight_for_buy_membership(params)]
         pub fn buy_membership(
             origin,
             params: BuyMembershipParameters<T::AccountId, T::MemberId>
@@ -489,12 +477,7 @@ decl_module! {
         /// - DB:
         ///    - O(1) doesn't depend on the state or parameters
         /// # </weight>
-        #[weight = match (new_root_account.is_some(), new_controller_account.is_some()) {
-            (true, true) => WeightInfoMembership::<T>::update_accounts_both(),
-            (false, true) => WeightInfoMembership::<T>::update_accounts_root(),
-            (true, false) => WeightInfoMembership::<T>::update_accounts_controller(),
-            _ => WeightInfoMembership::<T>::update_accounts_both(),
-        }]
+        #[weight = Module::<T>::calculate_weight_for_update_account(new_root_account, new_controller_account)]
         pub fn update_accounts(
             origin,
             member_id: T::MemberId,
@@ -915,6 +898,40 @@ decl_module! {
 }
 
 impl<T: Trait> Module<T> {
+    pub fn calculate_weight_for_update_account(
+        new_root_account: &Option<T::AccountId>,
+        new_controller_account: &Option<T::AccountId>,
+    ) -> Weight {
+        match (new_root_account.is_some(), new_controller_account.is_some()) {
+            (true, true) => WeightInfoMembership::<T>::update_accounts_both(),
+            (false, true) => WeightInfoMembership::<T>::update_accounts_root(),
+            (true, false) => WeightInfoMembership::<T>::update_accounts_controller(),
+            _ => WeightInfoMembership::<T>::update_accounts_both(),
+        }
+    }
+
+    pub fn calculate_weight_for_buy_membership(
+        params: &BuyMembershipParameters<T::AccountId, T::MemberId>,
+    ) -> Weight {
+        if params.referrer_id.is_some() {
+            WeightInfoMembership::<T>::buy_membership_with_referrer(
+                params
+                    .handle
+                    .as_ref()
+                    .map(|handle| handle.len().saturated_into())
+                    .unwrap_or_default(),
+            )
+        } else {
+            WeightInfoMembership::<T>::buy_membership_without_referrer(
+                params
+                    .handle
+                    .as_ref()
+                    .map(|handle| handle.len().saturated_into())
+                    .unwrap_or_default(),
+            )
+        }
+    }
+
     /// Provided that the member_id exists return its membership. Returns error otherwise.
     fn ensure_membership(member_id: T::MemberId) -> Result<Membership<T>, Error<T>> {
         Self::ensure_membership_with_error(member_id, Error::<T>::MemberProfileNotFound)

+ 10 - 10
scripts/generate-weights.sh

@@ -34,8 +34,8 @@ benchmark() {
 # So uncomment this when we move to a version that contains that PR.
 # See issue: #1979
 # benchmark frame_system
-benchmark pallet_utility
-benchmark pallet_session
+# benchmark pallet_utility
+# benchmark pallet_session
 # benchmark pallet_timestamp
 
 # This benchmark takes too long with 50 steps and 20 repeats in a normal laptop.
@@ -45,12 +45,12 @@ benchmark pallet_session
 # benchmark pallet_im_online
 
 # Joystrem benchmarks
-benchmark proposals_discussion
-benchmark proposals_engine
-benchmark proposals_codex
-benchmark pallet_constitution
-benchmark working_group
-benchmark council
-benchmark referendum
-benchmark forum
+# benchmark proposals_discussion
+# benchmark proposals_engine
+# benchmark proposals_codex
+# benchmark pallet_constitution
+# benchmark working_group
+# benchmark council
+# benchmark referendum
+# benchmark forum
 benchmark membership