Skip to content

Commit

Permalink
feat(starknet_gateway): create transaction generator towards benchmar…
Browse files Browse the repository at this point in the history
…king
  • Loading branch information
ArniStarkware committed Dec 25, 2024
1 parent cdc7718 commit e4fac15
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions crates/starknet_gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ blockifier = { workspace = true, features = ["testing"] }
cairo-lang-starknet-classes.workspace = true
futures.workspace = true
mempool_test_utils.workspace = true
mockall.workspace = true
papyrus_config.workspace = true
papyrus_network_types.workspace = true
papyrus_rpc.workspace = true
Expand All @@ -39,8 +40,7 @@ validator.workspace = true
[dev-dependencies]
assert_matches.workspace = true
cairo-lang-sierra-to-casm.workspace = true
criterion = { workspace = true, features = ["html_reports"] }
mockall.workspace = true
criterion = { workspace = true, features = ["html_reports", "async_tokio"] }
mockito.workspace = true
num-bigint.workspace = true
papyrus_network_types = { workspace = true, features = ["testing"] }
Expand All @@ -55,3 +55,4 @@ tracing-test.workspace = true
harness = false
name = "gateway_bench"
path = "bench/gateway_bench.rs"
required-features = ["testing"]
11 changes: 9 additions & 2 deletions crates/starknet_gateway/bench/gateway_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
//!
//! Run the benchmarks using `cargo bench --bench gateway_bench`.
use criterion::{criterion_group, criterion_main, Criterion};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use starknet_gateway::test_utils::bench_test_setup::{BenchTestSetup, BenchTestSetupConfig};

pub fn declare_benchmark(c: &mut Criterion) {
c.bench_function("declares", |benchmark| benchmark.iter(|| {}));
Expand All @@ -20,7 +21,13 @@ pub fn deploy_account_benchmark(c: &mut Criterion) {
}

pub fn invoke_benchmark(c: &mut Criterion) {
c.bench_function("invokes", |benchmark| benchmark.iter(|| {}));
let tx_generator_config = BenchTestSetupConfig::default();
let n_txs = tx_generator_config.n_txs;

let test_setup = BenchTestSetup::new(tx_generator_config);
c.bench_with_input(BenchmarkId::new("invoke", n_txs), &test_setup, |b, s| {
b.to_async(tokio::runtime::Runtime::new().unwrap()).iter(|| s.send_txs_to_gateway());
});
}

pub fn gateway_benchmark(c: &mut Criterion) {
Expand Down
6 changes: 3 additions & 3 deletions crates/starknet_gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ pub mod rpc_state_reader;
#[cfg(test)]
mod rpc_state_reader_test;
pub mod state_reader;
#[cfg(test)]
#[cfg(any(feature = "testing", test))]
mod state_reader_test_utils;
mod stateful_transaction_validator;
mod stateless_transaction_validator;
mod sync_state_reader;
#[cfg(test)]
mod test_utils;
#[cfg(any(feature = "testing", test))]
pub mod test_utils;
mod utils;
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn test_instantiate_validator(stateful_validator: StatefulTransactionValidator)

let mut mock_state_reader_factory = MockStateReaderFactory::new();

// Make sure stateful_validator uses the latest block in the initiall call.
// Make sure stateful_validator uses the latest block in the initial call.
let latest_state_reader = state_reader_factory.get_state_reader_from_latest_block();
mock_state_reader_factory
.expect_get_state_reader_from_latest_block()
Expand Down
12 changes: 11 additions & 1 deletion crates/starknet_gateway/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use mockall::predicate::eq;
use mockall::predicate::{always, eq};
use starknet_api::block::GasPrice;
use starknet_api::core::ContractAddress;
use starknet_api::data_availability::DataAvailabilityMode;
Expand Down Expand Up @@ -29,6 +29,8 @@ use crate::config::GatewayConfig;
use crate::gateway::Gateway;
use crate::state_reader_test_utils::TestStateReaderFactory;

pub mod bench_test_setup;

pub const NON_EMPTY_RESOURCE_BOUNDS: ResourceBounds =
ResourceBounds { max_amount: GasAmount(1), max_price_per_unit: GasPrice(1) };

Expand Down Expand Up @@ -162,7 +164,15 @@ impl MockDependencies {
)
}

#[allow(
dead_code,
reason = "This function is used with cfg(test) but not with the 'testing' feature"
)]
pub fn expect_add_tx(&mut self, args: AddTransactionArgsWrapper) {
self.mock_mempool_client.expect_add_tx().once().with(eq(args)).return_once(|_| Ok(()));
}

