|
| 1 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 2 | +use sqlite::ConnectionThreadSafe; |
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use mithril_aggregator::{ |
| 6 | + database::repository::CardanoTransactionRepository, services::TransactionStore, |
| 7 | +}; |
| 8 | +use mithril_common::{entities::CardanoTransaction, test_utils::TempDir}; |
| 9 | +use mithril_persistence::sqlite::ConnectionBuilder; |
| 10 | + |
| 11 | +fn cardano_tx_db_connection() -> ConnectionThreadSafe { |
| 12 | + let db_path = |
| 13 | + TempDir::create("aggregator_benches", "bench_store_transactions").join("cardano_tx.db"); |
| 14 | + |
| 15 | + if db_path.exists() { |
| 16 | + std::fs::remove_file(db_path.clone()).unwrap(); |
| 17 | + } |
| 18 | + |
| 19 | + ConnectionBuilder::open_file(&db_path) |
| 20 | + .with_migrations( |
| 21 | + mithril_aggregator::database::cardano_transaction_migration::get_migrations(), |
| 22 | + ) |
| 23 | + .build() |
| 24 | + .unwrap() |
| 25 | +} |
| 26 | + |
| 27 | +fn generate_transactions(nb_transactions: usize) -> Vec<CardanoTransaction> { |
| 28 | + (0..nb_transactions) |
| 29 | + .map(|i| { |
| 30 | + CardanoTransaction::new( |
| 31 | + format!("tx_hash-{}", i), |
| 32 | + i as u64, |
| 33 | + i as u64 + 1, |
| 34 | + format!("block_hash-{}", i), |
| 35 | + i as u64 + 2, |
| 36 | + ) |
| 37 | + }) |
| 38 | + .collect() |
| 39 | +} |
| 40 | + |
| 41 | +fn bench_store_transactions(c: &mut Criterion) { |
| 42 | + const NB_CARDANO_TRANSACTIONS: usize = 100000; |
| 43 | + let runtime = tokio::runtime::Runtime::new().unwrap(); |
| 44 | + |
| 45 | + let mut group = c.benchmark_group("Store transactions"); |
| 46 | + group.bench_function("store_transactions", |bencher| { |
| 47 | + bencher.to_async(&runtime).iter(|| async { |
| 48 | + let connection = Arc::new(cardano_tx_db_connection()); |
| 49 | + let repository = CardanoTransactionRepository::new(connection); |
| 50 | + repository |
| 51 | + .store_transactions(generate_transactions(NB_CARDANO_TRANSACTIONS)) |
| 52 | + .await |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + group.finish(); |
| 57 | +} |
| 58 | + |
| 59 | +criterion_group!(benches, bench_store_transactions); |
| 60 | +criterion_main!(benches); |
0 commit comments