embellama 0.9.2

High-performance Rust library for generating text embeddings using llama-cpp
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
// Copyright 2025 Embellama Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Integration tests for the embellama library

use embellama::{EmbeddingEngine, EngineConfig, NormalizationMode, PoolingStrategy};
use serial_test::serial;
use std::fs;
use std::path::PathBuf;
use std::sync::Once;
use tempfile::tempdir;

/// Gets the path to the test model (only call when env var is known to be set)
fn get_test_model_path() -> PathBuf {
    PathBuf::from(std::env::var("EMBELLAMA_TEST_MODEL").unwrap())
}

// Initialize global tracing once for all tests
static INIT: Once = Once::new();

fn init_test_tracing() {
    INIT.call_once(|| {
        // Use global tracing subscriber for tests
        let filter = tracing_subscriber::EnvFilter::try_from_default_env()
            .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"));

        tracing_subscriber::fmt()
            .with_env_filter(filter)
            .with_test_writer()
            .init();
    });
}

/// Creates a dummy model file for testing
fn create_test_model_file() -> (tempfile::TempDir, PathBuf) {
    let dir = tempdir().unwrap();
    let model_path = dir.path().join("test_model.gguf");
    fs::write(&model_path, b"dummy model file").unwrap();
    (dir, model_path)
}

