-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from florian1345/more-benches
Add benches for ejecting insert and ejecting/non-ejecting mutate
- Loading branch information
Showing
8 changed files
with
151 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,33 @@ | ||
use criterion::Criterion; | ||
use lru_mem::LruCache; | ||
|
||
use crate::bencher_extensions::CacheBenchmarkGroup; | ||
|
||
pub(crate) fn insert_benchmark(c: &mut Criterion) { | ||
let mut group = crate::make_group(c, "insert"); | ||
#[inline] | ||
fn insert_and_increment(cache: &mut LruCache<String, String>, key_idx: &mut u64) { | ||
cache.insert(format!("{:012x}", key_idx), String::new()).unwrap(); | ||
*key_idx += 1; | ||
} | ||
|
||
pub(crate) fn insert_no_eject_benchmark(c: &mut Criterion) { | ||
let mut group = crate::make_group(c, "insert-no-eject"); | ||
|
||
for &size in crate::CONSTANT_TIME_SIZES { | ||
let min_size = size * 7 / 8; | ||
let mut key_idx: u64 = 0; | ||
|
||
group.bench_with_depleted_cache( | ||
|cache| insert_and_increment(cache, &mut key_idx), min_size, size); | ||
} | ||
} | ||
|
||
pub(crate) fn insert_eject_benchmark(c: &mut Criterion) { | ||
let mut group = crate::make_group(c, "insert-eject"); | ||
|
||
for &size in crate::CONSTANT_TIME_SIZES { | ||
let mut key_idx: u32 = 0; | ||
let mut key_idx: u64 = 0; | ||
|
||
group.bench_with_depleted_cache(|cache| { | ||
cache.insert(format!("{:08x}", key_idx), String::new()).unwrap(); | ||
key_idx += 1; | ||
}, size * 7 / 8, size); | ||
group.bench_with_capped_cache( | ||
|cache, _| insert_and_increment(cache, &mut key_idx), size); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use criterion::Criterion; | ||
use rand::Rng; | ||
|
||
use crate::bencher_extensions::CacheBenchmarkGroup; | ||
|
||
fn mutate_in_place(string: &mut String) { | ||
let old = string.pop().unwrap(); | ||
let new = (b'0' + b'f' - old as u8) as char; | ||
|
||
string.push(new); | ||
} | ||
|
||
fn mutate_expanding(string: &mut String) { | ||
let old_capacity = string.capacity(); | ||
|
||
while string.capacity() <= old_capacity { | ||
string.push('0') | ||
} | ||
} | ||
|
||
pub(crate) fn mutate_no_eject_benchmark(c: &mut Criterion) { | ||
let mut group = crate::make_group(c, "mutate-no-eject"); | ||
let mut rng = rand::thread_rng(); | ||
|
||
for &size in crate::CONSTANT_TIME_SIZES { | ||
group.bench_with_capped_cache(|cache, keys| { | ||
let key_index = rng.gen_range(0..keys.len()); | ||
cache.mutate(&keys[key_index], mutate_in_place).unwrap(); | ||
}, size); | ||
} | ||
} | ||
|
||
pub(crate) fn mutate_eject_benchmark(c: &mut Criterion) { | ||
let mut group = crate::make_group(c, "mutate-eject"); | ||
let mut rng = rand::thread_rng(); | ||
|
||
for &size in crate::CONSTANT_TIME_SIZES { | ||
group.bench_with_refilled_capped_cache(|cache, keys| { | ||
let key_index = rng.gen_range(0..keys.len()); | ||
let key = &keys[key_index]; | ||
let lru_key = cache.peek_lru().unwrap().0.clone(); | ||
cache.mutate(key, mutate_expanding).unwrap(); | ||
[lru_key, key.clone()] | ||
}, size); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters