microsoft-webui-handler 3.0.3

Core rust handler for WebUI 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use serde_json::json;
use serde_json::Value;
use std::collections::HashMap;
use std::hint::black_box;
use webui_handler::plugin::fast_v2::FastV2HydrationPlugin;
use webui_handler::{Protocol, RenderOptions, ResponseWriter, WebUIHandler};
use webui_protocol::plugin::FastElementData;
use webui_protocol::{
    ComparisonOperator, ConditionExpr, FragmentList, LogicalOperator, WebUIFragment, WebUIProtocol,
};

struct BenchWriter {
    output: String,
}

impl BenchWriter {
    fn new(capacity: usize) -> Self {
        Self {
            output: String::with_capacity(capacity),
        }
    }

    fn clear(&mut self) {
        self.output.clear();
    }

    fn len(&self) -> usize {
        self.output.len()
    }
}

impl ResponseWriter for BenchWriter {
    fn write(&mut self, content: &str) -> webui_handler::Result<()> {
        self.output.push_str(content);
        Ok(())
    }

    webui_handler::string_response_writer_methods!(output);

    fn end(&mut self) -> webui_handler::Result<()> {
        Ok(())
    }
}

fn build_state(item_count: usize) -> Value {
    let mut items = Vec::with_capacity(item_count);

    for idx in 0..item_count {
        items.push(json!({
            "id": idx,
            "name": format!("Item {}", idx),
            "enabled": idx % 2 == 0
        }));
    }

    json!({
        "title": "Benchmark Title",
        "is_disabled": false,
        "theme": "dark",
        "size": "md",
        "show_footer": true,
        "footer": "Footer Content",
        "items": items
    })
}

fn build_mixed_protocol() -> Protocol {
    let mut fragments = HashMap::new();

    fragments.insert(
        "index.html".to_string(),
        FragmentList {
            fragments: vec![
                WebUIFragment::raw("<section class=\"root\">"),
                WebUIFragment::component("card"),
                WebUIFragment::raw("</section>"),
            ],
            contains_boundary: false,
        },
    );

    fragments.insert(
        "card".to_string(),
        FragmentList {
            fragments: vec![
                WebUIFragment::raw("<x-card"),
                WebUIFragment::attribute("title", "title"),
                WebUIFragment::attribute_template("class", "class-template"),
                WebUIFragment::attribute_boolean(
                    "disabled",
                    ConditionExpr::identifier("is_disabled"),
                ),
                WebUIFragment::raw(">"),
                WebUIFragment::raw("<h2>"),
                WebUIFragment::signal("title", false),
                WebUIFragment::raw("</h2><ul>"),
                WebUIFragment::for_loop("item", "items", "item-frag"),
                WebUIFragment::raw("</ul>"),
                WebUIFragment::if_cond(ConditionExpr::identifier("show_footer"), "footer-frag"),
                WebUIFragment::plugin(
                    FastElementData { binding_count: 3 }
                        .encode_v2(false)
                        .to_vec(),
                ),
                WebUIFragment::raw("</x-card>"),
            ],
            contains_boundary: false,
        },
    );

    fragments.insert(
        "class-template".to_string(),
        FragmentList {
            fragments: vec![
                WebUIFragment::raw("card "),
                WebUIFragment::signal("theme", false),
                WebUIFragment::raw(" size-"),
                WebUIFragment::signal("size", false),
            ],
            contains_boundary: false,
        },
    );

    fragments.insert(
        "item-frag".to_string(),
        FragmentList {
            fragments: vec![
                WebUIFragment::raw("<li"),
                WebUIFragment::attribute("data-id", "item.id"),
                WebUIFragment::raw(">"),
                WebUIFragment::signal("item.name", false),
                WebUIFragment::raw("</li>"),
            ],
            contains_boundary: false,
        },
    );

    fragments.insert(
        "footer-frag".to_string(),
        FragmentList {
            fragments: vec![
                WebUIFragment::raw("<footer>"),
                WebUIFragment::signal("footer", false),
                WebUIFragment::raw("</footer>"),
            ],
            contains_boundary: false,
        },
    );

    Protocol::new(WebUIProtocol::new(fragments))
}

