From a10e795af9841df0d4047a6f7a99ec338e4fe9dd Mon Sep 17 00:00:00 2001 From: Eagle941 <8973725+Eagle941@users.noreply.github.com> Date: Mon, 9 Sep 2024 02:45:29 +0100 Subject: [PATCH] feat: added benchmark 'execution_benchmark' to measure performance of 'execute' function --- crates/blockifier/bench/blockifier_bench.rs | 63 ++++++++++++++++++- .../blockifier/src/concurrency/fee_utils.rs | 5 +- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/crates/blockifier/bench/blockifier_bench.rs b/crates/blockifier/bench/blockifier_bench.rs index a59d71d6c0..bb38260006 100644 --- a/crates/blockifier/bench/blockifier_bench.rs +++ b/crates/blockifier/bench/blockifier_bench.rs @@ -5,12 +5,27 @@ //! The main benchmark function is `transfers_benchmark`, which measures the performance //! of transfers between randomly created accounts, which are iterated over round-robin. //! -//! Run the benchmarks using `cargo bench --bench blockifier_bench`. +//! The other benchmark function is `execution_benchmark` which measures the performance of the +//! method [`blockifier::transactions::transaction::ExecutableTransaction::execute`] by executing +//! the entry point `advance_counter` of the test contract. +//! +//! //! Run the benchmarks using `cargo bench --bench blockifier_bench`. +use blockifier::context::BlockContext; +use blockifier::invoke_tx_args; +use blockifier::state::cached_state::CachedState; +use blockifier::test_utils::contracts::FeatureContract; +use blockifier::test_utils::dict_state_reader::DictStateReader; +use blockifier::test_utils::initial_test_state::test_state; use blockifier::test_utils::transfers_generator::{ RecipientGeneratorType, TransfersGenerator, TransfersGeneratorConfig, }; -use criterion::{criterion_group, criterion_main, Criterion}; +use blockifier::test_utils::{create_calldata, CairoVersion, NonceManager, BALANCE}; +use blockifier::transaction::account_transaction::AccountTransaction; +use blockifier::transaction::test_utils::{account_invoke_tx, block_context, max_resource_bounds}; +use blockifier::transaction::transactions::ExecutableTransaction; +use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; +use starknet_api::felt; pub fn transfers_benchmark(c: &mut Criterion) { let transfers_generator_config = TransfersGeneratorConfig { @@ -27,5 +42,47 @@ pub fn transfers_benchmark(c: &mut Criterion) { }); } -criterion_group!(benches, transfers_benchmark); +pub fn execution_benchmark(c: &mut Criterion) { + /// This function sets up and returns all the objects required to execute an invoke transaction. + fn prepare_account_tx() -> (AccountTransaction, CachedState, BlockContext) { + let block_context = block_context(); + let max_resource_bounds = max_resource_bounds(); + let cairo_version = CairoVersion::Cairo1; + let account = FeatureContract::AccountWithoutValidations(cairo_version); + let test_contract = FeatureContract::TestContract(cairo_version); + let state = + test_state(block_context.chain_info(), BALANCE, &[(account, 1), (test_contract, 1)]); + let account_address = account.get_instance_address(0); + let contract_address = test_contract.get_instance_address(0); + let index = felt!(123_u32); + let base_tx_args = invoke_tx_args! { + resource_bounds: max_resource_bounds, + sender_address: account_address, + }; + + let mut nonce_manager = NonceManager::default(); + let counter_diffs = [101_u32, 102_u32]; + let initial_counters = [felt!(counter_diffs[0]), felt!(counter_diffs[1])]; + let calldata_args = vec![index, initial_counters[0], initial_counters[1]]; + + let account_tx = account_invoke_tx(invoke_tx_args! { + nonce: nonce_manager.next(account_address), + calldata: + create_calldata(contract_address, "advance_counter", &calldata_args), + ..base_tx_args + }); + (account_tx, state, block_context) + } + c.bench_function("execution", move |benchmark| { + benchmark.iter_batched( + prepare_account_tx, + |(account_tx, mut state, block_context)| { + account_tx.execute(&mut state, &block_context, true, true).unwrap() + }, + BatchSize::SmallInput, + ) + }); +} + +criterion_group!(benches, transfers_benchmark, execution_benchmark); criterion_main!(benches); diff --git a/crates/blockifier/src/concurrency/fee_utils.rs b/crates/blockifier/src/concurrency/fee_utils.rs index b9ad04942e..8a0800a2e2 100644 --- a/crates/blockifier/src/concurrency/fee_utils.rs +++ b/crates/blockifier/src/concurrency/fee_utils.rs @@ -72,9 +72,8 @@ pub fn fill_sequencer_balance_reads( ) { let storage_read_values = if fee_transfer_call_info.inner_calls.is_empty() { &mut fee_transfer_call_info.storage_read_values - } else - // Proxy pattern. - { + } else { + // Proxy pattern. assert_eq!( fee_transfer_call_info.inner_calls.len(), 1,