-
-
Notifications
You must be signed in to change notification settings - Fork 677
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
seek_exact + cost based intersection #2538
Open
PSeitz
wants to merge
3
commits into
main
Choose a base branch
from
fix_size_hint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
use std::fmt::Display; | ||
use std::net::Ipv6Addr; | ||
use std::ops::RangeInclusive; | ||
|
||
use binggan::plugins::PeakMemAllocPlugin; | ||
use binggan::{black_box, BenchRunner, OutputValue, PeakMemAlloc, INSTRUMENTED_SYSTEM}; | ||
use columnar::MonotonicallyMappableToU128; | ||
use rand::rngs::StdRng; | ||
use rand::{Rng, SeedableRng}; | ||
use tantivy::collector::{Count, TopDocs}; | ||
use tantivy::query::QueryParser; | ||
use tantivy::schema::*; | ||
use tantivy::{doc, Index}; | ||
|
||
#[global_allocator] | ||
pub static GLOBAL: &PeakMemAlloc<std::alloc::System> = &INSTRUMENTED_SYSTEM; | ||
|
||
fn main() { | ||
bench_range_query(); | ||
} | ||
|
||
fn bench_range_query() { | ||
let index = get_index_0_to_100(); | ||
let mut runner = BenchRunner::new(); | ||
runner.add_plugin(PeakMemAllocPlugin::new(GLOBAL)); | ||
|
||
runner.set_name("range_query on u64"); | ||
let field_name_and_descr: Vec<_> = vec![ | ||
("id", "Single Valued Range Field"), | ||
("ids", "Multi Valued Range Field"), | ||
]; | ||
let range_num_hits = vec![ | ||
("90_percent", get_90_percent()), | ||
("10_percent", get_10_percent()), | ||
("1_percent", get_1_percent()), | ||
]; | ||
|
||
test_range(&mut runner, &index, &field_name_and_descr, range_num_hits); | ||
|
||
runner.set_name("range_query on ip"); | ||
let field_name_and_descr: Vec<_> = vec![ | ||
("ip", "Single Valued Range Field"), | ||
("ips", "Multi Valued Range Field"), | ||
]; | ||
let range_num_hits = vec![ | ||
("90_percent", get_90_percent_ip()), | ||
("10_percent", get_10_percent_ip()), | ||
("1_percent", get_1_percent_ip()), | ||
]; | ||
|
||
test_range(&mut runner, &index, &field_name_and_descr, range_num_hits); | ||
} | ||
|
||
fn test_range<T: Display>( | ||
runner: &mut BenchRunner, | ||
index: &Index, | ||
field_name_and_descr: &[(&str, &str)], | ||
range_num_hits: Vec<(&str, RangeInclusive<T>)>, | ||
) { | ||
for (field, suffix) in field_name_and_descr { | ||
let term_num_hits = vec![ | ||
("", ""), | ||
("1_percent", "veryfew"), | ||
("10_percent", "few"), | ||
("90_percent", "most"), | ||
]; | ||
let mut group = runner.new_group(); | ||
group.set_name(suffix); | ||
// all intersect combinations | ||
for (range_name, range) in &range_num_hits { | ||
for (term_name, term) in &term_num_hits { | ||
let index = &index; | ||
let test_name = if term_name.is_empty() { | ||
format!("id_range_hit_{}", range_name) | ||
} else { | ||
format!( | ||
"id_range_hit_{}_intersect_with_term_{}", | ||
range_name, term_name | ||
) | ||
}; | ||
group.register(test_name, move |_| { | ||
let query = if term_name.is_empty() { | ||
"".to_string() | ||
} else { | ||
format!("AND id_name:{}", term) | ||
}; | ||
black_box(execute_query(field, range, &query, index)); | ||
}); | ||
} | ||
} | ||
group.run(); | ||
} | ||
} | ||
|
||
fn get_index_0_to_100() -> Index { | ||
let mut rng = StdRng::from_seed([1u8; 32]); | ||
let num_vals = 100_000; | ||
let docs: Vec<_> = (0..num_vals) | ||
.map(|_i| { | ||
let id_name = if rng.gen_bool(0.01) { | ||
"veryfew".to_string() // 1% | ||
} else if rng.gen_bool(0.1) { | ||
"few".to_string() // 9% | ||
} else { | ||
"most".to_string() // 90% | ||
}; | ||
Doc { | ||
id_name, | ||
id: rng.gen_range(0..100), | ||
// Multiply by 1000, so that we create most buckets in the compact space | ||
// The benches depend on this range to select n-percent of elements with the | ||
// methods below. | ||
ip: Ipv6Addr::from_u128(rng.gen_range(0..100) * 1000), | ||
} | ||
}) | ||
.collect(); | ||
|
||
create_index_from_docs(&docs) | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Doc { | ||
pub id_name: String, | ||
pub id: u64, | ||
pub ip: Ipv6Addr, | ||
} | ||
|
||
pub fn create_index_from_docs(docs: &[Doc]) -> Index { | ||
let mut schema_builder = Schema::builder(); | ||
let id_u64_field = schema_builder.add_u64_field("id", INDEXED | STORED | FAST); | ||
let ids_u64_field = | ||
schema_builder.add_u64_field("ids", NumericOptions::default().set_fast().set_indexed()); | ||
|
||
let id_f64_field = schema_builder.add_f64_field("id_f64", INDEXED | STORED | FAST); | ||
let ids_f64_field = schema_builder.add_f64_field( | ||
"ids_f64", | ||
NumericOptions::default().set_fast().set_indexed(), | ||
); | ||
|
||
let id_i64_field = schema_builder.add_i64_field("id_i64", INDEXED | STORED | FAST); | ||
let ids_i64_field = schema_builder.add_i64_field( | ||
"ids_i64", | ||
NumericOptions::default().set_fast().set_indexed(), | ||
); | ||
|
||
let text_field = schema_builder.add_text_field("id_name", STRING | STORED); | ||
let text_field2 = schema_builder.add_text_field("id_name_fast", STRING | STORED | FAST); | ||
|
||
let ip_field = schema_builder.add_ip_addr_field("ip", FAST); | ||
let ips_field = schema_builder.add_ip_addr_field("ips", FAST); | ||
|
||
let schema = schema_builder.build(); | ||
|
||
let index = Index::create_in_ram(schema); | ||
|
||
{ | ||
let mut index_writer = index.writer_with_num_threads(1, 50_000_000).unwrap(); | ||
for doc in docs.iter() { | ||
index_writer | ||
.add_document(doc!( | ||
ids_i64_field => doc.id as i64, | ||
ids_i64_field => doc.id as i64, | ||
ids_f64_field => doc.id as f64, | ||
ids_f64_field => doc.id as f64, | ||
ids_u64_field => doc.id, | ||
ids_u64_field => doc.id, | ||
id_u64_field => doc.id, | ||
id_f64_field => doc.id as f64, | ||
id_i64_field => doc.id as i64, | ||
text_field => doc.id_name.to_string(), | ||
text_field2 => doc.id_name.to_string(), | ||
ips_field => doc.ip, | ||
ips_field => doc.ip, | ||
ip_field => doc.ip, | ||
)) | ||
.unwrap(); | ||
} | ||
|
||
index_writer.commit().unwrap(); | ||
} | ||
index | ||
} | ||
|
||
fn get_90_percent() -> RangeInclusive<u64> { | ||
0..=90 | ||
} | ||
|
||
fn get_10_percent() -> RangeInclusive<u64> { | ||
0..=10 | ||
} | ||
|
||
fn get_1_percent() -> RangeInclusive<u64> { | ||
10..=10 | ||
} | ||
|
||
fn get_90_percent_ip() -> RangeInclusive<Ipv6Addr> { | ||
let start = Ipv6Addr::from_u128(0); | ||
let end = Ipv6Addr::from_u128(90 * 1000); | ||
start..=end | ||
} | ||
|
||
fn get_10_percent_ip() -> RangeInclusive<Ipv6Addr> { | ||
let start = Ipv6Addr::from_u128(0); | ||
let end = Ipv6Addr::from_u128(10 * 1000); | ||
start..=end | ||
} | ||
|
||
fn get_1_percent_ip() -> RangeInclusive<Ipv6Addr> { | ||
let start = Ipv6Addr::from_u128(10 * 1000); | ||
let end = Ipv6Addr::from_u128(10 * 1000); | ||
start..=end | ||
} | ||
|
||
struct NumHits { | ||
count: usize, | ||
} | ||
impl OutputValue for NumHits { | ||
fn column_title() -> &'static str { | ||
"NumHits" | ||
} | ||
fn format(&self) -> Option<String> { | ||
Some(self.count.to_string()) | ||
} | ||
} | ||
|
||
fn execute_query<T: Display>( | ||
field: &str, | ||
id_range: &RangeInclusive<T>, | ||
suffix: &str, | ||
index: &Index, | ||
) -> NumHits { | ||
let gen_query_inclusive = |from: &T, to: &T| { | ||
format!( | ||
"{}:[{} TO {}] {}", | ||
field, | ||
&from.to_string(), | ||
&to.to_string(), | ||
suffix | ||
) | ||
}; | ||
|
||
let query = gen_query_inclusive(id_range.start(), id_range.end()); | ||
execute_query_(&query, index) | ||
} | ||
|
||
fn execute_query_(query: &str, index: &Index) -> NumHits { | ||
let query_from_text = |text: &str| { | ||
QueryParser::for_index(index, vec![]) | ||
.parse_query(text) | ||
.unwrap() | ||
}; | ||
let query = query_from_text(query); | ||
let reader = index.reader().unwrap(); | ||
let searcher = reader.searcher(); | ||
let num_hits = searcher | ||
.search(&query, &(TopDocs::with_limit(10), Count)) | ||
.unwrap() | ||
.1; | ||
NumHits { count: num_hits } | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
let's find a better name