-
Notifications
You must be signed in to change notification settings - Fork 8
created benches with criterion and moved one performance test to it #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v0.41-dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| use std::time::Duration; | ||
|
|
||
| use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; | ||
| use dash_spv::{ | ||
| storage::{DiskStorageManager, StorageManager}, | ||
| Hash, | ||
| }; | ||
| use dashcore::{block::Version, BlockHash, CompactTarget, Header}; | ||
| use tempfile::TempDir; | ||
| use tokio::runtime::Builder; | ||
|
|
||
| fn create_test_header(height: u32) -> Header { | ||
| Header { | ||
| version: Version::from_consensus(1), | ||
| prev_blockhash: BlockHash::all_zeros(), | ||
| merkle_root: dashcore_hashes::sha256d::Hash::all_zeros().into(), | ||
| time: height, | ||
| bits: CompactTarget::from_consensus(0x207fffff), | ||
| nonce: height, | ||
| } | ||
| } | ||
|
Comment on lines
+12
to
+21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Extract duplicated test helper to a shared module. The Consider creating a // In dash-spv/tests/common/mod.rs
use dashcore::{block::Version, BlockHash, CompactTarget, Header};
pub fn create_test_header(height: u32) -> Header {
Header {
version: Version::from_consensus(1),
prev_blockhash: BlockHash::all_zeros(),
merkle_root: dashcore_hashes::sha256d::Hash::all_zeros().into(),
time: height,
bits: CompactTarget::from_consensus(0x207fffff),
nonce: height,
}
}Then import it in both files: // In benches and tests
mod common;
use common::create_test_header;🤖 Prompt for AI Agents |
||
|
|
||
| fn bench_disk_storage(c: &mut Criterion) { | ||
| const CHUNK_SIZE: u32 = 13_000; | ||
| const NUM_ELEMENTS: u32 = CHUNK_SIZE * 20; | ||
|
|
||
| let rt = Builder::new_multi_thread().worker_threads(4).enable_all().build().unwrap(); | ||
|
|
||
| let headers = (0..NUM_ELEMENTS).map(create_test_header).collect::<Vec<Header>>(); | ||
|
|
||
| c.bench_function("storage/disk/store", |b| { | ||
| b.to_async(&rt).iter_batched( | ||
| || async { | ||
| DiskStorageManager::new(TempDir::new().unwrap().path().to_path_buf()).await.unwrap() | ||
| }, | ||
| |a| async { | ||
| let mut storage = a.await; | ||
|
|
||
| for chunk in headers.chunks(CHUNK_SIZE as usize) { | ||
| storage.store_headers(chunk).await.unwrap(); | ||
| } | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ) | ||
| }); | ||
|
Comment on lines
+31
to
+45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Benchmark measures initialization overhead along with storage operations. The If the intent is to benchmark only storage operations, consider setting up the storage manager once outside the benchmark loop, or document that this benchmark intentionally measures initialization + storage. 🤖 Prompt for AI Agents |
||
|
|
||
| let temp_dir = TempDir::new().unwrap(); | ||
|
|
||
| let mut storage = rt.block_on(async { | ||
| let mut storage = DiskStorageManager::new(temp_dir.path().to_path_buf()).await.unwrap(); | ||
|
|
||
| for chunk in headers.chunks(CHUNK_SIZE as usize) { | ||
| storage.store_headers(chunk).await.unwrap(); | ||
| } | ||
|
|
||
| storage | ||
| }); | ||
|
|
||
| c.bench_function("storage/disk/get", |b| { | ||
| b.to_async(&rt).iter_batched( | ||
| || rand::random::<u32>() % NUM_ELEMENTS, | ||
| async |height| { | ||
| let _ = storage.get_header(height).await.unwrap(); | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ) | ||
| }); | ||
|
|
||
| c.bench_function("storage/disk/reverse_index", |b| { | ||
| b.to_async(&rt).iter_batched( | ||
| || { | ||
| let height = rand::random::<u32>() % NUM_ELEMENTS; | ||
| headers[height as usize].block_hash() | ||
| }, | ||
| async |hash| { | ||
| let _ = storage.get_header_height_by_hash(&hash).await.unwrap(); | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ) | ||
| }); | ||
|
Comment on lines
+59
to
+80
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use deterministic inputs for reproducible benchmarks. The benchmarks use As per coding guidelines, consider using a seeded RNG or a deterministic sequence: +use rand::{rngs::StdRng, Rng, SeedableRng};
+
fn bench_disk_storage(c: &mut Criterion) {
const CHUNK_SIZE: u32 = 13_000;
const NUM_ELEMENTS: u32 = CHUNK_SIZE * 20;
+ const SEED: u64 = 42;
let rt = Builder::new_multi_thread().worker_threads(4).enable_all().build().unwrap();
let headers = (0..NUM_ELEMENTS).map(create_test_header).collect::<Vec<Header>>();
+ let mut rng = StdRng::seed_from_u64(SEED);
// ... storage setup ...
c.bench_function("storage/disk/get", |b| {
b.to_async(&rt).iter_batched(
- || rand::random::<u32>() % NUM_ELEMENTS,
+ || rng.gen::<u32>() % NUM_ELEMENTS,
async |height| {
let _ = storage.get_header(height).await.unwrap();
},
BatchSize::SmallInput,
)
});
|
||
|
|
||
| rt.block_on(async { | ||
| storage.shutdown().await; | ||
| }); | ||
| } | ||
|
|
||
| criterion_group!( | ||
| name = disk_storage; | ||
| config = Criterion::default() | ||
| .sample_size(10) | ||
| .warm_up_time(Duration::from_secs(1)); | ||
| targets = bench_disk_storage); | ||
| criterion_main!(disk_storage); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix typo in the documentation.
The text reads "dash-spc/src considerations" but should likely be "dash-spv/src considerations" to match the project naming convention.
Apply this diff:
📝 Committable suggestion
🤖 Prompt for AI Agents