Skip to content

Commit

Permalink
feat: add more gas cost measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoks committed May 3, 2024
1 parent 5f1ea31 commit 32fcec1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
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)
}
}
33 changes: 32 additions & 1 deletion 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,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");
Expand Down Expand Up @@ -74,6 +81,22 @@ 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:
Expand All @@ -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();
Expand Down

0 comments on commit 32fcec1

Please sign in to comment.