#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity055a_throughput_methodology() {
println!("PARITY-055a: Throughput Calculation Methodology");
println!("===============================================");
println!();
println!(" BASELINE SINGLE-REQUEST:");
println!(" - Current: 64 tok/s (CPU KV cache path)");
println!(" - Limited by sequential token generation");
println!(" - Each token requires full forward pass");
println!();
println!(" BATCH THROUGHPUT MODEL:");
println!(" throughput = single_tok_s * batch_size");
println!();
println!(" CALCULATIONS:");
let single_tok_s = 64.0_f64;
for batch_size in [1, 2, 4, 8, 16, 32] {
let throughput = single_tok_s * batch_size as f64;
let parity = if throughput >= 192.0 { "✅ M4" } else { "❌" };
println!(
" batch={:2}: {:4.0} tok/s {}",
batch_size, throughput, parity
);
}
println!();
println!(" M4 PARITY THRESHOLD:");
println!(" Target: 192 tok/s");
println!(" Achieved at: batch >= 3");
println!(" Optimal at: batch = 16 (1024 tok/s)");
assert_eq!(
(single_tok_s * 3.0) as usize,
192,
"PARITY-055a: M4 parity at batch=3"
);
assert_eq!(
(single_tok_s * 16.0) as usize,
1024,
"PARITY-055a: Optimal batch throughput"
);
}
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity055b_benchmark_config() {
println!("PARITY-055b: Benchmark Configuration");
println!("====================================");
println!();
println!(" LOAD GENERATOR:");
println!(" Tool: ab (Apache Bench) or wrk");
println!(" Concurrency: -c 16 (match batch size)");
println!(" Requests: -n 1000 (statistically significant)");
println!(" Keep-alive: -k (reuse connections)");
println!();
println!(" SERVER CONFIGURATION:");
println!(" BatchConfig::default():");
println!(" window_ms: 10");
println!(" min_batch: 4");
println!(" optimal_batch: 16");
println!(" max_batch: 32");
println!(" queue_size: 1024");
println!();
println!(" BENCHMARK COMMAND:");
println!(" # Start server with batch enabled");
println!(" cargo run --release -- serve --batch");
println!();
println!(" # Run benchmark");
println!(" ab -n 1000 -c 16 -k -p payload.json \\");
println!(" -T application/json http://localhost:8080/v1/completions");
println!();
println!(" METRICS TO COLLECT:");
println!(" - Requests/second (total throughput)");
println!(" - Mean response time (latency)");
println!(" - p95/p99 latency (tail latency)");
println!(" - tokens/second = requests/sec * avg_tokens_per_request");
let requests_per_sec = 64.0_f64; let avg_tokens = 10.0_f64;
let tokens_per_sec = requests_per_sec * avg_tokens;
assert!(
tokens_per_sec >= 192.0,
"PARITY-055b: Throughput model valid"
);
}
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity055c_latency_tradeoff() {
println!("PARITY-055c: Latency vs Throughput Tradeoff");
println!("==========================================");
println!();
println!(" SINGLE-REQUEST (batch disabled):");
println!(" Latency: ~15ms per request");
println!(" Throughput: 64 tok/s");
println!(" Best for: Interactive use, low-latency requirements");
println!();
println!(" BATCH MODE (batch enabled):");
println!(" Latency: 15-25ms (includes batch window)");
println!(" Throughput: 192-1024 tok/s");
println!(" Best for: High-volume APIs, batch processing");
println!();
println!(" PRESETS:");
println!(" low_latency:");
println!(" window_ms: 5");
println!(" optimal_batch: 8");
println!(" Expected latency: +5ms");
println!();
println!(" high_throughput:");
println!(" window_ms: 20");
println!(" optimal_batch: 32");
println!(" Expected latency: +20ms");
println!();
println!(" TRADEOFF CURVE:");
let base_latency = 15.0_f64; for (name, window, batch) in [
("single", 0, 1),
("low_latency", 5, 8),
("default", 10, 16),
("high_throughput", 20, 32),
] {
let latency = base_latency + window as f64;
let throughput = 64.0 * batch as f64;
println!(
" {:16} latency={:2}ms throughput={:4.0} tok/s",
name, latency as i32, throughput
);
}
assert!(
64.0 * 16.0 >= 192.0,
"PARITY-055c: Default config achieves M4 parity"
);
}
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity055d_concurrent_estimation() {
println!("PARITY-055d: Concurrent Batch Estimation");
println!("========================================");
println!();
println!(" CURRENT IMPLEMENTATION:");
println!(" - Concurrent: parallel tokio tasks per request");
println!(" - Each request calls generate_with_cache() independently");
println!(" - CPU-bound operations interleave across cores");
println!();
println!(" THROUGHPUT SCALING:");
let single_tok_s = 64.0_f64;
let cpu_cores = 16; println!(" Cores available: {}", cpu_cores);
println!(" Single-core: {:.0} tok/s", single_tok_s);
println!();
println!(" SCALING FACTORS:");
println!(" batch=1: {:.0} tok/s (no parallelism)", single_tok_s);
println!(
" batch=4: {:.0} tok/s (4x parallel)",
single_tok_s * 4.0
);
println!(
" batch=8: {:.0} tok/s (8x parallel)",
single_tok_s * 8.0
);
println!(
" batch=16: {:.0} tok/s (16x parallel, CPU saturated)",
single_tok_s * 16.0
);
println!();
println!(" REALISTIC EXPECTATIONS:");
println!(" - Perfect scaling up to core count");
println!(" - Diminishing returns beyond CPU cores");
println!(" - Memory bandwidth may become bottleneck");
println!(" - Thermal throttling under sustained load");
let m4_target = 192.0;
let batch_for_parity = (m4_target / single_tok_s).ceil() as usize;
assert_eq!(
batch_for_parity, 3,
"PARITY-055d: Need batch>=3 for M4 parity"
);
}
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity055e_benchmark_script() {
println!("PARITY-055e: Benchmark Execution Script");
println!("=======================================");
println!();
println!(" SCRIPT: scripts/bench-batch-throughput.sh");
println!();
println!(" #!/bin/bash");
println!(" set -e");
println!();
println!(" # Build release");
println!(" cargo build --release --features cuda");
println!();
println!(" # Create payload");
println!(" cat > /tmp/payload.json << 'EOF'");
println!(" {{\"prompt\": \"Hello\", \"max_tokens\": 10}}");
println!(" EOF");
println!();
println!(" # Start server with batch mode (background)");
println!(" ./target/release/realizar serve --batch &");
println!(" SERVER_PID=$!");
println!(" sleep 2 # Wait for startup");
println!();
println!(" # Run benchmarks at different concurrency levels");
println!(" for C in 1 4 8 16 32; do");
println!(" echo \"=== Concurrency: $C ===\"");
println!(" ab -n 100 -c $C -k -p /tmp/payload.json \\");
println!(" -T application/json http://localhost:8080/v1/completions");
println!(" done");
println!();
println!(" # Cleanup");
println!(" kill $SERVER_PID");
println!();
println!(" OUTPUT METRICS:");
println!(" - Requests per second");
println!(" - Time per request");
println!(" - Transfer rate");
assert!(true, "PARITY-055e: Benchmark script documented");
}
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity055f_summary() {
println!("PARITY-055f: Batch Throughput Benchmark Summary");
println!("===============================================");
println!();
println!(" M4 PARITY TARGET: 192 tok/s");
println!();
println!(" THEORETICAL ANALYSIS:");
println!(" ✅ Single-request baseline: 64 tok/s");
println!(" ✅ Batch=3 achieves: 192 tok/s (M4 parity)");
println!(" ✅ Batch=16 achieves: 1024 tok/s (5.3x M4)");
println!(" ✅ Batch=32 achieves: 2048 tok/s (10.7x M4)");
println!();
println!(" IMPLEMENTATION STATUS:");
println!(" ✅ PARITY-052: Batch queue structs");
println!(" ✅ PARITY-053: Background processor");
println!(" ✅ PARITY-054: Handler integration");
println!(" ✅ PARITY-055: Benchmark methodology");
println!();
println!(" VALIDATION:");
println!(" Run: scripts/bench-batch-throughput.sh");
println!(" Expected: >192 tok/s at concurrency >= 4");
println!();
println!(" NEXT STEPS:");
println!(" PARITY-056: Execute benchmark, record results");
println!(" PARITY-057: Optimize based on results");
let single_tok_s = 64.0_f64;
let m4_target = 192.0_f64;
let min_batch = (m4_target / single_tok_s).ceil() as usize;
assert_eq!(min_batch, 3, "PARITY-055f: M4 parity requires batch >= 3");
assert!(
single_tok_s * 16.0 > m4_target * 5.0,
"PARITY-055f: Optimal batch exceeds 5x M4"
);
}
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity056a_benchmark_prerequisites() {
println!("PARITY-056a: Benchmark Execution Prerequisites");
println!("==============================================");
println!();
println!(" REQUIRED SOFTWARE:");
println!(" - ab (Apache Bench): apt install apache2-utils");
println!(" - OR wrk: apt install wrk");
println!(" - curl: for health checks");
println!();
println!(" SERVER BUILD:");
println!(" cargo build --release --features cuda");
println!();
println!(" MODEL REQUIREMENTS:");
println!(" - GGUF model file (any Q4_K quantized)");
println!(" - Sufficient RAM for model loading");
println!(" - RTX 4090 available for CUDA operations");
println!();
println!(" ENVIRONMENT:");
println!(" - CPU: 16+ cores for optimal batch scaling");
println!(" - RAM: 16GB+ for model + concurrent requests");
println!(" - No other heavy processes running");
println!();
println!(" VERIFICATION:");
println!(" # Check tools installed");
println!(" which ab wrk curl");
println!();
println!(" # Check GPU available");
println!(" nvidia-smi");
assert!(true, "PARITY-056a: Prerequisites documented");
}
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity056b_expected_results() {
println!("PARITY-056b: Expected Benchmark Results");
println!("=======================================");
println!();
println!(" BASELINE MEASUREMENTS (from PARITY-044 to PARITY-050):");
println!(" Single-request: 64 tok/s");
println!(" With KV cache: O(n) per token");
println!(" CPU SIMD optimized: AVX2 4-accumulator");
println!();
println!(" EXPECTED BATCH THROUGHPUT:");
let baseline = 64.0_f64;
let m4_target = 192.0_f64;
println!(" | Concurrency | Expected tok/s | M4 Status |");
println!(" |-------------|----------------|-----------|");
for c in [1, 2, 4, 8, 16, 32] {
let expected = baseline * c as f64;
let status = if expected >= m4_target {
"✅ PARITY"
} else {
"❌"
};
println!(" | {:>11} | {:>14.0} | {:>9} |", c, expected, status);
}
println!();
println!(" EXPECTED REQUEST LATENCY:");
println!(" | Concurrency | Expected ms |");
println!(" |-------------|-------------|");
for c in [1, 4, 16, 32] {
let base_ms = 15.0_f64;
let window_ms = 10.0_f64;
let overhead_per_req = 0.5_f64;
let expected_ms = base_ms + window_ms + (overhead_per_req * c as f64);
println!(" | {:>11} | {:>11.1} |", c, expected_ms);
}
println!();
println!(" M4 PARITY THRESHOLD:");
println!(" Target: {} tok/s", m4_target as i64);
println!(" Expected at: concurrency >= 3");
println!(" Comfortable at: concurrency >= 4");
assert!(baseline * 4.0 >= m4_target, "PARITY-056b: M4 parity at c=4");
}
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity056c_execution_steps() {
println!("PARITY-056c: Benchmark Execution Steps");
println!("======================================");
println!();
println!(" STEP 1: Start Server with Batch Mode");
println!(" # Terminal 1");
println!(" cargo run --release --features cuda -- serve --demo --batch");
println!();
println!(" STEP 2: Wait for Server Ready");
println!(" # Wait for 'Listening on' message");
println!(" curl -s http://localhost:8080/health | jq .");
println!();
println!(" STEP 3: Create Request Payload");
println!(" cat > /tmp/bench_payload.json << 'EOF'");
println!(" {{");
println!(" \"prompt\": \"Hello, world\",");
println!(" \"max_tokens\": 10,");
println!(" \"temperature\": 0.7");
println!(" }}");
println!(" EOF");
println!();
println!(" STEP 4: Run Benchmark at Each Concurrency Level");
println!(" for C in 1 4 8 16 32; do");
println!(" echo \"=== Concurrency: $C ===\"");
println!(" ab -n 100 -c $C -k \\");
println!(" -p /tmp/bench_payload.json \\");
println!(" -T application/json \\");
println!(" http://localhost:8080/v1/completions 2>&1 | \\");
println!(" grep -E '(Requests per second|Time per request|Transfer rate)'");
println!(" done");
println!();
println!(" STEP 5: Calculate Token Throughput");
println!(" # tokens/sec = requests/sec * avg_tokens_per_response");
println!(" # Example: 10 req/s * 10 tokens = 100 tok/s");
assert!(true, "PARITY-056c: Execution steps documented");
}
include!("parity056d_interpret.rs");
include!("parity058c_performance.rs");
include!("parity060b_draft_generate.rs");
include!("parity062a_benchmark.rs");
include!("parity063e_checklist.rs");