fn handler_plugin_fast_bench(c: &mut Criterion) {
    let mut group = c.benchmark_group("handler_plugin_fast");
    let protocol = build_mixed_protocol();
    let state = build_state(120);

    let baseline_handler = WebUIHandler::new();
    let mut baseline_writer = BenchWriter::new(16 * 1024);
    baseline_handler
        .render(
            &protocol,
            &state,
            &RenderOptions::new("index.html", "/"),
            &mut baseline_writer,
        )
        .unwrap_or_else(|error| panic!("baseline render failed: {error}"));
    group.throughput(Throughput::Bytes(baseline_writer.len() as u64));

    group.bench_function(BenchmarkId::new("render", "without_plugin"), |b| {
        let handler = WebUIHandler::new();
        let mut writer = BenchWriter::new(16 * 1024);

        b.iter(|| {
            writer.clear();
            handler
                .render(
                    black_box(&protocol),
                    black_box(&state),
                    &RenderOptions::new("index.html", "/"),
                    &mut writer,
                )
                .unwrap_or_else(|error| panic!("render without plugin failed: {error}"));
        });
    });

    group.bench_function(BenchmarkId::new("render", "with_fast_plugin"), |b| {
        let handler = WebUIHandler::with_plugin(|| Box::new(FastV2HydrationPlugin::new()));
        let mut writer = BenchWriter::new(24 * 1024);

        b.iter(|| {
            writer.clear();
            handler
                .render(
                    black_box(&protocol),
                    black_box(&state),
                    &RenderOptions::new("index.html", "/"),
                    &mut writer,
                )
                .unwrap_or_else(|error| panic!("render with fast plugin failed: {error}"));
        });
    });

    group.finish();
}

fn handler_loop_scaling_bench(c: &mut Criterion) {
    let mut group = c.benchmark_group("handler_loop_scaling");
    let protocol = build_mixed_protocol();

    for &count in &[10usize, 100, 500, 2000] {
        let state = build_state(count);

        // Pre-render to measure output size for throughput
        let handler = WebUIHandler::new();
        let mut writer = BenchWriter::new(count * 80 + 1024);
        handler
            .render(
                &protocol,
                &state,
                &RenderOptions::new("index.html", "/"),
                &mut writer,
            )
            .unwrap_or_else(|error| panic!("loop scaling warmup failed for {count}: {error}"));
        group.throughput(Throughput::Bytes(writer.len() as u64));

        group.bench_with_input(BenchmarkId::new("items", count), &state, |b, st| {
            let h = WebUIHandler::new();
            let mut w = BenchWriter::new(count * 80 + 1024);

            b.iter(|| {
                w.clear();
                h.render(
                    black_box(&protocol),
                    black_box(st),
                    &RenderOptions::new("index.html", "/"),
                    &mut w,
                )
                .unwrap_or_else(|error| panic!("loop scaling render failed: {error}"));
            });
        });
    }

    group.finish();
}

