shardex 0.1.0

A high-performance memory-mapped vector search engine with ACID transactions and incremental updates
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
//! API performance regression tests
//!
//! This test suite establishes baseline performance metrics and validates
//! that performance doesn't regress significantly between versions.

use apithing::ApiOperation;
use shardex::api::{
    AddPostings, AddPostingsParams, CreateIndex, CreateIndexParams, Flush, FlushParams, Search, SearchParams,
    ShardexContext,
};
use shardex::{DocumentId, Posting};
use std::time::{Duration, Instant};
use tempfile::TempDir;

/// Performance test configuration
const PERFORMANCE_VECTOR_SIZE: usize = 256;
const PERFORMANCE_SHARD_SIZE: usize = 10000;
const SMALL_DATASET_SIZE: usize = 100;
const SEARCH_ITERATIONS: usize = 50;

/// Performance thresholds (these are conservative to avoid flaky tests)
const MAX_INDEX_CREATION_TIME: Duration = Duration::from_secs(5);
const MAX_POSTING_ADD_TIME_PER_100: Duration = Duration::from_secs(10);
const MAX_SEARCH_TIME_PER_QUERY: Duration = Duration::from_millis(500);
const _MAX_TEXT_STORAGE_TIME_PER_DOC: Duration = Duration::from_millis(100);
const _MAX_BATCH_PROCESSING_TIME_PER_100_DOCS: Duration = Duration::from_secs(30);

/// Create a temporary directory for performance tests
fn create_temp_directory() -> TempDir {
    tempfile::tempdir().expect("Failed to create temporary directory")
}

/// Generate deterministic test postings for performance testing
fn generate_performance_postings(count: usize, vector_size: usize) -> Vec<Posting> {
    (0..count)
        .map(|i| {
            let document_id = DocumentId::from_raw((i + 1) as u128);
            let mut vector = vec![0.0; vector_size];

            // Create realistic but deterministic vectors
            for (j, item) in vector.iter_mut().enumerate().take(vector_size) {
                *item = ((i * 31 + j * 47) as f32 % 200.0 - 100.0) / 100.0;
            }

            // Normalize vector
            let magnitude: f32 = vector.iter().map(|x| x * x).sum::<f32>().sqrt();
            if magnitude > 0.0 {
                for value in &mut vector {
                    *value /= magnitude;
                }
            }

            Posting {
                document_id,
                start: (i * 50) as u32,
                length: 50,
                vector,
            }
        })
        .collect()
}

#[test]
fn test_index_creation_performance() {
    let temp_dir = create_temp_directory();
    let mut context = ShardexContext::new();

    let create_params = CreateIndexParams::builder()
        .directory_path(temp_dir.path().to_path_buf())
        .vector_size(PERFORMANCE_VECTOR_SIZE)
        .shard_size(PERFORMANCE_SHARD_SIZE)
        .build()
        .expect("Failed to build CreateIndexParams");

    let start_time = Instant::now();
    CreateIndex::execute(&mut context, &create_params).expect("CreateIndex should succeed");
    let creation_time = start_time.elapsed();

    println!("Index creation time: {:?}", creation_time);
    assert!(
        creation_time < MAX_INDEX_CREATION_TIME,
        "Index creation took {:?}, which exceeds maximum of {:?}",
        creation_time,
        MAX_INDEX_CREATION_TIME
    );
}

