microsoft-webui 3.0.3

Programmatic API for the WebUI build-time rendering framework
Documentation
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

//! Memory + CPU benchmark for the streaming render paths.
//!
//! Measures **per-render resource usage** — not just wall-clock time —
//! across the five writer paths exercised by `crates/webui/benches/
//! streaming_bench.rs`:
//!
//! 1. `string`                            — pre-allocated `String` buffer.
//! 2. `streaming`                         — `StreamingWriter` alone.
//! 3. `streaming+inject(opts)`            — production composition with
//!    `RenderOptions::with_head_inject` / `with_body_inject` (handler
//!    emits at the parser-synthesized `head_end` / `body_end` signals).
//! 4. `string+postinject`                 — legacy `lr.inject(&buf)` reference.
//! 5. `streaming+inject(opts) POOLED`     — production path with shared
//!    `ChunkPool` for chunk-buffer recycling.
//!
//! For each path × scale (10 / 100 / 1000 contacts) it reports:
//!
//! * **allocations**  — count of `alloc` calls (custom GlobalAlloc)
//! * **bytes allocated** — total bytes requested
//! * **CPU user time** — `getrusage(RUSAGE_SELF).ru_utime` delta on Unix,
//!   `GetProcessTimes` user time delta on Windows
//! * **peak RSS** — `ru_maxrss` high-water mark on Unix,
//!   `PROCESS_MEMORY_COUNTERS.PeakWorkingSetSize` on Windows
//!
//! Unlike criterion (which only reports wall-clock), this gives a
//! direct allocator-level view useful for verifying that the streaming
//! writer's "zero per-write allocation" claim actually holds in the
//! production path.
//!
//! Usage:
//!
//! ```sh
//! cargo run --release --example streaming_resource_bench -p microsoft-webui
//! ```

#![allow(missing_docs)]
// SAFETY EXEMPTION: This is a benchmark example, not library code.
// `GlobalAlloc` and process resource APIs require `unsafe` blocks; their
// callers here have correct contracts (forwarding to System allocator with
// original layouts; resource structs are fully initialised before FFI calls).
// The workspace `unsafe_code = "deny"` lint applies to production library code;
// benchmarking infrastructure is exempted at the file level with this attribute.
#![allow(unsafe_code)]

use bytes::Bytes;
use serde_json::{json, Value};
use std::alloc::{GlobalAlloc, Layout, System};
use std::path::PathBuf;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::mpsc;
use webui::streaming::{ChunkPool, StreamingWriter};
use webui::{build, BuildOptions, CssStrategy, Protocol, ResponseWriter, WebUIHandler};
use webui_handler::RenderOptions;

// ── Counting allocator ────────────────────────────────────────────────

struct CountingAlloc;

static ALLOC_COUNT: AtomicUsize = AtomicUsize::new(0);
static ALLOC_BYTES: AtomicUsize = AtomicUsize::new(0);

unsafe impl GlobalAlloc for CountingAlloc {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        ALLOC_COUNT.fetch_add(1, Ordering::Relaxed);
        ALLOC_BYTES.fetch_add(layout.size(), Ordering::Relaxed);
        // SAFETY: forwarded with the same layout the caller produced.
        unsafe { System.alloc(layout) }
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        // SAFETY: forwarded; ptr/layout came from `alloc` above.
        unsafe { System.dealloc(ptr, layout) }
    }

    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
        ALLOC_COUNT.fetch_add(1, Ordering::Relaxed);
        ALLOC_BYTES.fetch_add(layout.size(), Ordering::Relaxed);
        // SAFETY: forwarded with the same layout the caller produced.
        unsafe { System.alloc_zeroed(layout) }
    }

    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
        // Realloc to a strictly larger size counts as one new allocation
        // for the size delta — matches what most heap profilers do.
        if new_size > layout.size() {
            ALLOC_COUNT.fetch_add(1, Ordering::Relaxed);
            ALLOC_BYTES.fetch_add(new_size - layout.size(), Ordering::Relaxed);
        }
        // SAFETY: forwarded; ptr/layout came from `alloc` above.
        unsafe { System.realloc(ptr, layout, new_size) }
    }
}

