Skip to content

encode: add standard benchmark and conformance harness - #7804

Merged
mholt merged 1 commit into
caddyserver:masterfrom
ottenhoff:encode-correctness-harness
Jun 11, 2026
Merged

mholt merged 1 commit into
caddyserver:masterfrom
ottenhoff:encode-correctness-harness

Conversation

@ottenhoff

Copy link
Copy Markdown
Contributor

This came from a discussion in #7699 where @steadytao wrote:

I do not think we have a single standard benchmark suite for this. A useful harness would compare against Caddy’s gzip/zstd paths across HTML, JSON, JS and CSS with several concurrency levels and Brotli levels plus correctness tests around Reset, Flush, Close, Vary, ETags, 304/HEAD/range/WebSocket behaviour and minimum_length.

I think the intent is basically: how can we assess a new encoder without a sense of correct handling.

So this is a shared encode_test harness with HTML/JSON/JS/CSS payloads taken from caddyserver.com website.

Benchmarks:

  • BenchmarkStandardEncodingPayloads: raw encoder NewEncoder/Write/Close path
  • BenchmarkEncodeHandlerCorpus: full Encode.ServeHTTP middleware path
  • Grid: 4 payloads × gzip levels 1/5/9 × zstd fastest/default/best
  • Each subtest runs with 4 parallel workers; compare runs on MB/s and allocs/op

Conformance tests:

  • Encoder contract: Reset, Flush, Close, and pool-style Reset-after-Close reuse
  • Corpus HTTP encoding: Content-Encoding, Vary, ETag suffix, header stripping
  • Response semantics: minimum_length, 304, HEAD, range, WebSocket bypass, If-None-Match rewrite, Cache-Control no-transform, content-type matcher rejection

Assistance Disclosure

I wrote the plan based on @steadytao post on #7699 and had Codex GPT 5.5-xhigh and Composer 2.5 implement my plan.

@ottenhoff

Copy link
Copy Markdown
Contributor Author
goos: darwin
goarch: arm64
pkg: github.com/caddyserver/caddy/v2/modules/caddyhttp/encode
cpu: Apple M4 Pro

