#[test]
#[ignore = "Requires llama.cpp server at localhost:8082"]
fn test_benchmark_runner_llamacpp() {
let config = HttpBenchmarkConfig {
cv_criterion: CvStoppingCriterion::new(3, 5, 0.50), warmup_iterations: 1,
prompt: "Hello".to_string(),
max_tokens: 10,
temperature: 0.1,
run_preflight: false, filter_outliers: false,
outlier_k_factor: 3.0,
};
let mut runner = HttpBenchmarkRunner::new(config);
let result = runner
.benchmark_llamacpp("http://localhost:8082")
.expect("Benchmark failed - is llama.cpp running?");
assert!(result.sample_count >= 3);
assert!(result.mean_latency_ms > 0.0);
assert!(result.throughput_tps > 0.0);
println!("llama.cpp Benchmark Results:");
println!(" Samples: {}", result.sample_count);
println!(" Filtered Samples: {}", result.filtered_sample_count);
println!(" Mean: {:.2}ms", result.mean_latency_ms);
println!(" P50: {:.2}ms", result.p50_latency_ms);
println!(" P99: {:.2}ms", result.p99_latency_ms);
println!(" TPS: {:.2}", result.throughput_tps);
println!(" CV: {:.4}", result.cv_at_stop);
println!(" Converged: {}", result.cv_converged);
println!(" Quality Metrics: {:?}", result.quality_metrics);
}
#[test]
fn test_preflight_checks_passed_empty_initially() {
let runner = HttpBenchmarkRunner::with_defaults();
assert!(runner.preflight_checks_passed().is_empty());
}
#[test]
fn test_quality_metrics_in_result() {
let latencies = vec![100.0, 105.0, 95.0, 100.0, 100.0];
let throughputs = vec![50.0, 48.0, 52.0, 50.0, 50.0];
let cold_start = 110.0;
let cv_threshold = 0.10;
let result =
HttpBenchmarkRunner::compute_results(&latencies, &throughputs, cold_start, cv_threshold);
assert!(result.quality_metrics.cv_at_stop < 0.10);
assert!(result.quality_metrics.cv_converged);
assert_eq!(result.quality_metrics.outliers_detected, 0);
assert!(result.quality_metrics.preflight_checks_passed.is_empty());
}
#[test]
fn test_filtered_samples_in_result() {
let latencies = vec![100.0, 105.0, 95.0];
let throughputs = vec![];
let result = HttpBenchmarkRunner::compute_results(&latencies, &throughputs, 100.0, 0.10);
assert_eq!(
result.latency_samples.len(),
result.latency_samples_filtered.len()
);
assert_eq!(result.sample_count, result.filtered_sample_count);
}
#[test]
#[ignore = "Requires running llama.cpp server on port 8082"]
fn test_imp_144a_llamacpp_real_throughput() {
let config = HttpBenchmarkConfig {
cv_criterion: CvStoppingCriterion::new(3, 10, 0.20),
warmup_iterations: 1,
prompt: "Hello".to_string(),
max_tokens: 10,
temperature: 0.0, ..Default::default()
};
let mut runner = HttpBenchmarkRunner::new(config);
let result = runner
.benchmark_llamacpp("http://127.0.0.1:8082")
.expect("IMP-144a: Should get llama.cpp benchmark result");
assert!(
result.throughput_tps > 0.0,
"IMP-144a: llama.cpp throughput should be > 0, got {} tok/s",
result.throughput_tps
);
assert!(
result.throughput_tps > 10.0,
"IMP-144a: llama.cpp throughput should be > 10 tok/s, got {} tok/s",
result.throughput_tps
);
println!("\nIMP-144a: llama.cpp Real-World Benchmark Results:");
println!(" Throughput: {:.1} tok/s", result.throughput_tps);
println!(" P50 Latency: {:.1} ms", result.p50_latency_ms);
println!(" P99 Latency: {:.1} ms", result.p99_latency_ms);
println!(" Samples: {}", result.sample_count);
println!(" CV: {:.4}", result.cv_at_stop);
}
#[test]
#[ignore = "Requires running Ollama server on port 11434"]
fn test_imp_144b_ollama_real_throughput() {
let config = HttpBenchmarkConfig {
cv_criterion: CvStoppingCriterion::new(3, 10, 0.20),
warmup_iterations: 1,
prompt: "Hello".to_string(),
max_tokens: 10,
temperature: 0.0,
..Default::default()
};
let mut runner = HttpBenchmarkRunner::new(config);
let result = runner
.benchmark_ollama("http://127.0.0.1:11434", "phi2:2.7b")
.expect("IMP-144b: Should get Ollama benchmark result");
assert!(
result.throughput_tps > 0.0,
"IMP-144b: Ollama throughput should be > 0, got {} tok/s",
result.throughput_tps
);
assert!(
result.throughput_tps > 10.0,
"IMP-144b: Ollama throughput should be > 10 tok/s, got {} tok/s",
result.throughput_tps
);
println!("\nIMP-144b: Ollama Real-World Benchmark Results:");
println!(" Throughput: {:.1} tok/s", result.throughput_tps);
println!(" P50 Latency: {:.1} ms", result.p50_latency_ms);
println!(" P99 Latency: {:.1} ms", result.p99_latency_ms);
println!(" Samples: {}", result.sample_count);
println!(" CV: {:.4}", result.cv_at_stop);
}
#[test]
fn test_imp_144c_throughput_comparison_logic() {
let llamacpp_tps = 256.0; let ollama_tps = 143.0; let realizar_tps = 80.0;
let gap_vs_llamacpp = llamacpp_tps / realizar_tps;
let gap_vs_ollama = ollama_tps / realizar_tps;
assert!(
gap_vs_ollama > 1.0 && gap_vs_ollama < 3.0,
"IMP-144c: Gap to Ollama should be ~1.5-1.8x, got {:.1}x",
gap_vs_ollama
);
assert!(
gap_vs_llamacpp > 2.0 && gap_vs_llamacpp < 5.0,
"IMP-144c: Gap to llama.cpp should be ~3x, got {:.1}x",
gap_vs_llamacpp
);
println!("\nIMP-144c: Throughput Gap Analysis:");
println!(" Realizar: {:.1} tok/s", realizar_tps);
println!(
" Ollama: {:.1} tok/s ({:.1}x gap)",
ollama_tps, gap_vs_ollama
);
println!(
" llama.cpp: {:.1} tok/s ({:.1}x gap)",
llamacpp_tps, gap_vs_llamacpp
);
}
#[test]
fn test_imp_144d_cv_stopping_for_throughput() {
let throughputs = vec![100.0, 102.0, 98.0, 101.0, 99.0];
let latencies = vec![10.0, 9.8, 10.2, 10.0, 10.0];
let result = HttpBenchmarkRunner::compute_results(&latencies, &throughputs, 12.0, 0.05);
assert!(
result.cv_converged,
"IMP-144d: CV should converge for stable throughput, cv={:.4}",
result.cv_at_stop
);
let expected_mean_tps = throughputs.iter().sum::<f64>() / throughputs.len() as f64;
assert!(
(result.throughput_tps - expected_mean_tps).abs() < 1.0,
"IMP-144d: Mean TPS should be ~{:.1}, got {:.1}",
expected_mean_tps,
result.throughput_tps
);
}
#[test]
fn test_imp_145a_deterministic_config_structure() {
let config = HttpBenchmarkConfig {
temperature: 0.0,
..Default::default()
};
assert_eq!(
config.temperature, 0.0,
"IMP-145a: Deterministic config should have temperature=0"
);
}
#[test]
fn test_imp_145b_local_determinism() {
let latencies = vec![100.0, 100.0, 100.0];
let throughputs = vec![50.0, 50.0, 50.0];
let result1 = HttpBenchmarkRunner::compute_results(&latencies, &throughputs, 100.0, 0.10);
let result2 = HttpBenchmarkRunner::compute_results(&latencies, &throughputs, 100.0, 0.10);
assert_eq!(
result1.mean_latency_ms, result2.mean_latency_ms,
"IMP-145b: Same inputs should produce identical mean latency"
);
assert_eq!(
result1.throughput_tps, result2.throughput_tps,
"IMP-145b: Same inputs should produce identical throughput"
);
}
#[test]
#[ignore = "Requires running llama.cpp server on port 8082"]
fn test_imp_145c_llamacpp_deterministic_output() {
let client = ModelHttpClient::with_timeout(30);
let request = CompletionRequest {
model: "default".to_string(),
prompt: "What is 2+2? Answer with just the number:".to_string(),
max_tokens: 5,
temperature: Some(0.0), stream: false,
};
let result1 = client
.llamacpp_completion("http://127.0.0.1:8082", &request)
.expect("IMP-145c: First llama.cpp call should succeed");
let result2 = client
.llamacpp_completion("http://127.0.0.1:8082", &request)
.expect("IMP-145c: Second llama.cpp call should succeed");
assert_eq!(
result1.text, result2.text,
"IMP-145c: llama.cpp should produce identical output in deterministic mode. \
Got '{}' vs '{}'",
result1.text, result2.text
);
println!("\nIMP-145c: llama.cpp Determinism Verification:");
println!(" Prompt: '{}'", request.prompt);
println!(" Output 1: '{}'", result1.text.trim());
println!(" Output 2: '{}'", result2.text.trim());
println!(" Match: {}", result1.text == result2.text);
}
#[test]
#[ignore = "Requires running Ollama server on port 11434"]
fn test_imp_145d_ollama_deterministic_output() {
let client = ModelHttpClient::with_timeout(30);
let request = OllamaRequest {
model: "phi2:2.7b".to_string(),
prompt: "What is 2+2? Answer with just the number:".to_string(),
stream: false,
options: Some(OllamaOptions {
num_predict: Some(5),
temperature: Some(0.0), }),
};
let result1 = client
.ollama_generate("http://127.0.0.1:11434", &request)
.expect("IMP-145d: First Ollama call should succeed");
let result2 = client
.ollama_generate("http://127.0.0.1:11434", &request)
.expect("IMP-145d: Second Ollama call should succeed");
assert_eq!(
result1.text, result2.text,
"IMP-145d: Ollama should produce identical output in deterministic mode. \
Got '{}' vs '{}'",
result1.text, result2.text
);
println!("\nIMP-145d: Ollama Determinism Verification:");
println!(" Prompt: '{}'", request.prompt);
println!(" Output 1: '{}'", result1.text.trim());
println!(" Output 2: '{}'", result2.text.trim());
println!(" Match: {}", result1.text == result2.text);
}
#[derive(Debug, Clone)]
pub struct ThroughputBaseline {
pub server: String,
pub throughput_tps: f64,
pub p50_latency_ms: f64,
pub p99_latency_ms: f64,
pub cv: f64,
pub samples: usize,
}
#[test]
fn test_imp_146a_baseline_struct() {
let baseline = ThroughputBaseline {
server: "llama.cpp".to_string(),
throughput_tps: 256.0,
p50_latency_ms: 162.0,
p99_latency_ms: 290.0,
cv: 0.045,
samples: 10,
};
assert_eq!(baseline.server, "llama.cpp");
assert!((baseline.throughput_tps - 256.0).abs() < 0.1);
assert!((baseline.p50_latency_ms - 162.0).abs() < 0.1);
assert!((baseline.cv - 0.045).abs() < 0.001);
assert_eq!(baseline.samples, 10);
}
#[derive(Debug, Clone)]
pub struct GapAnalysis {
pub realizar: ThroughputBaseline,
pub reference: ThroughputBaseline,
pub gap_ratio: f64,
pub throughput_gap_tps: f64,
pub parity_target_tps: f64,
}