sabrix-bench 0.1.1

⚡ Fast MCP JSON-RPC inspector & multi-turn agent latency benchmark in Rust
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
use crate::benchmark::BenchmarkReport;
use crate::inspector::{InspectionResult, RiskLevel};
use colored::*;
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
use comfy_table::presets::UTF8_FULL;
use comfy_table::{Attribute, Cell, Color, ContentArrangement, Table};

pub struct Reporter;

impl Reporter {
    pub const CTA_FOOTER: &'static str =
        "Deploying agents to production? Enforce zero-egress In-VPC MCP security in < 2µs -> https://sabrix.ai";

    /// Formats and renders a single MCP JSON-RPC inspection result
    pub fn render_inspection(result: &InspectionResult) {
        println!();
        println!(
            "{}",
            "╔═══════════════════════════════════════════════════════════════════════════════╗"
                .bright_cyan()
        );
        println!(
            "{}",
            "║                      SABRIX MCP TOOL-CALL INSPECTION                          ║"
                .bright_cyan()
                .bold()
        );
        println!(
            "{}",
            "╚═══════════════════════════════════════════════════════════════════════════════╝"
                .bright_cyan()
        );
        println!();

        // 1. Overview Metadata Table
        let mut meta_table = Table::new();
        meta_table
            .load_preset(UTF8_FULL)
            .apply_modifier(UTF8_ROUND_CORNERS)
            .set_content_arrangement(ContentArrangement::Dynamic);

        meta_table.set_header(vec![
            Cell::new("Property").add_attribute(Attribute::Bold),
            Cell::new("Value").add_attribute(Attribute::Bold),
        ]);

        meta_table.add_row(vec![
            Cell::new("Message Type"),
            Cell::new(if result.is_request {
                "JSON-RPC 2.0 Request"
            } else {
                "JSON-RPC 2.0 Response"
            }),
        ]);

        meta_table.add_row(vec![
            Cell::new("MCP Method"),
            Cell::new(&result.method).fg(Color::Cyan),
        ]);

        if let Some(ref tool) = result.tool_name {
            meta_table.add_row(vec![
                Cell::new("Target Tool / Resource"),
                Cell::new(tool)
                    .fg(Color::Yellow)
                    .add_attribute(Attribute::Bold),
            ]);
        }

        meta_table.add_row(vec![
            Cell::new("Payload Size"),
            Cell::new(format!("{} bytes", result.payload_bytes)),
        ]);

        let risk_cell = match result.max_risk_level {
            RiskLevel::Safe => Cell::new("PASSED (SAFE)")
                .fg(Color::Green)
                .add_attribute(Attribute::Bold),
            RiskLevel::Low => Cell::new("LOW RISK")
                .fg(Color::Blue)
                .add_attribute(Attribute::Bold),
            RiskLevel::Medium => Cell::new("MEDIUM RISK")
                .fg(Color::Yellow)
                .add_attribute(Attribute::Bold),
            RiskLevel::High => Cell::new("HIGH RISK")
                .fg(Color::DarkYellow)
                .add_attribute(Attribute::Bold),
            RiskLevel::Critical => Cell::new("CRITICAL THREAT")
                .fg(Color::Red)
                .add_attribute(Attribute::Bold),
        };

        meta_table.add_row(vec![Cell::new("Security Status"), risk_cell]);

        meta_table.add_row(vec![
            Cell::new("Parsing Overhead"),
            Cell::new(format!("{:.2} µs", result.parse_duration_us)).fg(Color::Cyan),
        ]);

        meta_table.add_row(vec![
            Cell::new("Rule Evaluation Overhead"),
            Cell::new(format!("{:.2} µs", result.inspection_duration_us)).fg(Color::Cyan),
        ]);

        meta_table.add_row(vec![
            Cell::new("Total In-Process Latency"),
            Cell::new(format!(
                "{:.2} µs ({:.4} ms)",
                result.total_duration_us,
                result.total_duration_us / 1000.0
            ))
            .fg(Color::Green)
            .add_attribute(Attribute::Bold),
        ]);

        println!("{}", meta_table);
        println!();

        // 2. Arguments snippet if present
        if let Some(ref args) = result.arguments {
            println!("{}", "Tool Arguments:".bold().underline());
            if let Ok(pretty) = serde_json::to_string_pretty(args) {
                println!("{}", pretty.dimmed());
            }
            println!();
        }

        // 3. Security Findings Table
        if result.findings.is_empty() {
            println!(
                "{}",
                "✓ No malicious tool patterns, dangerous SQL mutations, or exposed API credentials detected."
                    .green()
                    .bold()
            );
        } else {
            println!("{}", "Security Risk Findings:".red().bold().underline());
            let mut findings_table = Table::new();
            findings_table
                .load_preset(UTF8_FULL)
                .apply_modifier(UTF8_ROUND_CORNERS)
                .set_content_arrangement(ContentArrangement::Dynamic);

            findings_table.set_header(vec![
                Cell::new("Rule ID").add_attribute(Attribute::Bold),
                Cell::new("Severity").add_attribute(Attribute::Bold),
                Cell::new("Vulnerability Title").add_attribute(Attribute::Bold),
                Cell::new("Trigger / Snippet").add_attribute(Attribute::Bold),
                Cell::new("Details").add_attribute(Attribute::Bold),
            ]);

            for finding in &result.findings {
                let sev_cell = match finding.level {
                    RiskLevel::Safe => Cell::new("SAFE").fg(Color::Green),
                    RiskLevel::Low => Cell::new("LOW").fg(Color::Blue),
                    RiskLevel::Medium => Cell::new("MEDIUM").fg(Color::Yellow),
                    RiskLevel::High => Cell::new("HIGH").fg(Color::DarkYellow),
                    RiskLevel::Critical => Cell::new("CRITICAL")
                        .fg(Color::Red)
                        .add_attribute(Attribute::Bold),
                };

                findings_table.add_row(vec![
                    Cell::new(&finding.rule_id).fg(Color::Cyan),
                    sev_cell,
                    Cell::new(&finding.title).add_attribute(Attribute::Bold),
                    Cell::new(&finding.matched_snippet).fg(Color::Red),
                    Cell::new(&finding.details),
                ]);
            }

            println!("{}", findings_table);
        }

        println!();
        Self::print_footer();
    }