#[test]
fn test_posting_addition_performance() {
    let temp_dir = create_temp_directory();
    let mut context = ShardexContext::new();

    // Create index
    let create_params = CreateIndexParams::builder()
        .directory_path(temp_dir.path().to_path_buf())
        .vector_size(PERFORMANCE_VECTOR_SIZE)
        .shard_size(PERFORMANCE_SHARD_SIZE)
        .build()
        .expect("Failed to build CreateIndexParams");

    CreateIndex::execute(&mut context, &create_params).expect("CreateIndex should succeed");

    // Test posting addition performance
    let test_postings = generate_performance_postings(SMALL_DATASET_SIZE, PERFORMANCE_VECTOR_SIZE);
    let add_params = AddPostingsParams::new(test_postings).expect("Failed to create AddPostingsParams");

    let start_time = Instant::now();
    AddPostings::execute(&mut context, &add_params).expect("AddPostings should succeed");
    let addition_time = start_time.elapsed();

    println!(
        "Posting addition time for {} documents: {:?}",
        SMALL_DATASET_SIZE, addition_time
    );
    println!(
        "Throughput: {:.2} docs/sec",
        SMALL_DATASET_SIZE as f64 / addition_time.as_secs_f64()
    );

    assert!(
        addition_time < MAX_POSTING_ADD_TIME_PER_100,
        "Adding {} postings took {:?}, which exceeds maximum of {:?}",
        SMALL_DATASET_SIZE,
        addition_time,
        MAX_POSTING_ADD_TIME_PER_100
    );
}

#[test]
fn test_search_performance() {
    let temp_dir = create_temp_directory();
    let mut context = ShardexContext::new();

    // Create and populate index
    let create_params = CreateIndexParams::builder()
        .directory_path(temp_dir.path().to_path_buf())
        .vector_size(PERFORMANCE_VECTOR_SIZE)
        .shard_size(PERFORMANCE_SHARD_SIZE)
        .build()
        .expect("Failed to build CreateIndexParams");

    CreateIndex::execute(&mut context, &create_params).expect("CreateIndex should succeed");

    let test_postings = generate_performance_postings(SMALL_DATASET_SIZE, PERFORMANCE_VECTOR_SIZE);
    let add_params = AddPostingsParams::new(test_postings).expect("Failed to create AddPostingsParams");

    AddPostings::execute(&mut context, &add_params).expect("AddPostings should succeed");

    // Flush to ensure data is available for search
    let flush_params = FlushParams::with_stats();
    Flush::execute(&mut context, &flush_params).expect("Flush should succeed");

    // Test search performance
    let query_vector = vec![0.5; PERFORMANCE_VECTOR_SIZE];
    let search_params = SearchParams::builder()
        .query_vector(query_vector)
        .k(10)
        .build()
        .expect("Failed to build SearchParams");

    let mut total_search_time = Duration::ZERO;
    let mut total_results = 0;

    for i in 0..SEARCH_ITERATIONS {
        let start_time = Instant::now();
        let results = Search::execute(&mut context, &search_params).expect("Search should succeed");
        let search_time = start_time.elapsed();

        total_search_time += search_time;
        total_results += results.len();

        if i == 0 {
            println!("First search returned {} results", results.len());
        }
    }

    let average_search_time = total_search_time / SEARCH_ITERATIONS as u32;
    let average_results = total_results as f64 / SEARCH_ITERATIONS as f64;

    println!("Average search time: {:?}", average_search_time);
    println!("Average results per search: {:.2}", average_results);
    println!(
        "Search throughput: {:.2} searches/sec",
        1.0 / average_search_time.as_secs_f64()
    );

    assert!(
        average_search_time < MAX_SEARCH_TIME_PER_QUERY,
        "Average search time {:?} exceeds maximum of {:?}",
        average_search_time,
        MAX_SEARCH_TIME_PER_QUERY
    );
}

