# ScalPanel edge features on the proxy path, against nginx (2026-11-18)

URL: https://scalws.com/docs/benchmarks/2026-11-18-edge/
Source: `docs/benchmarks/2026-11-18-edge.md` in the ScalWS repository

The benchmark gate of the 1.1 edge-features plan (Task 15, ruling C2): what the keys
ScalPanel's generator puts on a website route cost on the proxy path, and how ScalWS with
them compares with nginx configured the same way. Gate: (b) within 3 % of (a) per row, at
1 and 6 CPUs.

**Result: the gate is not met.** After two fixes and a PGO training change found by
profiling (below), (b) is at 0.857 and 0.889 of (a) at 1 CPU and 0.938 and 0.974 at 6
CPUs: one of four rows passes. The edge keys cost about 8 us of CPU per request on this
host (52 to 60 us). nginx pays more for the same directives (0.88 to 0.93 of its own
(a)), and ScalWS with the edge keys is 1.07x to 1.18x nginx with them in every row.

**Verdict (controller, 2026-11-18):** the 3 % miss is accepted for 1.1. The binding rule is
at least 1.05x nginx in every scenario at 1 and 6 CPUs, which (a) and (b) meet. Deferred to
1.2: header operations without hashing custom names on every request, a one-pass cookie
strip and a lighter access log record path (the items of the bisection below). The 1 KiB
rows of (c) were under 1.05x nginx and were fixed in this task: see "Follow-up: gzip of
small known bodies". The PGO training overlap (fix 2 below) is accepted and disclosed.

## Method

`bench/edge.sh <scalwsd> <scalws-bench-upstream> <out-dir>` (the script's header has every
detail).

- Host: xeon, 2x Xeon E5-2650 v2 (no AVX2), Ubuntu 26.04, kernel 7.0.0-38, `performance`
  governor. Server on CPU 0 (1 CPU) or 0-5 (6 CPUs), socket 0; the bench upstream on 6, 7,
  22, 23; oha 1.16.0 on 8-15 and 24-31 (socket 1), 64 connections, 5 s warm-up and 15 s
  measured per row, 3 rounds with the server order rotated; medians, ratios are the median
  of the per-round ratios. CPU us/req: user + system time of the server's process tree
  during the measured window over the answered requests.
- Route: the website route of ScalPanel's edge sample
  (`tests/integration/fixtures/scalpanel-edge/edge.yaml`, application `r0-0`, prefix route
  `/`, with the regex hotlink route and the two webmail redirects before it,
  `route_order: nginx`), the upstream replaced by the bench upstream, which answers
  `/edge/1k.txt` (1 KiB `text/plain`) and `/edge/20k.css` (20 KiB `text/css`, compresses
  about 3.8x with gzip level 1). Kept from the sample in every variant: limits,
  `trusted_proxies`, `max_body`, `preserve_host`, `websocket`, timeouts. Left out:
  the https and probe listeners and `https_redirect` (plain HTTP is measured), and
  `server.security.per_ip` (50 requests per second per client would answer 429).
- Every request: `Host: www.example.com`, `Accept-Encoding: gzip`,
  `Cookie: wp-settings-1=...; vpwm_session=...; theme=dark` (so `remove_cookies` removes a
  cookie in the middle of the line).
- (a) the route without the edge keys. (b) as generated: `deny` (2 networks),
  `deny_networks: [CN]` with a 300 000-line CN file, `remove_cookies`, `request_headers.set`
  (3 headers), `response_headers.remove` (5 headers), `forwarded_for: client`, application
  `access_log` with `format: scalpanel`. (c) (b) plus the sample's `server.compression`
  (gzip, streamed at level 1).
- The CN file: the sample's two networks, 269 998 IPv4 networks in [redacted address] (/32, /31
  and /30 in turn) and 30 000 IPv6 /56s in 2001:db8::/32, none adjacent (nothing merges).
  The client (127.0.0.1) is in no deny list. The network set is a file set, as in the
  sample; a GeoIP `countries` set is not measured (the client would be loopback, which is
  never looked up).