fn build_condition_protocol() -> Protocol {
    let mut fragments = HashMap::new();

    fragments.insert(
        "index.html".to_string(),
        FragmentList {
            fragments: vec![
                WebUIFragment::raw("<div class=\"conditions\">"),
                // Simple identifier condition
                WebUIFragment::if_cond(ConditionExpr::identifier("isAdmin"), "admin-frag"),
                // Predicate condition (equality)
                WebUIFragment::if_cond(
                    ConditionExpr::predicate("status", ComparisonOperator::Equal, "'active'"),
                    "status-frag",
                ),
                // Negated condition
                WebUIFragment::if_cond(
                    ConditionExpr::negated(ConditionExpr::identifier("isDisabled")),
                    "enabled-frag",
                ),
                // Compound AND condition
                WebUIFragment::if_cond(
                    ConditionExpr::compound(
                        ConditionExpr::identifier("isLoggedIn"),
                        LogicalOperator::And,
                        ConditionExpr::identifier("hasPermission"),
                    ),
                    "auth-frag",
                ),
                // Compound OR condition
                WebUIFragment::if_cond(
                    ConditionExpr::compound(
                        ConditionExpr::identifier("isOwner"),
                        LogicalOperator::Or,
                        ConditionExpr::identifier("isAdmin"),
                    ),
                    "access-frag",
                ),
                WebUIFragment::raw("</div>"),
            ],
            contains_boundary: false,
        },
    );

    for (id, content) in [
        ("admin-frag", "<div class=\"admin-bar\">Admin Mode</div>"),
        ("status-frag", "<span class=\"badge\">Active</span>"),
        ("enabled-frag", "<button>Click me</button>"),
        ("auth-frag", "<nav>Authenticated Menu</nav>"),
        ("access-frag", "<div>Access granted</div>"),
    ] {
        fragments.insert(
            id.to_string(),
            FragmentList {
                fragments: vec![WebUIFragment::raw(content)],
                contains_boundary: false,
            },
        );
    }

    Protocol::new(WebUIProtocol::new(fragments))
}

fn build_condition_state() -> Value {
    json!({
        "isAdmin": true,
        "status": "active",
        "isDisabled": false,
        "isLoggedIn": true,
        "hasPermission": true,
        "isOwner": false,
    })
}

fn handler_condition_variety_bench(c: &mut Criterion) {
    let mut group = c.benchmark_group("handler_condition_variety");
    let protocol = build_condition_protocol();

    let state_true = build_condition_state();
    let handler = WebUIHandler::new();
    let mut writer = BenchWriter::new(1024);
    handler
        .render(
            &protocol,
            &state_true,
            &RenderOptions::new("index.html", "/"),
            &mut writer,
        )
        .unwrap_or_else(|error| panic!("condition warmup failed: {error}"));
    group.throughput(Throughput::Bytes(writer.len() as u64));

    group.bench_function("all_true", |b| {
        let h = WebUIHandler::new();
        let mut w = BenchWriter::new(1024);
        b.iter(|| {
            w.clear();
            h.render(
                black_box(&protocol),
                black_box(&state_true),
                &RenderOptions::new("index.html", "/"),
                &mut w,
            )
            .unwrap_or_else(|error| panic!("condition render failed: {error}"));
        });
    });

    let state_mixed = json!({
        "isAdmin": false,
        "status": "inactive",
        "isDisabled": true,
        "isLoggedIn": true,
        "hasPermission": false,
        "isOwner": false,
    });

    group.bench_function("mixed", |b| {
        let h = WebUIHandler::new();
        let mut w = BenchWriter::new(1024);
        b.iter(|| {
            w.clear();
            h.render(
                black_box(&protocol),
                black_box(&state_mixed),
                &RenderOptions::new("index.html", "/"),
                &mut w,
            )
            .unwrap_or_else(|error| panic!("condition mixed render failed: {error}"));
        });
    });

    group.finish();
}

fn build_nested_component_protocol() -> Protocol {
    let mut fragments = HashMap::new();

    fragments.insert(
        "index.html".to_string(),
        FragmentList {
            fragments: vec![
                WebUIFragment::raw("<!DOCTYPE html><html><body>"),
                WebUIFragment::component("app"),
                WebUIFragment::raw("</body></html>"),
            ],
            contains_boundary: false,
        },
    );

    fragments.insert(
        "app".to_string(),
        FragmentList {
            fragments: vec![
                WebUIFragment::raw("<div class=\"app\"><header><h1>"),
                WebUIFragment::signal("title", false),
                WebUIFragment::raw("</h1><span>"),
                WebUIFragment::signal("items.length", false),
                WebUIFragment::raw(" items</span></header><ul>"),
                WebUIFragment::for_loop("item", "items", "item-component"),
                WebUIFragment::raw("</ul></div>"),
            ],
            contains_boundary: false,
        },
    );

    fragments.insert(
        "item-component".to_string(),
        FragmentList {
            fragments: vec![
                WebUIFragment::raw("<li"),
                WebUIFragment::attribute("data-id", "item.id"),
                WebUIFragment::raw("><span>"),
                WebUIFragment::signal("item.name", false),
                WebUIFragment::raw("</span>"),
                WebUIFragment::if_cond(ConditionExpr::identifier("item.enabled"), "enabled-badge"),
                WebUIFragment::raw("</li>"),
            ],
            contains_boundary: false,
        },
    );

    fragments.insert(
        "enabled-badge".to_string(),
        FragmentList {
            fragments: vec![WebUIFragment::raw("<span class=\"badge\">✓</span>")],
            contains_boundary: false,
        },
    );

    Protocol::new(WebUIProtocol::new(fragments))
}

