microsoft-webui-handler 0.0.8

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
// 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::FastHydrationPlugin;
use webui_handler::{RenderOptions, ResponseWriter, WebUIHandler};
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(())
    }

    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() -> WebUIProtocol {
    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>"),
            ],
        },
    );

    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"),
                // Simulate parser-plugin payload consumed by FastHydrationPlugin.
                WebUIFragment::plugin((3u32).to_le_bytes().to_vec()),
                WebUIFragment::raw("</x-card>"),
            ],
        },
    );

    fragments.insert(
        "class-template".to_string(),
        FragmentList {
            fragments: vec![
                WebUIFragment::raw("card "),
                WebUIFragment::signal("theme", false),
                WebUIFragment::raw(" size-"),
                WebUIFragment::signal("size", 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>"),
            ],
        },
    );

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

    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 mut baseline_handler = WebUIHandler::new();
    let mut baseline_writer = BenchWriter::new(16 * 1024);
    baseline_handler
        .handle(
            &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
                .handle(
                    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(FastHydrationPlugin::new()));
        let mut writer = BenchWriter::new(24 * 1024);

        b.iter(|| {
            writer.clear();
            handler
                .handle(
                    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
            .handle(
                &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 mut h = WebUIHandler::new();
            let mut w = BenchWriter::new(count * 80 + 1024);

            b.iter(|| {
                w.clear();
                h.handle(
                    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() -> WebUIProtocol {
    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>"),
            ],
        },
    );

    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)],
            },
        );
    }

    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
        .handle(
            &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 mut h = WebUIHandler::new();
        let mut w = BenchWriter::new(1024);
        b.iter(|| {
            w.clear();
            h.handle(
                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 mut h = WebUIHandler::new();
        let mut w = BenchWriter::new(1024);
        b.iter(|| {
            w.clear();
            h.handle(
                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() -> WebUIProtocol {
    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>"),
            ],
        },
    );

    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>"),
            ],
        },
    );

    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>"),
            ],
        },
    );

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

    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
        .handle(
            &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 mut h = WebUIHandler::new();
        let mut w = BenchWriter::new(8 * 1024);
        b.iter(|| {
            w.clear();
            h.handle(
                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) -> WebUIProtocol {
    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>"),
            ],
        },
    );
    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 mut h = WebUIHandler::new();
            let mut w = BenchWriter::new(256);
            b.iter(|| {
                w.clear();
                h.handle(
                    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();
}

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