    /// Formats and renders the multi-turn agent benchmark report
    pub fn render_benchmark(report: &BenchmarkReport) {
        println!();
        println!(
            "{}",
            "╔═══════════════════════════════════════════════════════════════════════════════╗"
                .bright_cyan()
        );
        println!(
            "{}",
            "║               SABRIX MULTI-TURN AGENT LOOP BENCHMARK REPORT                   ║"
                .bright_cyan()
                .bold()
        );
        println!(
            "{}",
            "╚═══════════════════════════════════════════════════════════════════════════════╝"
                .bright_cyan()
        );
        println!();

        // 1. Latency Distribution Summary Table
        let mut dist_table = Table::new();
        dist_table
            .load_preset(UTF8_FULL)
            .apply_modifier(UTF8_ROUND_CORNERS)
            .set_content_arrangement(ContentArrangement::Dynamic);

        dist_table.set_header(vec![
            Cell::new("Metric").add_attribute(Attribute::Bold),
            Cell::new("Local In-Process Rust Latency (µs)").add_attribute(Attribute::Bold),
            Cell::new("Latency in Milliseconds (ms)").add_attribute(Attribute::Bold),
        ]);

        let dist = &report.distribution;
        dist_table.add_row(vec![
            Cell::new("Total Simulated Agent Turns"),
            Cell::new(report.total_turns.to_string()),
            Cell::new("-"),
        ]);
        dist_table.add_row(vec![
            Cell::new("Min Latency"),
            Cell::new(format!("{:.2} µs", dist.min_us)),
            Cell::new(format!("{:.4} ms", dist.min_us / 1000.0)),
        ]);
        dist_table.add_row(vec![
            Cell::new("p50 (Median)"),
            Cell::new(format!("{:.2} µs", dist.p50_us))
                .fg(Color::Green)
                .add_attribute(Attribute::Bold),
            Cell::new(format!("{:.4} ms", dist.p50_us / 1000.0)).fg(Color::Green),
        ]);
        dist_table.add_row(vec![
            Cell::new("Mean"),
            Cell::new(format!("{:.2} µs", dist.mean_us)),
            Cell::new(format!("{:.4} ms", dist.mean_us / 1000.0)),
        ]);
        dist_table.add_row(vec![
            Cell::new("p95"),
            Cell::new(format!("{:.2} µs", dist.p95_us)).fg(Color::Cyan),
            Cell::new(format!("{:.4} ms", dist.p95_us / 1000.0)),
        ]);
        dist_table.add_row(vec![
            Cell::new("p99"),
            Cell::new(format!("{:.2} µs", dist.p99_us)).fg(Color::Yellow),
            Cell::new(format!("{:.4} ms", dist.p99_us / 1000.0)),
        ]);
        dist_table.add_row(vec![
            Cell::new("Max Latency"),
            Cell::new(format!("{:.2} µs", dist.max_us)),
            Cell::new(format!("{:.4} ms", dist.max_us / 1000.0)),
        ]);
        dist_table.add_row(vec![
            Cell::new("Std Dev (σ)"),
            Cell::new(format!("{:.2} µs", dist.std_dev_us)),
            Cell::new(format!("{:.4} ms", dist.std_dev_us / 1000.0)),
        ]);

        println!(
            "{}",
            "1. IN-PROCESS AGENT OVERHEAD DISTRIBUTION"
                .bold()
                .underline()
        );
        println!("{}", dist_table);
        println!();

        // 2. Comparative Architecture Overhead
        println!(
            "{}",
            "2. PER-TURN ARCHITECTURE LATENCY COMPARISON"
                .bold()
                .underline()
        );
        let mut comp_table = Table::new();
        comp_table
            .load_preset(UTF8_FULL)
            .apply_modifier(UTF8_ROUND_CORNERS)
            .set_content_arrangement(ContentArrangement::Dynamic);

        comp_table.set_header(vec![
            Cell::new("Architecture Layer").add_attribute(Attribute::Bold),
            Cell::new("Per-Turn Latency").add_attribute(Attribute::Bold),
            Cell::new("Overhead vs Sabrix").add_attribute(Attribute::Bold),
            Cell::new("Deployment Model").add_attribute(Attribute::Bold),
        ]);

        let local_mean_ms = dist.mean_us / 1000.0;
        let legacy_speedup = (report.legacy_proxy_per_turn_ms / local_mean_ms.max(0.0001)) as u64;
        let saas_speedup = (report.saas_firewall_per_turn_ms / local_mean_ms.max(0.0001)) as u64;

        comp_table.add_row(vec![
            Cell::new("Sabrix In-VPC / In-Process Engine")
                .fg(Color::Green)
                .add_attribute(Attribute::Bold),
            Cell::new(format!("{:.2} µs ({:.4} ms)", dist.mean_us, local_mean_ms)).fg(Color::Green),
            Cell::new("1x (Baseline - Zero Overhead)").fg(Color::Green),
            Cell::new("Embedded / Local Sidecar (Zero-Egress)"),
        ]);

        comp_table.add_row(vec![
            Cell::new("Legacy Python / Node Proxy").fg(Color::Yellow),
            Cell::new(format!("{:.1} ms", report.legacy_proxy_per_turn_ms)).fg(Color::Yellow),
            Cell::new(format!("{}x SLOWER", legacy_speedup))
                .fg(Color::Yellow)
                .add_attribute(Attribute::Bold),
            Cell::new("Local / Intra-cluster HTTP wrapper"),
        ]);

        comp_table.add_row(vec![
            Cell::new("Legacy SaaS AI Firewall").fg(Color::Red),
            Cell::new(format!("{:.1} ms", report.saas_firewall_per_turn_ms)).fg(Color::Red),
            Cell::new(format!("{}x SLOWER", saas_speedup))
                .fg(Color::Red)
                .add_attribute(Attribute::Bold),
            Cell::new("Remote Cloud Egress (TLS + Network Hop)"),
        ]);

        println!("{}", comp_table);
        println!();

        // 3. Compounded Agent Loop Latency Tax Table
        println!(
            "{}",
            "3. COMPOUNDED AGENT LOOP LATENCY TAX (MULTI-TURN ACCELERATION)"
                .bold()
                .underline()
        );
        let mut tax_table = Table::new();
        tax_table
            .load_preset(UTF8_FULL)
            .apply_modifier(UTF8_ROUND_CORNERS)
            .set_content_arrangement(ContentArrangement::Dynamic);

        tax_table.set_header(vec![
            Cell::new("Agent Loop Depth").add_attribute(Attribute::Bold),
            Cell::new("Sabrix In-VPC Engine").add_attribute(Attribute::Bold),
            Cell::new("Legacy Python Proxy").add_attribute(Attribute::Bold),
            Cell::new("SaaS AI Firewall").add_attribute(Attribute::Bold),
            Cell::new("Time Saved per User Request").add_attribute(Attribute::Bold),
        ]);

        let loop_scenarios = [5, 10, 20, 30, 50];
        for &turns in &loop_scenarios {
            let sabrix_total_ms = (dist.mean_us * turns as f64) / 1000.0;
            let legacy_total_ms = report.legacy_proxy_per_turn_ms * turns as f64;
            let saas_total_ms = report.saas_firewall_per_turn_ms * turns as f64;
            let saved_saas_sec = (saas_total_ms - sabrix_total_ms) / 1000.0;

            tax_table.add_row(vec![
                Cell::new(format!("{} Turns", turns)).add_attribute(Attribute::Bold),
                Cell::new(format!("{:.2} ms", sabrix_total_ms)).fg(Color::Green),
                Cell::new(format!(
                    "{:.0} ms ({:.2} s)",
                    legacy_total_ms,
                    legacy_total_ms / 1000.0
                ))
                .fg(Color::Yellow),
                Cell::new(format!(
                    "{:.0} ms ({:.2} s)",
                    saas_total_ms,
                    saas_total_ms / 1000.0
                ))
                .fg(Color::Red),
                Cell::new(format!("+{:.2} seconds", saved_saas_sec))
                    .fg(Color::Cyan)
                    .add_attribute(Attribute::Bold),
            ]);
        }

        println!("{}", tax_table);
        println!();

        Self::print_footer();
    }

