laurus 0.4.2

Unified search library for lexical, vector, and semantic retrieval
Documentation
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Criterion benchmarks for the Laurus search engine.
//!
//! This suite measures hotspots across analysis, lexical, and vector subsystems
//! so regressions can be caught before release. Covered areas include:
//! - Text analysis and tokenization
//! - Vector similarity search (HNSW)
//! - Spell correction
//! - Parallel operations

use std::hint::black_box;

use criterion::{Criterion, Throughput, criterion_group, criterion_main};

use laurus::analysis::analyzer::analyzer::Analyzer;
use laurus::analysis::analyzer::standard::StandardAnalyzer;
use laurus::spelling::corrector::SpellingCorrector;
use laurus::vector::core::distance::DistanceMetric;
use laurus::vector::core::vector::Vector;
use laurus::vector::index::config::HnswIndexConfig;
use laurus::vector::index::hnsw::writer::HnswIndexWriter;
use laurus::vector::writer::VectorIndexWriter;

/// Generate test documents for benchmarking.
fn generate_test_documents(count: usize) -> Vec<String> {
    let words = vec![
        "search",
        "engine",
        "full",
        "text",
        "index",
        "query",
        "document",
        "field",
        "term",
        "phrase",
        "boolean",
        "vector",
        "similarity",
        "relevance",
        "score",
        "analysis",
        "tokenization",
        "stemming",
        "normalization",
        "clustering",
        "machine",
        "learning",
        "algorithm",
        "data",
        "structure",
        "performance",
        "optimization",
        "memory",
        "storage",
        "retrieval",
        "ranking",
        "filtering",
    ];

    let mut documents = Vec::with_capacity(count);
    for i in 0..count {
        let doc_length = 50 + (i % 100); // Variable length documents
        let mut doc_words = Vec::with_capacity(doc_length);

        for j in 0..doc_length {
            let word_idx = (i * 7 + j * 13) % words.len(); // Pseudo-random distribution
            doc_words.push(words[word_idx]);
        }

        documents.push(doc_words.join(" "));
    }

    documents
}

/// Generate test vectors for benchmarking.
fn generate_test_vectors(count: usize, dimension: usize) -> Vec<Vector> {
    let mut vectors = Vec::with_capacity(count);

    for i in 0..count {
        let mut data = Vec::with_capacity(dimension);
        for j in 0..dimension {
            // Create somewhat realistic vector data with patterns
            let value = ((i as f32 * 0.1 + j as f32 * 0.01).sin() * 0.5 + 0.5) * 2.0 - 1.0;
            data.push(value);
        }
        vectors.push(Vector::new(data));
    }

    vectors
}

/// Benchmark text analysis and tokenization.
fn bench_text_analysis(c: &mut Criterion) {
    let mut group = c.benchmark_group("text_analysis");

    let analyzer = StandardAnalyzer::new().unwrap();
    let texts = generate_test_documents(1000);

    // Single document analysis
    group.bench_function("analyze_single_document", |b| {
        b.iter(|| {
            let result = analyzer.analyze(black_box(&texts[0]));
            black_box(result)
        })
    });

    // Batch document analysis
    group.throughput(Throughput::Elements(100));
    group.bench_function("analyze_batch_documents", |b| {
        b.iter(|| {
            for text in texts.iter().take(100) {
                let result = analyzer.analyze(black_box(text));
                let _ = black_box(result);
            }
        })
    });

    group.finish();
}

