Skip to content

Commit 95b8fb9

Browse files
committed
Add a benchmark for transactions retrieval
It consist of two rounds with different db size (respectively 1 000 000 and 10 000 000 transactions) to check that the db size doesn't matter. Each round will retrieve 100, 10 000, 100 000, and 1 000 000 transactions.
1 parent 936ffe2 commit 95b8fb9

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

mithril-aggregator/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ repository = { workspace = true }
1313
name = "cardano_transactions_import"
1414
harness = false
1515

16+
[[bench]]
17+
name = "cardano_transactions_get"
18+
harness = false
19+
1620
[dependencies]
1721
anyhow = "1.0.79"
1822
async-trait = "0.1.77"
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
use std::sync::Arc;
2+
3+
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
4+
use sqlite::ConnectionThreadSafe;
5+
6+
use mithril_aggregator::{
7+
database::repository::CardanoTransactionRepository, services::TransactionStore,
8+
};
9+
use mithril_common::{entities::CardanoTransaction, test_utils::TempDir};
10+
use mithril_persistence::sqlite::ConnectionBuilder;
11+
12+
fn cardano_tx_db_connection(db_file_name: &str) -> ConnectionThreadSafe {
13+
let db_path =
14+
TempDir::create("aggregator_benches", "bench_get_transactions").join(db_file_name);
15+
16+
if db_path.exists() {
17+
std::fs::remove_file(db_path.clone()).unwrap();
18+
}
19+
20+
ConnectionBuilder::open_file(&db_path)
21+
.with_migrations(
22+
mithril_aggregator::database::cardano_transaction_migration::get_migrations(),
23+
)
24+
.build()
25+
.unwrap()
26+
}
27+
28+
fn generate_transactions(nb_transactions: usize) -> Vec<CardanoTransaction> {
29+
// Note: we irrealistically generate transactions where each are on a different block.
30+
// This is to trick the repository `get_transactions_in_range` method to read the expected number
31+
// of transactions.
32+
(0..nb_transactions)
33+
.map(|i| {
34+
CardanoTransaction::new(
35+
format!("tx_hash-{}", i),
36+
i as u64,
37+
i as u64 * 100,
38+
format!("block_hash-{}", i),
39+
i as u64 + 1,
40+
)
41+
})
42+
.collect()
43+
}
44+
45+
async fn init_db(nb_transaction_in_db: usize) -> CardanoTransactionRepository {
46+
println!("Generating a db with {nb_transaction_in_db} transactions, one per block ...");
47+
let transactions = generate_transactions(nb_transaction_in_db);
48+
let connection = Arc::new(cardano_tx_db_connection(&format!(
49+
"cardano_tx-{nb_transaction_in_db}.db",
50+
)));
51+
let repository = CardanoTransactionRepository::new(connection);
52+
repository.store_transactions(transactions).await.unwrap();
53+
54+
repository
55+
}
56+
57+
fn run_bench(c: &mut Criterion, nb_transaction_in_db: usize) {
58+
let runtime = tokio::runtime::Runtime::new().unwrap();
59+
let repository = runtime.block_on(async { init_db(nb_transaction_in_db).await });
60+
61+
let mut group = c.benchmark_group(format!(
62+
"Get transactions - {nb_transaction_in_db} tx in db"
63+
));
64+
for max_block_number in [100, 10_000, 100_000, 1_000_000] {
65+
group.bench_with_input(
66+
BenchmarkId::from_parameter(format!(
67+
"get_transactions_in_range(0..{max_block_number})"
68+
)),
69+
&max_block_number,
70+
|b, &max_block_number| {
71+
b.to_async(&runtime).iter(|| async {
72+
let _transactions = repository
73+
.get_transactions_in_range(0..max_block_number)
74+
.await
75+
.unwrap();
76+
});
77+
},
78+
);
79+
}
80+
group.finish();
81+
}
82+
83+
fn bench_get_transactions(c: &mut Criterion) {
84+
// Two rounds of benchmarks: one with 1M transactions in the db, and one with 10M transactions.
85+
// Each time the number of transactions to read is 100, 10_000, 100_000, 1_000_000.
86+
run_bench(c, 1_000_000);
87+
run_bench(c, 10_000_000);
88+
}
89+
90+
criterion_group! {
91+
name = benches;
92+
config = Criterion::default().sample_size(20);
93+
targets = bench_get_transactions
94+
}
95+
criterion_main!(benches);

0 commit comments

Comments
 (0)