Skip to content

Commit

Permalink
fix: make file db benchmark runable (#364)
Browse files Browse the repository at this point in the history
* fix: make file_db benchmark runable

* chore: cargo fmt
  • Loading branch information
hsyodyssey authored Sep 9, 2024
1 parent 16e4b2c commit 7c706a3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 30 deletions.
9 changes: 6 additions & 3 deletions benches/file_db.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloy::primitives::b256;
use config::Config;
use consensus::database::{Database, FileDB};
use criterion::{criterion_group, criterion_main, Criterion};
Expand All @@ -16,7 +17,8 @@ criterion_main!(file_db);
/// Benchmark saving/writing a checkpoint to the file db.
pub fn save_checkpoint(c: &mut Criterion) {
c.bench_function("save_checkpoint", |b| {
let checkpoint: &[u8] = &[1, 2, 3];
let checkpoint: alloy::primitives::FixedBytes<32> =
b256!("c7fc7b2f4b548bfc9305fa80bc1865ddc6eea4557f0a80507af5dc34db7bd9ce");
b.iter(|| {
let data_dir = Some(tempdir().unwrap().into_path());
let config = Config {
Expand All @@ -39,8 +41,9 @@ pub fn load_checkpoint(c: &mut Criterion) {
..Default::default()
};
let db = FileDB::new(&config).unwrap();
let written_checkpoint = vec![1; 32];
db.save_checkpoint(&written_checkpoint.clone()).unwrap();
let written_checkpoint =
b256!("c7fc7b2f4b548bfc9305fa80bc1865ddc6eea4557f0a80507af5dc34db7bd9ce");
db.save_checkpoint(written_checkpoint.clone()).unwrap();

// Then read from the db
b.iter(|| {
Expand Down
6 changes: 3 additions & 3 deletions benches/get_balance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloy::primitives::Address;
use criterion::{criterion_group, criterion_main, Criterion};
use ethers::prelude::*;
use helios::types::BlockTag;
use std::str::FromStr;

Expand Down Expand Up @@ -32,7 +32,7 @@ pub fn bench_mainnet_get_balance(c: &mut Criterion) {
// Execute the benchmark asynchronously.
b.to_async(rt).iter(|| async {
let inner = std::sync::Arc::clone(&client);
inner.get_balance(&addr, block).await.unwrap()
inner.get_balance(addr, block).await.unwrap()
})
});
}
Expand Down Expand Up @@ -64,7 +64,7 @@ pub fn bench_goerli_get_balance(c: &mut Criterion) {
// Execute the benchmark asynchronously.
b.to_async(rt).iter(|| async {
let inner = std::sync::Arc::clone(&client);
inner.get_balance(&addr, block).await.unwrap()
inner.get_balance(addr, block).await.unwrap()
})
});
}
6 changes: 3 additions & 3 deletions benches/get_code.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloy::primitives::Address;
use criterion::{criterion_group, criterion_main, Criterion};
use ethers::prelude::*;
use helios::types::BlockTag;
use std::str::FromStr;

Expand Down Expand Up @@ -32,7 +32,7 @@ pub fn bench_mainnet_get_code(c: &mut Criterion) {
// Execute the benchmark asynchronously.
b.to_async(rt).iter(|| async {
let inner = std::sync::Arc::clone(&client);
inner.get_code(&addr, block).await.unwrap()
inner.get_code(addr, block).await.unwrap()
})
});
}
Expand All @@ -57,7 +57,7 @@ pub fn bench_goerli_get_code(c: &mut Criterion) {
// Execute the benchmark asynchronously.
b.to_async(rt).iter(|| async {
let inner = std::sync::Arc::clone(&client);
inner.get_code(&addr, block).await.unwrap()
inner.get_code(addr, block).await.unwrap()
})
});
}
31 changes: 16 additions & 15 deletions benches/harness.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
#![allow(dead_code)]

use std::str::FromStr;

use ::client::Client;

use ethers::{
abi::Address,
types::{H256, U256},
use alloy::primitives::{Address, B256, U256};
use helios::{
client::{self, FileDB},
config::networks,
types::BlockTag,
};
use helios::{client, config::networks, types::BlockTag};
use std::{path::PathBuf, str::FromStr};

/// Fetches the latest mainnet checkpoint from the fallback service.
///
/// Uses the [CheckpointFallback](config::CheckpointFallback).
/// The `build` method will fetch a list of [CheckpointFallbackService](config::CheckpointFallbackService)s from a community-mainained list by ethPandaOps.
/// This list is NOT guaranteed to be secure, but is provided in good faith.
/// The raw list can be found here: https://github.com/ethpandaops/checkpoint-sync-health-checks/blob/master/_data/endpoints.yaml
pub async fn fetch_mainnet_checkpoint() -> eyre::Result<H256> {
pub async fn fetch_mainnet_checkpoint() -> eyre::Result<B256> {
let cf = config::CheckpointFallback::new().build().await.unwrap();
cf.fetch_latest_checkpoint(&networks::Network::MAINNET)
.await
Expand All @@ -28,23 +26,26 @@ pub async fn fetch_mainnet_checkpoint() -> eyre::Result<H256> {
/// The client is parameterized with a [FileDB](client::FileDB).
/// It will also use the environment variable `MAINNET_EXECUTION_RPC` to connect to a mainnet node.
/// The client will use `https://www.lightclientdata.org` as the consensus RPC.
pub fn construct_mainnet_client(rt: &tokio::runtime::Runtime) -> eyre::Result<Client> {
pub fn construct_mainnet_client(rt: &tokio::runtime::Runtime) -> eyre::Result<Client<FileDB>> {
rt.block_on(inner_construct_mainnet_client())
}

pub async fn inner_construct_mainnet_client() -> eyre::Result<Client> {
pub async fn inner_construct_mainnet_client() -> eyre::Result<Client<FileDB>> {
let benchmark_rpc_url = std::env::var("MAINNET_EXECUTION_RPC")?;
let mut client = client::ClientBuilder::new()
.network(networks::Network::MAINNET)
.consensus_rpc("https://www.lightclientdata.org")
.execution_rpc(&benchmark_rpc_url)
.load_external_fallback()
.data_dir(PathBuf::from("/tmp/helios"))
.build()?;
client.start().await?;
Ok(client)
}

pub async fn construct_mainnet_client_with_checkpoint(checkpoint: &str) -> eyre::Result<Client> {
pub async fn construct_mainnet_client_with_checkpoint(
checkpoint: B256,
) -> eyre::Result<Client<FileDB>> {
let benchmark_rpc_url = std::env::var("MAINNET_EXECUTION_RPC")?;
let mut client = client::ClientBuilder::new()
.network(networks::Network::MAINNET)
Expand Down Expand Up @@ -74,7 +75,7 @@ pub fn construct_runtime() -> tokio::runtime::Runtime {
/// The client is parameterized with a [FileDB](client::FileDB).
/// It will also use the environment variable `GOERLI_EXECUTION_RPC` to connect to a mainnet node.
/// The client will use `http://testing.prater.beacon-api.nimbus.team` as the consensus RPC.
pub fn construct_goerli_client(rt: &tokio::runtime::Runtime) -> eyre::Result<Client> {
pub fn construct_goerli_client(rt: &tokio::runtime::Runtime) -> eyre::Result<Client<FileDB>> {
rt.block_on(async {
let benchmark_rpc_url = std::env::var("GOERLI_EXECUTION_RPC")?;
let mut client = client::ClientBuilder::new()
Expand All @@ -91,13 +92,13 @@ pub fn construct_goerli_client(rt: &tokio::runtime::Runtime) -> eyre::Result<Cli
/// Gets the balance of the given address on mainnet.
pub fn get_balance(
rt: &tokio::runtime::Runtime,
client: Client,
client: Client<FileDB>,
address: &str,
) -> eyre::Result<U256> {
rt.block_on(async {
let block = BlockTag::Latest;
let address = Address::from_str(address)?;
client.get_balance(&address, block).await
client.get_balance(address, block).await
})
}

Expand Down
10 changes: 4 additions & 6 deletions benches/sync.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloy::primitives::Address;
use criterion::{criterion_group, criterion_main, Criterion};
use ethers::types::Address;
use helios::types::BlockTag;

mod harness;
Expand All @@ -19,13 +19,12 @@ criterion_group! {
pub fn bench_full_sync(c: &mut Criterion) {
// Externally, let's fetch the latest checkpoint from our fallback service so as not to benchmark the checkpoint fetch.
let checkpoint = harness::await_future(harness::fetch_mainnet_checkpoint()).unwrap();
let checkpoint = hex::encode(checkpoint);

// On client construction, it will sync to the latest checkpoint using our fetched checkpoint.
c.bench_function("full_sync", |b| {
b.to_async(harness::construct_runtime()).iter(|| async {
let _client = std::sync::Arc::new(
harness::construct_mainnet_client_with_checkpoint(&checkpoint)
harness::construct_mainnet_client_with_checkpoint(checkpoint)
.await
.unwrap(),
);
Expand All @@ -38,21 +37,20 @@ pub fn bench_full_sync(c: &mut Criterion) {
pub fn bench_full_sync_with_call(c: &mut Criterion) {
// Externally, let's fetch the latest checkpoint from our fallback service so as not to benchmark the checkpoint fetch.
let checkpoint = harness::await_future(harness::fetch_mainnet_checkpoint()).unwrap();
let checkpoint = hex::encode(checkpoint);

// On client construction, it will sync to the latest checkpoint using our fetched checkpoint.
c.bench_function("full_sync_call", |b| {
b.to_async(harness::construct_runtime()).iter(|| async {
let client = std::sync::Arc::new(
harness::construct_mainnet_client_with_checkpoint(&checkpoint)
harness::construct_mainnet_client_with_checkpoint(checkpoint)
.await
.unwrap(),
);
let addr = "0x00000000219ab540356cbb839cbe05303d7705fa"
.parse::<Address>()
.unwrap();
let block = BlockTag::Latest;
client.get_balance(&addr, block).await.unwrap()
client.get_balance(addr, block).await.unwrap()
})
});
}
Expand Down

0 comments on commit 7c706a3

Please sign in to comment.