#[global_allocator]
static GLOBAL: CountingAlloc = CountingAlloc;

fn alloc_snapshot() -> (usize, usize) {
    (
        ALLOC_COUNT.load(Ordering::Relaxed),
        ALLOC_BYTES.load(Ordering::Relaxed),
    )
}

// ── Process resource helpers ───────────────────────────────────────────

#[derive(Copy, Clone)]
struct ProcessUsage {
    user_cpu: Duration,
    sys_cpu: Duration,
    /// Maximum resident set size, normalized to bytes.
    max_rss_bytes: i64,
}

impl ProcessUsage {
    #[cfg(unix)]
    fn now() -> Self {
        let mut usage: libc::rusage = unsafe { std::mem::zeroed() };
        // SAFETY: `usage` is a valid mutable pointer to a fully-initialised
        // (zeroed) rusage struct; getrusage(2) writes to it.
        let rc = unsafe { libc::getrusage(libc::RUSAGE_SELF, &mut usage) };
        assert_eq!(rc, 0, "getrusage failed");
        Self {
            user_cpu: timeval_to_duration(usage.ru_utime),
            sys_cpu: timeval_to_duration(usage.ru_stime),
            max_rss_bytes: unix_rss_to_bytes(usage.ru_maxrss as i64),
        }
    }

    #[cfg(windows)]
    fn now() -> Self {
        use windows_sys::Win32::Foundation::FILETIME;
        use windows_sys::Win32::System::ProcessStatus::{
            K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS,
        };
        use windows_sys::Win32::System::Threading::{GetCurrentProcess, GetProcessTimes};

        // GetCurrentProcess returns a pseudo-handle owned by the process; it
        // must not be closed.
        let process = unsafe { GetCurrentProcess() };
        let mut creation_time = FILETIME::default();
        let mut exit_time = FILETIME::default();
        let mut kernel_time = FILETIME::default();
        let mut user_time = FILETIME::default();
        // SAFETY: all pointers refer to writable FILETIME values and the
        // pseudo-handle returned by GetCurrentProcess is valid for this call.
        let times_ok = unsafe {
            GetProcessTimes(
                process,
                &mut creation_time,
                &mut exit_time,
                &mut kernel_time,
                &mut user_time,
            )
        };
        assert_ne!(times_ok, 0, "GetProcessTimes failed");

        let counters_size = process_memory_counters_size();
        // SAFETY: zeroed PROCESS_MEMORY_COUNTERS is valid after its cb field is
        // set to the structure size required by GetProcessMemoryInfo.
        let mut counters: PROCESS_MEMORY_COUNTERS = unsafe { std::mem::zeroed() };
        counters.cb = counters_size;
        // SAFETY: counters points to writable memory with cb initialized, and
        // the pseudo-handle returned by GetCurrentProcess is valid for this call.
        let memory_ok = unsafe { K32GetProcessMemoryInfo(process, &mut counters, counters_size) };
        assert_ne!(memory_ok, 0, "GetProcessMemoryInfo failed");

        Self {
            user_cpu: filetime_to_duration(user_time),
            sys_cpu: filetime_to_duration(kernel_time),
            max_rss_bytes: usize_to_i64_saturating(counters.PeakWorkingSetSize),
        }
    }

    #[cfg(not(any(unix, windows)))]
    fn now() -> Self {
        Self {
            user_cpu: Duration::ZERO,
            sys_cpu: Duration::ZERO,
            max_rss_bytes: -1,
        }
    }

    fn max_rss_bytes(&self) -> i64 {
        self.max_rss_bytes
    }
}

#[cfg(unix)]
fn timeval_to_duration(tv: libc::timeval) -> Duration {
    let secs = tv.tv_sec as u64;
    let usecs = tv.tv_usec as u32;
    Duration::new(secs, usecs * 1_000)
}

