Skip to content

Commit

Permalink
[CLI] introducing local simulation, benchmarking and replay (#12832)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgao1996 authored Apr 15, 2024
1 parent afcf9fd commit 47af66c
Show file tree
Hide file tree
Showing 10 changed files with 405 additions and 51 deletions.
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion aptos-move/aptos-debugger/src/aptos_debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl AptosDebugger {

// Deprecated.
TransactionPayload::ModuleBundle(..) => {
unreachable!("Module bundle payload has already been checked")
unreachable!("Module bundle payload has already been checked because before this function is called")
},
};
Ok(gas_profiler)
Expand Down
2 changes: 1 addition & 1 deletion aptos-move/aptos-vm/src/aptos_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1716,7 +1716,7 @@ impl AptosVM {
})
}

fn execute_user_transaction(
pub fn execute_user_transaction(
&self,
resolver: &impl AptosMoveResolver,
txn: &SignedTransaction,
Expand Down
3 changes: 3 additions & 0 deletions crates/aptos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ aptos-temppath = { workspace = true }
aptos-types = { workspace = true }
aptos-vm = { workspace = true, features = ["testing"] }
aptos-vm-genesis = { workspace = true }
aptos-vm-logging = { workspace = true }
aptos-vm-types = { workspace = true }
async-trait = { workspace = true }
base64 = { workspace = true }
bcs = { workspace = true }
Expand Down Expand Up @@ -97,6 +99,7 @@ toml = { workspace = true }
tonic = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
url = { workspace = true }
version-compare = { workspace = true }

[target.'cfg(unix)'.dependencies]
Expand Down
136 changes: 136 additions & 0 deletions crates/aptos/src/common/local_simulation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

use crate::common::types::{CliError, CliTypedResult};
use aptos_crypto::HashValue;
use aptos_gas_profiling::FrameName;
use aptos_move_debugger::aptos_debugger::AptosDebugger;
use aptos_types::transaction::SignedTransaction;
use aptos_vm::{data_cache::AsMoveResolver, AptosVM};
use aptos_vm_logging::log_schema::AdapterLogSchema;
use aptos_vm_types::{output::VMOutput, resolver::StateStorageView};
use move_core_types::vm_status::VMStatus;
use std::{path::Path, time::Instant};

pub fn run_transaction_using_debugger(
debugger: &AptosDebugger,
version: u64,
transaction: SignedTransaction,
_hash: HashValue,
) -> CliTypedResult<(VMStatus, VMOutput)> {
let state_view = debugger.state_view_at_version(version);
let resolver = state_view.as_move_resolver();

let vm = AptosVM::new(&resolver, None);
let log_context = AdapterLogSchema::new(resolver.id(), 0);

let (vm_status, vm_output) = vm.execute_user_transaction(&resolver, &transaction, &log_context);

Ok((vm_status, vm_output))
}

pub fn benchmark_transaction_using_debugger(
debugger: &AptosDebugger,
version: u64,
transaction: SignedTransaction,
_hash: HashValue,
) -> CliTypedResult<(VMStatus, VMOutput)> {
let state_view = debugger.state_view_at_version(version);
let resolver = state_view.as_move_resolver();

let vm = AptosVM::new(&resolver, None);
let log_context = AdapterLogSchema::new(resolver.id(), 0);

let (vm_status, vm_output) = vm.execute_user_transaction(&resolver, &transaction, &log_context);

let time_cold = {
let n = 15;

let mut times = vec![];
for _i in 0..n {
// Create a new VM each time so to include code loading as part of the
// total running time.
let vm = AptosVM::new(&resolver, None);
let log_context = AdapterLogSchema::new(resolver.id(), 0);

let t1 = Instant::now();
std::hint::black_box(vm.execute_user_transaction(
&resolver,
&transaction,
&log_context,
));
let t2 = Instant::now();

times.push(t2 - t1);
}
times.sort();

times[n / 2]
};

let time_warm = {
let mut times = vec![];
let n = 15;

for _i in 0..n {
// Reuse the existing VM with warm code cache so to measure only the
// execution time.
let t1 = Instant::now();
std::hint::black_box(vm.execute_user_transaction(
&resolver,
&transaction,
&log_context,
));
let t2 = Instant::now();

times.push(t2 - t1);
}

times[n / 2]
};

println!("Running time (cold code cache): {:?}", time_cold);
println!("Running time (warm code cache): {:?}", time_warm);

Ok((vm_status, vm_output))
}

pub fn profile_transaction_using_debugger(
debugger: &AptosDebugger,
version: u64,
transaction: SignedTransaction,
hash: HashValue,
) -> CliTypedResult<(VMStatus, VMOutput)> {
let (vm_status, vm_output, gas_log) = debugger
.execute_transaction_at_version_with_gas_profiler(version, transaction)
.map_err(|err| {
CliError::UnexpectedError(format!("failed to simulate txn with gas profiler: {}", err))
})?;

// Generate a humen-readable name for the report
let entry_point = gas_log.entry_point();

let human_readable_name = match entry_point {
FrameName::Script => "script".to_string(),
FrameName::Function {
module_id, name, ..
} => {
let addr_short = module_id.address().short_str_lossless();
let addr_truncated = if addr_short.len() > 4 {
&addr_short[..4]
} else {
addr_short.as_str()
};
format!("0x{}-{}-{}", addr_truncated, module_id.name(), name)
},
};
let raw_file_name = format!("txn-{}-{}", hash, human_readable_name);

// Generate the report
let path = Path::new("gas-profiling").join(raw_file_name);
gas_log.generate_html_report(&path, format!("Gas Report - {}", human_readable_name))?;

println!("Gas report saved to {}.", path.display());

Ok((vm_status, vm_output))
}
1 change: 1 addition & 0 deletions crates/aptos/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

pub mod init;
pub mod local_simulation;
pub mod types;
pub mod utils;
Loading

0 comments on commit 47af66c

Please sign in to comment.