Skip to content

Commit

Permalink
chore: update execute benchmark test
Browse files Browse the repository at this point in the history
- the final `execute` benchmarks with a script that access modules and storage
  • Loading branch information
Rqnsom committed May 23, 2024
1 parent ac61010 commit 1438a93
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 114 deletions.
2 changes: 2 additions & 0 deletions pallet/src/assets/move-projects/gas-costs/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ version = "0.0.0"
[dependencies]
MoveStdlib = { git = "https://github.com/eigerco/move-stdlib", rev = "main" }
car-wash-example = { local = "../car-wash-example/" }
basic_coin = { local = "../basic_coin" }

[addresses]
std = "0x1"
DeveloperBob = "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"
CafeAccount = "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"
2 changes: 1 addition & 1 deletion pallet/src/assets/move-projects/gas-costs/build.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/sh
cd $(dirname $0)
ALICE=5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
sh ./gen_cal_scripts.sh
# Build the project
smove build
sh ./gen_cal_scripts.sh
# Create all Script-Transactions
smove create-transaction --compiled-script-path build/gas-costs/bytecode_scripts/short_cheap_script.mv
smove create-transaction --compiled-script-path build/gas-costs/bytecode_scripts/short_expensive_script.mv --args signer:$ALICE
Expand Down
34 changes: 6 additions & 28 deletions pallet/src/assets/move-projects/gas-costs/gen_cal_scripts.sh
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
#!/bin/sh
cd $(dirname $0)

MOVE_SRC="./sources/Calibration.move"
BASH_SH="./gen_smove_instr.sh"
ITERATIONS=25

function write_method() {
printf "script {\n" >> $MOVE_SRC
printf " fun cal_gas_cost_$1(v0: u8" >> $MOVE_SRC
if [ $1 -gt 0 ]
then
for i in $(seq 1 $1)
do
printf ", v$i: u8" >> $MOVE_SRC
done
fi
printf ") {\n" >> $MOVE_SRC
for i in $(seq 0 $1)
do
printf " assert!(v$i == $i, $i);\n" >> $MOVE_SRC
done
printf " }\n" >> $MOVE_SRC
printf "}\n" >> $MOVE_SRC
}
ALICE=5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
BOB=5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty

function write_smove_cmd() {
printf "\nsmove create-transaction -c build/gas-costs/bytecode_scripts/cal_gas_cost_$1.mv --args" >> $BASH_SH
for i in $(seq 0 $1)
do
printf " u8:$i" >> $BASH_SH
done
cp build/gas-costs/bytecode_scripts/mint.mv build/gas-costs/bytecode_scripts/mint_$1.mv
printf "\nsmove create-transaction -c build/gas-costs/bytecode_scripts/mint_$1.mv --args signer:$BOB address:$ALICE u64:$1" >> $BASH_SH
}

printf "" > $MOVE_SRC
printf "#!/bin/sh" > $BASH_SH
for i in $(seq 0 $(($ITERATIONS-1)))
for i in $(seq 1 $(($ITERATIONS)))
do
write_method $i
write_smove_cmd $i
done
printf "\nsmove create-transaction -c build/gas-costs/bytecode_scripts/publish_basic_balance.mv --args signer:$ALICE" >> $BASH_SH
21 changes: 21 additions & 0 deletions pallet/src/assets/move-projects/gas-costs/sources/GetCoin.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
script {
use CafeAccount::BasicCoin;

fun publish_basic_balance(s: signer) {
BasicCoin::publish_balance(&s);
}
}

script {
use CafeAccount::BasicCoin;

fun mint(module_owner: signer, rx_addr: address, amount: u64) {
//BasicCoin::mint(&module_owner, rx_addr, amount);

let i = 1;
while (i <= amount) {
BasicCoin::mint(&module_owner, rx_addr, 1);
i = i + 1
};
}
}
139 changes: 84 additions & 55 deletions pallet/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::{mock_utils as utils, *};