/// Benchmark vector operations.
fn bench_vector_search(c: &mut Criterion) {
    let mut group = c.benchmark_group("vector_search");
    group.sample_size(20); // Reduce sample size for vector operations

    let dimension = 128;
    let vectors = generate_test_vectors(1000, dimension);

    // HNSW index construction
    group.throughput(Throughput::Elements(100));
    group.bench_function("hnsw_index_construction", |b| {
        b.iter_with_setup(
            || {
                let index_config = HnswIndexConfig {
                    dimension,
                    distance_metric: DistanceMetric::Cosine,
                    m: 16,
                    ef_construction: 200,
                    ..Default::default()
                };
                let writer_config = laurus::vector::writer::VectorIndexWriterConfig::default();
                HnswIndexWriter::new(index_config, writer_config, "bench_vectors").unwrap()
            },
            |mut builder| {
                let field_name = "default".to_string();
                let indexed_vectors: Vec<(u64, String, Vector)> = vectors
                    .iter()
                    .take(100)
                    .enumerate()
                    .map(|(i, v)| (i as u64, field_name.clone(), v.clone()))
                    .collect();
                let _ = builder.build(indexed_vectors);
                black_box(builder);
            },
        )
    });

    // Vector operations simplified (search requires full implementation)
    group.bench_function("vector_operations_basic", |b| {
        let query_vector = vectors[0].clone();

        b.iter(|| {
            let mut results = Vec::with_capacity(50);
            for (i, vector) in vectors.iter().take(50).enumerate() {
                let distance = DistanceMetric::Cosine
                    .distance(black_box(&query_vector.data), black_box(&vector.data))
                    .unwrap();
                results.push((i as u64, distance));
            }
            results.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
            black_box(results)
        })
    });

    // Vector distance calculations
    group.bench_function("cosine_distance_batch", |b| {
        let query = &vectors[0];
        let targets = &vectors[1..101]; // 100 vectors

        b.iter(|| {
            for target in targets {
                let distance = DistanceMetric::Cosine
                    .distance(black_box(&query.data), black_box(&target.data))
                    .unwrap();
                black_box(distance);
            }
        })
    });

    // Vector normalization
    group.throughput(Throughput::Elements(100));
    group.bench_function("vector_normalization", |b| {
        b.iter_with_setup(
            || vectors[0..100].to_vec(),
            |mut test_vectors| {
                for vector in &mut test_vectors {
                    vector.normalize();
                }
                black_box(test_vectors);
            },
        )
    });

    group.finish();
}

/// Benchmark spell correction operations.
fn bench_spell_correction(c: &mut Criterion) {
    let mut group = c.benchmark_group("spell_correction");
    group.sample_size(20); // Reduce sample size for faster execution

    let mut corrector = SpellingCorrector::new();

    // Common misspellings (reduced set for faster execution)
    let misspellings = vec!["searc", "engin", "documnet", "qurey", "algortihm"];

    // Single word correction
    group.bench_function("correct_single_word", |b| {
        b.iter(|| {
            let result = corrector.correct(black_box("searc"));
            black_box(result)
        })
    });

    // Batch correction
    group.throughput(Throughput::Elements(misspellings.len() as u64));
    group.bench_function("correct_batch_words", |b| {
        b.iter(|| {
            for word in &misspellings {
                let result = corrector.correct(black_box(word));
                black_box(result);
            }
        })
    });

    group.finish();
}

/// Benchmark parallel operations.
fn bench_parallel_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("parallel_operations");

    let vectors = generate_test_vectors(1000, 128);
    let query_vector = &vectors[0];

    // Parallel distance calculation
    group.throughput(Throughput::Elements(500));
    group.bench_function("parallel_distance_calculation", |b| {
        b.iter(|| {
            use rayon::prelude::*;
            let distances: Vec<_> = vectors[1..501]
                .par_iter()
                .map(|v| {
                    DistanceMetric::Cosine
                        .distance(&query_vector.data, &v.data)
                        .unwrap()
                })
                .collect();
            black_box(distances);
        })
    });

    // Sequential distance calculation for comparison
    group.bench_function("sequential_distance_calculation", |b| {
        b.iter(|| {
            let distances: Vec<_> = vectors[1..501]
                .iter()
                .map(|v| {
                    DistanceMetric::Cosine
                        .distance(&query_vector.data, &v.data)
                        .unwrap()
                })
                .collect();
            black_box(distances);
        })
    });

    // Parallel vector normalization
    group.throughput(Throughput::Elements(500));
    group.bench_function("parallel_vector_normalization", |b| {
        b.iter_with_setup(
            || vectors[0..500].to_vec(),
            |mut test_vectors| {
                use rayon::prelude::*;
                test_vectors.par_iter_mut().for_each(|v| v.normalize());
                black_box(test_vectors);
            },
        )
    });

    group.finish();
}

