From 0b3d22012a3a9ee459fbf473fc89833642fb4163 Mon Sep 17 00:00:00 2001 From: Michael Eberhardt <64731211+neutrinoks@users.noreply.github.com> Date: Fri, 24 May 2024 08:23:28 +0400 Subject: [PATCH] feat: add gas-cost-bundles asset for benchmark improvement --- .gitignore | 3 - .../gas-costs-bundles/.gitignore | 1 + .../move-projects/gas-costs-bundles/build.sh | 32 +++++ .../assets/move-projects/gas-costs/.gitignore | 2 + pallet/src/benchmarking.rs | 130 ++++++++++-------- pallet/src/lib.rs | 15 +- pallet/src/weights.rs | 51 +++---- 7 files changed, 131 insertions(+), 103 deletions(-) create mode 100644 pallet/src/assets/move-projects/gas-costs-bundles/.gitignore create mode 100755 pallet/src/assets/move-projects/gas-costs-bundles/build.sh create mode 100644 pallet/src/assets/move-projects/gas-costs/.gitignore diff --git a/.gitignore b/.gitignore index 715ba0f..c71ddf5 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,3 @@ gas_costs.txt # Move Build Output build/ - -Calibration.move -gen_smove_instr.sh diff --git a/pallet/src/assets/move-projects/gas-costs-bundles/.gitignore b/pallet/src/assets/move-projects/gas-costs-bundles/.gitignore new file mode 100644 index 0000000..a56e2cd --- /dev/null +++ b/pallet/src/assets/move-projects/gas-costs-bundles/.gitignore @@ -0,0 +1 @@ +bundle* diff --git a/pallet/src/assets/move-projects/gas-costs-bundles/build.sh b/pallet/src/assets/move-projects/gas-costs-bundles/build.sh new file mode 100755 index 0000000..b927634 --- /dev/null +++ b/pallet/src/assets/move-projects/gas-costs-bundles/build.sh @@ -0,0 +1,32 @@ +#!/bin/sh +cd $(dirname $0) + +ITERATIONS=25 + +function write_module() { + printf "module AiBob::CalMod$2 {\n" >> $1 + printf " fun fun_fun(a: u8) {\n" >> $1 + printf " assert!(a == 0, 0);\n" >> $1 + printf " }\n" >> $1 + printf "}\n" >> $1 +} + +function create_move_project() { + NAME=bundle$1 + smove new $NAME + TOML=$NAME/Move.toml + printf 'AiBob = "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"' >> $TOML + for i in $(seq 1 $1) + do + FILE=$NAME/sources/Modules.move + write_module $FILE $i + done + sync + smove bundle -p $NAME +} + +rm -rf bundle* +for i in $(seq 1 $ITERATIONS) +do + create_move_project $i +done diff --git a/pallet/src/assets/move-projects/gas-costs/.gitignore b/pallet/src/assets/move-projects/gas-costs/.gitignore new file mode 100644 index 0000000..f6c9f06 --- /dev/null +++ b/pallet/src/assets/move-projects/gas-costs/.gitignore @@ -0,0 +1,2 @@ +gen_smove_instr.sh +Calibration.move diff --git a/pallet/src/benchmarking.rs b/pallet/src/benchmarking.rs index 154adcd..a03c7d4 100644 --- a/pallet/src/benchmarking.rs +++ b/pallet/src/benchmarking.rs @@ -22,6 +22,22 @@ macro_rules! impl_gas_costs_cal_fns { }; } +macro_rules! impl_gas_costs_cal_bundles { + ($name:tt) => { + pub fn $name() -> &'static [u8] { + core::include_bytes!(concat!( + "assets/move-projects/gas-costs-bundles/", + stringify!($name), + "/build/", + stringify!($name), + "/bundles/", + stringify!($name), + ".mvb" + )) + } + }; +} + #[benchmarks( where T: Config + SysConfig, @@ -94,40 +110,42 @@ mod benchmarks { } #[benchmark] - fn publish_module(n: Linear<0, 2>) { - let bob_32 = utils::account::(utils::BOB_ADDR); - - let module_bcs = [ - multiple_signers_module().to_vec(), - car_wash_example_module().to_vec(), - base58_smove_build_module().to_vec(), - ]; - let gas = [661, 732, 100]; - - #[extrinsic_call] - publish_module( - RawOrigin::Signed(bob_32), - module_bcs[n as usize].clone(), - gas[n as usize], - ); - } - - #[benchmark] - fn publish_module_bundle(n: Linear<0, 2>) { + fn publish_module_generic(n: Linear<0, 24>) { let bob_32 = utils::account::(utils::BOB_ADDR); let bundles = [ - multiple_signers_module_as_bundle().to_vec(), - car_wash_example_module_as_bundle().to_vec(), - base58_smove_build_module_as_bundle().to_vec(), + bundle1().to_vec(), + bundle2().to_vec(), + bundle3().to_vec(), + bundle4().to_vec(), + bundle5().to_vec(), + bundle6().to_vec(), + bundle7().to_vec(), + bundle8().to_vec(), + bundle9().to_vec(), + bundle10().to_vec(), + bundle11().to_vec(), + bundle12().to_vec(), + bundle13().to_vec(), + bundle14().to_vec(), + bundle15().to_vec(), + bundle16().to_vec(), + bundle17().to_vec(), + bundle18().to_vec(), + bundle19().to_vec(), + bundle20().to_vec(), + bundle21().to_vec(), + bundle22().to_vec(), + bundle23().to_vec(), + bundle24().to_vec(), + bundle25().to_vec(), ]; - let gas = [664, 735, 102]; #[extrinsic_call] publish_module_bundle( RawOrigin::Signed(bob_32), bundles[n as usize].clone(), - gas[n as usize], + (n + 1) * 114, ); } @@ -169,42 +187,6 @@ mod benchmarks { use benchmark_only::*; mod benchmark_only { - // Base58 build example - pub fn base58_smove_build_module() -> &'static [u8] { - core::include_bytes!( - "assets/move-projects/base58_smove_build/build/base58_smove_build/bytecode_modules/BobBase58.mv" - ) - } - pub fn base58_smove_build_module_as_bundle() -> &'static [u8] { - core::include_bytes!( - "assets/move-projects/base58_smove_build/build/base58_smove_build/bundles/base58_smove_build.mvb" - ) - } - - // Car Wash Example - pub fn car_wash_example_module() -> &'static [u8] { - core::include_bytes!( - "assets/move-projects/car-wash-example/build/car-wash-example/bytecode_modules/CarWash.mv" - ) - } - pub fn car_wash_example_module_as_bundle() -> &'static [u8] { - core::include_bytes!( - "assets/move-projects/car-wash-example/build/car-wash-example/bundles/car-wash-example.mvb" - ) - } - - // Multiple Signers Example - pub fn multiple_signers_module() -> &'static [u8] { - core::include_bytes!( - "assets/move-projects/multiple-signers/build/multiple-signers/bytecode_modules/Dorm.mv" - ) - } - pub fn multiple_signers_module_as_bundle() -> &'static [u8] { - core::include_bytes!( - "assets/move-projects/multiple-signers/build/multiple-signers/bundles/multiple-signers.mvb" - ) - } - // Basic Coin Example pub fn publish_basic_coin() -> &'static [u8] { core::include_bytes!( @@ -243,4 +225,30 @@ mod benchmark_only { impl_gas_costs_cal_fns!(mint_23); impl_gas_costs_cal_fns!(mint_24); impl_gas_costs_cal_fns!(mint_25); + + impl_gas_costs_cal_bundles!(bundle1); + impl_gas_costs_cal_bundles!(bundle2); + impl_gas_costs_cal_bundles!(bundle3); + impl_gas_costs_cal_bundles!(bundle4); + impl_gas_costs_cal_bundles!(bundle5); + impl_gas_costs_cal_bundles!(bundle6); + impl_gas_costs_cal_bundles!(bundle7); + impl_gas_costs_cal_bundles!(bundle8); + impl_gas_costs_cal_bundles!(bundle9); + impl_gas_costs_cal_bundles!(bundle10); + impl_gas_costs_cal_bundles!(bundle11); + impl_gas_costs_cal_bundles!(bundle12); + impl_gas_costs_cal_bundles!(bundle13); + impl_gas_costs_cal_bundles!(bundle14); + impl_gas_costs_cal_bundles!(bundle15); + impl_gas_costs_cal_bundles!(bundle16); + impl_gas_costs_cal_bundles!(bundle17); + impl_gas_costs_cal_bundles!(bundle18); + impl_gas_costs_cal_bundles!(bundle19); + impl_gas_costs_cal_bundles!(bundle20); + impl_gas_costs_cal_bundles!(bundle21); + impl_gas_costs_cal_bundles!(bundle22); + impl_gas_costs_cal_bundles!(bundle23); + impl_gas_costs_cal_bundles!(bundle24); + impl_gas_costs_cal_bundles!(bundle25); } diff --git a/pallet/src/lib.rs b/pallet/src/lib.rs index a928087..09079dd 100644 --- a/pallet/src/lib.rs +++ b/pallet/src/lib.rs @@ -34,8 +34,11 @@ pub mod weight_info { /// Weight functions needed for pallet_move. pub trait WeightInfo { fn execute(gas: u32) -> Weight; - fn publish_module(gas: u32) -> Weight; - fn publish_module_bundle(gas: u32) -> Weight; + // Both `publish_module` and `publish_module_bundle` share the same weight cost calculation + // since both functions are almost identical and should cost the same. More info: + // `publish_module` internally just invokes `publish_module_bundle` with a single-module + // bundle. + fn publish_module_generic(gas: u32) -> Weight; fn update_stdlib_bundle() -> Weight; } } @@ -350,7 +353,7 @@ pub mod pallet { /// Publish a Move module sent by the user. /// Module is published under its sender's address. #[pallet::call_index(1)] - #[pallet::weight(T::WeightInfo::publish_module(*gas_limit))] + #[pallet::weight(T::WeightInfo::publish_module_generic(*gas_limit))] pub fn publish_module( origin: OriginFor, bytecode: Vec, @@ -379,7 +382,7 @@ pub mod pallet { /// /// Bundle is just a set of multiple modules. #[pallet::call_index(2)] - #[pallet::weight(T::WeightInfo::publish_module_bundle(*gas_limit))] + #[pallet::weight(T::WeightInfo::publish_module_generic(*gas_limit))] pub fn publish_module_bundle( origin: OriginFor, bundle: Vec, @@ -650,7 +653,7 @@ pub mod pallet { Ok(MoveApiEstimation { vm_status_code: vm_result.status_code.into(), gas_used: vm_result.gas_used, - total_weight_including_gas_used: T::WeightInfo::publish_module( + total_weight_including_gas_used: T::WeightInfo::publish_module_generic( vm_result.gas_used as u32, ), }) @@ -666,7 +669,7 @@ pub mod pallet { Ok(MoveApiEstimation { vm_status_code: vm_result.status_code.into(), gas_used: vm_result.gas_used, - total_weight_including_gas_used: T::WeightInfo::publish_module_bundle( + total_weight_including_gas_used: T::WeightInfo::publish_module_generic( vm_result.gas_used as u32, ), }) diff --git a/pallet/src/weights.rs b/pallet/src/weights.rs index 821511d..759e4d9 100644 --- a/pallet/src/weights.rs +++ b/pallet/src/weights.rs @@ -2,9 +2,9 @@ //! Autogenerated weights for `pallet_move` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 32.0.0 -//! DATE: 2024-05-23, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2024-05-24, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` -//! HOSTNAME: `bonka-MacBook-Pro.local`, CPU: `` +//! HOSTNAME: `michaeleberhardts-MacBook-Pro.local`, CPU: `` //! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: 1024 // Executed Command: @@ -49,42 +49,27 @@ impl crate::weight_info::WeightInfo for SubstrateWeight // Proof Size summary in bytes: // Measured: `8622` // Estimated: `17037` - // Minimum execution time: 345_000_000 picoseconds. - Weight::from_parts(348_832_865, 0) + // Minimum execution time: 346_000_000 picoseconds. + Weight::from_parts(348_557_924, 0) .saturating_add(Weight::from_parts(0, 17037)) - // Standard Error: 25_764 - .saturating_add(Weight::from_parts(5_607_737, 0).saturating_mul(n.into())) + // Standard Error: 8_132 + .saturating_add(Weight::from_parts(5_921_881, 0).saturating_mul(n.into())) .saturating_add(T::DbWeight::get().reads(6)) .saturating_add(T::DbWeight::get().writes(3)) } /// Storage: `MoveModule::VMStorage` (r:1 w:1) /// Proof: `MoveModule::VMStorage` (`max_values`: None, `max_size`: None, mode: `Measured`) - /// The range of component `n` is `[0, 2]`. - fn publish_module(n: u32, ) -> Weight { - // Proof Size summary in bytes: - // Measured: `7796` - // Estimated: `14073` - // Minimum execution time: 42_000_000 picoseconds. - Weight::from_parts(310_274_033, 0) - .saturating_add(Weight::from_parts(0, 14073)) - // Standard Error: 3_569_607 - .saturating_add(Weight::from_parts(4_038_397, 0).saturating_mul(n.into())) - .saturating_add(T::DbWeight::get().reads(2)) - .saturating_add(T::DbWeight::get().writes(1)) - } - /// Storage: `MoveModule::VMStorage` (r:1 w:1) - /// Proof: `MoveModule::VMStorage` (`max_values`: None, `max_size`: None, mode: `Measured`) - /// The range of component `n` is `[0, 2]`. - fn publish_module_bundle(n: u32, ) -> Weight { + /// The range of component `n` is `[0, 24]`. + fn publish_module_generic(n: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `7796` - // Estimated: `14073` - // Minimum execution time: 43_000_000 picoseconds. - Weight::from_parts(311_709_392, 0) - .saturating_add(Weight::from_parts(0, 14073)) - // Standard Error: 3_599_218 - .saturating_add(Weight::from_parts(4_712_707, 0).saturating_mul(n.into())) - .saturating_add(T::DbWeight::get().reads(2)) + // Measured: `111` + // Estimated: `3576` + // Minimum execution time: 56_000_000 picoseconds. + Weight::from_parts(54_952_033, 0) + .saturating_add(Weight::from_parts(0, 3576)) + // Standard Error: 10_473 + .saturating_add(Weight::from_parts(27_356_941, 0).saturating_mul(n.into())) + .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } /// Storage: `MoveModule::VMStorage` (r:1 w:1) @@ -93,8 +78,8 @@ impl crate::weight_info::WeightInfo for SubstrateWeight // Proof Size summary in bytes: // Measured: `7796` // Estimated: `11261` - // Minimum execution time: 198_000_000 picoseconds. - Weight::from_parts(200_000_000, 0) + // Minimum execution time: 208_000_000 picoseconds. + Weight::from_parts(210_000_000, 0) .saturating_add(Weight::from_parts(0, 11261)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1))