From ff565d4deed6c4d821caae36a35db3b7861f099f Mon Sep 17 00:00:00 2001 From: Teng Zhang Date: Fri, 23 Aug 2024 14:14:11 -0700 Subject: [PATCH] add gas diff --- .../src/execution.rs | 52 +++++++++++++++---- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/aptos-move/aptos-e2e-comparison-testing/src/execution.rs b/aptos-move/aptos-e2e-comparison-testing/src/execution.rs index e224156d41ae2b..da7b7e15997165 100644 --- a/aptos-move/aptos-e2e-comparison-testing/src/execution.rs +++ b/aptos-move/aptos-e2e-comparison-testing/src/execution.rs @@ -27,6 +27,8 @@ use move_model::metadata::CompilerVersion; use std::{cmp, collections::HashMap, env, path::PathBuf, sync::Arc}; // use std::cmp::min; +const GAS_DIFF_PERCENTAGE: u64 = 5; + fn add_packages_to_data_store( data_store: &mut FakeDataStore, package_info: &PackageInfo, @@ -353,10 +355,10 @@ impl Execution { self.print_mismatches(cur_version, &res_main, &res_other); } else { match res_main { - Ok(((write_set, events), txn_status)) => { + Ok(((write_set, events), txn_status, gas)) => { self.output_result_str(format!( - "version:{}\nwrite set:{:?}\n events:{:?}, txn_status:{:?}\n", - cur_version, write_set, events, txn_status + "version:{}\nwrite set:{:?}\n events:{:?}, txn_status:{:?}, gas:{}\n", + cur_version, write_set, events, txn_status, gas )); }, Err(vm_status) => { @@ -378,7 +380,7 @@ impl Execution { compiled_package_cache: &HashMap>>, debugger_opt: Option>, v2_flag: bool, - ) -> Result<((WriteSet, Vec), TransactionStatus), VMStatus> { + ) -> Result<((WriteSet, Vec), TransactionStatus, u64), VMStatus> { // Always add Aptos (0x1) packages. add_aptos_packages_to_data_store(&mut state, compiled_package_cache); @@ -412,7 +414,11 @@ impl Execution { .execute_transaction_block_with_state_view(txns, &data_view, false) .map(|mut res| { let res_i = res.pop().unwrap(); - (res_i.clone().into(), res_i.status().clone()) + ( + res_i.clone().into(), + res_i.status().clone(), + res_i.gas_used(), + ) }) } else { let res = executor @@ -425,7 +431,11 @@ impl Execution { // res_i.gas_used(), // res_i.status() // ); - (res_i.clone().into(), res_i.status().clone()) + ( + res_i.clone().into(), + res_i.status().clone(), + res_i.gas_used(), + ) }); res } @@ -453,9 +463,24 @@ impl Execution { fn print_mismatches( &self, cur_version: u64, - res_1: &Result<((WriteSet, Vec), TransactionStatus), VMStatus>, - res_2: &Result<((WriteSet, Vec), TransactionStatus), VMStatus>, + res_1: &Result<((WriteSet, Vec), TransactionStatus, u64), VMStatus>, + res_2: &Result<((WriteSet, Vec), TransactionStatus, u64), VMStatus>, ) { + let gas_diff = |gas_1: u64, gas_2: u64, x: u64| -> (f64, bool, bool) { + let gas2_ge_gas1 = gas_2 >= gas_1; + let mut denominator = gas_1; + let mut difference = gas_2 as i64 - gas_1 as i64; + if !gas2_ge_gas1 { + difference = gas_1 as i64 - gas_2 as i64; + denominator = gas_2; + } + let percentage_difference = difference as f64 / denominator as f64 * 100.0; + ( + percentage_difference, + gas2_ge_gas1 && percentage_difference > x as f64, + !gas2_ge_gas1, + ) + }; match (res_1, res_2) { (Err(e1), Err(e2)) => { if e1.message() != e2.message() || e1.status_code() != e2.status_code() { @@ -479,7 +504,7 @@ impl Execution { cur_version )); }, - (Ok((res_1, txn_status_1)), Ok((res_2, txn_status_2))) => { + (Ok((res_1, txn_status_1, gas_used_1)), Ok((res_2, txn_status_2, gas_used_2))) => { // compare txn status if txn_status_1 != txn_status_2 { println!("txn status is different at version: {}", cur_version); @@ -563,6 +588,15 @@ impl Execution { cur_version )); } + let (diff, gas2_gt_gas1, gas1_gt_gas_2) = + gas_diff(*gas_used_1, *gas_used_2, GAS_DIFF_PERCENTAGE); + let greater_version = if gas1_gt_gas_2 { "v1" } else { "v2" }; + if gas2_gt_gas1 || gas1_gt_gas_2 { + self.output_result_str(format!( + "gas diff: {}'s gas usage is {} percent more than the other at version: {}, v1 status:{:?}, v2 status:{:?}", + greater_version, diff, cur_version, txn_status_1, txn_status_2 + )); + } }, } }