#[test]
fn test_memory_usage_baseline() {
    let temp_dir = create_temp_directory();
    let mut context = ShardexContext::new();

    // Create index
    let create_params = CreateIndexParams::builder()
        .directory_path(temp_dir.path().to_path_buf())
        .vector_size(PERFORMANCE_VECTOR_SIZE)
        .shard_size(PERFORMANCE_SHARD_SIZE)
        .build()
        .expect("Failed to build CreateIndexParams");

    CreateIndex::execute(&mut context, &create_params).expect("CreateIndex should succeed");

    // Add a small number of postings
    let small_batch_size = 25;
    let postings = generate_performance_postings(small_batch_size, PERFORMANCE_VECTOR_SIZE);
    let add_params = AddPostingsParams::new(postings).expect("Failed to create AddPostingsParams");

    AddPostings::execute(&mut context, &add_params).expect("AddPostings should succeed");

    // Flush to get accurate statistics
    let flush_params = FlushParams::new();
    Flush::execute(&mut context, &flush_params).expect("Flush should succeed");

    // Get memory usage statistics
    let stats = shardex::api::GetStats::execute(&mut context, &shardex::api::GetStatsParams::new())
        .expect("GetStats should succeed");

    println!(
        "Baseline memory usage: {:.2} MB for {} postings",
        stats.memory_usage as f64 / 1024.0 / 1024.0,
        small_batch_size
    );

    assert!(stats.memory_usage > 0, "Should use some memory");
}

#[test]
fn test_memory_usage_batch_processing() {
    let temp_dir = create_temp_directory();
    let mut context = ShardexContext::new();

    // Create index
    let create_params = CreateIndexParams::builder()
        .directory_path(temp_dir.path().to_path_buf())
        .vector_size(PERFORMANCE_VECTOR_SIZE)
        .shard_size(PERFORMANCE_SHARD_SIZE)
        .build()
        .expect("Failed to build CreateIndexParams");

    CreateIndex::execute(&mut context, &create_params).expect("CreateIndex should succeed");

    // Test memory usage with 3 smaller batches
    let batch_size = 20;
    let num_batches = 3;
    let mut memory_growth = Vec::new();

    for batch in 0..num_batches {
        let postings = generate_performance_postings(batch_size, PERFORMANCE_VECTOR_SIZE);
        let add_params = AddPostingsParams::new(postings).expect("Failed to create AddPostingsParams");

        AddPostings::execute(&mut context, &add_params).expect("AddPostings should succeed");

        // Flush to get accurate statistics
        let flush_params = FlushParams::new();
        Flush::execute(&mut context, &flush_params).expect("Flush should succeed");

        // Get memory usage statistics
        let stats = shardex::api::GetStats::execute(&mut context, &shardex::api::GetStatsParams::new())
            .expect("GetStats should succeed");

        memory_growth.push(stats.memory_usage);
        println!(
            "Batch {}: memory usage: {:.2} MB",
            batch + 1,
            stats.memory_usage as f64 / 1024.0 / 1024.0
        );
    }

    // Memory should grow with each batch (allow for some tolerance due to memory management)
    let growth_1_to_2 = memory_growth[1] >= memory_growth[0];
    let growth_2_to_3 = memory_growth[2] >= memory_growth[1];

    // At least one should show growth, or final should be higher than first
    assert!(
        growth_1_to_2 || growth_2_to_3 || memory_growth[2] > memory_growth[0],
        "Memory should generally increase with more postings. Growth: {} -> {} -> {}",
        memory_growth[0],
        memory_growth[1],
        memory_growth[2]
    );
}