BenchmarkEncodeHandlerCorpus/payload-html/encoder-gzip/compress-level-1-12              	    4346	     26835 ns/op	2708.61 MB/s	    6242 B/op	      10 allocs/op
BenchmarkEncodeHandlerCorpus/payload-html/encoder-gzip/compress-level-5-12              	    3162	     53851 ns/op	1349.74 MB/s	    9404 B/op	      10 allocs/op
BenchmarkEncodeHandlerCorpus/payload-html/encoder-gzip/compress-level-9-12              	     435	    277501 ns/op	 261.93 MB/s	   70945 B/op	      13 allocs/op
BenchmarkEncodeHandlerCorpus/payload-html/encoder-zstd/compress-level-fastest-12        	    4191	     29281 ns/op	2482.33 MB/s	    7905 B/op	      10 allocs/op
BenchmarkEncodeHandlerCorpus/payload-html/encoder-zstd/compress-level-default-12        	    3517	     39310 ns/op	1849.00 MB/s	   15736 B/op	      10 allocs/op
BenchmarkEncodeHandlerCorpus/payload-html/encoder-zstd/compress-level-best-12           	     531	    225351 ns/op	 322.54 MB/s	 1836202 B/op	      14 allocs/op
BenchmarkEncodeHandlerCorpus/payload-json/encoder-gzip/compress-level-1-12              	     874	    156258 ns/op	1878.11 MB/s	   40567 B/op	      11 allocs/op
BenchmarkEncodeHandlerCorpus/payload-json/encoder-gzip/compress-level-5-12              	     556	    224974 ns/op	1304.46 MB/s	   57672 B/op	      12 allocs/op
BenchmarkEncodeHandlerCorpus/payload-json/encoder-gzip/compress-level-9-12              	     134	    826199 ns/op	 355.20 MB/s	  216392 B/op	      20 allocs/op
BenchmarkEncodeHandlerCorpus/payload-json/encoder-zstd/compress-level-fastest-12        	    1260	    117385 ns/op	2500.06 MB/s	   44849 B/op	      13 allocs/op
BenchmarkEncodeHandlerCorpus/payload-json/encoder-zstd/compress-level-default-12        	     805	    184375 ns/op	1591.70 MB/s	  135120 B/op	      15 allocs/op
BenchmarkEncodeHandlerCorpus/payload-json/encoder-zstd/compress-level-best-12           	     126	    837183 ns/op	 350.54 MB/s	 8656840 B/op	      31 allocs/op
BenchmarkEncodeHandlerCorpus/payload-js/encoder-gzip/compress-level-1-12                	    1394	    140602 ns/op	1122.25 MB/s	   28431 B/op	      11 allocs/op
BenchmarkEncodeHandlerCorpus/payload-js/encoder-gzip/compress-level-5-12                	     586	    241300 ns/op	 653.92 MB/s	   76045 B/op	      12 allocs/op
BenchmarkEncodeHandlerCorpus/payload-js/encoder-gzip/compress-level-9-12                	     202	    559961 ns/op	 281.79 MB/s	  174517 B/op	      17 allocs/op
BenchmarkEncodeHandlerCorpus/payload-js/encoder-zstd/compress-level-fastest-12          	    1537	    118371 ns/op	1333.03 MB/s	   40778 B/op	      13 allocs/op
BenchmarkEncodeHandlerCorpus/payload-js/encoder-zstd/compress-level-default-12          	     774	    197076 ns/op	 800.66 MB/s	  228731 B/op	      16 allocs/op
BenchmarkEncodeHandlerCorpus/payload-js/encoder-zstd/compress-level-best-12             	      99	   1031647 ns/op	 152.95 MB/s	10853508 B/op	      36 allocs/op
BenchmarkEncodeHandlerCorpus/payload-css/encoder-gzip/compress-level-1-12               	    9120	     14393 ns/op	3734.72 MB/s	    2521 B/op	      10 allocs/op
BenchmarkEncodeHandlerCorpus/payload-css/encoder-gzip/compress-level-5-12               	    6092	     20347 ns/op	2641.79 MB/s	    5157 B/op	      10 allocs/op
BenchmarkEncodeHandlerCorpus/payload-css/encoder-gzip/compress-level-9-12               	     504	    228173 ns/op	 235.58 MB/s	   86043 B/op	      13 allocs/op
BenchmarkEncodeHandlerCorpus/payload-css/encoder-zstd/compress-level-fastest-12         	    9782	     15447 ns/op	3479.86 MB/s	    3431 B/op	      10 allocs/op
BenchmarkEncodeHandlerCorpus/payload-css/encoder-zstd/compress-level-default-12         	    7218	     18212 ns/op	2951.48 MB/s	    9243 B/op	      10 allocs/op
BenchmarkEncodeHandlerCorpus/payload-css/encoder-zstd/compress-level-best-12            	    1580	    122157 ns/op	 440.02 MB/s	  543273 B/op	      11 allocs/op
PASS
ok  	github.com/caddyserver/caddy/v2/modules/caddyhttp/encode	12.242s

@steadytao steadytao left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the mostly right intermediate step to me with benchmarking and conformance coverage first, separate from whether Brotli belongs in core.

Two things I would want fixed before merge:

  • Trailing whitespace in modules/caddyhttp/encode/testdata/caddy_home.html
  • I am not sure we should lock in the current 206 Partial Content behaviour as “conformance”. If a range response is encoded, Content-Range needs careful handling because the original byte range may no longer describe the transferred encoded representation. I would either make range responses bypass encoding in the test expectation or explicitly assert the intended Content-Range behaviour.

Once those are addressed, I think this is useful groundwork for #7699 without us prematurely adding Brotli to core.

@ottenhoff

Copy link
Copy Markdown
Contributor Author

Thanks for the review @steadytao!

  • Trailing whitespace in modules/caddyhttp/encode/testdata/caddy_home.html

