Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple benchmark for the transact function #1969

Merged
merged 10 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ wasm-tonic-build = { version = "0.9.2", default-features = false, features = [ "
alloy-primitives = { version = "0.7.2", default-features = false }
alloy-sol-types = { version = "0.7.2", default-features = false }

criterion = "0.5.1"

[patch.crates-io]
cairo-felt = { git = "https://github.com/dojoengine/cairo-rs.git", rev = "1031381" }
cairo-vm = { git = "https://github.com/dojoengine/cairo-rs.git", rev = "1031381" }
Expand Down
10 changes: 9 additions & 1 deletion crates/katana/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,24 @@ cairo-vm = { workspace = true, optional = true }
[dev-dependencies]
anyhow.workspace = true
cairo-vm.workspace = true
katana-provider.workspace = true
katana-provider = { workspace = true, features = [ "test-utils" ] }
katana-rpc-types.workspace = true
rstest.workspace = true
rstest_reuse.workspace = true
serde_json.workspace = true
similar-asserts.workspace = true
tokio.workspace = true

criterion.workspace = true
pprof = { version = "0.13.0", features = [ "criterion", "flamegraph" ] }

[features]
blockifier = [ "dep:blockifier", "dep:cairo-vm" ]
default = [ "blockifier" ]
# native = [ "sir", "sir/cairo-native" ]
# sir = [ "dep:sir", "dep:starknet-types-core" ]

[[bench]]
harness = false
name = "execution"
required-features = [ "blockifier" ]
67 changes: 67 additions & 0 deletions crates/katana/executor/benches/execution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::time::Duration;

use blockifier::state::cached_state::{CachedState, GlobalContractCache};
use criterion::measurement::WallTime;
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkGroup, Criterion};
use katana_executor::{SimulationFlag, StateProviderDb};
use katana_primitives::env::{BlockEnv, CfgEnv};
use katana_primitives::transaction::ExecutableTxWithHash;
use katana_provider::test_utils;
use katana_provider::traits::state::StateFactoryProvider;
use pprof::criterion::{Output, PProfProfiler};

use crate::utils::{envs, tx};

mod utils;

fn executor_transact(c: &mut Criterion) {
let mut group = c.benchmark_group("Invoke.ERC20.transfer");
group.warm_up_time(Duration::from_millis(200));

let provider = test_utils::test_in_memory_provider();
let flags = SimulationFlag::new();

let tx = tx();
let envs = envs();

blockifier(&mut group, &provider, &flags, &envs, tx);
}

fn blockifier(
group: &mut BenchmarkGroup<'_, WallTime>,
provider: impl StateFactoryProvider,
execution_flags: &SimulationFlag,
block_envs: &(BlockEnv, CfgEnv),
tx: ExecutableTxWithHash,
) {
use katana_executor::implementation::blockifier::utils::{block_context_from_envs, transact};

// convert to blockifier block context
let block_context = block_context_from_envs(&block_envs.0, &block_envs.1);

group.bench_function("Blockifier", |b| {
// we need to set up the cached state for each iteration as it's not cloneable
b.iter_batched(
|| {
// setup state
let state = provider.latest().expect("failed to get latest state");

// setup blockifier cached state
let contract_cache = GlobalContractCache::new(100);
let state = CachedState::new(StateProviderDb::from(state), contract_cache);

(state, &block_context, execution_flags, tx.clone())
},
|(mut state, block_context, flags, tx)| transact(&mut state, block_context, flags, tx),
BatchSize::SmallInput,
)
});
}

criterion_group! {
name = benches;
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
targets = executor_transact
}

criterion_main!(benches);
67 changes: 67 additions & 0 deletions crates/katana/executor/benches/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::collections::HashMap;

use cairo_vm::vm::runners::builtin_runner::{
BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME, KECCAK_BUILTIN_NAME,
OUTPUT_BUILTIN_NAME, POSEIDON_BUILTIN_NAME, RANGE_CHECK_BUILTIN_NAME,
SEGMENT_ARENA_BUILTIN_NAME, SIGNATURE_BUILTIN_NAME,
};
use katana_primitives::block::GasPrices;
use katana_primitives::env::{BlockEnv, CfgEnv, FeeTokenAddressses};
use katana_primitives::genesis::constant::DEFAULT_FEE_TOKEN_ADDRESS;
use katana_primitives::transaction::{ExecutableTxWithHash, InvokeTx, InvokeTxV1};
use katana_primitives::FieldElement;
use starknet::macros::{felt, selector};

pub fn tx() -> ExecutableTxWithHash {
let invoke = InvokeTx::V1(InvokeTxV1 {
sender_address: felt!("0x1").into(),
calldata: vec![
DEFAULT_FEE_TOKEN_ADDRESS.into(),
selector!("transfer"),
FieldElement::THREE,
felt!("0x100"),
FieldElement::ONE,
FieldElement::ZERO,
],
max_fee: 10_000,
..Default::default()
});

ExecutableTxWithHash::new(invoke.into())
}

pub fn envs() -> (BlockEnv, CfgEnv) {
let block = BlockEnv {
l1_gas_prices: GasPrices { eth: 1, strk: 1 },
sequencer_address: felt!("0x1337").into(),
..Default::default()
};
let cfg = CfgEnv {
max_recursion_depth: 100,
validate_max_n_steps: 4_000_000,
invoke_tx_max_n_steps: 4_000_000,
vm_resource_fee_cost: vm_resource_fee_cost(),
fee_token_addresses: FeeTokenAddressses {
eth: DEFAULT_FEE_TOKEN_ADDRESS,
strk: DEFAULT_FEE_TOKEN_ADDRESS,
},
..Default::default()
};

(block, cfg)
}

fn vm_resource_fee_cost() -> HashMap<String, f64> {
HashMap::from([
(String::from("n_steps"), 1_f64),
(HASH_BUILTIN_NAME.to_string(), 1_f64),
(RANGE_CHECK_BUILTIN_NAME.to_string(), 1_f64),
(SIGNATURE_BUILTIN_NAME.to_string(), 1_f64),
(BITWISE_BUILTIN_NAME.to_string(), 1_f64),
(POSEIDON_BUILTIN_NAME.to_string(), 1_f64),
(OUTPUT_BUILTIN_NAME.to_string(), 1_f64),
(EC_OP_BUILTIN_NAME.to_string(), 1_f64),
(KECCAK_BUILTIN_NAME.to_string(), 1_f64),
(SEGMENT_ARENA_BUILTIN_NAME.to_string(), 1_f64),
])
}
Loading
Loading