Skip to content

Commit

Permalink
RwLock -> Mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-pousette committed Jun 25, 2021
1 parent 668db36 commit 8c27107
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 185 deletions.
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ categories = ["text-processing"]
exclude = [".github/**", ".gitignore", ".rustfmt.toml"]

[dev-dependencies]
lazy_static = "1.4.0"
lazy_static = "1.4.0"
rand = "0.8.3"
time = "*"
criterion = {version ="0.3" , features = ["html_reports"]}



[[bench]]
name = "test_benchmark"
harness = false
68 changes: 68 additions & 0 deletions benches/test_benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use criterion::{criterion_group, criterion_main, Criterion};
use probly_search::index::{add_document_to_index, create_index, Index};

criterion_group!(benches, test_speed);
criterion_main!(benches);
struct DocX {
id: usize,
title: String,
}

fn filter(s: &String) -> String {
s.to_owned()
}
fn tokenizer(s: &str) -> Vec<String> {
s.split(' ')
.map(|slice| slice.to_owned())
.collect::<Vec<String>>()
}

pub fn test_speed(c: &mut Criterion) {
use lazy_static::lazy_static;
use rand::seq::SliceRandom;

lazy_static! {
static ref ALLOWED: Vec<char> = "abcdefghilkjapqrstuvwxyz".chars().collect();
}

pub fn generate_string(min: i32, max: i32) -> String {
let mut rng = rand::thread_rng();
let mut s = String::from("");

for _ in min..=max {
s.push(ALLOWED.choose(&mut rng).map(|&c| c as char).unwrap())
}
s
}
fn title_extract_x(d: &DocX) -> Option<&str> {
Some(d.title.as_str())
}

c.bench_function("add_100k_docs", |b| {
let mut idx: Index<usize> = create_index(1);
let mut random_strings: Vec<String> = Vec::new();
for _ in 1..100000 {
let mut new_rand = generate_string(0, 4);
new_rand.push_str(" ");
new_rand.push_str(&generate_string(0, 4));
random_strings.push(new_rand);
}
// whatever you want to do
let extractor = [title_extract_x as fn(&_) -> Option<&str>];
b.iter(|| add_all_documents(&mut idx, &extractor, &&random_strings));
});
}

fn add_all_documents(
mut idx: &mut Index<usize>,
extractor: &[fn(&DocX) -> Option<&str>],
random_strings: &Vec<String>,
) {
for (i, s) in random_strings.iter().enumerate() {
let d = DocX {
id: i,
title: s.to_owned(),
};
add_document_to_index(&mut idx, &extractor, tokenizer, filter, d.id, d);
}
}
Loading

0 comments on commit 8c27107

Please sign in to comment.