#[cfg(unix)]
fn unix_rss_to_bytes(raw: i64) -> i64 {
    if cfg!(target_os = "macos") {
        raw
    } else {
        raw.saturating_mul(1024)
    }
}

#[cfg(windows)]
fn filetime_to_duration(filetime: windows_sys::Win32::Foundation::FILETIME) -> Duration {
    let ticks = (u64::from(filetime.dwHighDateTime) << 32) | u64::from(filetime.dwLowDateTime);
    let secs = ticks / 10_000_000;
    let nanos = match u32::try_from((ticks % 10_000_000) * 100) {
        Ok(value) => value,
        Err(_) => panic!("FILETIME nanosecond remainder must fit in u32"),
    };
    Duration::new(secs, nanos)
}

#[cfg(windows)]
fn usize_to_i64_saturating(value: usize) -> i64 {
    match i64::try_from(value) {
        Ok(value) => value,
        Err(_) => i64::MAX,
    }
}

#[cfg(windows)]
fn process_memory_counters_size() -> u32 {
    match u32::try_from(std::mem::size_of::<
        windows_sys::Win32::System::ProcessStatus::PROCESS_MEMORY_COUNTERS,
    >()) {
        Ok(value) => value,
        Err(_) => panic!("PROCESS_MEMORY_COUNTERS size must fit in u32"),
    }
}

#[derive(Copy, Clone)]
struct ResourceDelta {
    iters: usize,
    allocs: usize,
    bytes: usize,
    user_cpu: Duration,
    sys_cpu: Duration,
    wall_time: Duration,
    rss_high_water_bytes: i64,
}

impl ResourceDelta {
    fn per_iter(&self) -> PerIter {
        let n = self.iters as f64;
        PerIter {
            allocs: self.allocs as f64 / n,
            bytes: self.bytes as f64 / n,
            user_cpu_us: self.user_cpu.as_secs_f64() * 1_000_000.0 / n,
            sys_cpu_us: self.sys_cpu.as_secs_f64() * 1_000_000.0 / n,
            wall_us: self.wall_time.as_secs_f64() * 1_000_000.0 / n,
            rss_bytes: self.rss_high_water_bytes,
        }
    }
}

struct PerIter {
    allocs: f64,
    bytes: f64,
    user_cpu_us: f64,
    sys_cpu_us: f64,
    wall_us: f64,
    rss_bytes: i64,
}

// ── State + protocol setup ────────────────────────────────────────────

const FIRST_NAMES: &[&str] = &[
    "Sarah", "Marcus", "Yuki", "Priya", "James", "Amara", "Luis", "Emma", "David", "Fatima",
];
const LAST_NAMES: &[&str] = &[
    "Chen",
    "Johnson",
    "Tanaka",
    "Sharma",
    "O'Brien",
    "Okafor",
    "Ramirez",
    "Lindström",
    "Kim",
    "Al-Hassan",
];
const GROUPS: &[&str] = &["Family", "Work", "Friends", "Other"];

fn generate_contact(idx: usize) -> Value {
    let first = FIRST_NAMES[idx % FIRST_NAMES.len()];
    let last = LAST_NAMES[idx % LAST_NAMES.len()];
    json!({
        "id": (idx + 1).to_string(),
        "firstName": first,
        "lastName": last,
        "email": format!("{}.{}@example.com", first.to_lowercase(), last.to_lowercase()),
        "phone": format!("+1 (555) {:03}-{:04}", (idx * 111) % 1000, (idx * 1234) % 10000),
        "company": "Contoso Ltd",
        "group": GROUPS[idx % GROUPS.len()],
        "favorite": idx.is_multiple_of(3),
        "initials": format!("{}{}", &first[..1], &last[..1]),
        "avatarColor": "#4A90D9",
        "notes": String::new(),
        "address": format!("{} St, Seattle, WA", (idx + 1) * 100),
    })
}

