File size: 6,477 Bytes
84d2a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#[cfg(not(target_os = "windows"))]
mod prof;

use common::types::PointOffsetType;
use criterion::{criterion_group, criterion_main, Criterion};
use itertools::Itertools;
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use segment::fixtures::payload_context_fixture::{
    create_plain_payload_index, create_struct_payload_index,
};
use segment::fixtures::payload_fixtures::{random_match_any_filter, random_must_filter};
use segment::index::PayloadIndex;
use tempfile::Builder;

const NUM_POINTS: usize = 100000;
const CHECK_SAMPLE_SIZE: usize = 1000;

fn conditional_plain_search_benchmark(c: &mut Criterion) {
    let seed = 42;

    let mut rng = StdRng::seed_from_u64(seed);
    let mut group = c.benchmark_group("conditional-search-group");

    let dir = Builder::new().prefix("storage_dir").tempdir().unwrap();
    let plain_index = create_plain_payload_index(dir.path(), NUM_POINTS, seed);

    let mut result_size = 0;
    let mut query_count = 0;

    group.bench_function("conditional-search-query-points", |b| {
        b.iter(|| {
            let filter = random_must_filter(&mut rng, 2);
            result_size += plain_index.query_points(&filter).len();
            query_count += 1;
        })
    });
    if query_count != 0 {
        eprintln!(
            "result_size / query_count = {:#?}",
            result_size / query_count
        );
    }

    let mut result_size = 0;
    let mut query_count = 0;

    // Same benchmark, but with larger expected result
    group.bench_function("conditional-search-query-points-large", |b| {
        b.iter(|| {
            let filter = random_must_filter(&mut rng, 1);
            result_size += plain_index.query_points(&filter).len();
            query_count += 1;
        })
    });
    if query_count != 0 {
        eprintln!(
            "result_size / query_count = {:#?}",
            result_size / query_count
        );
    }

    let mut result_size = 0;
    let mut query_count = 0;

    group.bench_function("conditional-search-context-check", |b| {
        b.iter(|| {
            let filter = random_must_filter(&mut rng, 2);
            let sample = (0..CHECK_SAMPLE_SIZE)
                .map(|_| rng.gen_range(0..NUM_POINTS) as PointOffsetType)
                .collect_vec();
            let context = plain_index.filter_context(&filter);

            let filtered_sample = sample
                .into_iter()
                .filter(|id| context.check(*id))
                .collect_vec();
            result_size += filtered_sample.len();
            query_count += 1;
        })
    });

    if query_count != 0 {
        eprintln!(
            "result_size / query_count = {:#?}",
            result_size / query_count
        );
    }

    let mut result_size = 0;
    let mut query_count = 0;

    group.bench_function("conditional-search-match-any", |b| {
        let filter = random_match_any_filter(&mut rng, 2, 51.0);
        b.iter(|| {
            let sample = (0..CHECK_SAMPLE_SIZE)
                .map(|_| rng.gen_range(0..NUM_POINTS) as PointOffsetType)
                .collect_vec();
            let context = plain_index.filter_context(&filter);
            let filtered_sample = sample
                .into_iter()
                .filter(|id| context.check(*id))
                .collect_vec();
            result_size += filtered_sample.len();
            query_count += 1;
        });
    });

    group.bench_function("conditional-search-large-match-any", |b| {
        let filter = random_match_any_filter(&mut rng, 1000, 15.0);
        b.iter(|| {
            let sample = (0..CHECK_SAMPLE_SIZE)
                .map(|_| rng.gen_range(0..NUM_POINTS) as PointOffsetType)
                .collect_vec();
            let context = plain_index.filter_context(&filter);
            let filtered_sample = sample
                .into_iter()
                .filter(|id| context.check(*id))
                .collect_vec();
            result_size += filtered_sample.len();
            query_count += 1;
        });
    });

    group.finish();
}

fn conditional_struct_search_benchmark(c: &mut Criterion) {
    let mut rng = StdRng::seed_from_u64(42);
    let mut group = c.benchmark_group("conditional-search-group");

    let seed = 42;

    let dir = Builder::new().prefix("storage_dir").tempdir().unwrap();
    let struct_index = create_struct_payload_index(dir.path(), NUM_POINTS, seed);

    let mut result_size = 0;
    let mut query_count = 0;

    let filter = random_must_filter(&mut rng, 2);
    let cardinality = struct_index.estimate_cardinality(&filter);

    let indexed_fields = struct_index.indexed_fields();

    eprintln!("cardinality = {cardinality:#?}");
    eprintln!("indexed_fields = {indexed_fields:#?}");

    group.bench_function("struct-conditional-search-query-points", |b| {
        b.iter(|| {
            let filter = random_must_filter(&mut rng, 2);
            result_size += struct_index.query_points(&filter).len();
            query_count += 1;
        })
    });
    if query_count != 0 {
        eprintln!(
            "result_size / query_count = {:#?}",
            result_size / query_count
        );
    }

    let mut result_size = 0;
    let mut query_count = 0;

    group.bench_function("struct-conditional-search-context-check", |b| {
        b.iter(|| {
            let filter = random_must_filter(&mut rng, 2);
            let sample = (0..CHECK_SAMPLE_SIZE)
                .map(|_| rng.gen_range(0..NUM_POINTS) as PointOffsetType)
                .collect_vec();
            let context = struct_index.filter_context(&filter);

            let filtered_sample = sample
                .into_iter()
                .filter(|id| context.check(*id))
                .collect_vec();
            result_size += filtered_sample.len();
            query_count += 1;
        })
    });

    if query_count != 0 {
        eprintln!(
            "result_size / query_count = {:#?}",
            result_size / query_count
        );
    }

    group.finish();
}

#[cfg(not(target_os = "windows"))]
criterion_group! {
    name = benches;
    config = Criterion::default().with_profiler(prof::FlamegraphProfiler::new(100));
    targets = conditional_struct_search_benchmark, conditional_plain_search_benchmark
}

#[cfg(target_os = "windows")]
criterion_group! {
    name = benches;
    config = Criterion::default();
    targets = conditional_struct_search_benchmark, conditional_plain_search_benchmark
}

criterion_main!(benches);