type SourceOf<T> = <<T as SysConfig>::Lookup as sp_runtime::traits::StaticLookup>::Source;

const MAX_GAS_AMOUNT: u32 = u32::MAX;

macro_rules! impl_gas_costs_cal_fns {
($name:tt) => {
pub fn $name() -> &'static [u8] {
Expand All @@ -30,47 +32,61 @@ mod benchmarks {
use super::*;

/// Because it is challenging to determine a reliable and fixed relation between gas costs and
/// Substrate weights, we created Move scripts with known gas costs and increasing steps of 20.
/// Twenty-five scripts with rising gas costs of about 20 for each iteration step were used as
/// input for this benchmark. Therefore, the original output was divided by 20 afterwards.
/// Substrate weights, we created Move scripts with known gas costs and increasing steps of 403.
/// Twenty-five scripts with rising gas costs of about 403 for each iteration step were used as
/// input for this benchmark.
#[benchmark]
fn execute(n: Linear<0, 24>) {
let alice_32 = utils::account::<T>(utils::ALICE_ADDR);
let bob_32 = utils::account::<T>(utils::BOB_ADDR);

// Our benchmark plan (each is a test scenario with different parameters).
let script_bcs = [
cal_gas_cost_0().to_vec(),
cal_gas_cost_1().to_vec(),
cal_gas_cost_2().to_vec(),
cal_gas_cost_3().to_vec(),
cal_gas_cost_4().to_vec(),
cal_gas_cost_5().to_vec(),
cal_gas_cost_6().to_vec(),
cal_gas_cost_7().to_vec(),
cal_gas_cost_8().to_vec(),
cal_gas_cost_9().to_vec(),
cal_gas_cost_10().to_vec(),
cal_gas_cost_11().to_vec(),
cal_gas_cost_12().to_vec(),
cal_gas_cost_13().to_vec(),
cal_gas_cost_14().to_vec(),
cal_gas_cost_15().to_vec(),
cal_gas_cost_16().to_vec(),
cal_gas_cost_17().to_vec(),
cal_gas_cost_18().to_vec(),
cal_gas_cost_19().to_vec(),
cal_gas_cost_20().to_vec(),
cal_gas_cost_21().to_vec(),
cal_gas_cost_22().to_vec(),
cal_gas_cost_23().to_vec(),
cal_gas_cost_24().to_vec(),
mint_1().to_vec(),
mint_2().to_vec(),
mint_3().to_vec(),
mint_4().to_vec(),
mint_5().to_vec(),
mint_6().to_vec(),
mint_7().to_vec(),
mint_8().to_vec(),
mint_9().to_vec(),
mint_10().to_vec(),
mint_11().to_vec(),
mint_12().to_vec(),
mint_13().to_vec(),
mint_14().to_vec(),
mint_15().to_vec(),
mint_16().to_vec(),
mint_17().to_vec(),
mint_18().to_vec(),
mint_19().to_vec(),
mint_20().to_vec(),
mint_21().to_vec(),
mint_22().to_vec(),
mint_23().to_vec(),
mint_24().to_vec(),
mint_25().to_vec(),
];

Pallet::<T>::publish_module(
RawOrigin::Signed(bob_32.clone()).into(),
publish_basic_coin().to_vec(),
MAX_GAS_AMOUNT,
).unwrap();

Pallet::<T>::execute(
RawOrigin::Signed(alice_32.clone()).into(),
publish_basic_balance().to_vec(),
MAX_GAS_AMOUNT,
0u128.into(),
).unwrap();

#[extrinsic_call]
execute(
RawOrigin::Signed(alice_32),
RawOrigin::Signed(bob_32),
script_bcs[n as usize].clone(),
(n + 1) * 20,
19 + (n + 1) * 403,
0u128.into(),
)
}
Expand Down Expand Up @@ -187,29 +203,42 @@ mod benchmark_only {
)
}

impl_gas_costs_cal_fns!(cal_gas_cost_0);
impl_gas_costs_cal_fns!(cal_gas_cost_1);
impl_gas_costs_cal_fns!(cal_gas_cost_2);
impl_gas_costs_cal_fns!(cal_gas_cost_3);
impl_gas_costs_cal_fns!(cal_gas_cost_4);
impl_gas_costs_cal_fns!(cal_gas_cost_5);
impl_gas_costs_cal_fns!(cal_gas_cost_6);
impl_gas_costs_cal_fns!(cal_gas_cost_7);
impl_gas_costs_cal_fns!(cal_gas_cost_8);
impl_gas_costs_cal_fns!(cal_gas_cost_9);
impl_gas_costs_cal_fns!(cal_gas_cost_10);
impl_gas_costs_cal_fns!(cal_gas_cost_11);
impl_gas_costs_cal_fns!(cal_gas_cost_12);
impl_gas_costs_cal_fns!(cal_gas_cost_13);
impl_gas_costs_cal_fns!(cal_gas_cost_14);
impl_gas_costs_cal_fns!(cal_gas_cost_15);
impl_gas_costs_cal_fns!(cal_gas_cost_16);
impl_gas_costs_cal_fns!(cal_gas_cost_17);
impl_gas_costs_cal_fns!(cal_gas_cost_18);
impl_gas_costs_cal_fns!(cal_gas_cost_19);
impl_gas_costs_cal_fns!(cal_gas_cost_20);
impl_gas_costs_cal_fns!(cal_gas_cost_21);
impl_gas_costs_cal_fns!(cal_gas_cost_22);
impl_gas_costs_cal_fns!(cal_gas_cost_23);
impl_gas_costs_cal_fns!(cal_gas_cost_24);
// Basic Coin Example
pub fn publish_basic_coin() -> &'static [u8] {
core::include_bytes!(
"assets/move-projects/gas-costs/build/gas-costs/bytecode_modules/dependencies/basic_coin/BasicCoin.mv"
)
}

pub fn publish_basic_balance() -> &'static [u8] {
core::include_bytes!(
"assets/move-projects/gas-costs/build/gas-costs/script_transactions/publish_basic_balance.mvt"
)
}