/// Memory usage and allocation benchmarks.
fn bench_memory_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("memory_operations");

    // Vector allocation
    group.throughput(Throughput::Elements(1000));
    group.bench_function("vector_allocation", |b| {
        b.iter(|| {
            let mut vectors = Vec::new();
            for i in 0..1000 {
                let data: Vec<f32> = (0..128).map(|j| (i + j) as f32).collect();
                vectors.push(Vector::new(data));
            }
            black_box(vectors);
        })
    });

    group.finish();
}

/// Comprehensive benchmark suite covering different data sizes.
fn bench_scalability(c: &mut Criterion) {
    let mut group = c.benchmark_group("scalability");
    group.sample_size(10);

    for size in [100, 200].iter() {
        // Reduced sizes for faster execution
        // Vector indexing scalability
        group.bench_with_input(
            format!("vector_index_{size}_vectors"),
            size,
            |b, &vector_count| {
                let vectors = generate_test_vectors(vector_count, 128);

                b.iter_with_setup(
                    || {
                        let index_config = HnswIndexConfig {
                            dimension: 128,
                            distance_metric: DistanceMetric::Cosine,
                            m: 16,
                            ef_construction: 200,
                            ..Default::default()
                        };
                        let writer_config =
                            laurus::vector::writer::VectorIndexWriterConfig::default();
                        HnswIndexWriter::new(
                            index_config,
                            writer_config,
                            "bench_scalability_vectors",
                        )
                        .unwrap()
                    },
                    |mut builder| {
                        let field_name = "default".to_string();
                        let indexed_vectors: Vec<(u64, String, Vector)> = vectors
                            .iter()
                            .enumerate()
                            .map(|(i, v)| (i as u64, field_name.clone(), v.clone()))
                            .collect();
                        let _ = builder.build(indexed_vectors);
                        black_box(builder);
                    },
                )
            },
        );
    }

    group.finish();
}

/// Benchmark synonym dictionary operations.
fn bench_synonym_dictionary(c: &mut Criterion) {
    let mut group = c.benchmark_group("synonym_dictionary");

    // Create dictionary with varying sizes
    let small_dict = create_test_dictionary(100);
    let medium_dict = create_test_dictionary(1000);
    let large_dict = create_test_dictionary(10000);

    // Benchmark single lookup
    group.bench_function("lookup_small_100", |b| {
        b.iter(|| {
            let result = small_dict.get_synonyms(black_box("term_50"));
            black_box(result)
        })
    });

    group.bench_function("lookup_medium_1k", |b| {
        b.iter(|| {
            let result = medium_dict.get_synonyms(black_box("term_500"));
            black_box(result)
        })
    });

    group.bench_function("lookup_large_10k", |b| {
        b.iter(|| {
            let result = large_dict.get_synonyms(black_box("term_5000"));
            black_box(result)
        })
    });

    // Benchmark batch lookups
    group.throughput(Throughput::Elements(100));
    group.bench_function("batch_lookup_100", |b| {
        b.iter(|| {
            for i in 0..100 {
                let term = format!("term_{}", i);
                let result = large_dict.get_synonyms(black_box(&term));
                black_box(result);
            }
        })
    });

    // Benchmark dictionary creation
    group.bench_function("build_dict_1k", |b| {
        b.iter(|| {
            let dict = create_test_dictionary(1000);
            black_box(dict)
        })
    });

    group.finish();
}

/// Create a test dictionary with specified number of synonym groups.
fn create_test_dictionary(
    num_groups: usize,
) -> laurus::analysis::synonym::dictionary::SynonymDictionary {
    use laurus::analysis::synonym::dictionary::SynonymDictionary;

    let mut groups = Vec::new();
    for i in 0..num_groups {
        groups.push(vec![
            format!("term_{}", i),
            format!("synonym_a_{}", i),
            format!("synonym_b_{}", i),
        ]);
    }

    let mut dict = SynonymDictionary::new(None).unwrap();
    for group in groups {
        dict.add_synonym_group(group);
    }
    dict
}

// Group all benchmarks - core benchmarks for faster execution
criterion_group!(
    benches,
    bench_text_analysis,
    bench_vector_search,
    bench_parallel_operations,
    bench_memory_operations,
    bench_synonym_dictionary
);

// Separate group for slower benchmarks
criterion_group!(slow_benches, bench_spell_correction, bench_scalability);

criterion_main!(benches, slow_benches);