fn handler_nested_components_bench(c: &mut Criterion) {
    let mut group = c.benchmark_group("handler_nested_components");
    let protocol = build_nested_component_protocol();
    let state = build_state(50);

    let handler = WebUIHandler::new();
    let mut writer = BenchWriter::new(8 * 1024);
    handler
        .render(
            &protocol,
            &state,
            &RenderOptions::new("index.html", "/"),
            &mut writer,
        )
        .unwrap_or_else(|error| panic!("nested components warmup failed: {error}"));
    group.throughput(Throughput::Bytes(writer.len() as u64));

    group.bench_function("three_levels_50_items", |b| {
        let h = WebUIHandler::new();
        let mut w = BenchWriter::new(8 * 1024);
        b.iter(|| {
            w.clear();
            h.render(
                black_box(&protocol),
                black_box(&state),
                &RenderOptions::new("index.html", "/"),
                &mut w,
            )
            .unwrap_or_else(|error| panic!("nested components render failed: {error}"));
        });
    });

    group.finish();
}

fn build_signal_protocol(signal_path: &str) -> Protocol {
    let mut fragments = HashMap::new();
    fragments.insert(
        "index.html".to_string(),
        FragmentList {
            fragments: vec![
                WebUIFragment::raw("<div>"),
                WebUIFragment::signal(signal_path, false),
                WebUIFragment::raw("</div>"),
            ],
            contains_boundary: false,
        },
    );
    Protocol::new(WebUIProtocol::new(fragments))
}

fn handler_state_depth_bench(c: &mut Criterion) {
    let mut group = c.benchmark_group("handler_state_depth");

    let cases: Vec<(&str, &str, Value)> = vec![
        ("flat", "name", json!({"name": "Alice"})),
        ("depth_2", "user.name", json!({"user": {"name": "Alice"}})),
        (
            "depth_3",
            "user.profile.name",
            json!({"user": {"profile": {"name": "Alice"}}}),
        ),
        (
            "depth_5",
            "a.b.c.d.name",
            json!({"a": {"b": {"c": {"d": {"name": "Alice"}}}}}),
        ),
    ];

    for (label, path, state) in &cases {
        let protocol = build_signal_protocol(path);

        group.bench_function(*label, |b| {
            let h = WebUIHandler::new();
            let mut w = BenchWriter::new(256);
            b.iter(|| {
                w.clear();
                h.render(
                    black_box(&protocol),
                    black_box(state),
                    &RenderOptions::new("index.html", "/"),
                    &mut w,
                )
                .unwrap_or_else(|error| panic!("state depth render failed for {label}: {error}"));
            });
        });
    }

    group.finish();
}

