Skip to content

Commit

Permalink
Merge pull request #183 from 0xqd/feat_add_uniswapv2_simulate_benchmark
Browse files Browse the repository at this point in the history
Feat add uniswapv2_simulate benchmark
  • Loading branch information
0xKitsune authored Jul 22, 2024
2 parents 2a7d008 + 9901505 commit 3ec419b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ state-space = ["arraydeque"]
artemis = ["artemis-core"]

[dev-dependencies]
rand = "0.8.5"
tracing-subscriber = "0.3"
criterion = "0.5"
tokio = { version = "1.38", default-features = false, features = [ "rt-multi-thread" ] }
Expand All @@ -56,6 +57,10 @@ alloy = { version = "0.2", features = [
name = "state_space"
harness = false

[[bench]]
name = "uniswapv2_simulate"
harness = false

[profile.release]
opt-level = 3
lto = true
Expand Down
37 changes: 37 additions & 0 deletions benches/uniswapv2_simulate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use alloy::primitives::{address, U256};
use criterion::{Criterion, criterion_group, criterion_main};

use amms::amm::AutomatedMarketMaker;
use amms::amm::uniswap_v2::UniswapV2Pool;

/// Generate a random ether amount between `from` and `to`
fn random_ether(from: f32, to: f32) -> u128 {
let random = rand::random::<f32>() * (to - from) + from;
random as u128 * 10u128.pow(18)
}

fn criterion_benchmark(c: &mut Criterion) {
let token_a = address!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2");

let mut pool = UniswapV2Pool {
address: address!("ddF8390cEd9fAD414b1ca1DD4Fe14F881C2Cfa70"),
token_a,
token_a_decimals: 18,
token_b: address!("fc0d6cf33e38bce7ca7d89c0e292274031b7157a"),
token_b_decimals: 18,
reserve_0: 0_u128,
reserve_1: 0_u128,
fee: 300,
};
c.bench_function("uniswapv2_simuluate", |b| {
b.iter(|| {
pool.reserve_0 = random_ether(1.0, 1000.0);
pool.reserve_1 = random_ether(1.0, 1000.0);
let swap_amount = U256::from(random_ether(1.0, 10.0));
let _ = pool.simulate_swap(token_a, swap_amount).unwrap();
})
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

0 comments on commit 3ec419b

Please sign in to comment.