fn build_state(count: usize) -> Value {
    let contacts: Vec<Value> = (0..count).map(generate_contact).collect();
    let recent: Vec<Value> = contacts[count.saturating_sub(5)..].to_vec();
    json!({
        "page": "dashboard",
        "searchQuery": "",
        "activeGroup": "all",
        "groups": GROUPS,
        "totalContacts": count,
        "totalFavorites": 0,
        "totalGroups": GROUPS.len(),
        "contacts": contacts.clone(),
        "filteredContacts": contacts,
        "recentContacts": recent,
        "favoriteContacts": Vec::<Value>::new(),
        "selectedContact": null,
    })
}

fn build_protocol() -> Protocol {
    let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    let app_dir = manifest
        .join("..")
        .join("..")
        .join("examples")
        .join("app")
        .join("contact-book-manager")
        .join("src");
    let document = build(BuildOptions {
        app_dir,
        entry: "index.html".to_string(),
        css: CssStrategy::Style,
        ..BuildOptions::default()
    })
    .expect("failed to build contact-book-manager protocol")
    .protocol;
    Protocol::new(document)
}

const HEAD_INJECT: &str = r#"<link rel="preload" as="image" href="/img/hero.jpg" fetchpriority="high"><link rel="preload" as="image" href="/img/p1.jpg"><link rel="preload" as="image" href="/img/p2.jpg">"#;
const BODY_INJECT: &str = r#"<script>(function(){var e=new EventSource('/__webui/livereload');e.addEventListener('reload',function(){location.reload()})})();</script>"#;

// ── Writers ────────────────────────────────────────────────────────────

struct StringWriter {
    buf: String,
}
impl StringWriter {
    fn with_capacity(cap: usize) -> Self {
        Self {
            buf: String::with_capacity(cap),
        }
    }
}
impl ResponseWriter for StringWriter {
    fn write(&mut self, content: &str) -> webui_handler::Result<()> {
        self.buf.push_str(content);
        Ok(())
    }
    fn end(&mut self) -> webui_handler::Result<()> {
        Ok(())
    }
}

fn drain_total(mut rx: mpsc::Receiver<Bytes>) -> usize {
    let mut total = 0;
    while let Some(chunk) = rx.blocking_recv() {
        total += chunk.len();
    }
    total
}

fn post_inject(html: &str, script: &str) -> String {
    if let Some(idx) = html
        .as_bytes()
        .windows(7)
        .position(|w| w.eq_ignore_ascii_case(b"</body>"))
    {
        let mut out = String::with_capacity(html.len() + script.len() + 2);
        out.push_str(&html[..idx]);
        out.push_str(script);
        out.push_str(&html[idx..]);
        out
    } else {
        let mut out = String::with_capacity(html.len() + script.len());
        out.push_str(html);
        out.push_str(script);
        out
    }
}

// ── Per-path drivers ──────────────────────────────────────────────────

fn run_string(protocol: &Protocol, state: &Value, output_size: usize) -> usize {
    let h = WebUIHandler::new();
    let mut w = StringWriter::with_capacity(output_size);
    h.render(
        protocol,
        state,
        &RenderOptions::new("index.html", "/"),
        &mut w,
    )
    .expect("render");
    w.buf.len()
}

fn run_streaming(protocol: &Protocol, state: &Value, output_size: usize) -> usize {
    let h = WebUIHandler::new();
    let cap = (output_size / StreamingWriter::CHUNK_TARGET) + 4;
    let (tx, rx) = mpsc::channel::<Bytes>(cap);
    let mut w = StreamingWriter::new(tx);
    h.render(
        protocol,
        state,
        &RenderOptions::new("index.html", "/"),
        &mut w,
    )
    .expect("render");
    drop(w);
    drain_total(rx)
}

