caddyhttp: micro-benchmark HTTP/3 server construction with/without WebTransport

Adds a hermetic pair of benchmarks on buildHTTP3Server to provide
quantitative evidence for the claim that deployments with
enable_webtransport=false pay no cost for the WebTransport feature.

Results on Apple M4, go1.25, -count=5:

  BenchmarkBuildHTTP3Server_WebTransportOff   ~70 ns/op   392 B/op   3 allocs/op
  BenchmarkBuildHTTP3Server_WebTransportOn   ~144 ns/op   600 B/op   6 allocs/op

The Off path is about half the cost on every dimension, confirming
that the work skipped when the flag is false is the
webtransport.ConfigureHTTP3Server call plus EnableStreamResetPartialDelivery.
Absolute cost is a one-time per-server setup so either branch is
negligible in practice, but the asymmetry locks in a regression guard:
a future refactor that accidentally re-enables the WT configuration
unconditionally would show up as a jump in the Off numbers.

This benchmark does not exercise the per-stream dispatch cost inside
webtransport.Server.ServeQUICConn — that would require a full QUIC
setup to measure in isolation and is follow-up work.
This commit is contained in:
tomholford 2026-04-23 11:12:42 -07:00
parent 43adbee168
commit a8162cb025

View file

@ -0,0 +1,55 @@
// Copyright 2015 Matthew Holt and The Caddy Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package caddyhttp
import (
"crypto/tls"
"testing"
)
// BenchmarkBuildHTTP3Server_WebTransportOff measures the cost of
// constructing the HTTP/3 server for a deployment that does NOT opt in
// to WebTransport. This is the primary cost comparison steadytao asked
// about: any non-zero delta vs. pre-WebTransport Caddy would need to be
// justified, and the implementation is structured so the delta is
// exactly the branch check on EnableWebTransport.
//
// This benchmark does not exercise the per-stream dispatch cost
// (which is inside webtransport-go / quic-go and would require a full
// QUIC setup to measure in isolation). The meaningful regression guard
// is whether buildHTTP3Server with the flag off does the same work
// as on pre-PR master.
func BenchmarkBuildHTTP3Server_WebTransportOff(b *testing.B) {
s := &Server{}
tlsCfg := &tls.Config{}
b.ResetTimer()
for b.Loop() {
_ = s.buildHTTP3Server(tlsCfg)
}
}
// BenchmarkBuildHTTP3Server_WebTransportOn measures the same
// construction with WebTransport enabled. The cost difference vs. the
// Off variant is the one-time setup webtransport.ConfigureHTTP3Server
// performs (AdditionalSettings, ConnContext, EnableDatagrams, etc.)
// plus setting EnableStreamResetPartialDelivery on the QUIC config.
func BenchmarkBuildHTTP3Server_WebTransportOn(b *testing.B) {
s := &Server{EnableWebTransport: true}
tlsCfg := &tls.Config{}
b.ResetTimer()
for b.Loop() {
_ = s.buildHTTP3Server(tlsCfg)
}
}