Is this file failing a linter? I was thinking we'd want to preserve the source data (caddyserver.com as of June 2026) as opposed to editing and correcting it?

... I would either make range responses bypass encoding in the test expectation ...

Nice find! It seems like you found a bug in encode.go? I think range responses on dynamic content (not pre-compressed static files) must bypass encode.

I think range responses on pre-compressed static files should be valid and easy for Caddy to serve so I would suggest a new test in modules/caddyhttp/fileserver/staticfiles_test.go ... wdyt?

This is a shared encode_test harness with HTML/JSON/JS/CSS payloads taken from caddyserver.com

Benchmarks:
- BenchmarkStandardEncodingPayloads: raw encoder NewEncoder/Write/Close path
- BenchmarkEncodeHandlerCorpus: full Encode.ServeHTTP middleware path
- Grid: 4 payloads × gzip levels 1/5/9 × zstd fastest/default/best
- Each subtest runs with 4 parallel workers; compare runs on MB/s and allocs/op

Conformance tests:
- Encoder contract: Reset, Flush, Close, and pool-style Reset-after-Close reuse
- Corpus HTTP encoding: Content-Encoding, Vary, ETag suffix, header stripping
- Response semantics: minimum_length, 304, HEAD, range, WebSocket bypass,
  If-None-Match rewrite, Cache-Control no-transform, content-type matcher rejection
@ottenhoff
ottenhoff force-pushed the encode-correctness-harness branch from 451d549 to 42de932 Compare June 10, 2026 17:43
@steadytao

Copy link
Copy Markdown
Member

Is this file failing a linter? I was thinking we'd want to preserve the source data (caddyserver.com as of June 2026) as opposed to editing and correcting it?

No, just git diff --check. I am fine with lightly normalising whitespace rather than preserving the exact fetched bytes. The payload only needs to be a representative benchmark corpus; it does not need to be a byte-for-byte archive of the site.

I think range responses on pre-compressed static files should be valid and easy for Caddy to serve so I would suggest a new test in modules/caddyhttp/fileserver/staticfiles_test.go ... wdyt?

Agreed. Dynamic 206 Partial Content responses should not be re-encoded because the range metadata describes the selected representation being served. The separate precompressed file-server test is the right distinction. Serving a range over an existing .gz sidecar is a range over the encoded representation so that case can remain valid.

I am also going to do one more pass over this before approving since this PR now also includes a small runtime behaviour fix rather than only the harness in encode.go.

@ottenhoff

Copy link
Copy Markdown
Contributor Author

Thanks for the review, and yes, we are now changing encode.go behavior subtly ... I believe it is now correct. It's likely that no one has ever run into it before as ranges are usually done across large video files and not on dynmically-compressed material. But potentially sending the partial bytes through the encode just seems always-wrong

@steadytao steadytao left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super simple change in encode ( is correct, don't worry c: ). I am happy with this.

@steadytao

Copy link
Copy Markdown
Member

Will get a secondary opinion on this as it is quite a large change 😁

@mholt @francislavoie @mohammed90 @WeidiDeng bumping this your way for opinions.

@steadytao steadytao added the do not merge ⛔ Not ready yet! label Jun 10, 2026
@francislavoie

Copy link
Copy Markdown
Member

Looks fine to me, just added tests and benchmarks.

@mholt mholt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this is also okay. Big change but it's all tests.

@mholt mholt removed the do not merge ⛔ Not ready yet! label Jun 11, 2026
@mholt
mholt merged commit 997d3f6 into caddyserver:master Jun 11, 2026
34 checks passed
@mholt mholt added this to the v2.11.5 milestone Jun 11, 2026
elee1766 added a commit to elee1766/caddy that referenced this pull request Oct 26, 2026
…e-control-no-transform

Resolve encode.go to keep upstream's If-None-Match list handling without
the no-transform check, drop TestIsEncodeAllowed, and update the
no-transform conformance cases added in caddyserver#7804 to expect encoding.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants