uce/site/demo/index.uce
root 4f84ac544d feat: request_perf() worker-side timing hostcall; restore demo System Info
Units run in the wasm sandbox, so my_pid/parent_pid/context.server->request_count
read as sandbox stubs — the demo System Info counters were broken, and there was
no authoritative server-side request timing available to unit code (client-side
measurement cannot see queue/dispatch latency).

Add a request_perf() unit API backed by a new uce_host_request_perf hostcall.
The native worker answers it live, returning a DValue:
  worker_pid, parent_pid, request_count,
  accept_us  = (time_start - time_init)*1e6   (entry -> dispatch wait),
  running_us = (now - time_start)*1e6         (since dispatch, live),
  total_us   = (now - time_init)*1e6          (since the request entered UCE),
  workspace_birth_us.
time_init is captured at request entry (handle_request, with a handle_complete
fallback); a RequestPerfSnapshot {pids, request_count, time_init, time_start} is
threaded from wasm_backend_serve through wasm_worker_serve onto the workspace,
and the hostcall computes the live deltas at call time. Wired like uce_host_units
(sized DValue hostcall): core_hostcalls.syms + sys.cpp/sys.h request_perf().

site/demo/index.uce System Info now uses request_perf() and shows the real worker
PID, an incrementing per-worker request count, and the timing counters.

Implemented via the pi agent (gpt-5.3-codex-spark); a review of the live numbers
caught accept_us mistakenly computed as (now - time_init) (== total_us), fixed to
the dispatch wait (time_start - time_init). Independently verified on the host:
System Info shows non-zero PIDs, incrementing count, accept_us ~50us << total_us
~2.4ms with accept+running==total; run_cli_tests --include-wasm-kill => 87 passed,
0 failed, 0 skipped.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-15 08:18:04 +00:00

108 lines
5.4 KiB
Plaintext

#include "demo_guard.h"
void render_card(String url, String title, String desc)
{
<><a class="test-card" href="<?= url ?>">
<strong><?= title ?></strong>
<span><?= desc ?></span>
</a></>
}
RENDER(Request& context)
{
DValue p;
p.set(context.params);
bool allow_server_demos = test_demo_request_allowed(context);
<><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1"></meta>
<link rel="stylesheet" href='style.css?v=<?= time() ?>'></link>
</head>
<body>
<h1>
<a href="index.uce">UCE Test Suite</a>
<a class="docs-link" href="../doc/index.uce">API Docs &rarr;</a>
</h1>
<div class="test-grid">
<div class="grid-heading">Basics</div>
<? render_card("hello.uce", "Hello World", "Basic output and server time"); ?>
<? render_card("header.uce", "Headers", "HTTP response headers"); ?>
<? if(allow_server_demos) { render_card("working-dir.uce", "Working Directory", "Server paths and cwd"); } ?>
<? if(allow_server_demos) { render_card("error-reporting.uce", "Error Reporting", "Error handling and output"); } ?>
<div class="grid-heading">Data Types &amp; Parsing</div>
<? render_card("dvalue.uce", "DValue", "Dynamic hierarchical data tree"); ?>
<? render_card("json.uce", "JSON", "Parse and encode JSON data"); ?>
<? render_card("xml.uce", "XML", "Structural XML encode/decode with DValue"); ?>
<? render_card("yaml.uce", "YAML", "Concise config files with DValue"); ?>
<? render_card("preprocessor-comments.uce", "Preprocessor Comments", "Regression coverage for comment parsing in templates"); ?>
<? render_card("regex.uce", "Regular Expressions", "PCRE2 matching, captures, replacement, and splitting"); ?>
<? render_card("string.uce", "String", "String operations"); ?>
<? render_card("collections.uce", "Collection Helpers", "Map, filter, group, pick, and omit render data"); ?>
<? render_card("str_replace.uce", "String Replace", "Search and replace in strings"); ?>
<? render_card("utf8.uce", "UTF-8", "Unicode string handling"); ?>
<? render_card("random.uce", "RNG / Noise", "Random generation and noise"); ?>
<? render_card("parse_time.uce", "time_parse()", "Date and time parsing"); ?>
<div class="grid-heading">HTTP &amp; Forms</div>
<? render_card("post.uce", "Form POST", "URL-encoded form submission"); ?>
<? render_card("post-multipart.uce", "Multipart POST", "File upload and multipart forms"); ?>
<? render_card("uri.uce", "URI", "URI encoding and decoding"); ?>
<? render_card("cookie.uce", "Cookies", "Read and write browser cookies"); ?>
<? render_card("session.uce", "Session", "Server-side session state"); ?>
<div class="grid-heading">Storage &amp; I/O</div>
<? render_card("fileio.uce", "File I/O", "Read and write files"); ?>
<? if(allow_server_demos) { render_card("zip.uce", "ZIP", "Create, list, read, and extract ZIP archives"); } ?>
<? if(allow_server_demos) { render_card("file_append.uce", "File Append", "Append data to files"); } ?>
<? if(allow_server_demos) { render_card("shell.uce", "Shell", "Execute shell commands"); } ?>
<? if(allow_server_demos) { render_card("memcached.uce", "Memcached", "Memcached key-value store"); } ?>
<? if(allow_server_demos) { render_card("sqlite.uce", "SQLite", "Embedded SQLite database connector"); } ?>
<? if(allow_server_demos) { render_card("mysql.uce", "MySQL", "MySQL database connector"); } ?>
<div class="grid-heading">Advanced</div>
<? render_card("call_file.uce", "unit_call()", "Dynamic file inclusion"); ?>
<? render_card("components.uce", "Components", "Reusable component system"); ?>
<? render_card("once-init.uce", "ONCE / INIT", "Unit lifecycle hooks for worker load and request entry"); ?>
<? render_card("markdown.uce", "Markdown", "Markdown parsing with components"); ?>
<? render_card("script.uce", "Script", "UCE script integration"); ?>
<? render_card("websockets.ws.uce", "WebSockets", "Real-time WebSocket chat"); ?>
<? render_card("unit-browser.uce", "Unit Browser", "units_list(), unit_info(), unit_compile()"); ?>
<? if(allow_server_demos) { render_card("task.uce", "Task", "Background task execution"); } ?>
<? if(allow_server_demos) { render_card("task_repeat.uce", "Task Repeat", "Recurring task scheduling"); } ?>
</div>
<? if(!allow_server_demos) { ?>
<div class="system-info">
<h3>Restricted On Public Access</h3>
<pre>Local-only demos are hidden here because they can execute shell commands, access infrastructure services, mutate server state, compile units, or expose internal runtime details.</pre>
</div>
<? } else { ?>
<div class="system-info">
<h3>System Info</h3>
<pre><?
DValue perf = request_perf();
print("Worker PID: ", (u64)perf["worker_pid"].to_u64(), "\n");
print("Parent PID: ", (u64)perf["parent_pid"].to_u64(), "\n");
print("Request #: ", (u64)perf["request_count"].to_u64(), "\n");
print("Accept us: ", (f64)perf["accept_us"].to_f64(), "\n");
print("Running us: ", (f64)perf["running_us"].to_f64(), "\n");
print("Total us: ", (f64)perf["total_us"].to_f64(), "\n");
if(perf["workspace_birth_us"].type != 'S')
print("Workspace birth us: ", (u64)perf["workspace_birth_us"].to_u64(), "\n");
print("Output buffer size: ", context.ob->str().length(), "\n");
?></pre>
</div>
<? } ?>
<details>
<summary>Request Parameters</summary>
<pre><?= (var_dump(p)) ?></pre>
</details>
</body>
</html></>
}