Skip to content

Commit e404f98

Browse files
committed
Enhance the test checking cache speed
By using larger files (64kb instead of less than 1kb), making results with cache far faster.
1 parent 6d2ade1 commit e404f98

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

mithril-common/src/digesters/cardano_immutable_digester.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,15 @@ mod tests {
410410
#[tokio::test]
411411
async fn hash_computation_is_quicker_with_a_full_cache() {
412412
let immutable_db = db_builder("hash_computation_is_quicker_with_a_full_cache")
413-
.with_immutables(&(1..=300).collect::<Vec<ImmutableFileNumber>>())
413+
.with_immutables(&(1..=50).collect::<Vec<ImmutableFileNumber>>())
414414
.append_immutable_trio()
415+
.set_file_size(65536)
415416
.build();
416417
let cache = MemoryImmutableFileDigestCacheProvider::default();
417418
let logger = create_logger();
418419
let digester =
419420
CardanoImmutableDigester::new(immutable_db.dir, Some(Arc::new(cache)), logger.clone());
420-
let beacon = Beacon::new("devnet".to_string(), 1, 300);
421+
let beacon = Beacon::new("devnet".to_string(), 1, 50);
421422

422423
let now = Instant::now();
423424
digester
@@ -433,9 +434,13 @@ mod tests {
433434
.expect("compute_digest must not fail");
434435
let elapsed_with_cache = now.elapsed();
435436

437+
// Note real performance doesn't matter here, the purpose is only to check that the computation
438+
// time is faster with cache.
439+
// We set the limit to 90% to avoid flakiness and ensure that the cache is useful (Note: Real
440+
// performance is around ~100 times faster in debug).
436441
assert!(
437442
elapsed_with_cache < (elapsed_without_cache * 9 / 10),
438-
"digest computation with full cache should be at least 10% faster than without cache,\
443+
"digest computation with full cache should be faster than without cache,\
439444
time elapsed: with cache {:?}, without cache {:?}",
440445
elapsed_with_cache,
441446
elapsed_without_cache

mithril-common/src/digesters/dummy_immutable_db_builder.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct DummyImmutablesDbBuilder {
1111
immutables_to_write: Vec<ImmutableFileNumber>,
1212
non_immutables_to_write: Vec<String>,
1313
append_uncompleted_trio: bool,
14+
file_size: Option<u64>,
1415
}
1516

1617
pub struct DummyImmutableDb {
@@ -28,6 +29,7 @@ impl DummyImmutablesDbBuilder {
2829
immutables_to_write: vec![],
2930
non_immutables_to_write: vec![],
3031
append_uncompleted_trio: false,
32+
file_size: None,
3133
}
3234
}
3335

@@ -41,14 +43,22 @@ impl DummyImmutablesDbBuilder {
4143
self
4244
}
4345

44-
/// Makes [Self::build] add another trio of immutables file, that won't be included
46+
/// Makes [build][Self::build] add another trio of immutables file, that won't be included
4547
/// in its returned vec, to simulate the last 3 'uncompleted / wip' files that can be found in
4648
/// a cardano immutable db.
4749
pub fn append_immutable_trio(&mut self) -> &mut Self {
4850
self.append_uncompleted_trio = true;
4951
self
5052
}
5153

54+
/// Set the size of all files written by [build][Self::build] to the given `file_size` in bytes.
55+
///
56+
/// Note: by default the size of the produced files is less than a 1kb.
57+
pub fn set_file_size(&mut self, file_size: u64) -> &mut Self {
58+
self.file_size = Some(file_size);
59+
self
60+
}
61+
5262
pub fn build(&self) -> DummyImmutableDb {
5363
let mut non_immutables_files = vec![];
5464
let mut immutable_numbers = self.immutables_to_write.clone();
@@ -113,7 +123,14 @@ impl DummyImmutablesDbBuilder {
113123
fn write_dummy_file(&self, filename: &str) -> PathBuf {
114124
let file = self.dir.join(Path::new(filename));
115125
let mut source_file = File::create(&file).unwrap();
126+
116127
write!(source_file, "This is a test file named '{}'", filename).unwrap();
128+
129+
if let Some(file_size) = self.file_size {
130+
writeln!(source_file).unwrap();
131+
source_file.set_len(file_size).unwrap();
132+
}
133+
117134
file
118135
}
119136
}

0 commit comments

Comments
 (0)