impl_gas_costs_cal_fns!(mint_1);
impl_gas_costs_cal_fns!(mint_2);
impl_gas_costs_cal_fns!(mint_3);
impl_gas_costs_cal_fns!(mint_4);
impl_gas_costs_cal_fns!(mint_5);
impl_gas_costs_cal_fns!(mint_6);
impl_gas_costs_cal_fns!(mint_7);
impl_gas_costs_cal_fns!(mint_8);
impl_gas_costs_cal_fns!(mint_9);
impl_gas_costs_cal_fns!(mint_10);
impl_gas_costs_cal_fns!(mint_11);
impl_gas_costs_cal_fns!(mint_12);
impl_gas_costs_cal_fns!(mint_13);
impl_gas_costs_cal_fns!(mint_14);
impl_gas_costs_cal_fns!(mint_15);
impl_gas_costs_cal_fns!(mint_16);
impl_gas_costs_cal_fns!(mint_17);
impl_gas_costs_cal_fns!(mint_18);
impl_gas_costs_cal_fns!(mint_19);
impl_gas_costs_cal_fns!(mint_20);
impl_gas_costs_cal_fns!(mint_21);
impl_gas_costs_cal_fns!(mint_22);
impl_gas_costs_cal_fns!(mint_23);
impl_gas_costs_cal_fns!(mint_24);
impl_gas_costs_cal_fns!(mint_25);
}
58 changes: 28 additions & 30 deletions pallet/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! 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: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `michaeleberhardts-MacBook-Pro.local`, CPU: `<UNKNOWN>`
//! HOSTNAME: `bonka-MacBook-Pro.local`, CPU: `<UNKNOWN>`
//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: 1024
// Executed Command:
Expand Down Expand Up @@ -33,31 +33,29 @@
use frame_support::{traits::Get, weights::Weight};
use core::marker::PhantomData;