/// Build a document whose fragment graph is wide enough that per-fragment
/// preparation cost is visible, including a large sibling route table.
fn build_construction_document(fragment_count: usize, routes_per_page: usize) -> WebUIProtocol {
    let mut fragments = HashMap::new();

    let mut root = Vec::with_capacity(fragment_count + routes_per_page);
    for route in 0..routes_per_page {
        root.push(WebUIFragment::route(
            format!("/section-{route}"),
            format!("route-{route}.html"),
        ));
        fragments.insert(
            format!("route-{route}.html"),
            FragmentList {
                fragments: vec![WebUIFragment::raw("<p>route body</p>")],
                contains_boundary: false,
            },
        );
    }
    for page in 0..fragment_count {
        root.push(WebUIFragment::component(format!("card-{page}")));
        fragments.insert(
            format!("card-{page}"),
            FragmentList {
                fragments: vec![
                    WebUIFragment::attribute("data-card-label", format!("card {page}")),
                    WebUIFragment::attribute(":card-detail-text", format!("detail {page}")),
                    WebUIFragment::signal(format!("cards.{page}.title"), false),
                    WebUIFragment::raw("</div>"),
                ],
                contains_boundary: false,
            },
        );
    }

    fragments.insert(
        "index.html".to_string(),
        FragmentList {
            fragments: root,
            contains_boundary: false,
        },
    );

    WebUIProtocol::new(fragments)
}

/// Control for the load-time half of the render fragment index: a render-time
/// win must not be paid for by a disproportionate `Protocol::new` regression.
fn handler_protocol_construction_bench(c: &mut Criterion) {
    let mut group = c.benchmark_group("handler_protocol_construction");

    let cases = [
        ("small_16_fragments", 16usize, 0usize),
        ("medium_128_fragments", 128, 0),
        ("wide_128_routes", 32, 128),
        ("large_512_fragments", 512, 0),
    ];

    for (label, fragment_count, routes) in cases {
        let document = build_construction_document(fragment_count, routes);
        group.throughput(Throughput::Elements(
            u64::try_from(document.fragments.len()).unwrap_or(u64::MAX),
        ));
        group.bench_function(label, |b| {
            b.iter_batched(
                || document.clone(),
                |document| Protocol::new(black_box(document)),
                criterion::BatchSize::SmallInput,
            );
        });
    }

    group.finish();
}

fn handler_route_outlet_bench(c: &mut Criterion) {
    use webui_protocol::WebUiFragmentRoute;

    let protocol = Protocol::new(WebUIProtocol::new(HashMap::from([
        (
            "index.html".into(),
            FragmentList {
                fragments: vec![WebUIFragment::route_from(WebUiFragmentRoute {
                    path: "/".into(),
                    fragment_id: "app-shell".into(),
                    children: ["", "projects", "*rest"]
                        .into_iter()
                        .map(|path| WebUiFragmentRoute {
                            path: path.into(),
                            fragment_id: "bench-page".into(),
                            exact: true,
                            ..Default::default()
                        })
                        .collect(),
                    ..Default::default()
                })],
                ..Default::default()
            },
        ),
        (
            "app-shell".into(),
            FragmentList {
                fragments: vec![WebUIFragment::outlet()],
                ..Default::default()
            },
        ),
        (
            "bench-page".into(),
            FragmentList {
                fragments: vec![WebUIFragment::raw("<p>Page</p>")],
                ..Default::default()
            },
        ),
    ])));
    let handler = WebUIHandler::new();
    let state = Value::Null;
    let mut writer = BenchWriter::new(1024);
    let mut group = c.benchmark_group("handler_route_outlet");
    for path in ["/", "/projects", "/missing"] {
        let options = RenderOptions::new("index.html", path);
        writer.clear();
        handler
            .render(&protocol, &state, &options, &mut writer)
            .unwrap_or_else(|error| panic!("route warmup failed: {error}"));
        group.throughput(Throughput::Bytes(writer.len() as u64));
        group.bench_function(path, |b| {
            b.iter(|| {
                writer.clear();
                handler
                    .render(black_box(&protocol), &state, &options, &mut writer)
                    .unwrap_or_else(|error| panic!("route render failed: {error}"));
            });
        });
    }
    group.finish();
}

criterion_group!(
    benches,
    handler_plugin_fast_bench,
    handler_loop_scaling_bench,
    handler_condition_variety_bench,
    handler_nested_components_bench,
    handler_state_depth_bench,
    handler_protocol_construction_bench,
    handler_route_outlet_bench
);
criterion_main!(benches);