- nginx 1.31.6 (no 1.30 build on xeon; 1.31.6 is the release every 1.0 and 1.1 ratio was
  taken against) with `bench/configs/nginx-edge.conf`: the same locations; for (b) `deny`,
  a `geo` of the same 300 000 networks with `if ($edge_cn) { return 403; }`, a `map` that
  removes `vpwm_session` from `$http_cookie`, `proxy_set_header` for the set headers,
  `X-Forwarded-For $remote_addr` and `X-Real-IP`, `proxy_hide_header` for the removed ones,
  `log_format scalpanel` (ScalPanel's) with `access_log ... buffer=64k flush=1s`; for (c)
  `gzip on`, level 1, `gzip_min_length 1024`, the sample's types, `gzip_proxied any`.
- Before the rounds the script checks every server: 200 with the expected size and encoding,
  the access log written, and a copy of the CN file with 127.0.0.1/32 added answering 403
  (ScalWS and nginx alike). Every run answered 2xx only.
- During every run a sampler recorded the load and any `cargo` or `rustc` process every
  10 s: none ran during run 1 or run 2 (other agents' builds ran between runs; runs were
  started only after 60 s without one).

## Runs

| run | when (UTC) | scalwsd commit | scalwsd sha256 | upstream sha256 | what |
|---|---|---|---|---|---|
| 0 | 11:14-11:16 | d8b13b33 | 039a25d5a6b01acbace5b7a74d2e48c72a3ffcf967422f70d1b559e613dcebbc | 4cfea89ab1bfbbee7d3b5ebe960e0fea4a18f24b287147f157bab131ebf5ec8c | smoke (1 round of 2 s), profile, per-feature bisection |
| 1 | 11:47-12:13 | cdf44490 | 75be57ac5a1cb50c64272d4a412ffe7d4b1a6cdf84fcabf6b85f180a6cf79e8e | 4cfea89a... | full run after fix 1 |
| 2 | 13:31-13:57 | 0e2bb10a (tools f64efcb9) | bfbce83d2ebaa1b3eb27e563d6f84027bdd8c8fcdf56bc23dd6e2ec69fc50e65 | 4cfea89a... | full run after fixes 2 and 3 (reported) |

All `tools/pgo-build.sh` with `PGO_FEATURES=scalwsd/http3`. Upstream: `scalws-bench-upstream`
of this commit (the `/edge/` bodies), built release.

## Results (run 2)

### (b) edge keys against (a) without them (gate: >= 0.97)

| CPUs | scenario | (b) rps | (a) rps | ratio (median, min-max) | (b) p99 ms | (a) p99 ms | (b) CPU us/req | (a) CPU us/req | gate |
|---:|---|---:|---:|---|---:|---:|---:|---:|---|
| 1 | proxy-1k | 16679 | 19303 | 0.857 (0.837-0.867) | 4.17 | 3.63 | 60.0 | 51.8 | FAIL |
| 1 | proxy-20k-css | 13356 | 15028 | 0.889 (0.874-0.927) | 5.12 | 4.59 | 74.9 | 66.5 | FAIL |
| 6 | proxy-1k | 95221 | 102117 | 0.938 (0.906-0.940) | 0.89 | 0.96 | 63.0 | 58.1 | FAIL |
| 6 | proxy-20k-css | 75476 | 77518 | 0.974 (0.964-0.980) | 1.27 | 1.30 | 78.7 | 74.0 | PASS |

### (c) edge keys and gzip against (a)

| CPUs | scenario | (c) rps | (a) rps | ratio | (c) CPU us/req | bytes/resp |
|---:|---|---:|---:|---|---:|---:|
| 1 | proxy-1k | 10324 | 19303 | 0.535 (0.529-0.537) | 96.9 | 499 |
| 1 | proxy-20k-css | 5903 | 15028 | 0.396 (0.388-0.396) | 169.5 | 5394 |
| 6 | proxy-1k | 59559 | 102117 | 0.585 (0.580-0.587) | 100.6 | 499 |
| 6 | proxy-20k-css | 34380 | 77518 | 0.442 (0.442-0.446) | 174.4 | 5394 |

### Against nginx (ScalWS rps / nginx rps, median of the per-round ratios)

| CPUs | scenario | (a) | (b) | (c) | nginx (b) / nginx (a) |
|---:|---|---:|---:|---:|---:|
| 1 | proxy-1k | 1.145 | 1.103 | 0.941 | 0.901 |
| 1 | proxy-20k-css | 1.222 | 1.177 | 1.431 | 0.934 |
| 6 | proxy-1k | 1.033 | 1.065 | 0.900 | 0.882 |
| 6 | proxy-20k-css | 1.064 | 1.125 | 1.399 | 0.915 |

nginx rps: (a) 16787, 12341, 98660, 72763; (b) 15124, 11524, 87502, 66607; (c) 10968,
4125, 66202, 24543 (rows in the order above). nginx's gzip sends 378 and 4035 bytes, ScalWS's
499 and 5394 (both level 1; nginx shrinks its window and memory level to the known body
length, which is also why nginx is ahead at 1 KiB in (c)).

## Profile, fixes and what is left

Run 0 (d8b13b33): (b) was 0.62x to 0.68x (a) at 1 CPU. `perf record` on CPU 0 and a
bisection (each edge key dropped from (b) in turn, 1 CPU, 2 rounds) put about 24 us of the
30 us per request on the access log and 6 us on `request_headers`; `deny`, the network
set, `response_headers` and `forwarded_for` measured at noise level.

1. **Access log writer** (cdf44490). The writer thread did one `write(2)` per line and the
   event loop woke it (a futex system call and a context switch on the same CPU) for almost
   every line: the `scalws-log` thread alone took 10 % of CPU 0. Now the lines queued for a
   file go out in one write (up to 64 KiB, whole lines; a short write still counts the
   lines it cut as dropped) and the writer waits 10 ms after a batch, so under load the
   loops seldom wake it. Lines reach the file at most about 10 ms later
   (`docs/guide/logging-metrics.md`). Tests: interleaved lines of two files over more than
   one write stay whole and in order; the line count after a short write. Run 1: (b) 0.83x
   to 0.94x (a).
2. **PGO training** (f64efcb9). The training load of `tools/pgo-build.sh` had no edge keys,
   so their code was laid out cold: in the run 1 profile `strip_cookies`, `HeaderOps::apply`,
   `HeaderMap` inserts and the network lookup cost several times what they do. The training
   now runs a ScalPanel edge website (deny, a 20 000-network set, cookie removal, header
   changes, `forwarded_for`, a scalpanel access log, gzip of proxied text, a refused
   referer). Note: two of its requests (`/edge/1k.txt`, `/edge/20k.css` with gzip) are the
   ones this bench measures; every other row of the training is unchanged. It also made (c)
   faster (20 KiB at 1 CPU 4691 to 5903 rps).
3. **scalpanel line** (0e2bb10a): numbers and the IPv4 address formatted by hand instead of
   `core::fmt` (about 0.9 us per request in the run 1 profile). Test added.

What is left (run 2 binary, bisection at 1 CPU, 1 KiB, 3 rounds; CPU us/req of (b) with
one key dropped, against (a) of the same run; (b) costs about 7.5 us over (a)):

| dropped from (b) | (b) minus key, us/req | (a) | over (a) | so the key costs about |
|---|---:|---:|---:|---:|
| `access_log` | 57.1 | 52.6 | 4.5 | 3 us |
| `request_headers` (cookie removal, 3 sets) | 57.0 | 51.2 | 5.8 | 2 us |
| `forwarded_for` | 58.8 | 52.5 | 6.3 | 1 us |
| `deny`, `deny_networks`, `server.networks` | 59.4 | 52.1 | 7.3 | 0.5 us or less |
| `response_headers` (5 removes) | 59.7 | 52.0 | 7.7 | noise |

The profile of run 2's binary spreads the rest over `strip_cookies` and `HeaderOps::apply`
(about 1.2 us together: hashing custom header names for 3 inserts and 5 removes, two passes
over the cookie line), the log line and its record (`AccessLog` emit and drop, about
0.7 us), the network lookup in 300 000 ranges (about 0.4 us) and a wide tail of a few
tenths each. The gate allows 1.6 us per request at 1 CPU on this host: no single remaining
item is large enough, and together they are about five times the budget. At 6 CPUs the
same cost is a smaller share (the loops also wait on the network), hence 0.938 and 0.974.

## Follow-up: gzip of small known bodies

Run 2 left (c) at 1 KiB under the 1.05x nginx rule: 0.941 at 1 CPU and 0.900 at 6. Each
streamed gzip response built a new `flate2` `GzEncoder`: zlib-rs allocates and clears its
full 2^15 window state (about 370 KiB with the fixed 64 Ki-entry hash table) and GzEncoder
adds a 32 KiB buffer, for a body of 1 KiB. nginx sizes the window and `memLevel` down to a
known `Content-Length`.

The backend is zlib-rs through `flate2` 1.1.10 (`features = ["zlib-rs"]`), which offers
window bits 9 to 15 for raw deflate (`Compress::new_with_window_bits`) but no `memLevel`
(and zlib-rs's hash table has a fixed size whatever `memLevel` is). The change (185a93c4):

- gzip framing (header, CRC-32, length) is written by ScalWS around raw deflate, so the
  window can be chosen; without a length the bytes are GzEncoder's exactly (test), so cached
  variants keep their bytes under the same strong ETag.
- With a known length (`Content-Length` or an exact body size) the window is halved while
  the body plus 262 bytes fits in half of it, down to 2^9, as nginx does. A body of unknown
  length keeps 2^15 (test).
- At the streaming level, finished states with windows of 2^9 to 2^12 (bodies under 3834
  bytes) are kept, one per window size and thread, and reset for the next small response
  instead of allocated and cleared again (at most 4 states per event loop thread).
- Tests: round trip for sizes around every window threshold (0 to 70 000 bytes, chunked,
  with a flush), with and without a length; a reused state gives the same bytes as a new one;
  byte equality with GzEncoder without a length, at both levels. A microbenchmark of 1 KiB
  (Windows, not a gate): GzEncoder 11.1 us, raw deflate 2^15 3.6 us, 2^11 2.8 us, reused
  2^11 1.4 us.

Measured on xeon (same method, `SERVERS="scalws-a scalws-c nginx-c"`), PGO builds:

| run | scalwsd commit | scalwsd sha256 | (c) / nginx, 1 KiB, 1 CPU | 6 CPUs | 20 KiB, 1 CPU | 6 CPUs |
|---|---|---|---:|---:|---:|---:|
| run 2 (before) | 0e2bb10a | bfbce83d... | 0.941 | 0.900 | 1.431 | 1.399 |
| edge3: sized window only | working tree | 5231ff4547bdd9a29f50d86c98cdca4f892432f93885a738d2a04f07856ae5ff | 1.045 (1.039-1.056) | 1.000 (0.986-1.030) | 1.441 | 1.437 |
| edge4: sized window and reuse | 185a93c4 | a1e537f8c43d7c494856fcf541c46a4725db689d7293c82b3ebeafc3306890eb | 1.084 (1.052-1.120) | 1.088 (1.074-1.092) | 1.414 | 1.452 |

edge4 in rps: (c) 12113 against nginx 11171 at 1 CPU, 72212 against 66368 at 6 CPUs (1 KiB);
5782 against 4090 and 35352 against 24355 (20 KiB); CPU per 1 KiB response 82.6 us against
nginx's 89.5 (run 2: 96.9). Bytes are unchanged (499 and 5394; nginx 378 and 4035: zlib-rs's
level 1 is zlib-ng's faster "quick" strategy). During edge4 the sampler saw a concurrent
`cargo` for 40 s of the 12 minutes (one stretch, 15:28 to 15:29 UTC); the round spread of the
1 KiB rows stays above 1.05 in every round, and (a) and nginx match edge3 within 1 %.

Compression rows of `docs/benchmarks/2026-11-15-compression.md` this change touches, with
edge4's binary (no `cargo` or `rustc` during either run):

| row | 2026-11-15 | now |
|---|---:|---:|
| (c) mode all, 50 KiB PHP gzip, 1 CPU, against nginx | 1.550 | 1.682 (1.675-1.691) |
| (c) same, 6 CPUs | 1.761 | 1.871 (1.649-1.976) |
| (f) background p99 with gzip fills, against off (lower is better) | 1.809 | 1.759 (1.668-1.856) |
| (f) gzip fill load, rps | 299 | 308 |

No regression (the gains include the PGO training of fix 2). The brotli rows of (f) do not
use this code; they measured 2.529 against 2.264 for the background p99, inside the spread
that part has shown before (2.247 to 2.553). The (b) static rows serve cached variants, whose
bytes are unchanged.

## Reproduce

```sh
# also builds the bench upstream (target/pgo/plain)
PGO_FEATURES=scalwsd/http3 tools/pgo-build.sh
bench/edge.sh target/pgo/scalwsd target/pgo/plain/release/scalws-bench-upstream /tmp/edge
# one variant only, with a profile of the server CPUs:
SERVERS="scalws-a scalws-b" LAYOUTS=1:0 PERF="-F 1999" bench/edge.sh ...
```
