forked from KillingSpark/zstd-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
test(bench): expand zstd benchmark suite #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
polaz
merged 30 commits into
main
from
test/#24-comprehensive-benchmark-suite-against-c-zstd
Mar 28, 2026
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
5138d7a
test(bench): expand zstd benchmark suite
polaz 3a419fa
docs(readme): add benchmark dashboard link
polaz 15a51bd
fix(bench): harden matrix scripts and edge scenarios
polaz 480a307
fix(bench): tighten flamegraph and decode benchmarks
polaz 11f779b
docs(bench): clarify decode benchmark asymmetry rationale
polaz 65cda3a
perf(bench): remove redundant decode buffer fill
polaz aa774de
fix(bench): scope large default to CI and enforce ratio rows
polaz 54b3dd4
feat(bench): add memory and dictionary benchmark reporting
polaz 7e032ba
test(bench): align decompression benchmark paths
polaz bc3bc2f
test(bench): include scenario ids in report tables
polaz 007d523
fix(bench): guard dictionary ratio division
polaz c4e58d1
fix(bench): bound Silesia fixture loading
polaz e5cdee1
docs(bench): clarify memory estimates in reports
polaz 4fd6c11
perf(bench): cache benchmark scenario generation
polaz 0b2813b
chore(bench): drop unused stats_alloc dep
polaz 86f27c8
fix(bench): allow filtered runs without dict rows
polaz 42cfc46
fix(bench): avoid duplicate dict fallback samples
polaz bf8bba5
style(bench): add is_empty for Scenario
polaz a3a54a4
fix(bench): remove needless borrows in scenario loops
polaz c99d33b
fix(bench): sanitize Silesia scenario report fields
polaz c9639b5
perf(bench): bound Silesia dir walk by max_files
polaz d63a2b8
build(bench): ship decode corpus fixture in crate
polaz 60c4ec4
fix(bench): avoid packaging decode corpus fixtures
polaz 26ad87b
fix(bench): parse escaped labels in report script
polaz 6994c8a
fix(bench): pass criterion filter correctly to flamegraph
polaz c42f34c
style(bench): format runtime corpus loader
polaz 63144cc
fix(bench): stabilize corpus fallback scenarios
polaz dc11bd6
fix(bench): gate report precompute and escape labels
polaz 94f1c6d
fix(bench): harden silesia fixture identity and size checks
polaz c2a2988
fix(bench): tighten label escaping and id dedupe guard
polaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,239 @@ | ||
| #!/bin/bash | ||
| # Run compare_ffi benchmarks and produce github-action-benchmark JSON. | ||
| # Output: benchmark-results.json (customSmallerIsBetter format — lower time = better) | ||
| # Run the Criterion benchmark matrix and produce: | ||
| # - benchmark-results.json for github-action-benchmark | ||
| # - benchmark-report.md for human review | ||
| # | ||
| # Output format note: | ||
| # - benchmark JSON uses customSmallerIsBetter (lower ms/iter is better) | ||
| # - report markdown also includes per-scenario compression size + ratio summaries | ||
| set -eo pipefail | ||
|
|
||
| echo "Running benchmarks..." >&2 | ||
| echo "Running benchmark matrix..." >&2 | ||
|
|
||
| # Run criterion benchmarks, capture output | ||
| cargo bench --bench compare_ffi -p structured-zstd -- --output-format bencher | tee /tmp/bench-raw.txt | ||
| if [ -n "${GITHUB_ACTIONS:-}" ] && [ -z "${STRUCTURED_ZSTD_BENCH_LARGE_BYTES:-}" ]; then | ||
| export STRUCTURED_ZSTD_BENCH_LARGE_BYTES=16777216 | ||
| fi | ||
| BENCH_RAW_FILE="$(mktemp -t structured-zstd-bench-raw.XXXXXX)" | ||
| trap 'rm -f "$BENCH_RAW_FILE"' EXIT | ||
|
|
||
| export STRUCTURED_ZSTD_EMIT_REPORT=1 | ||
| cargo bench --bench compare_ffi -p structured-zstd -- --output-format bencher | tee "$BENCH_RAW_FILE" | ||
|
|
||
| echo "Parsing results..." >&2 | ||
|
|
||
| # Parse criterion bencher output into github-action-benchmark JSON | ||
| # Format: "test <name> ... bench: <ns> ns/iter (+/- <variance>)" | ||
| python3 - <<'PYEOF' | ||
| import json, re, sys | ||
|
|
||
| results = [] | ||
| with open("/tmp/bench-raw.txt") as f: | ||
| for line in f: | ||
| m = re.match(r"test (\S+)\s+\.\.\. bench:\s+([\d,]+) ns/iter", line) | ||
| if m: | ||
| name = m.group(1) | ||
| ns = int(m.group(2).replace(",", "")) | ||
| # Convert ns to ms for readability | ||
| BENCH_RAW_FILE="$BENCH_RAW_FILE" python3 - <<'PYEOF' | ||
| import json | ||
| import os | ||
| import re | ||
| import sys | ||
|
|
||
| BENCH_RE = re.compile(r"test (\S+)\s+\.\.\. bench:\s+([\d,]+) ns/iter") | ||
| REPORT_RE = re.compile( | ||
| r'^REPORT scenario=(\S+) label="((?:[^"\\]|\\.)+)" level=(\S+) input_bytes=(\d+) rust_bytes=(\d+) ffi_bytes=(\d+) rust_ratio=([0-9.]+) ffi_ratio=([0-9.]+)$' | ||
| ) | ||
| MEM_RE = re.compile( | ||
| r'^REPORT_MEM scenario=(\S+) label="((?:[^"\\]|\\.)+)" level=(\S+) stage=(\S+) rust_buffer_bytes_estimate=(\d+) ffi_buffer_bytes_estimate=(\d+)$' | ||
| ) | ||
| DICT_RE = re.compile( | ||
| r'^REPORT_DICT scenario=(\S+) label="((?:[^"\\]|\\.)+)" level=(\S+) dict_bytes=(\d+) train_ms=([0-9.]+) ffi_no_dict_bytes=(\d+) ffi_with_dict_bytes=(\d+) ffi_no_dict_ratio=([0-9.]+) ffi_with_dict_ratio=([0-9.]+)$' | ||
| ) | ||
|
|
||
| def unescape_report_label(value): | ||
| output = [] | ||
| i = 0 | ||
| while i < len(value): | ||
| ch = value[i] | ||
| if ch == "\\" and i + 1 < len(value): | ||
| i += 1 | ||
| output.append(value[i]) | ||
| else: | ||
| output.append(ch) | ||
| i += 1 | ||
| return "".join(output) | ||
|
|
||
| def markdown_table_escape(value): | ||
| escaped = value.strip() | ||
| escaped = escaped.replace("\\", "\\\\") | ||
| escaped = escaped.replace("|", "\\|") | ||
| escaped = escaped.replace("`", "\\`") | ||
| escaped = escaped.replace("[", "\\[") | ||
| escaped = escaped.replace("]", "\\]") | ||
| escaped = escaped.replace("*", "\\*") | ||
| escaped = escaped.replace("_", "\\_") | ||
| escaped = escaped.replace("<", "<") | ||
| escaped = escaped.replace(">", ">") | ||
| escaped = escaped.replace("%", "%") | ||
| return escaped.replace("\n", "<br>") | ||
|
|
||
| benchmark_results = [] | ||
| timings = [] | ||
| ratios = [] | ||
| memory_rows = [] | ||
| dictionary_rows = [] | ||
| raw_path = os.environ["BENCH_RAW_FILE"] | ||
|
|
||
| with open(raw_path) as f: | ||
| for raw_line in f: | ||
| line = raw_line.strip() | ||
|
|
||
| bench_match = BENCH_RE.match(line) | ||
| if bench_match: | ||
| name = bench_match.group(1) | ||
| ns = int(bench_match.group(2).replace(",", "")) | ||
| ms = ns / 1_000_000 | ||
| results.append({ | ||
| benchmark_results.append({ | ||
| "name": name, | ||
| "unit": "ms", | ||
| "value": round(ms, 3), | ||
| }) | ||
| timings.append((name, ms)) | ||
| continue | ||
|
|
||
| if not results: | ||
| report_match = REPORT_RE.match(line) | ||
| if report_match: | ||
| scenario, label, level, input_bytes, rust_bytes, ffi_bytes, rust_ratio, ffi_ratio = report_match.groups() | ||
| label = unescape_report_label(label) | ||
| ratios.append({ | ||
| "scenario": scenario, | ||
| "label": label, | ||
| "level": level, | ||
| "input_bytes": int(input_bytes), | ||
| "rust_bytes": int(rust_bytes), | ||
| "ffi_bytes": int(ffi_bytes), | ||
| "rust_ratio": float(rust_ratio), | ||
| "ffi_ratio": float(ffi_ratio), | ||
| }) | ||
| continue | ||
|
|
||
| mem_match = MEM_RE.match(line) | ||
| if mem_match: | ||
| ( | ||
| scenario, | ||
| label, | ||
| level, | ||
| stage, | ||
| rust_buffer_bytes_estimate, | ||
| ffi_buffer_bytes_estimate, | ||
| ) = mem_match.groups() | ||
| label = unescape_report_label(label) | ||
| memory_rows.append({ | ||
| "scenario": scenario, | ||
| "label": label, | ||
| "level": level, | ||
| "stage": stage, | ||
| "rust_buffer_bytes_estimate": int(rust_buffer_bytes_estimate), | ||
| "ffi_buffer_bytes_estimate": int(ffi_buffer_bytes_estimate), | ||
| }) | ||
| continue | ||
|
|
||
| dict_match = DICT_RE.match(line) | ||
| if dict_match: | ||
| ( | ||
| scenario, | ||
| label, | ||
| level, | ||
| dict_bytes, | ||
| train_ms, | ||
| ffi_no_dict_bytes, | ||
| ffi_with_dict_bytes, | ||
| ffi_no_dict_ratio, | ||
| ffi_with_dict_ratio, | ||
| ) = dict_match.groups() | ||
| label = unescape_report_label(label) | ||
| dictionary_rows.append({ | ||
| "scenario": scenario, | ||
| "label": label, | ||
| "level": level, | ||
| "dict_bytes": int(dict_bytes), | ||
| "train_ms": float(train_ms), | ||
| "ffi_no_dict_bytes": int(ffi_no_dict_bytes), | ||
| "ffi_with_dict_bytes": int(ffi_with_dict_bytes), | ||
| "ffi_no_dict_ratio": float(ffi_no_dict_ratio), | ||
| "ffi_with_dict_ratio": float(ffi_with_dict_ratio), | ||
| }) | ||
|
|
||
| if not benchmark_results: | ||
| print("ERROR: No benchmark results parsed!", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
|
polaz marked this conversation as resolved.
|
||
| if not ratios: | ||
| print( | ||
| "ERROR: No REPORT ratio lines parsed; benchmark-report.md would have an empty ratio section.", | ||
| file=sys.stderr, | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
| if not memory_rows: | ||
| print("ERROR: No REPORT_MEM lines parsed; memory section would be empty.", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| if not dictionary_rows: | ||
| print("WARN: No REPORT_DICT lines parsed; dictionary section will be empty.", file=sys.stderr) | ||
|
|
||
| with open("benchmark-results.json", "w") as f: | ||
| json.dump(results, f, indent=2) | ||
| json.dump(benchmark_results, f, indent=2) | ||
|
|
||
| lines = [ | ||
| "# Benchmark Report", | ||
| "", | ||
| "Generated by `.github/scripts/run-benchmarks.sh` from `cargo bench --bench compare_ffi`.", | ||
| "", | ||
| "## Compression Ratios", | ||
| "", | ||
| "| Scenario | Label | Level | Input bytes | Rust bytes | C bytes | Rust ratio | C ratio |", | ||
| "| --- | --- | --- | ---: | ---: | ---: | ---: | ---: |", | ||
| ] | ||
|
|
||
| for row in sorted(ratios, key=lambda item: (item["scenario"], item["level"])): | ||
| label = markdown_table_escape(row["label"]) | ||
| lines.append( | ||
| f'| {row["scenario"]} | {label} | {row["level"]} | {row["input_bytes"]} | {row["rust_bytes"]} | {row["ffi_bytes"]} | {row["rust_ratio"]:.4f} | {row["ffi_ratio"]:.4f} |' | ||
| ) | ||
|
polaz marked this conversation as resolved.
|
||
|
|
||
| lines.extend([ | ||
| "", | ||
| "## Buffer Size Estimates (Input + Output)", | ||
| "", | ||
| "| Scenario | Label | Level | Stage | Rust buffer bytes (estimate) | C buffer bytes (estimate) |", | ||
| "| --- | --- | --- | --- | ---: | ---: |", | ||
| ]) | ||
|
|
||
| for row in sorted(memory_rows, key=lambda item: (item["scenario"], item["level"], item["stage"])): | ||
| label = markdown_table_escape(row["label"]) | ||
| lines.append( | ||
| f'| {row["scenario"]} | {label} | {row["level"]} | {row["stage"]} | {row["rust_buffer_bytes_estimate"]} | {row["ffi_buffer_bytes_estimate"]} |' | ||
| ) | ||
|
|
||
| lines.extend([ | ||
| "", | ||
| "## Dictionary Compression (C FFI)", | ||
| "", | ||
| "| Scenario | Label | Level | Dict bytes | Train ms | C bytes (no dict) | C bytes (with dict) | C ratio (no dict) | C ratio (with dict) |", | ||
| "| --- | --- | --- | ---: | ---: | ---: | ---: | ---: | ---: |", | ||
| ]) | ||
|
|
||
| for row in sorted(dictionary_rows, key=lambda item: (item["scenario"], item["level"])): | ||
| label = markdown_table_escape(row["label"]) | ||
| lines.append( | ||
| f'| {row["scenario"]} | {label} | {row["level"]} | {row["dict_bytes"]} | {row["train_ms"]:.3f} | {row["ffi_no_dict_bytes"]} | {row["ffi_with_dict_bytes"]} | {row["ffi_no_dict_ratio"]:.4f} | {row["ffi_with_dict_ratio"]:.4f} |' | ||
| ) | ||
|
|
||
| lines.extend([ | ||
| "", | ||
| "## Timing Metrics", | ||
| "", | ||
| "| Benchmark | ms/iter |", | ||
| "| --- | ---: |", | ||
| ]) | ||
|
|
||
| for name, ms in sorted(timings): | ||
| lines.append(f"| `{name}` | {ms:.3f} |") | ||
|
|
||
| with open("benchmark-report.md", "w") as f: | ||
| f.write("\n".join(lines) + "\n") | ||
|
|
||
| print(f"Wrote {len(results)} benchmark results to benchmark-results.json", file=sys.stderr) | ||
| for r in results: | ||
| print(f" {r['name']}: {r['value']} {r['unit']}", file=sys.stderr) | ||
| print(f"Wrote {len(benchmark_results)} timing results to benchmark-results.json", file=sys.stderr) | ||
| print(f"Wrote {len(ratios)} ratio rows to benchmark-report.md", file=sys.stderr) | ||
| print(f"Wrote {len(memory_rows)} memory rows to benchmark-report.md", file=sys.stderr) | ||
| print(f"Wrote {len(dictionary_rows)} dictionary rows to benchmark-report.md", file=sys.stderr) | ||
| PYEOF | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,5 +6,7 @@ Cargo.lock | |
| /orig-zstd | ||
| fuzz_decodecorpus | ||
| perf.data* | ||
| benchmark-results.json | ||
| benchmark-report.md | ||
| fuzz/corpus | ||
| .idea | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # Benchmark Suite | ||
|
|
||
| `structured-zstd` keeps its compression/decompression performance tracking in the Criterion bench | ||
| matrix at `zstd/benches/compare_ffi.rs`. | ||
|
|
||
| ## Scenarios | ||
|
|
||
| The current matrix covers: | ||
|
|
||
| - small random payloads (`1 KiB`, `10 KiB`) | ||
| - a small structured log payload (`4 KiB`) | ||
| - a repository corpus fixture (`decodecorpus_files/z000033`) | ||
| - high entropy random payloads (`1 MiB`) | ||
| - low entropy repeated payloads (`1 MiB`) | ||
| - a large structured stream (`100 MiB`) | ||
| - optional Silesia corpus files when `STRUCTURED_ZSTD_SILESIA_DIR=/path/to/silesia` is set | ||
| - load is bounded by `STRUCTURED_ZSTD_SILESIA_MAX_FILES` (default `12`) and | ||
| `STRUCTURED_ZSTD_SILESIA_MAX_FILE_BYTES` (default `67108864`) | ||
|
|
||
| The local default for the large scenario is `100 MiB`. In GitHub Actions, when | ||
| `STRUCTURED_ZSTD_BENCH_LARGE_BYTES` is unset, `.github/scripts/run-benchmarks.sh` defaults it to | ||
| `16 MiB` to keep CI regression runs bounded while still exercising the same code path. | ||
|
|
||
| ## Level Mapping | ||
|
|
||
| The benchmark suite only compares levels that are currently implemented end-to-end in the pure Rust | ||
| encoder: | ||
|
|
||
| - `structured-zstd::Fastest` vs `zstd` level `1` | ||
| - `structured-zstd::Default` vs `zstd` level `3` | ||
|
|
||
| `Better` and `Best` are intentionally excluded until the encoder implements them. | ||
|
|
||
| Dictionary benchmarks are tracked separately with C FFI `with_dict` vs `without_dict` runs, using a | ||
| dictionary trained from scenario samples. Pure Rust dictionary compression is still pending and is | ||
| therefore not part of the pure-Rust-vs-C timing matrix yet. | ||
|
|
||
| ## Commands | ||
|
|
||
| Run the full Criterion matrix: | ||
|
|
||
| ```bash | ||
| cargo bench --bench compare_ffi -p structured-zstd -- --output-format bencher | ||
| ``` | ||
|
|
||
| Generate the CI-style JSON and markdown report locally: | ||
|
|
||
| ```bash | ||
| bash .github/scripts/run-benchmarks.sh | ||
| ``` | ||
|
|
||
| Generate a flamegraph for a hot path: | ||
|
|
||
| ```bash | ||
| bash scripts/bench-flamegraph.sh | ||
| ``` | ||
|
|
||
| Override the benchmark targeted by the flamegraph script: | ||
|
|
||
| ```bash | ||
| bash scripts/bench-flamegraph.sh decompress/default/decodecorpus-z000033/matrix/pure_rust | ||
| ``` | ||
|
|
||
| ## Outputs | ||
|
|
||
| `run-benchmarks.sh` writes: | ||
|
|
||
| - `benchmark-results.json` for GitHub regression tracking | ||
| - `benchmark-report.md` with: | ||
| - compression ratio tables (`REPORT`) | ||
| - input+output buffer size estimate tables (`REPORT_MEM`) | ||
| - dictionary compression tables (`REPORT_DICT`) | ||
| - timing rows for all benchmark functions | ||
|
|
||
| Criterion also writes its usual detailed estimates under `target/criterion/`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.