forked from aserebryakov/trie-rs
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add benchmark with criterion, add benchmark job to test workflow
- Loading branch information
Showing
4 changed files
with
273 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
|
||
use std::collections::HashMap; | ||
|
||
fn generate_keys() -> Vec<String> { | ||
let mut keys = Vec::new(); | ||
for i in 1..9 { | ||
for j in 1..9 { | ||
for k in 1..9 { | ||
for l in 1..9 { | ||
keys.push(format!("{}{}{}{}", i, j, k, l)); | ||
} | ||
} | ||
} | ||
} | ||
keys | ||
} | ||
|
||
fn trie_benchmark(c: &mut Criterion) { | ||
c.bench_function("trie_match", |b| { | ||
let mut t = ptrie::Trie::new(); | ||
t.insert(black_box("test".bytes()), black_box(String::from("test"))); | ||
b.iter(|| { | ||
assert!(t.contains_key(black_box("test".bytes()))); | ||
}); | ||
}); | ||
|
||
c.bench_function("trie_mismatch", |b| { | ||
let mut t = ptrie::Trie::new(); | ||
t.insert("test".bytes(), String::from("test")); | ||
b.iter(|| { | ||
assert!(!t.contains_key("tst".bytes())); | ||
}); | ||
}); | ||
|
||
c.bench_function("trie_massive_match", |b| { | ||
let mut t = ptrie::Trie::new(); | ||
let keys = generate_keys(); | ||
for key in &keys { | ||
t.insert(key.bytes(), key.clone()); | ||
} | ||
b.iter(|| { | ||
for key in &keys { | ||
assert!(t.contains_key(key.bytes())); | ||
} | ||
}); | ||
}); | ||
|
||
c.bench_function("trie_massive_mismatch_on_0", |b| { | ||
let mut t = ptrie::Trie::new(); | ||
let mismatching = String::from("0999"); | ||
let keys = generate_keys(); | ||
|
||
for key in &keys { | ||
t.insert(key.bytes(), key.clone()); | ||
} | ||
|
||
b.iter(|| { | ||
for _ in 0..keys.len() { | ||
assert!(!t.contains_key(mismatching.bytes())); | ||
} | ||
}); | ||
}); | ||
|
||
c.bench_function("trie_massive_mismatch_on_1", |b| { | ||
let mut t = ptrie::Trie::new(); | ||
let mismatching = String::from("9099"); | ||
let keys = generate_keys(); | ||
|
||
for key in &keys { | ||
t.insert(key.bytes(), key.clone()); | ||
} | ||
|
||
b.iter(|| { | ||
for _ in 0..keys.len() { | ||
assert!(!t.contains_key(mismatching.bytes())); | ||
} | ||
}); | ||
}); | ||
|
||
c.bench_function("trie_massive_mismatch_on_2", |b| { | ||
let mut t = ptrie::Trie::new(); | ||
let mismatching = String::from("9909"); | ||
let keys = generate_keys(); | ||
|
||
for key in &keys { | ||
t.insert(key.bytes(), key.clone()); | ||
} | ||
|
||
b.iter(|| { | ||
for _ in 0..keys.len() { | ||
assert!(!t.contains_key(mismatching.bytes())); | ||
} | ||
}); | ||
}); | ||
|
||
c.bench_function("trie_massive_mismatch_on_3", |b| { | ||
let mut t = ptrie::Trie::new(); | ||
let mismatching = String::from("9990"); | ||
let keys = generate_keys(); | ||
for key in &keys { | ||
t.insert(key.bytes(), key.clone()); | ||
} | ||
b.iter(|| { | ||
for _ in 0..keys.len() { | ||
assert!(!t.contains_key(mismatching.bytes())); | ||
} | ||
}); | ||
}); | ||
|
||
c.bench_function("trie_prefixes_match", |b| { | ||
let mut t = ptrie::Trie::new(); | ||
let keys = generate_keys(); | ||
for key in &keys { | ||
t.insert(key.bytes(), key.clone()); | ||
} | ||
b.iter(|| { | ||
for key in &keys { | ||
assert!(!t.find_prefixes(key.bytes()).is_empty()); | ||
} | ||
}); | ||
}); | ||
|
||
c.bench_function("trie_postfixes_match", |b| { | ||
let mut t = ptrie::Trie::new(); | ||
let keys = generate_keys(); | ||
for key in &keys { | ||
t.insert(key.bytes(), key.clone()); | ||
} | ||
b.iter(|| { | ||
for key in &keys { | ||
assert!(!t.find_postfixes(key.bytes()).is_empty()); | ||
} | ||
}); | ||
}); | ||
|
||
c.bench_function("trie_prefix_longest_match", |b| { | ||
let mut t = ptrie::Trie::new(); | ||
let keys = generate_keys(); | ||
for key in &keys { | ||
t.insert(key.bytes(), key.clone()); | ||
} | ||
b.iter(|| { | ||
for key in &keys { | ||
assert!(t.find_longest_prefix(key.bytes()).is_some()); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
fn hashmap_benchmark(c: &mut Criterion) { | ||
c.bench_function("hashmap_match", |b| { | ||
let mut h = HashMap::new(); | ||
let key = String::from("test"); | ||
|
||
h.insert(key.clone(), true); | ||
|
||
b.iter(|| { | ||
h.get(&key); | ||
}); | ||
}); | ||
|
||
c.bench_function("hashmap_mismatch", |b| { | ||
let mut h = HashMap::new(); | ||
let key = String::from("test"); | ||
let notkey = String::from("tst"); | ||
|
||
h.insert(key, true); | ||
|
||
b.iter(|| { | ||
h.get(¬key); | ||
}); | ||
}); | ||
|
||
c.bench_function("hashmap_massive_match", |b| { | ||
let mut h = HashMap::new(); | ||
let keys = generate_keys(); | ||
|
||
for key in &keys { | ||
h.insert(key.clone(), key.clone()); | ||
} | ||
|
||
b.iter(|| { | ||
for key in &keys { | ||
assert!(h.contains_key(key)); | ||
} | ||
}); | ||
}); | ||
|
||
c.bench_function("hashmap_massive_mismatch_on_0", |b| { | ||
let mut h = HashMap::new(); | ||
let mismatching = String::from("0999"); | ||
let keys = generate_keys(); | ||
|
||
for key in &keys { | ||
h.insert(key.clone(), key.clone()); | ||
} | ||
|
||
b.iter(|| { | ||
for _ in 0..keys.len() { | ||
assert!(!h.contains_key(&mismatching)); | ||
} | ||
}); | ||
}); | ||
|
||
c.bench_function("hashmap_massive_mismatch_on_0_one_symbol_key", |b| { | ||
let mut h = HashMap::new(); | ||
let mismatching = String::from("0"); | ||
let keys = generate_keys(); | ||
|
||
for key in &keys { | ||
h.insert(key.clone(), key.clone()); | ||
} | ||
|
||
b.iter(|| { | ||
for _ in 0..keys.len() { | ||
assert!(!h.contains_key(&mismatching)); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
criterion_group!(benches, trie_benchmark, hashmap_benchmark,); | ||
criterion_main!(benches); |
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