Skip to content

Commit

Permalink
feat: new gas cost measurement scripts and integration
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoks committed May 13, 2024
1 parent 5f1ea31 commit b3f9fd7
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 29 deletions.
11 changes: 0 additions & 11 deletions pallet/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@ fn build_move_projects() -> Result<(), Box<dyn Error>> {
// 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(())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions pallet/src/assets/move-projects/smove-build-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ build_dir=(
"balance"
"base58_smove_build"
"car-wash-example"
"gas-costs"
"get-resource"
"move-basics"
"multiple-signers"
Expand Down
9 changes: 0 additions & 9 deletions pallet/src/assets/move-projects/smove-module-weights.sh

This file was deleted.

45 changes: 36 additions & 9 deletions pallet/src/tests/gas_costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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");
Expand Down Expand Up @@ -74,23 +77,47 @@ fn pseudo_benchmark_gas_costs() {
.gas_used;
let weight4: Weight = SubstrateWeight::<Test>::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::<Test>::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::<Test>::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();
Expand Down

0 comments on commit b3f9fd7

Please sign in to comment.