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 24, 2024
1 parent 456c83f commit a10fc80
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 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.

2 changes: 1 addition & 1 deletion crates/starknet_gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ validator.workspace = true
[dev-dependencies]
assert_matches.workspace = true
cairo-lang-sierra-to-casm.workspace = true
criterion = { workspace = true, features = ["html_reports"] }
criterion = { workspace = true, features = ["html_reports", "async_tokio"] }
mockall.workspace = true
mockito.workspace = true
num-bigint.workspace = true
Expand Down
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 n_txs = 100;
let tx_generator_config = BenchTestSetupConfig { n_txs, ..Default::default() };

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
4 changes: 2 additions & 2 deletions crates/starknet_gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ 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;
2 changes: 2 additions & 0 deletions crates/starknet_gateway/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use starknet_types_core::felt::Felt;

use crate::compiler_version::VersionId;

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
75 changes: 75 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,75 @@
use std::sync::Arc;

use blockifier::test_utils::contracts::FeatureContract;
use blockifier::test_utils::CairoVersion;
use mempool_test_utils::starknet_api_test_utils::MultiAccountTransactionGenerator;
use starknet_api::rpc_transaction::RpcTransaction;
use starknet_mempool_types::communication::MockMempoolClient;
use starknet_sierra_compile::config::SierraToCasmCompilationConfig;

use crate::compilation::GatewayCompiler;
use crate::config::{GatewayConfig, RpcStateReaderConfig};
use crate::gateway::Gateway;
use crate::rpc_state_reader::RpcStateReaderFactory;

const N_TXS: usize = 1000;

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

impl Default for BenchTestSetupConfig {
fn default() -> Self {
Self {
n_txs: N_TXS,
gateway_config: GatewayConfig::default(),
rpc_state_reader_config: RpcStateReaderConfig::default(),
compiler_config: SierraToCasmCompilationConfig::default(),
}
}
}

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

impl BenchTestSetup {
pub fn new(config: BenchTestSetupConfig) -> Self {
// TODO(Arni): Register accounts see [`register_account_for_flow_test`].
let mut tx_generator = MultiAccountTransactionGenerator::new();
let default_account = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo0);

tx_generator.register_account(default_account);

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

let state_reader_factory =
Arc::new(RpcStateReaderFactory { config: config.rpc_state_reader_config.clone() });
let gateway_compiler =
GatewayCompiler::new_command_line_compiler(config.compiler_config.clone());
let mempool_client = Arc::new(MockMempoolClient::new());
let gateway = Gateway::new(
config.gateway_config.clone(),
state_reader_factory,
gateway_compiler,
mempool_client,
);

Self { 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;
}
}
}

0 comments on commit a10fc80

Please sign in to comment.