/// Streaming with `RenderOptions::with_head_inject` /
/// `with_body_inject`. Note: the contact-book template is a Shadow
/// DOM template with no `<head>`/`<body>` tags, so `head_end` /
/// `body_end` signals never fire and the inject strings are NOT
/// emitted. This row therefore measures "inject configured but never
/// triggered" — which on the new signal-based path costs **nothing**
/// (just two `Option<String>` fields on the context). The legacy
/// byte-scanner approach had to scan every output byte looking for
/// never-present markers, costing ~14 µs of pure overhead.
fn run_streaming_with_inject(protocol: &Protocol, state: &Value, output_size: usize) -> usize {
    let h = WebUIHandler::new();
    let cap = (output_size / StreamingWriter::CHUNK_TARGET) + 4;
    let (tx, rx) = mpsc::channel::<Bytes>(cap);
    let mut w = StreamingWriter::new(tx);
    let opts = RenderOptions::new("index.html", "/")
        .with_head_inject(HEAD_INJECT)
        .with_body_inject(BODY_INJECT);
    h.render(protocol, state, &opts, &mut w).expect("render");
    drop(w);
    drain_total(rx)
}

/// Production composition with the lock-free shared chunk pool +
/// signal-based inject. `pool` is shared across all calls (lives for
/// the whole bench run) to mirror the actual server's startup-time
/// pool.
fn run_streaming_pooled_with_inject(
    protocol: &Protocol,
    state: &Value,
    output_size: usize,
    pool: &Arc<ChunkPool>,
) -> usize {
    let h = WebUIHandler::new();
    let cap = (output_size / StreamingWriter::CHUNK_TARGET) + 4;
    let (tx, rx) = mpsc::channel::<Bytes>(cap);
    let mut w = StreamingWriter::new_pooled(tx, Arc::clone(pool));
    let opts = RenderOptions::new("index.html", "/")
        .with_head_inject(HEAD_INJECT)
        .with_body_inject(BODY_INJECT);
    h.render(protocol, state, &opts, &mut w).expect("render");
    drop(w);
    // Drain consumes the Bytes — drops PooledChunk owners — releases
    // chunk Vec back to the pool. This is exactly the actix lifecycle.
    drain_total(rx)
}

fn run_string_postinject(protocol: &Protocol, state: &Value, output_size: usize) -> usize {
    let h = WebUIHandler::new();
    let mut w = StringWriter::with_capacity(output_size);
    h.render(
        protocol,
        state,
        &RenderOptions::new("index.html", "/"),
        &mut w,
    )
    .expect("render");
    let merged = post_inject(&w.buf, BODY_INJECT);
    merged.len()
}

// ── Measurement loop ──────────────────────────────────────────────────

fn measure<F>(iters: usize, mut f: F) -> ResourceDelta
where
    F: FnMut(),
{
    // Warm up: first runs are dominated by lazy initialisations
    // (formatter caches, allocator slabs, etc.).
    for _ in 0..3 {
        f();
    }

    let (a0, b0) = alloc_snapshot();
    let r0 = ProcessUsage::now();
    let t0 = Instant::now();

    for _ in 0..iters {
        f();
    }

    let wall = t0.elapsed();
    let r1 = ProcessUsage::now();
    let (a1, b1) = alloc_snapshot();

    ResourceDelta {
        iters,
        allocs: a1.saturating_sub(a0),
        bytes: b1.saturating_sub(b0),
        user_cpu: r1.user_cpu.saturating_sub(r0.user_cpu),
        sys_cpu: r1.sys_cpu.saturating_sub(r0.sys_cpu),
        wall_time: wall,
        rss_high_water_bytes: r1.max_rss_bytes(),
    }
}

// ── Reporting ─────────────────────────────────────────────────────────

fn print_header() {
    println!();
    println!(
        "| {:<26} | {:>7} | {:>10} | {:>13} | {:>9} | {:>11} | {:>10} | {:>14} |",
        "path/scale (output bytes)",
        "iters",
        "allocs/run",
        "bytes/run",
        "wall µs",
        "user µs/run",
        "sys µs/run",
        "process RSS",
    );
    println!(
        "|{:-<28}|{:->9}|{:->12}|{:->15}|{:->11}|{:->13}|{:->12}|{:->16}|",
        "", "", "", "", "", "", "", ""
    );
}

