-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CLI] introducing local simulation, benchmarking and replay (#12832)
- Loading branch information
Showing
10 changed files
with
405 additions
and
51 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
pub mod init; | ||
pub mod local_simulation; | ||
pub mod types; | ||
pub mod utils; |
Oops, something went wrong.