Skip to content

Commit

Permalink
Add bench store_transactions for in mithril-aggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
dlachaume committed May 3, 2024
1 parent c9062df commit 4708d6d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions mithril-aggregator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ homepage = { workspace = true }
license = { workspace = true }
repository = { workspace = true }

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

[dependencies]
anyhow = "1.0.79"
async-trait = "0.1.77"
Expand Down Expand Up @@ -50,6 +54,7 @@ zstd = { version = "0.13.0", features = ["zstdmt"] }
tikv-jemallocator = { version = "0.5.4", optional = true }

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports", "async_tokio"] }
httpmock = "0.7.0"
mithril-common = { path = "../mithril-common", features = [
"allow_skip_signer_certification",
Expand Down
60 changes: 60 additions & 0 deletions mithril-aggregator/benches/cardano_transactions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use criterion::{criterion_group, criterion_main, Criterion};
use sqlite::ConnectionThreadSafe;
use std::sync::Arc;

use mithril_aggregator::{
database::repository::CardanoTransactionRepository, services::TransactionStore,
};
use mithril_common::{entities::CardanoTransaction, test_utils::TempDir};
use mithril_persistence::sqlite::ConnectionBuilder;

fn cardano_tx_db_connection() -> ConnectionThreadSafe {
let db_path =
TempDir::create("aggregator_benches", "bench_store_transactions").join("cardano_tx.db");

if db_path.exists() {
std::fs::remove_file(db_path.clone()).unwrap();
}

ConnectionBuilder::open_file(&db_path)
.with_migrations(
mithril_aggregator::database::cardano_transaction_migration::get_migrations(),
)
.build()
.unwrap()
}

fn generate_transactions(nb_transactions: usize) -> Vec<CardanoTransaction> {
(0..nb_transactions)
.map(|i| {
CardanoTransaction::new(
format!("tx_hash-{}", i),
i as u64,
i as u64 + 1,
format!("block_hash-{}", i),
i as u64 + 2,
)
})
.collect()
}

fn bench_store_transactions(c: &mut Criterion) {
const NB_CARDANO_TRANSACTIONS: usize = 100000;
let runtime = tokio::runtime::Runtime::new().unwrap();

let mut group = c.benchmark_group("Store transactions");
group.bench_function("store_transactions", |bencher| {
bencher.to_async(&runtime).iter(|| async {
let connection = Arc::new(cardano_tx_db_connection());
let repository = CardanoTransactionRepository::new(connection);
repository
.store_transactions(generate_transactions(NB_CARDANO_TRANSACTIONS))
.await
});
});

group.finish();
}

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

0 comments on commit 4708d6d

Please sign in to comment.