#![allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::float_cmp
)]
use super::{cosine_similarity_native, dot_product_native, warmup_simd_cache};
#[test]
#[ignore = "performance test - run with --ignored or PERF_TESTS=1"]
fn test_warmup_reduces_first_request_latency() {
warmup_simd_cache();
let size = 768;
let a: Vec<f32> = (0..size).map(|i| ((i * 7) % 100) as f32 * 0.01).collect();
let b: Vec<f32> = (0..size)
.map(|i| (((size - i) * 13) % 100) as f32 * 0.01)
.collect();
let start = std::time::Instant::now();
let _ = dot_product_native(&a, &b);
let first_call_ns = start.elapsed().as_nanos() as f64;
assert!(
first_call_ns < 250.0,
"First call after warmup too slow: {:.2}ns (should be < 250ns)",
first_call_ns
);
let result = dot_product_native(&a, &b);
let expected: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
assert!(
(result - expected).abs() < 1e-3,
"Warmup affected correctness: got {}, expected {}",
result,
expected
);
}
#[test]
fn test_warmup_idempotent() {
warmup_simd_cache();
warmup_simd_cache();
warmup_simd_cache();
let a: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0];
let b: Vec<f32> = vec![5.0, 6.0, 7.0, 8.0];
let result = dot_product_native(&a, &b);
assert!(
(result - 70.0).abs() < 1e-6,
"Multiple warmups broke correctness"
);
}
#[test]
fn test_warmup_all_functions() {
warmup_simd_cache();
let a: Vec<f32> = (0..768).map(|i| i as f32 * 0.01).collect();
let b: Vec<f32> = (0..768).map(|i| (767 - i) as f32 * 0.01).collect();
let dot_result = dot_product_native(&a, &b);
assert!(dot_result > 0.0, "Dot product after warmup failed");
let cos_result = cosine_similarity_native(&a, &b);
assert!(
(-1.0..=1.0).contains(&cos_result),
"Cosine after warmup failed"
);
}