fn print_row(label: &str, delta: ResourceDelta) {
    let pi = delta.per_iter();
    println!(
        "| {:<26} | {:>7} | {:>10.2} | {:>13} | {:>9.2} | {:>11.2} | {:>10.2} | {:>14} |",
        label,
        delta.iters,
        pi.allocs,
        format_bytes_per_run(pi.bytes),
        pi.wall_us,
        pi.user_cpu_us,
        pi.sys_cpu_us,
        format_total_rss(pi.rss_bytes),
    );
}

fn format_bytes_per_run(bytes: f64) -> String {
    if bytes < 1024.0 {
        format!("{bytes:.0} B")
    } else if bytes < 1024.0 * 1024.0 {
        format!("{:.1} KiB", bytes / 1024.0)
    } else {
        format!("{:.2} MiB", bytes / (1024.0 * 1024.0))
    }
}

fn format_total_rss(bytes: i64) -> String {
    if bytes < 0 {
        return "n/a".to_string();
    }
    if bytes < 1024 * 1024 {
        format!("{:.1} KiB", bytes as f64 / 1024.0)
    } else {
        format!("{:.2} MiB", bytes as f64 / (1024.0 * 1024.0))
    }
}

fn warmup_output_size(protocol: &Protocol, state: &Value) -> usize {
    let h = WebUIHandler::new();
    let mut w = StringWriter::with_capacity(128 * 1024);
    h.render(
        protocol,
        state,
        &RenderOptions::new("index.html", "/"),
        &mut w,
    )
    .expect("warmup");
    w.buf.len()
}

// ── Snapshot serialization ────────────────────────────────────────────

/// One row of the bench, in JSON-friendly form (no formatters).
#[derive(serde::Serialize, serde::Deserialize)]
struct SnapshotRow {
    label: String,
    iters: usize,
    allocs_per_run: f64,
    bytes_per_run: f64,
    user_cpu_us_per_run: f64,
    sys_cpu_us_per_run: f64,
    wall_us_per_run: f64,
    rss_high_water_bytes: i64,
}

#[derive(serde::Serialize, serde::Deserialize)]
struct Snapshot {
    schema: u32,
    name: String,
    timestamp_unix: u64,
    rows: Vec<SnapshotRow>,
}

const SNAPSHOT_SCHEMA: u32 = 1;

fn snapshot_path(name: &str) -> std::path::PathBuf {
    let manifest = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    manifest
        .join("..")
        .join("..")
        .join("target")
        .join("bench-baselines")
        .join(format!("resource-{name}.json"))
}

fn save_snapshot(name: &str, rows: &[SnapshotRow]) {
    let path = snapshot_path(name);
    if let Some(parent) = path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    let snap = Snapshot {
        schema: SNAPSHOT_SCHEMA,
        name: name.to_string(),
        timestamp_unix: std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0),
        rows: rows.iter().map(SnapshotRow::clone_data).collect(),
    };
    let json = match serde_json::to_string_pretty(&snap) {
        Ok(s) => s,
        Err(e) => {
            eprintln!("snapshot: serialize failed: {e}");
            return;
        }
    };
    if let Err(e) = std::fs::write(&path, json) {
        eprintln!("snapshot: write {} failed: {e}", path.display());
        return;
    }
    println!();
    println!("✔ Baseline saved to {}", path.display());
}

fn load_snapshot(name: &str) -> Option<Snapshot> {
    let path = snapshot_path(name);
    let bytes = match std::fs::read(&path) {
        Ok(b) => b,
        Err(_) => {
            eprintln!(
                "compare: baseline '{name}' not found at {} — run with --save {name} first",
                path.display()
            );
            return None;
        }
    };
    match serde_json::from_slice::<Snapshot>(&bytes) {
        Ok(s) if s.schema == SNAPSHOT_SCHEMA => Some(s),
        Ok(s) => {
            eprintln!(
                "compare: baseline '{name}' has schema {} (expected {SNAPSHOT_SCHEMA}); regenerate with --save",
                s.schema
            );
            None
        }
        Err(e) => {
            eprintln!("compare: parse {} failed: {e}", path.display());
            None
        }
    }
}