// Note: The weights have not only been generated by the Substrate benchmarks with a template-node.
// A couple of manual and additional modifications have been done which are marked in the
// comments.

/// Weight functions for `pallet_move`.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> crate::weight_info::WeightInfo for SubstrateWeight<T> {
/// Because it is challenging to determine a reliable and fixed relation between gas costs and
/// Substrate weights, we created Move scripts with known gas costs and increasing steps of 20.
/// Twenty-five scripts with rising gas costs of about 20 for each iteration step were used as
/// input for this benchmark. Therefore, the original output was divided by 20 afterwards.
/// Storage: `Balances::Locks` (r:1 w:1)
/// Proof: `Balances::Locks` (`max_values`: None, `max_size`: Some(1299), added: 3774, mode: `MaxEncodedLen`)
/// Storage: `Balances::Freezes` (r:1 w:0)
/// Proof: `Balances::Freezes` (`max_values`: None, `max_size`: Some(49), added: 2524, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `MoveModule::VMStorage` (r:3 w:1)
/// Proof: `MoveModule::VMStorage` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// The range of component `n` is `[0, 24]`.
fn execute(n: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 46_000_000 picoseconds.
Weight::from_parts(44_730_881, 0)
.saturating_add(Weight::from_parts(0, 0))
// Standard Error: 9_543
// Manually divided original output by 20: 13_002_180 / 20 = 650_109
.saturating_add(Weight::from_parts(650_109, 0).saturating_mul(n.into()))
// Manuelly added:
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(1))
// Measured: `8622`
// Estimated: `17037`
// Minimum execution time: 345_000_000 picoseconds.
Weight::from_parts(348_832_865, 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()))
.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`)
Expand All @@ -66,11 +64,11 @@ impl<T: frame_system::Config> crate::weight_info::WeightInfo for SubstrateWeight
// Proof Size summary in bytes:
// Measured: `7796`
// Estimated: `14073`
// Minimum execution time: 44_000_000 picoseconds.
Weight::from_parts(318_442_817, 0)
// Minimum execution time: 42_000_000 picoseconds.
Weight::from_parts(310_274_033, 0)
.saturating_add(Weight::from_parts(0, 14073))
// Standard Error: 3_649_708
.saturating_add(Weight::from_parts(4_313_812, 0).saturating_mul(n.into()))
// 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))
}
Expand All @@ -81,11 +79,11 @@ impl<T: frame_system::Config> crate::weight_info::WeightInfo for SubstrateWeight
// Proof Size summary in bytes:
// Measured: `7796`
// Estimated: `14073`
// Minimum execution time: 44_000_000 picoseconds.
Weight::from_parts(319_610_220, 0)
// Minimum execution time: 43_000_000 picoseconds.
Weight::from_parts(311_709_392, 0)
.saturating_add(Weight::from_parts(0, 14073))
// Standard Error: 3_670_322
.saturating_add(Weight::from_parts(4_522_651, 0).saturating_mul(n.into()))
// 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))
.saturating_add(T::DbWeight::get().writes(1))
}
Expand All @@ -95,8 +93,8 @@ impl<T: frame_system::Config> crate::weight_info::WeightInfo for SubstrateWeight
// Proof Size summary in bytes:
// Measured: `7796`
// Estimated: `11261`
// Minimum execution time: 204_000_000 picoseconds.
Weight::from_parts(206_000_000, 0)
// Minimum execution time: 198_000_000 picoseconds.
Weight::from_parts(200_000_000, 0)
.saturating_add(Weight::from_parts(0, 11261))
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
Expand Down

0 comments on commit 1438a93

Please sign in to comment.