pub fn expect_add_tx_always(&mut self) {
self.mock_mempool_client.expect_add_tx().times(..).with(always()).return_const(Ok(()));
}
}
108 changes: 108 additions & 0 deletions crates/starknet_gateway/src/test_utils/bench_test_setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use blockifier::context::ChainInfo;
use blockifier::test_utils::contracts::FeatureContract;
use blockifier::test_utils::{create_trivial_calldata, CairoVersion};
use mempool_test_utils::starknet_api_test_utils::test_valid_resource_bounds;
use starknet_api::core::ContractAddress;
use starknet_api::invoke_tx_args;
use starknet_api::rpc_transaction::RpcTransaction;
use starknet_api::test_utils::invoke::rpc_invoke_tx;
use starknet_api::test_utils::NonceManager;
use starknet_mempool_types::communication::MockMempoolClient;
use starknet_sierra_compile::config::SierraToCasmCompilationConfig;

use crate::compilation::GatewayCompiler;
use crate::config::GatewayConfig;
use crate::gateway::Gateway;
use crate::state_reader_test_utils::local_test_state_reader_factory;
use crate::test_utils::MockDependencies;

const N_TXS: usize = 100;

pub struct BenchTestSetupConfig {
pub n_txs: usize,
pub gateway_config: GatewayConfig,
pub compiler_config: SierraToCasmCompilationConfig,
}

impl Default for BenchTestSetupConfig {
fn default() -> Self {
Self {
n_txs: N_TXS,
gateway_config: GatewayConfig {
chain_info: ChainInfo::create_for_testing(),
..Default::default()
},
compiler_config: SierraToCasmCompilationConfig::default(),
}
}
}

pub struct BenchTestSetup {
gateway: Gateway,
txs: Vec<RpcTransaction>,
}

struct TransactionGenerator {
nonce_manager: NonceManager,
sender_address: ContractAddress,
test_contract_address: ContractAddress,
}

impl TransactionGenerator {
fn new(cairo_version: CairoVersion) -> Self {
let account_contract = FeatureContract::AccountWithoutValidations(cairo_version);
let test_contract = FeatureContract::TestContract(cairo_version);
let sender_address = account_contract.get_instance_address(0);
let test_contract_address = test_contract.get_instance_address(0);
Self { nonce_manager: NonceManager::default(), sender_address, test_contract_address }
}

fn generate_invoke(&mut self) -> RpcTransaction {
let invoke_args = invoke_tx_args!(
nonce: self.nonce_manager.next(self.sender_address),
sender_address: self.sender_address,
resource_bounds: test_valid_resource_bounds(),
calldata: create_trivial_calldata(self.test_contract_address),
);
rpc_invoke_tx(invoke_args)
}
}

impl BenchTestSetup {
pub fn new(config: BenchTestSetupConfig) -> Self {
let cairo_version = CairoVersion::Cairo0;
let mut tx_generator = TransactionGenerator::new(cairo_version);

let mut txs: Vec<RpcTransaction> = Vec::with_capacity(config.n_txs);
for _ in 0..config.n_txs {
txs.push(tx_generator.
// TODO(Arni): Do something smarter than generate raw invoke.
generate_invoke());
}

let state_reader_factory = local_test_state_reader_factory(cairo_version, false);
let gateway_compiler =
GatewayCompiler::new_command_line_compiler(config.compiler_config.clone());
let mock_mempool_client = MockMempoolClient::new();

let mut mock_dependencies = MockDependencies {
config: config.gateway_config,
compiler: gateway_compiler,
state_reader_factory,
mock_mempool_client,
};
mock_dependencies.expect_add_tx_always();

Self { gateway: mock_dependencies.gateway(), txs }
}

pub async fn send_txs_to_gateway(&self) {
for tx in &self.txs {
let _tx_hash = self
.gateway
.add_tx(tx.clone(), None)
.await
.expect("Some txs has failed in the gateway.");
}
}
}

0 comments on commit e4fac15

Please sign in to comment.