realizar 0.8.5

Pure Rust ML inference engine built from scratch - model serving for GGUF and safetensors
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

    // =========================================================================
    // run_visualization
    // =========================================================================

    #[test]
    fn test_run_visualization_with_color() {
        // Should not panic
        run_visualization(true, 50);
    }

    #[test]
    fn test_run_visualization_without_color() {
        run_visualization(false, 50);
    }

    #[test]
    fn test_run_visualization_small_samples() {
        run_visualization(false, 10);
    }

    #[test]
    fn test_run_visualization_large_samples() {
        run_visualization(true, 200);
    }

    // =========================================================================
    // run_convoy_test
    // =========================================================================

    #[test]
    fn test_run_convoy_test_no_args() {
        let result = run_convoy_test(None, None, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_convoy_test_with_runtime() {
        let result = run_convoy_test(Some("test-runtime".to_string()), None, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_convoy_test_with_model() {
        let result = run_convoy_test(None, Some("test-model".to_string()), None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_convoy_test_with_output() {
        let dir = tempfile::tempdir().expect("dir");
        let output_path = dir.path().join("convoy_results.json");
        let result = run_convoy_test(None, None, Some(output_path.to_string_lossy().to_string()));
        assert!(result.is_ok());
        assert!(output_path.exists());
        let contents = std::fs::read_to_string(&output_path).expect("contents");
        assert!(contents.contains("baseline_short_p99_ms"));
    }

    #[test]
    fn test_run_convoy_test_all_args() {
        let dir = tempfile::tempdir().expect("dir");
        let output_path = dir.path().join("convoy_all.json");
        let result = run_convoy_test(
            Some("realizar".to_string()),
            Some("llama".to_string()),
            Some(output_path.to_string_lossy().to_string()),
        );
        assert!(result.is_ok());
    }

    // =========================================================================
    // run_saturation_test
    // =========================================================================

    #[test]
    fn test_run_saturation_test_no_args() {
        let result = run_saturation_test(None, None, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_saturation_test_with_runtime() {
        let result = run_saturation_test(Some("test-runtime".to_string()), None, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_saturation_test_with_model() {
        let result = run_saturation_test(None, Some("test-model".to_string()), None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_saturation_test_with_output() {
        let dir = tempfile::tempdir().expect("dir");
        let output_path = dir.path().join("saturation_results.json");
        let result =
            run_saturation_test(None, None, Some(output_path.to_string_lossy().to_string()));
        assert!(result.is_ok());
        assert!(output_path.exists());
        let contents = std::fs::read_to_string(&output_path).expect("contents");
        assert!(contents.contains("baseline_throughput"));
    }

    // =========================================================================
    // parse_cargo_bench_output
    // =========================================================================

    #[test]
    fn test_parse_cargo_bench_output_valid_lines() {
        let output = "test benchmark_matmul ... bench:      1,234 ns/iter (+/- 56)\n\
                       test benchmark_softmax ... bench:        789 ns/iter (+/- 12)";
        let results = parse_cargo_bench_output(output, Some("tensor_ops"));
        assert_eq!(results.len(), 2);
        assert_eq!(results[0]["name"], "benchmark_matmul");
        assert_eq!(results[0]["time_ns"], 1234);
        assert_eq!(results[0]["suite"], "tensor_ops");
        assert_eq!(results[1]["name"], "benchmark_softmax");
        assert_eq!(results[1]["time_ns"], 789);
    }

    #[test]
    fn test_parse_cargo_bench_output_no_suite() {
        let output = "test bench_add ... bench:        100 ns/iter (+/- 5)";
        let results = parse_cargo_bench_output(output, None);
        assert_eq!(results.len(), 1);
        assert!(results[0]["suite"].is_null());
    }

    #[test]
    fn test_parse_cargo_bench_output_empty() {
        let results = parse_cargo_bench_output("", None);
        assert!(results.is_empty());
    }

    #[test]
    fn test_parse_cargo_bench_output_no_bench_lines() {
        let output = "Compiling realizar v0.3.5\nFinished release\nrunning 5 tests";
        let results = parse_cargo_bench_output(output, None);
        assert!(results.is_empty());
    }

    #[test]
    fn test_parse_cargo_bench_output_partial_match() {
        // Has "bench:" but not "ns/iter"
        let output = "test partial ... bench: 100 ms/iter (+/- 5)";
        let results = parse_cargo_bench_output(output, None);
        assert!(results.is_empty());
    }

    #[test]
    fn test_parse_cargo_bench_output_mixed_lines() {
        let output = "Compiling realizar v0.3.5\n\
                       test bench_fast ... bench:        50 ns/iter (+/- 2)\n\
                       running 1 test\n\
                       test bench_slow ... bench:      5,000 ns/iter (+/- 100)\n\
                       test result: ok. 0 passed";
        let results = parse_cargo_bench_output(output, Some("all"));
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_parse_cargo_bench_output_large_numbers() {
        let output = "test bench_huge ... bench: 1,234,567 ns/iter (+/- 1,000)";
        let results = parse_cargo_bench_output(output, None);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0]["time_ns"], 1234567);
    }

    #[test]
    fn test_parse_cargo_bench_output_unparseable_time() {
        // Time has non-numeric chars
        let output = "test bench_bad ... bench:      abc ns/iter (+/- 5)";
        let results = parse_cargo_bench_output(output, None);
        assert!(results.is_empty());
    }

    // =========================================================================
    // run_benchmarks (list mode only, since actual bench needs cargo)
    // =========================================================================

    #[test]
    fn test_run_benchmarks_list_mode() {
        let result = run_benchmarks(None, true, None, None, None, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_benchmarks_external_no_feature() {
        // Without bench-http feature, should return error
        let result = run_benchmarks(
            Some("all".to_string()),
            false,
            Some("ollama".to_string()),
            None,
            Some("http://localhost:11434".to_string()),
            None,
        );
        assert!(result.is_err());
    }

    // =========================================================================
    // Helper: create valid benchmark JSON
    // =========================================================================

    fn create_test_benchmark_json(runtime: &str) -> String {
        use crate::bench::*;
        let result = FullBenchmarkResult {
            version: "1.1".to_string(),
            timestamp: "2025-12-09T12:00:00Z".to_string(),
            config: BenchmarkConfig {
                model: "test".to_string(),
                format: "apr".to_string(),
                quantization: "q4_k".to_string(),
                runtime: runtime.to_string(),
                runtime_version: "1.0.0".to_string(),
            },
            hardware: HardwareSpec::default(),
            sampling: SamplingConfig::default(),
            thermal: ThermalInfo::default(),
            results: BenchmarkResults {
                ttft_ms: TtftResults {
                    p50: 7.0,
                    p95: 9.0,
                    p99: 10.0,
                    p999: 12.0,
                },
                itl_ms: ItlResults {
                    median: 10.0,
                    std_dev: 2.0,
                    p99: 15.0,
                },
                throughput_tok_s: ThroughputResults {
                    median: 100.0,
                    ci_95: (95.0, 105.0),
                },
                memory_mb: MemoryResults {
                    model_mb: 512,
                    peak_rss_mb: 1024,
                    kv_waste_pct: 3.0,
                },
                energy: EnergyResults {
                    total_joules: 50.0,
                    token_joules: 0.5,
                    idle_watts: 8.0,
                },
                cold_start_ms: ColdStartResults {
                    median: 100.0,
                    p99: 150.0,
                },
            },
            quality: QualityValidation {
                kl_divergence_vs_fp32: 0.03,
                perplexity_wikitext2: Some(5.89),
            },
        };
        serde_json::to_string_pretty(&result).expect("serialization")
    }

    // =========================================================================
    // run_bench_compare
    // =========================================================================

    #[test]
    fn test_run_bench_compare_valid_files() {
        let dir = tempfile::tempdir().expect("dir");
        let file1 = dir.path().join("baseline.json");
        let file2 = dir.path().join("current.json");

        let json = create_test_benchmark_json("realizar");
        std::fs::write(&file1, &json).expect("expected value");
        std::fs::write(&file2, &json).expect("expected value");

        let result = run_bench_compare(&file1.to_string_lossy(), &file2.to_string_lossy(), 5.0);
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_bench_compare_nonexistent_file1() {
        let result = run_bench_compare("/nonexistent/file1.json", "/nonexistent/file2.json", 5.0);
        assert!(result.is_err());
    }

    #[test]
    fn test_run_bench_compare_invalid_json() {
        let dir = tempfile::tempdir().expect("dir");
        let file1 = dir.path().join("bad1.json");
        let file2 = dir.path().join("bad2.json");
        std::fs::write(&file1, "not valid json").expect("expected value");
        std::fs::write(&file2, "also bad").expect("expected value");

        let result = run_bench_compare(&file1.to_string_lossy(), &file2.to_string_lossy(), 5.0);
        assert!(result.is_err());
    }

    #[test]
    fn test_run_bench_compare_file1_valid_file2_invalid() {
        let dir = tempfile::tempdir().expect("dir");
        let file1 = dir.path().join("good.json");
        let file2 = dir.path().join("bad.json");

        let json = create_test_benchmark_json("realizar");
        std::fs::write(&file1, &json).expect("expected value");
        std::fs::write(&file2, "not json").expect("expected value");

        let result = run_bench_compare(&file1.to_string_lossy(), &file2.to_string_lossy(), 5.0);
        assert!(result.is_err());
    }

    #[test]
    fn test_run_bench_compare_zero_threshold() {
        let dir = tempfile::tempdir().expect("dir");
        let file1 = dir.path().join("base.json");
        let file2 = dir.path().join("curr.json");

        let json = create_test_benchmark_json("realizar");
        std::fs::write(&file1, &json).expect("expected value");
        std::fs::write(&file2, &json).expect("expected value");

        let result = run_bench_compare(&file1.to_string_lossy(), &file2.to_string_lossy(), 0.0);
        assert!(result.is_ok());
    }

    // =========================================================================
    // run_bench_regression
    // =========================================================================

    #[test]
    fn test_run_bench_regression_no_regression() {
        let dir = tempfile::tempdir().expect("dir");
        let baseline = dir.path().join("baseline.json");
        let current = dir.path().join("current.json");

        let json = create_test_benchmark_json("realizar");
        std::fs::write(&baseline, &json).expect("expected value");
        std::fs::write(&current, &json).expect("expected value");

        let result = run_bench_regression(
            &baseline.to_string_lossy(),
            &current.to_string_lossy(),
            false, // strict
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_bench_regression_strict_mode() {
        let dir = tempfile::tempdir().expect("dir");
        let baseline = dir.path().join("baseline.json");
        let current = dir.path().join("current.json");

        let json = create_test_benchmark_json("realizar");
        std::fs::write(&baseline, &json).expect("expected value");
        std::fs::write(&current, &json).expect("expected value");

        // Strict mode with identical data
        let result = run_bench_regression(
            &baseline.to_string_lossy(),
            &current.to_string_lossy(),
            true, // strict
        );
        // May pass or fail depending on threshold check
        let _ = result;
    }

    #[test]
    fn test_run_bench_regression_nonexistent() {
        let result = run_bench_regression(
            "/nonexistent/baseline.json",
            "/nonexistent/current.json",
            false, // strict
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_run_bench_regression_invalid_json() {
        let dir = tempfile::tempdir().expect("dir");
        let baseline = dir.path().join("baseline.json");
        let current = dir.path().join("current.json");
        std::fs::write(&baseline, "bad json").expect("expected value");
        std::fs::write(&current, "also bad").expect("expected value");

        let result = run_bench_regression(
            &baseline.to_string_lossy(),
            &current.to_string_lossy(),
            false, // strict
        );
        assert!(result.is_err());
    }

    // =========================================================================
    // print_info
    // =========================================================================

    #[test]
    fn test_print_info_does_not_panic() {
        print_info();
    }

    // =========================================================================
    // home_dir
    // =========================================================================

    #[test]
    fn test_home_dir_returns_some() {
        // HOME is typically set in test environments
        let result = home_dir();
        // May or may not be Some depending on environment
        let _ = result;
    }

    // =========================================================================
    // validate_suite_name
    // =========================================================================

    #[test]
    fn test_validate_suite_name_valid() {
        assert!(validate_suite_name("tensor_ops"));
        assert!(validate_suite_name("inference"));
        assert!(validate_suite_name("cache"));
        assert!(validate_suite_name("tokenizer"));
        assert!(validate_suite_name("quantize"));
        assert!(validate_suite_name("lambda"));
        assert!(validate_suite_name("comparative"));
    }

    #[test]
    fn test_validate_suite_name_invalid() {
        assert!(!validate_suite_name("nonexistent"));
        assert!(!validate_suite_name(""));
        assert!(!validate_suite_name("TENSOR_OPS")); // case sensitive
        assert!(!validate_suite_name("tensor_ops ")); // trailing space
    }