impl SnapshotRow {
    fn clone_data(&self) -> SnapshotRow {
        SnapshotRow {
            label: self.label.clone(),
            iters: self.iters,
            allocs_per_run: self.allocs_per_run,
            bytes_per_run: self.bytes_per_run,
            user_cpu_us_per_run: self.user_cpu_us_per_run,
            sys_cpu_us_per_run: self.sys_cpu_us_per_run,
            wall_us_per_run: self.wall_us_per_run,
            rss_high_water_bytes: self.rss_high_water_bytes,
        }
    }
}

fn print_diff(current: &[SnapshotRow], baseline: &Snapshot) {
    println!();
    println!(
        "Diff vs baseline '{}' (saved {} ago)",
        baseline.name,
        format_age(baseline.timestamp_unix)
    );
    println!(
        "| {:<42} | {:>14} | {:>14} | {:>14} |",
        "row", "allocs Δ%", "bytes Δ%", "user_cpu Δ%"
    );
    println!("|{:-<44}|{:->16}|{:->16}|{:->16}|", "", "", "", "");
    for cur in current {
        let base = baseline.rows.iter().find(|b| b.label == cur.label);
        let (a, b, c) = match base {
            Some(base) => (
                pct_change(base.allocs_per_run, cur.allocs_per_run),
                pct_change(base.bytes_per_run, cur.bytes_per_run),
                pct_change(base.user_cpu_us_per_run, cur.user_cpu_us_per_run),
            ),
            None => {
                println!(
                    "| {:<42} | {:>14} | {:>14} | {:>14} |",
                    cur.label, "(new row)", "—", "—"
                );
                continue;
            }
        };
        println!(
            "| {:<42} | {:>13.1}% | {:>13.1}% | {:>13.1}% |",
            cur.label, a, b, c
        );
    }
    println!();
    println!("Negative Δ% = improvement; positive = regression. Threshold for action: ±5%.");
    println!();
}

fn pct_change(base: f64, current: f64) -> f64 {
    if base == 0.0 {
        return 0.0;
    }
    ((current - base) / base) * 100.0
}

fn format_age(then_unix: u64) -> String {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);
    let secs = now.saturating_sub(then_unix);
    if secs < 60 {
        format!("{secs}s")
    } else if secs < 3600 {
        format!("{}m", secs / 60)
    } else if secs < 86400 {
        format!("{}h", secs / 3600)
    } else {
        format!("{}d", secs / 86400)
    }
}

// ── CLI parsing ───────────────────────────────────────────────────────

enum Mode {
    Print,
    Save(String),
    Compare(String),
}

fn parse_args() -> Mode {
    let args: Vec<String> = std::env::args().skip(1).collect();
    let mut iter = args.into_iter();
    while let Some(arg) = iter.next() {
        match arg.as_str() {
            "--save" => {
                return iter.next().map(Mode::Save).unwrap_or_else(|| {
                    eprintln!("--save requires a baseline name");
                    std::process::exit(2);
                });
            }
            "--compare" => {
                return iter.next().map(Mode::Compare).unwrap_or_else(|| {
                    eprintln!("--compare requires a baseline name");
                    std::process::exit(2);
                });
            }
            "--help" | "-h" => {
                println!(
                    "Usage: streaming_resource_bench [--save NAME] [--compare NAME]\n\n\
                     With no args: prints the table.\n\
                     --save NAME: write current results to target/bench-baselines/resource-NAME.json\n\
                     --compare NAME: print results AND a Δ%-table vs the saved baseline"
                );
                std::process::exit(0);
            }
            other => {
                eprintln!("unknown arg: {other}");
                std::process::exit(2);
            }
        }
    }
    Mode::Print
}

// ── Main ──────────────────────────────────────────────────────────────

