From b3f9fd79210b93386ccd93576df96a57f8862c8c Mon Sep 17 00:00:00 2001 From: Michael Eberhardt <64731211+neutrinoks@users.noreply.github.com> Date: Fri, 10 May 2024 16:44:05 +0400 Subject: [PATCH] feat: new gas cost measurement scripts and integration --- pallet/build.rs | 11 ----- .../gas-costs/sources/ModuleNotModule.move | 32 +++++++++++++ .../gas-costs/sources/TheModule.move | 8 ++++ .../assets/move-projects/smove-build-all.sh | 1 + .../move-projects/smove-module-weights.sh | 9 ---- pallet/src/tests/gas_costs.rs | 45 +++++++++++++++---- 6 files changed, 77 insertions(+), 29 deletions(-) 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 delete mode 100755 pallet/src/assets/move-projects/smove-module-weights.sh diff --git a/pallet/build.rs b/pallet/build.rs index 5fad504..f3ee1d7 100644 --- a/pallet/build.rs +++ b/pallet/build.rs @@ -33,17 +33,6 @@ fn build_move_projects() -> Result<(), Box> { // Rerun in case Move source files are changed. println!("cargo:rerun-if-changed=tests/assets/move-projects"); - // Compile additionally move-project for gas cost pseudo-benchmark. - if cfg!(feature = "gas-cost-measurement") { - let smove_run = Command::new("bash") - .args(["src/assets/move-projects/gas-costs/build.sh"]) - .output() - .expect("failed to execute script which builds necessary move modules"); - eval_smove_run(smove_run)?; - - println!("cargo:warning=Move project 'gas-costs' built successfully"); - } - Ok(()) } 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/assets/move-projects/smove-build-all.sh b/pallet/src/assets/move-projects/smove-build-all.sh index 6902586..b1f53c4 100755 --- a/pallet/src/assets/move-projects/smove-build-all.sh +++ b/pallet/src/assets/move-projects/smove-build-all.sh @@ -7,6 +7,7 @@ build_dir=( "balance" "base58_smove_build" "car-wash-example" + "gas-costs" "get-resource" "move-basics" "multiple-signers" diff --git a/pallet/src/assets/move-projects/smove-module-weights.sh b/pallet/src/assets/move-projects/smove-module-weights.sh deleted file mode 100755 index b268a06..0000000 --- a/pallet/src/assets/move-projects/smove-module-weights.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -cd $(dirname $0) -# Our students who want to rent a dorm together. -BOB=5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty -# Estimate needed gas for a single script execution. -smove node rpc estimate-gas-publish-module -a $BOB -m move-basics/build/move-basics/bytecode_modules/EmptyBob.mv -smove node rpc estimate-gas-publish-module -a $BOB -m using_stdlib_natives/build/using_stdlib_natives/bytecode_modules/Vector.mv -smove node rpc estimate-gas-publish-module -a $BOB -m car-wash-example/build/car-wash-example/bytecode_modules/CarWash.mv -smove node rpc estimate-gas-publish-module -a $BOB -m multiple-signers/build/multiple-signers/bytecode_modules/Dorm.mv diff --git a/pallet/src/tests/gas_costs.rs b/pallet/src/tests/gas_costs.rs index a907713..bb28b7b 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,9 @@ 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,23 +77,47 @@ 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: - gas: {gas1:?} - weight: {weight1:?} + Gas: {gas1:?} + {weight1:?} Small and expensive script: - gas: {gas2:?} - weight: {weight2:?} + Gas: {gas2:?} + {weight2:?} Long and cheap script: - gas: {gas3:?} - weight: {weight3:?} + Gas: {gas3:?} + {weight3:?} Long and expensive script: - gas: {gas4:?} - weight: {weight4:?} + Gas: {gas4:?} + {weight4:?} + +Simple math script, no module used: + Gas: {gas5:?} + {weight5:?} + +Simple math script, module used: + Gas: {gas6:?} + {weight6:?} ", ); std::fs::write("./../gas_costs.txt", output).unwrap();