From 32fcec15cf28277135adc162603ef7869fee65f6 Mon Sep 17 00:00:00 2001 From: Michael Eberhardt <64731211+neutrinoks@users.noreply.github.com> Date: Fri, 3 May 2024 16:05:09 +0400 Subject: [PATCH] feat: add more gas cost measurements --- .../gas-costs/sources/ModuleNotModule.move | 32 ++++++++++++++++++ .../gas-costs/sources/TheModule.move | 8 +++++ pallet/src/tests/gas_costs.rs | 33 ++++++++++++++++++- 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 pallet/src/assets/move-projects/gas-costs/sources/ModuleNotModule.move create mode 100644 pallet/src/assets/move-projects/gas-costs/sources/TheModule.move diff --git a/pallet/src/assets/move-projects/gas-costs/sources/ModuleNotModule.move b/pallet/src/assets/move-projects/gas-costs/sources/ModuleNotModule.move new file mode 100644 index 0000000..ebb82c3 --- /dev/null +++ b/pallet/src/assets/move-projects/gas-costs/sources/ModuleNotModule.move @@ -0,0 +1,32 @@ +script { + fun uses_no_module() { + let a: u64 = 0; + let sum: u64 = 0; + let cnt = 0; + + while (cnt < 5000) { + a = a + 1; + sum = sum + a; + cnt = cnt + 1; + }; + + assert!(sum > 5000, 0); + } +} + +script { + use DeveloperBob::TheModule; + + fun uses_module() { + let a: u64 = 0; + let sum: u64 = 0; + let cnt = 0; + + while (cnt < 5000) { + (sum, a) = TheModule::stupid_algorithm(sum, a); + cnt = cnt + 1; + }; + + assert!(sum > 5000, 0); + } +} diff --git a/pallet/src/assets/move-projects/gas-costs/sources/TheModule.move b/pallet/src/assets/move-projects/gas-costs/sources/TheModule.move new file mode 100644 index 0000000..5e2a931 --- /dev/null +++ b/pallet/src/assets/move-projects/gas-costs/sources/TheModule.move @@ -0,0 +1,8 @@ +module DeveloperBob::TheModule { + // Useless algorithm to test difference between function calls and direct implementation. + public fun stupid_algorithm(sum: u64, a: u64): (u64, u64) { + a = a + 1; + sum = sum + a; + return (sum, a) + } +} diff --git a/pallet/src/tests/gas_costs.rs b/pallet/src/tests/gas_costs.rs index a907713..47357f9 100644 --- a/pallet/src/tests/gas_costs.rs +++ b/pallet/src/tests/gas_costs.rs @@ -22,7 +22,7 @@ fn pseudo_benchmark_gas_costs() { let script = utils::read_script_from_project("car-wash-example", "initial_coin_minting"); let script_bc = script_transaction!(script, no_type_args!(), &bob_addr_move); MoveModule::execute( - RuntimeOrigin::signed(bob_addr_32), + RuntimeOrigin::signed(bob_addr_32.clone()), script_bc, MAX_GAS_AMOUNT, 0, @@ -37,6 +37,13 @@ fn pseudo_benchmark_gas_costs() { 0, ) .unwrap(); + let module = utils::read_module_from_project("gas-costs", "TheModule"); + MoveModule::publish_module( + RuntimeOrigin::signed(bob_addr_32), + module, + MAX_GAS_AMOUNT, + ) + .unwrap(); // Short and cheap script. let script = utils::read_script_from_project("gas-costs", "short_cheap_script"); @@ -74,6 +81,22 @@ fn pseudo_benchmark_gas_costs() { .gas_used; let weight4: Weight = SubstrateWeight::::execute(gas4 as u32); + // Simple math script that doesn't use a module. + let script = utils::read_script_from_project("gas-costs", "uses_no_module"); + let script_bc = script_transaction!(script, no_type_args!()); + let gas5: u64 = MoveModule::rpc_estimate_gas_execute_script(script_bc) + .unwrap() + .gas_used; + let weight5: Weight = SubstrateWeight::::execute(gas5 as u32); + + // Simple math script that uses a module to do the same. + let script = utils::read_script_from_project("gas-costs", "uses_module"); + let script_bc = script_transaction!(script, no_type_args!()); + let gas6: u64 = MoveModule::rpc_estimate_gas_execute_script(script_bc) + .unwrap() + .gas_used; + let weight6: Weight = SubstrateWeight::::execute(gas6 as u32); + // Write all results to file "gas_costs.txt". let output = format!( "Short and cheap script: @@ -91,6 +114,14 @@ Long and cheap script: Long and expensive script: gas: {gas4:?} weight: {weight4:?} + +Simple math script, no module used: + gas: {gas5:?} + weight: {weight5:?} + +Simple math script, module used: + gas: {gas6:?} + weight: {weight6:?} ", ); std::fs::write("./../gas_costs.txt", output).unwrap();