Skip to content

Commit

Permalink
feat: added benchmark 'execution_benchmark' to measure performance of…
Browse files Browse the repository at this point in the history
… 'execute' function
  • Loading branch information
Eagle941 committed Sep 9, 2024
1 parent 2ca7610 commit a10e795
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
63 changes: 60 additions & 3 deletions crates/blockifier/bench/blockifier_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<DictStateReader>, 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);
5 changes: 2 additions & 3 deletions crates/blockifier/src/concurrency/fee_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a10e795

Please sign in to comment.