/// Creates a test configuration
fn create_test_config(model_path: PathBuf, name: &str) -> EngineConfig {
    EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name(name)
        .with_n_threads(1)
        .with_normalization_mode(NormalizationMode::L2)
        .with_pooling_strategy(PoolingStrategy::Mean)
        .build()
        .unwrap()
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn test_engine_creation_and_embedding() {
    init_test_tracing();

    let model_path = get_test_model_path();

    let normalize = true; // Store this before moving config
    let config = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("test-model")
        .with_normalization_mode(if normalize {
            NormalizationMode::L2
        } else {
            NormalizationMode::None
        })
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).expect("Failed to create engine");

    // Test single embedding
    let text = "Hello, world!";
    let embedding = engine
        .embed(None, text)
        .expect("Failed to generate embedding");

    assert!(!embedding.is_empty());
    assert!(!embedding.is_empty());

    // Check that embeddings are not all zeros (norm should be non-zero)
    // Note: Some models like MiniLM don't normalize by default
    let norm: f32 = embedding.iter().map(|x| x * x).sum::<f32>().sqrt();
    assert!(
        norm > 0.1,
        "Embedding norm too low (likely zeros), got norm: {norm}"
    );

    // If normalization is enabled in config, check it's close to 1.0
    if normalize {
        assert!(
            (norm - 1.0).abs() < 0.01,
            "Embedding should be normalized to 1.0, got norm: {norm}"
        );
    }

    // Clean up thread-local models before test ends
    engine.cleanup_thread_models();
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn test_batch_embeddings() {
    init_test_tracing();
    let model_path = get_test_model_path();

    let config = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("test-model")
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).expect("Failed to create engine");

    let texts = vec![
        "First document about technology",
        "Second document about science",
        "Third document about mathematics",
    ];

    let embeddings = engine
        .embed_batch(None, &texts)
        .expect("Failed to generate batch embeddings");

    assert_eq!(embeddings.len(), texts.len());

    // Check that all embeddings have the same dimension
    let dim = embeddings[0].len();
    for (i, emb) in embeddings.iter().enumerate() {
        assert_eq!(emb.len(), dim, "Embedding {i} has different dimension");
    }

    // Clean up thread-local models before test ends
    engine.cleanup_thread_models();
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn test_multiple_models() {
    init_test_tracing();
    let model_path = get_test_model_path();

    // Create engine with first model
    let config1 = EngineConfig::builder()
        .with_model_path(model_path.clone())
        .with_model_name("model1")
        .build()
        .unwrap();

    let mut engine = EmbeddingEngine::new(config1).expect("Failed to create engine");

    // Load second model with different config
    let config2 = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("model2")
        .with_normalization_mode(NormalizationMode::None) // Different config
        .build()
        .unwrap();

    engine
        .load_model(config2)
        .expect("Failed to load second model");

    // Test that both models are listed
    let models = engine.list_models();
    assert_eq!(models.len(), 2);
    assert!(models.contains(&"model1".to_string()));
    assert!(models.contains(&"model2".to_string()));

    // Generate embeddings with both models
    let text = "Test text";
    let emb1 = engine
        .embed(Some("model1"), text)
        .expect("Failed with model1");
    let emb2 = engine
        .embed(Some("model2"), text)
        .expect("Failed with model2");

    assert!(!emb1.is_empty());
    assert!(!emb2.is_empty());

    // Clean up thread-local models before test ends
    engine.cleanup_thread_models();
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn test_error_handling() {
    init_test_tracing();
    let model_path = get_test_model_path();

    let config = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("test-model")
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).expect("Failed to create engine");

    // Test empty text
    let result = engine.embed(None, "");
    assert!(result.is_err());

    // Test non-existent model
    let result = engine.embed(Some("non-existent"), "test");
    assert!(result.is_err());

    // Clean up thread-local models before test ends
    engine.cleanup_thread_models();
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn test_granular_unload_operations() {
    init_test_tracing();
    let model_path = get_test_model_path();

    // Create engine with a model
    let config = EngineConfig::builder()
        .with_model_path(model_path.clone())
        .with_model_name("test-model")
        .build()
        .unwrap();

    let mut engine = EmbeddingEngine::new(config).expect("Failed to create engine");

    // Load a second model
    let config2 = EngineConfig::builder()
        .with_model_path(model_path.clone())
        .with_model_name("test-model-2")
        .build()
        .unwrap();

    engine
        .load_model(config2)
        .expect("Failed to load second model");

    // Verify both models are listed
    assert_eq!(engine.list_models().len(), 2);

    // Test drop_model_from_thread - model should still be registered
    engine
        .drop_model_from_thread("test-model")
        .expect("Failed to drop from thread");
    assert_eq!(engine.list_models().len(), 2); // Still registered

    // Should be able to use the model again (it will reload)
    let emb = engine
        .embed(Some("test-model"), "test text")
        .expect("Failed to embed after drop");
    assert!(!emb.is_empty());

    // Test unregister_model - removes from registry but not necessarily from thread
    engine
        .unregister_model("test-model-2")
        .expect("Failed to unregister");
    assert_eq!(engine.list_models().len(), 1); // Only one model left
    assert!(!engine.list_models().contains(&"test-model-2".to_string()));

    // Should not be able to use unregistered model
    let result = engine.embed(Some("test-model-2"), "test text");
    assert!(result.is_err());

    // Test full unload_model (backward compatibility)
    let config3 = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("test-model-3")
        .build()
        .unwrap();

    engine
        .load_model(config3)
        .expect("Failed to load third model");
    assert_eq!(engine.list_models().len(), 2);

    engine
        .unload_model("test-model-3")
        .expect("Failed to unload");
    assert_eq!(engine.list_models().len(), 1);
    assert!(!engine.list_models().contains(&"test-model-3".to_string()));

    // Should not be able to use unloaded model
    let result = engine.embed(Some("test-model-3"), "test text");
    assert!(result.is_err());
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn test_model_registration_checking() {
    init_test_tracing();
    let model_path = get_test_model_path();

    // Create engine with a model
    let config = EngineConfig::builder()
        .with_model_path(model_path.clone())
        .with_model_name("test-model")
        .build()
        .unwrap();

    let mut engine = EmbeddingEngine::new(config).expect("Failed to create engine");

    // Check model is registered
    assert!(engine.is_model_registered("test-model"));
    assert!(!engine.is_model_registered("non-existent"));

    // The first model (from EmbeddingEngine::new) is loaded immediately
    assert!(engine.is_model_loaded_in_thread("test-model"));

    // Generate embedding (model is already loaded)
    let _ = engine
        .embed(Some("test-model"), "test text")
        .expect("Failed to embed");

    // Model should still be loaded in thread
    assert!(engine.is_model_loaded_in_thread("test-model"));
    assert!(engine.is_model_registered("test-model"));

    // Drop from thread
    engine
        .drop_model_from_thread("test-model")
        .expect("Failed to drop from thread");
    assert!(!engine.is_model_loaded_in_thread("test-model"));
    assert!(engine.is_model_registered("test-model")); // Still registered

    // Unregister model
    engine
        .unregister_model("test-model")
        .expect("Failed to unregister");
    assert!(!engine.is_model_registered("test-model"));
    assert!(!engine.is_model_loaded_in_thread("test-model"));

    // Test with multiple models - additional models are lazy loaded
    let config1 = EngineConfig::builder()
        .with_model_path(model_path.clone())
        .with_model_name("model1")
        .build()
        .unwrap();

    let config2 = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("model2")
        .build()
        .unwrap();

    engine.load_model(config1).expect("Failed to load model1");
    engine.load_model(config2).expect("Failed to load model2");

    assert!(engine.is_model_registered("model1"));
    assert!(engine.is_model_registered("model2"));
    // Additional models loaded via load_model() are lazy - not loaded in thread yet
    assert!(!engine.is_model_loaded_in_thread("model1"));
    assert!(!engine.is_model_loaded_in_thread("model2"));

    // Load model1 in thread
    let _ = engine
        .embed(Some("model1"), "test")
        .expect("Failed to embed");
    assert!(engine.is_model_loaded_in_thread("model1"));
    assert!(!engine.is_model_loaded_in_thread("model2"));

    // Load model2 in thread
    let _ = engine
        .embed(Some("model2"), "test")
        .expect("Failed to embed");
    assert!(engine.is_model_loaded_in_thread("model1"));
    assert!(engine.is_model_loaded_in_thread("model2"));
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn test_pooling_strategies() {
    init_test_tracing();
    let model_path = get_test_model_path();

    // Test different pooling strategies
    let strategies = vec![
        PoolingStrategy::Mean,
        PoolingStrategy::Cls,
        PoolingStrategy::Max,
        PoolingStrategy::MeanSqrt,
    ];

    // Create initial engine
    let config_initial = EngineConfig::builder()
        .with_model_path(model_path.clone())
        .with_model_name("model-Mean")
        .with_pooling_strategy(PoolingStrategy::Mean)
        .build()
        .unwrap();

    let mut engine = EmbeddingEngine::new(config_initial).expect("Failed to create engine");

    // Test first strategy with initial model
    let text = "Testing pooling strategy";
    let embedding = engine
        .embed(None, text)
        .expect("Failed to generate embedding");
    assert!(
        !embedding.is_empty(),
        "Embedding empty for strategy {:?}",
        PoolingStrategy::Mean
    );

    // Test remaining strategies by loading new models
    for strategy in strategies.into_iter().skip(1) {
        let config = EngineConfig::builder()
            .with_model_path(model_path.clone())
            .with_model_name(format!("model-{strategy:?}"))
            .with_pooling_strategy(strategy)
            .build()
            .unwrap();

        engine.load_model(config).expect("Failed to load model");

        let embedding = engine
            .embed(Some(&format!("model-{strategy:?}")), text)
            .expect("Failed to generate embedding");

        assert!(
            !embedding.is_empty(),
            "Embedding empty for strategy {strategy:?}"
        );
    }
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn test_model_warmup() {
    init_test_tracing();
    let model_path = get_test_model_path();

    let config = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("test-model")
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).expect("Failed to create engine");

    // Warmup should not fail
    engine.warmup_model(None).expect("Warmup failed");

    // After warmup, embeddings should work
    let embedding = engine.embed(None, "test").expect("Failed after warmup");
    assert!(!embedding.is_empty());
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn test_model_info() {
    init_test_tracing();
    let model_path = get_test_model_path();

    let config = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("test-model")
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).expect("Failed to create engine");

    let info = engine
        .model_info("test-model")
        .expect("Failed to get model info");

    assert_eq!(info.name, "test-model");
    assert!(info.dimensions > 0);
    assert!(info.max_tokens > 0);
    assert!(info.model_size.unwrap_or(0) > 0);
}

#[test]
fn test_configuration_validation() {
    // Test invalid model path
    let result = EngineConfig::builder()
        .with_model_path("/non/existent/path.gguf")
        .with_model_name("test")
        .build();
    assert!(result.is_err());

    // Test empty model name
    let (_dir, model_path) = create_test_model_file();
    let result = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("")
        .build();
    assert!(result.is_err());
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn test_thread_safety() {
    use std::sync::Arc;
    use std::thread;

    let model_path = get_test_model_path();

    let config = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("test-model")
        .build()
        .unwrap();

    let engine = Arc::new(EmbeddingEngine::new(config).expect("Failed to create engine"));

    // Spawn multiple threads that use the same engine
    let handles: Vec<_> = (0..4)
        .map(|i| {
            let engine = engine.clone();
            thread::spawn(move || {
                let text = format!("Thread {i} test text");
                let embedding = engine.embed(None, &text).expect("Failed in thread");
                assert!(!embedding.is_empty());
            })
        })
        .collect();

    // Wait for all threads
    for handle in handles {
        handle.join().unwrap();
    }
}

/// Performance benchmark for single embedding
#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn bench_single_embedding() {
    let model_path = get_test_model_path();

    let config = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("test-model")
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).expect("Failed to create engine");

    // Warmup
    engine.warmup_model(None).expect("Warmup failed");

    let text = "This is a sample text for benchmarking embedding generation performance.";

    let start = std::time::Instant::now();
    let iterations = 10;

    for _ in 0..iterations {
        let _ = engine
            .embed(None, text)
            .expect("Failed to generate embedding");
    }

    let duration = start.elapsed();
    let avg_time = duration / iterations;

    println!("Average time per embedding: {avg_time:?}");

    // Assert performance target (adjust based on hardware)
    assert!(
        avg_time.as_millis() < 1000,
        "Embedding generation too slow: {avg_time:?}"
    );
}

// ============================================================================
// Phase 4: Batch Processing Tests
// ============================================================================

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn test_batch_processing_basic() {
    init_test_tracing();
    let model_path = get_test_model_path();

    let config = create_test_config(model_path, "test_batch");
    let engine = EmbeddingEngine::new(config).unwrap();

    // Test batch of texts
    let texts = vec![
        "First text for batch processing",
        "Second text with different content",
        "Third text to complete the batch",
    ];

    let embeddings = engine.embed_batch(Some("test_batch"), &texts).unwrap();

    // Verify batch results
    assert_eq!(embeddings.len(), texts.len());

    // Each embedding should have the same dimensions
    let dim = embeddings[0].len();
    assert!(dim > 0);
    for emb in &embeddings {
        assert_eq!(emb.len(), dim);
    }

    // Embeddings should be different for different texts
    assert_ne!(embeddings[0], embeddings[1]);
    assert_ne!(embeddings[1], embeddings[2]);
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn test_batch_processing_large() {
    init_test_tracing();
    let model_path = get_test_model_path();

    let config = create_test_config(model_path, "test_large_batch");
    let engine = EmbeddingEngine::new(config).unwrap();

    // Create a large batch
    let mut texts = Vec::new();
    for i in 0..100 {
        texts.push(format!("Test document number {i}"));
    }
    let text_refs: Vec<&str> = texts.iter().map(std::string::String::as_str).collect();

    let start = std::time::Instant::now();
    let embeddings = engine
        .embed_batch(Some("test_large_batch"), &text_refs)
        .unwrap();
    let duration = start.elapsed();

    println!("Processed {} texts in {:?}", texts.len(), duration);
    let text_count = u32::try_from(texts.len()).unwrap_or(u32::MAX);
    println!("Average time per text: {:?}", duration / text_count);

    assert_eq!(embeddings.len(), texts.len());
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn test_batch_processing_empty() {
    init_test_tracing();
    let model_path = get_test_model_path();

    let config = create_test_config(model_path, "test_empty_batch");
    let engine = EmbeddingEngine::new(config).unwrap();

    // Test empty batch
    let texts: Vec<&str> = vec![];
    let embeddings = engine
        .embed_batch(Some("test_empty_batch"), &texts)
        .unwrap();

    assert!(embeddings.is_empty());
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
fn test_batch_processing_single_item() {
    init_test_tracing();
    let model_path = get_test_model_path();

    let config = create_test_config(model_path, "test_single_batch");
    let engine = EmbeddingEngine::new(config).unwrap();

    // Test single item batch
    let texts = vec!["Single text item"];
    let batch_embeddings = engine
        .embed_batch(Some("test_single_batch"), &texts)
        .unwrap();

    // Compare with single embedding
    let single_embedding = engine.embed(Some("test_single_batch"), texts[0]).unwrap();

    assert_eq!(batch_embeddings.len(), 1);
    assert_eq!(batch_embeddings[0], single_embedding);
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
#[ignore = "Batch vs sequential processing produces slightly different results due to numerical precision differences in llama.cpp batching"]
fn test_batch_processing_order_preservation() {
    init_test_tracing();
    let model_path = get_test_model_path();

    let config = create_test_config(model_path, "test_order");
    let engine = EmbeddingEngine::new(config).unwrap();

    // Test that order is preserved
    let texts = vec![
        "Alpha text",
        "Beta text",
        "Gamma text",
        "Delta text",
        "Epsilon text",
    ];

    let batch_embeddings = engine.embed_batch(Some("test_order"), &texts).unwrap();

    // Get individual embeddings
    let mut individual_embeddings = Vec::new();
    for text in &texts {
        individual_embeddings.push(engine.embed(Some("test_order"), text).unwrap());
    }

    // Verify order is preserved
    assert_eq!(batch_embeddings.len(), individual_embeddings.len());
    for (batch_emb, individual_emb) in batch_embeddings.iter().zip(individual_embeddings.iter()) {
        assert_eq!(batch_emb, individual_emb);
    }
}

#[serial]
#[test_with::env(EMBELLAMA_TEST_MODEL)]
#[ignore = "Batch vs sequential processing produces slightly different results due to numerical precision differences in llama.cpp batching"]
fn test_batch_vs_sequential_performance() {
    init_test_tracing();
    let model_path = get_test_model_path();

    let config = create_test_config(model_path, "test_perf");
    let engine = EmbeddingEngine::new(config).unwrap();

    // Create test texts
    let mut texts = Vec::new();
    for i in 0..20 {
        texts.push(format!("Performance test document {i}"));
    }
    let text_refs: Vec<&str> = texts.iter().map(std::string::String::as_str).collect();

    // Measure sequential processing time
    let sequential_start = std::time::Instant::now();
    let mut sequential_embeddings = Vec::new();
    for text in &text_refs {
        sequential_embeddings.push(engine.embed(Some("test_perf"), text).unwrap());
    }
    let sequential_duration = sequential_start.elapsed();

    // Measure batch processing time
    let batch_start = std::time::Instant::now();
    let batch_embeddings = engine.embed_batch(Some("test_perf"), &text_refs).unwrap();
    let batch_duration = batch_start.elapsed();

    println!("Sequential processing: {sequential_duration:?}");
    println!("Batch processing: {batch_duration:?}");
    println!(
        "Speedup: {:.2}x",
        sequential_duration.as_secs_f64() / batch_duration.as_secs_f64()
    );

    // Verify results are the same
    assert_eq!(batch_embeddings.len(), sequential_embeddings.len());
    for (batch_emb, seq_emb) in batch_embeddings.iter().zip(sequential_embeddings.iter()) {
        assert_eq!(batch_emb, seq_emb);
    }

    // > NOTE: Batch processing should be faster for tokenization and post-processing
    // Model inference remains sequential due to !Send constraint
}