    /// Formats and renders the comprehensive architecture comparison matrix
    pub fn render_comparison_matrix() {
        println!();
        println!(
            "{}",
            "╔═══════════════════════════════════════════════════════════════════════════════╗"
                .bright_cyan()
        );
        println!(
            "{}",
            "║       AGENT SECURITY & RUNTIME ARCHITECTURAL COMPARISON MATRIX                ║"
                .bright_cyan()
                .bold()
        );
        println!(
            "{}",
            "╚═══════════════════════════════════════════════════════════════════════════════╝"
                .bright_cyan()
        );
        println!();

        let mut table = Table::new();
        table
            .load_preset(UTF8_FULL)
            .apply_modifier(UTF8_ROUND_CORNERS)
            .set_content_arrangement(ContentArrangement::Dynamic);

        table.set_header(vec![
            Cell::new("Dimension / Capability").add_attribute(Attribute::Bold),
            Cell::new("Sabrix In-VPC Gateway")
                .fg(Color::Green)
                .add_attribute(Attribute::Bold),
            Cell::new("Legacy Python/Node Proxy")
                .fg(Color::Yellow)
                .add_attribute(Attribute::Bold),
            Cell::new("SaaS AI Firewalls")
                .fg(Color::Red)
                .add_attribute(Attribute::Bold),
        ]);

        table.add_row(vec![
            Cell::new("Per-Turn Inspection Latency"),
            Cell::new("< 2 µs (Zero Overhead)")
                .fg(Color::Green)
                .add_attribute(Attribute::Bold),
            Cell::new("30 - 50 ms (Process/GC tax)").fg(Color::Yellow),
            Cell::new("100 - 250 ms (WAN + TLS)").fg(Color::Red),
        ]);

        table.add_row(vec![
            Cell::new("Compounded 30-Turn Loop Delay"),
            Cell::new("< 0.06 ms (Imperceptible)")
                .fg(Color::Green)
                .add_attribute(Attribute::Bold),
            Cell::new("+1.05 seconds").fg(Color::Yellow),
            Cell::new("+3.60 seconds (Laggy UX)").fg(Color::Red),
        ]);

        table.add_row(vec![
            Cell::new("Data Privacy & Egress"),
            Cell::new("100% In-VPC / Zero Egress")
                .fg(Color::Green)
                .add_attribute(Attribute::Bold),
            Cell::new("Local cluster (No external egress)"),
            Cell::new("Full payload egress to 3rd party").fg(Color::Red),
        ]);

        table.add_row(vec![
            Cell::new("MCP Protocol Native Support"),
            Cell::new("Native JSON-RPC 2.0 parser").fg(Color::Green),
            Cell::new("Ad-hoc JSON middleware"),
            Cell::new("Generic HTTP proxy"),
        ]);

        table.add_row(vec![
            Cell::new("Memory Footprint"),
            Cell::new("< 15 MB resident memory").fg(Color::Green),
            Cell::new("150 MB - 400 MB (Python/Node runtime)").fg(Color::Yellow),
            Cell::new("N/A (Managed SaaS)").fg(Color::Cyan),
        ]);

        table.add_row(vec![
            Cell::new("Threat Detection Mode"),
            Cell::new("Deterministic pre-execution (< 2µs)")
                .fg(Color::Green)
                .add_attribute(Attribute::Bold),
            Cell::new("Async middleware / Regex"),
            Cell::new("External cloud async LLM guard").fg(Color::Red),
        ]);

        table.add_row(vec![
            Cell::new("Cost Scaling"),
            Cell::new("Flat infrastructure / Zero egress cost").fg(Color::Green),
            Cell::new("Compute cluster scaling cost"),
            Cell::new("Per-token / Per-call billing markup").fg(Color::Red),
        ]);

        println!("{}", table);
        println!();

        Self::print_footer();
    }

    pub fn print_footer() {
        println!("{}", "".repeat(80).dimmed());
        println!("{} {}", "🚀".bold(), Self::CTA_FOOTER.bright_cyan().bold());
        println!("{}", "".repeat(80).dimmed());
        println!();
    }
}