Benchmarking Header Parsers¶
The ietfparse.test package includes a packaged benchmark utility for the
public HTTP header parsers supported by this project. It exists to answer a
simple question: when parser behavior changes, what happened to runtime cost on
representative header values?
What it benchmarks¶
Version 1 benchmarks these public parser entry points:
- ietfparse.headers.parse_accept
- ietfparse.headers.parse_accept_charset
- ietfparse.headers.parse_accept_encoding
- ietfparse.headers.parse_accept_language
- ietfparse.headers.parse_cache_control
- ietfparse.headers.parse_content_type
- ietfparse.headers.parse_forwarded
- ietfparse.headers.parse_link
The fixture set is packaged with the wheel and grouped into three workload classes for every supported header:
realisticuses production-like values.complexuses valid values with quoting, parameters, ordering, escaping, or combinations that exercise harder parsing paths.largeuses valid values sized for larger header payloads, capped at8192bytes per sample.
Benchmark behavior¶
Before timing starts, the runner validates every selected sample by calling the public parser. Invalid benchmark fixtures fail fast instead of contributing parse errors to the timing loop.
Timing uses a simple deterministic loop:
time.perf_counter_ns()for elapsed time measurement- fixed
iterations * repeat - median elapsed time across repeats
For each selected header and workload, the runner reports:
- sample count
- total input bytes
- median elapsed nanoseconds
- nanoseconds per parser call
- calls per second
Parsed results are consumed during the loop so the benchmark measures real parser work rather than a trivially discarded result.
Result model¶
Each JSON result record includes:
headerworkloadimplementationsample_countbyte_countrepeatiterationsmedian_elapsed_nsns_per_callcalls_per_second
The field stays stable whether the benchmark runs the workspace parser or a compatible third-party implementation.
Comparing implementations¶
The benchmark CLI can compare multiple implementations when compatible parser surfaces exist. Today that includes:
workspacefor every packaged benchmark fixturewerkzeugfor the Accept-family headers viawerkzeug.http.parse_accept_headerand forCache-Controlviawerkzeug.http.parse_cache_control_headerrequestsforrequests.utils.parse_header_linkshttpxforhttpx.Response.links
Use --implementation one or more times to select implementations:
$ ietfparse-test run --header accept --implementation workspace --implementation werkzeug
$ ietfparse-test run --header accept-language --implementation workspace --implementation werkzeug
$ ietfparse-test run --header link --implementation workspace --implementation requests
$ ietfparse-test run --header link --implementation workspace --implementation httpx
If omitted, run defaults to the workspace parser implementation.
The werkzeug implementation only supports accept, accept-charset,
accept-encoding, accept-language, and cache-control. The requests and
httpx implementations only support link. Selecting any implementation for
other headers fails fast with a validation error.
To compare multiple implementations only on the headers they all support, use
compare implementation. It always includes workspace, computes the shared
header set automatically, and reports one row per header/workload with
per-implementation ns/call plus ratios against workspace:
$ ietfparse-test compare implementation werkzeug
$ ietfparse-test compare implementation werkzeug --workload realistic
To summarize release-to-release timing changes from saved
run --format json or compare implementation --format json outputs, use
diff. It compares the same header/workload rows across two files and reports
old/new ns/call plus the ratio and percentage change for each implementation:
For behavioral comparisons, use the dedicated comparison commands:
compare linkruns curatedLinkparsing edge cases through the available parser implementations.compare acceptruns curatedAcceptnegotiation cases through ietfparse.algorithms.select_content_type and Werkzeug'sAccept.best_match.compare cache-controlruns curatedCache-Controlparsing cases throughietfparse.headers.parse_cache_controland Werkzeug'sparse_cache_control_header.
Both commands emit either Rich summary output or detailed JSON:
$ ietfparse-test compare link
$ ietfparse-test compare link --format json
$ ietfparse-test compare accept
$ ietfparse-test compare accept --format json
$ ietfparse-test compare cache-control
$ ietfparse-test compare cache-control --format json
Using the utility¶
The benchmark CLI is part of the optional tests extra:
Once installed, run it either as a console script or as a module:
Use list to inspect available fixtures:
Use run to execute one or more benchmark selections:
$ ietfparse-test run
$ ietfparse-test run --header accept --workload realistic
$ ietfparse-test run --header accept --implementation workspace --implementation werkzeug
$ ietfparse-test run --header link --workload complex --iterations 5000 --repeat 5
$ ietfparse-test run --header link --implementation workspace --implementation requests
$ ietfparse-test run --header link --implementation workspace --implementation httpx
$ ietfparse-test compare implementation werkzeug --format json
$ ietfparse-test diff old.json new.json
$ ietfparse-test compare link --format json
$ ietfparse-test compare accept --format json
$ ietfparse-test compare cache-control --format json
$ python -m ietfparse.test run --format json
The --header and --workload options may be repeated. If omitted, the CLI
benchmarks every supported header and workload combination.
Interactive terminals default to Rich output. Non-interactive stdout defaults
to JSON. Passing --format rich or --format json overrides the default.