fn main() {
    let mode = parse_args();
    let scales = [10usize, 100, 1000];
    let iters_per_scale = 2_000;

    println!("WebUI streaming resource benchmark");
    println!("==================================");
    println!(
        "Build: {} | iterations per row: {}",
        if cfg!(debug_assertions) {
            "DEBUG (numbers will be misleading; rebuild with --release)"
        } else {
            "release"
        },
        iters_per_scale
    );
    println!(
        "RSS column = process-wide high-water mark observed at end of phase \
         (cumulative across all phases, only meaningful as a peak)."
    );
    print_header();

    let protocol = build_protocol();

    // One pool shared across the whole bench — this is exactly how the
    // production server uses it (constructed at startup, lives forever).
    let pool = Arc::new(ChunkPool::new(256, StreamingWriter::CHUNK_TARGET + 1024));

    let paths: &[(&str, fn(&Protocol, &Value, usize) -> usize)] = &[
        (
            "string",
            run_string as fn(&Protocol, &Value, usize) -> usize,
        ),
        ("streaming", run_streaming),
        ("streaming+inject(opts)", run_streaming_with_inject),
        ("string+postinject", run_string_postinject),
    ];

    let mut snapshot_rows: Vec<SnapshotRow> = Vec::new();

    for &scale in &scales {
        let state = build_state(scale);
        let output_size = warmup_output_size(&protocol, &state);
        for (label, f) in paths {
            let delta = measure(iters_per_scale, || {
                std::hint::black_box(f(&protocol, &state, output_size));
            });
            let row_label = format!("{label}/{scale}");
            print_row(&format!("{row_label} ({output_size}B)"), delta);
            snapshot_rows.push(delta_to_row(&row_label, delta));
        }
        // Pooled path measured separately because the closure needs to
        // capture the shared pool (can't use a fn pointer).
        let delta = measure(iters_per_scale, || {
            std::hint::black_box(run_streaming_pooled_with_inject(
                &protocol,
                &state,
                output_size,
                &pool,
            ));
        });
        let row_label = format!("streaming+inject(opts) POOLED/{scale}");
        print_row(&format!("{row_label} ({output_size}B)"), delta);
        snapshot_rows.push(delta_to_row(&row_label, delta));
        println!(
            "|{:-<28}|{:->9}|{:->12}|{:->15}|{:->11}|{:->13}|{:->12}|{:->16}|",
            "", "", "", "", "", "", "", ""
        );
    }
    println!();
    println!("Notes:");
    println!("  * `allocs/run` and `bytes/run` are exact (custom GlobalAlloc).");
    if cfg!(windows) {
        println!("  * `user µs/run` is `GetProcessTimes` user time delta / iters.");
        println!("  * `process RSS` is `PeakWorkingSetSize` for the process at");
    } else {
        println!("  * `user µs/run` is `getrusage(RUSAGE_SELF).ru_utime` delta / iters.");
        println!("  * `process RSS` is the high-water mark for the whole process at");
    }
    println!("    phase end. Per-iteration RSS is not directly observable; use");
    println!("    `bytes/run` to compare per-render heap pressure across paths.");

    match mode {
        Mode::Print => {}
        Mode::Save(name) => save_snapshot(&name, &snapshot_rows),
        Mode::Compare(name) => {
            if let Some(baseline) = load_snapshot(&name) {
                print_diff(&snapshot_rows, &baseline);
            }
        }
    }
}

fn delta_to_row(label: &str, delta: ResourceDelta) -> SnapshotRow {
    let pi = delta.per_iter();
    SnapshotRow {
        label: label.to_string(),
        iters: delta.iters,
        allocs_per_run: pi.allocs,
        bytes_per_run: pi.bytes,
        user_cpu_us_per_run: pi.user_cpu_us,
        sys_cpu_us_per_run: pi.sys_cpu_us,
        wall_us_per_run: pi.wall_us,
        rss_high_water_bytes: pi.rss_bytes,
    }
}