fix: benches (#184)

This commit is contained in:
Noah Citron 2023-01-31 18:23:55 -05:00 committed by GitHub
parent 5b9e90436a
commit 6b662f903b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 13 deletions

View File

@ -1,4 +1,5 @@
use client::database::Database; use client::database::Database;
use config::Config;
use criterion::{criterion_group, criterion_main, Criterion}; use criterion::{criterion_group, criterion_main, Criterion};
use helios::prelude::FileDB; use helios::prelude::FileDB;
use tempfile::tempdir; use tempfile::tempdir;
@ -17,8 +18,12 @@ pub fn save_checkpoint(c: &mut Criterion) {
c.bench_function("save_checkpoint", |b| { c.bench_function("save_checkpoint", |b| {
let checkpoint = vec![1, 2, 3]; let checkpoint = vec![1, 2, 3];
b.iter(|| { b.iter(|| {
let temp_dir = tempdir().unwrap().into_path(); let data_dir = Some(tempdir().unwrap().into_path());
let db = FileDB::new(temp_dir); let config = Config {
data_dir,
..Default::default()
};
let db = FileDB::new(&config).unwrap();
db.save_checkpoint(checkpoint.clone()).unwrap(); db.save_checkpoint(checkpoint.clone()).unwrap();
}) })
}); });
@ -28,14 +33,17 @@ pub fn save_checkpoint(c: &mut Criterion) {
pub fn load_checkpoint(c: &mut Criterion) { pub fn load_checkpoint(c: &mut Criterion) {
c.bench_function("load_checkpoint", |b| { c.bench_function("load_checkpoint", |b| {
// First write to the db // First write to the db
let temp_dir = tempdir().unwrap().into_path(); let data_dir = Some(tempdir().unwrap().into_path());
let db = FileDB::new(temp_dir.clone()); let config = Config {
let written_checkpoint = vec![1, 2, 3]; data_dir,
..Default::default()
};
let db = FileDB::new(&config).unwrap();
let written_checkpoint = vec![1; 32];
db.save_checkpoint(written_checkpoint.clone()).unwrap(); db.save_checkpoint(written_checkpoint.clone()).unwrap();
// Then read from the db // Then read from the db
b.iter(|| { b.iter(|| {
let db = FileDB::new(temp_dir.clone());
let checkpoint = db.load_checkpoint().unwrap(); let checkpoint = db.load_checkpoint().unwrap();
assert_eq!(checkpoint, written_checkpoint.clone()); assert_eq!(checkpoint, written_checkpoint.clone());
}) })

View File

@ -2,12 +2,12 @@
use std::{str::FromStr, sync::Arc}; use std::{str::FromStr, sync::Arc};
use ::client::Client; use ::client::{database::ConfigDB, Client};
use ethers::{ use ethers::{
abi::Address, abi::Address,
types::{H256, U256}, types::{H256, U256},
}; };
use helios::{client, config::networks, prelude::FileDB, types::BlockTag}; use helios::{client, config::networks, types::BlockTag};
/// Fetches the latest mainnet checkpoint from the fallback service. /// Fetches the latest mainnet checkpoint from the fallback service.
/// ///
@ -29,11 +29,11 @@ pub async fn fetch_mainnet_checkpoint() -> eyre::Result<H256> {
/// The client will use `https://www.lightclientdata.org` as the consensus RPC. /// The client will use `https://www.lightclientdata.org` as the consensus RPC.
pub fn construct_mainnet_client( pub fn construct_mainnet_client(
rt: &tokio::runtime::Runtime, rt: &tokio::runtime::Runtime,
) -> eyre::Result<client::Client<client::FileDB>> { ) -> eyre::Result<client::Client<ConfigDB>> {
rt.block_on(inner_construct_mainnet_client()) rt.block_on(inner_construct_mainnet_client())
} }
pub async fn inner_construct_mainnet_client() -> eyre::Result<client::Client<client::FileDB>> { pub async fn inner_construct_mainnet_client() -> eyre::Result<client::Client<ConfigDB>> {
let benchmark_rpc_url = std::env::var("MAINNET_RPC_URL")?; let benchmark_rpc_url = std::env::var("MAINNET_RPC_URL")?;
let mut client = client::ClientBuilder::new() let mut client = client::ClientBuilder::new()
.network(networks::Network::MAINNET) .network(networks::Network::MAINNET)
@ -47,7 +47,7 @@ pub async fn inner_construct_mainnet_client() -> eyre::Result<client::Client<cli
pub async fn construct_mainnet_client_with_checkpoint( pub async fn construct_mainnet_client_with_checkpoint(
checkpoint: &str, checkpoint: &str,
) -> eyre::Result<client::Client<client::FileDB>> { ) -> eyre::Result<client::Client<ConfigDB>> {
let benchmark_rpc_url = std::env::var("MAINNET_RPC_URL")?; let benchmark_rpc_url = std::env::var("MAINNET_RPC_URL")?;
let mut client = client::ClientBuilder::new() let mut client = client::ClientBuilder::new()
.network(networks::Network::MAINNET) .network(networks::Network::MAINNET)
@ -79,7 +79,7 @@ pub fn construct_runtime() -> tokio::runtime::Runtime {
/// The client will use `http://testing.prater.beacon-api.nimbus.team` as the consensus RPC. /// The client will use `http://testing.prater.beacon-api.nimbus.team` as the consensus RPC.
pub fn construct_goerli_client( pub fn construct_goerli_client(
rt: &tokio::runtime::Runtime, rt: &tokio::runtime::Runtime,
) -> eyre::Result<client::Client<client::FileDB>> { ) -> eyre::Result<client::Client<ConfigDB>> {
rt.block_on(async { rt.block_on(async {
let benchmark_rpc_url = std::env::var("GOERLI_RPC_URL")?; let benchmark_rpc_url = std::env::var("GOERLI_RPC_URL")?;
let mut client = client::ClientBuilder::new() let mut client = client::ClientBuilder::new()
@ -96,7 +96,7 @@ pub fn construct_goerli_client(
/// Gets the balance of the given address on mainnet. /// Gets the balance of the given address on mainnet.
pub fn get_balance( pub fn get_balance(
rt: &tokio::runtime::Runtime, rt: &tokio::runtime::Runtime,
client: Arc<Client<FileDB>>, client: Arc<Client<ConfigDB>>,
address: &str, address: &str,
) -> eyre::Result<U256> { ) -> eyre::Result<U256> {
rt.block_on(async { rt.block_on(async {