#[test]
fn test_memory_per_posting_efficiency() {
    let temp_dir = create_temp_directory();
    let mut context = ShardexContext::new();

    // Create index
    let create_params = CreateIndexParams::builder()
        .directory_path(temp_dir.path().to_path_buf())
        .vector_size(PERFORMANCE_VECTOR_SIZE)
        .shard_size(PERFORMANCE_SHARD_SIZE)
        .build()
        .expect("Failed to build CreateIndexParams");

    CreateIndex::execute(&mut context, &create_params).expect("CreateIndex should succeed");

    // Add a moderate number of postings for efficiency testing
    let posting_count = 100;
    let postings = generate_performance_postings(posting_count, PERFORMANCE_VECTOR_SIZE);
    let add_params = AddPostingsParams::new(postings).expect("Failed to create AddPostingsParams");

    AddPostings::execute(&mut context, &add_params).expect("AddPostings should succeed");

    // Flush to get accurate statistics
    let flush_params = FlushParams::new();
    Flush::execute(&mut context, &flush_params).expect("Flush should succeed");

    // Get memory usage statistics
    let stats = shardex::api::GetStats::execute(&mut context, &shardex::api::GetStatsParams::new())
        .expect("GetStats should succeed");

    let memory_per_posting = stats.memory_usage as f64 / posting_count as f64;

    println!(
        "Memory efficiency: {:.2} MB for {} postings",
        stats.memory_usage as f64 / 1024.0 / 1024.0,
        posting_count
    );
    println!("Memory per posting: {:.2} bytes", memory_per_posting);

    // Reasonable memory usage expectations (these are conservative)
    // Each posting has a 256-dim vector (1KB) plus indexing overhead (bloom filters, WAL, etc.)
    assert!(
        memory_per_posting < 150000.0,
        "Memory per posting should be reasonable (< 150KB), got {:.2} bytes",
        memory_per_posting
    );
}

#[test]
fn test_scaling_performance() {
    let temp_dir = create_temp_directory();
    let mut context = ShardexContext::new();

    // Create index optimized for larger datasets
    let create_params = CreateIndexParams::builder()
        .directory_path(temp_dir.path().to_path_buf())
        .vector_size(PERFORMANCE_VECTOR_SIZE)
        .shard_size(PERFORMANCE_SHARD_SIZE)
        .batch_write_interval_ms(50) // Faster batching
        .build()
        .expect("Failed to build CreateIndexParams");

    CreateIndex::execute(&mut context, &create_params).expect("CreateIndex should succeed");

    // Test performance scaling with different dataset sizes
    let test_sizes = vec![50, 100, 200];

    for size in test_sizes {
        println!("\nTesting with {} documents:", size);

        // Clear the index for each test (create new context)
        let mut test_context = ShardexContext::new();
        let temp_test_dir = create_temp_directory();
        let test_create_params = CreateIndexParams::builder()
            .directory_path(temp_test_dir.path().to_path_buf())
            .vector_size(PERFORMANCE_VECTOR_SIZE)
            .shard_size(PERFORMANCE_SHARD_SIZE)
            .batch_write_interval_ms(50)
            .build()
            .expect("Failed to build CreateIndexParams");

        CreateIndex::execute(&mut test_context, &test_create_params).expect("CreateIndex should succeed");

        // Add postings
        let postings = generate_performance_postings(size, PERFORMANCE_VECTOR_SIZE);
        let add_params = AddPostingsParams::new(postings).expect("Failed to create AddPostingsParams");

        let start_time = Instant::now();
        AddPostings::execute(&mut test_context, &add_params).expect("AddPostings should succeed");
        let add_time = start_time.elapsed();

        // Flush
        let flush_params = FlushParams::new();
        let flush_start = Instant::now();
        Flush::execute(&mut test_context, &flush_params).expect("Flush should succeed");
        let flush_time = flush_start.elapsed();

        // Search
        let query_vector = vec![0.3; PERFORMANCE_VECTOR_SIZE];
        let search_params = SearchParams::builder()
            .query_vector(query_vector)
            .k(10)
            .build()
            .expect("Failed to build SearchParams");

        let search_start = Instant::now();
        let results = Search::execute(&mut test_context, &search_params).expect("Search should succeed");
        let search_time = search_start.elapsed();

        println!(
            "  Add time: {:?} ({:.2} docs/sec)",
            add_time,
            size as f64 / add_time.as_secs_f64()
        );
        println!("  Flush time: {:?}", flush_time);
        println!("  Search time: {:?} ({} results)", search_time, results.len());

        // Performance should scale reasonably
        let throughput = size as f64 / add_time.as_secs_f64();
        assert!(
            throughput > 1.0,
            "Should achieve at least 1 doc/sec throughput for size {}",
            size
        );
    }
}