Skip to content

fix(go): preserve benchmark results in pipe filtering - #4927

Open
TechWizard9999 wants to merge 5 commits into
rtk-ai:developfrom
TechWizard9999:fix/go-4862-preserve-benchmark-results
Open

TechWizard9999 wants to merge 5 commits into
rtk-ai:developfrom
TechWizard9999:fix/go-4862-preserve-benchmark-results

Conversation

@TechWizard9999

@TechWizard9999 TechWizard9999 commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

rtk pipe -f go-test reduced a go test -json -bench stream to test counts and dropped every benchmark result (#4862). It now keeps the result rows Go prints, under their package, and counts them:

  • A row arrives whole, or as its name and its result in two output events (quiet mode; chatty repeats on Windows). Stdout the benchmark writes between the two does not break the row.
  • Only lines laid out as Go's rows count: the name ended by a tab, the iteration count, then value and unit pairs, or a count alone, padded by Go's %8d, when the run was too fast for the timer. A TestMain printing Benchmarks: 3, or a benchmark printing its own b.N, is not a row. A unit test's stdout never is.
  • Every sample is kept, including byte-identical repeats from -count (benchstat needs each one).

Closes #4862

Tests

Real Go 1.27.4 streams in tests/fixtures/go_test_pipe/ (capture_bench.sh), each with a whole-output assertion in tests/go_test_pipe_test.rs, through -f go-test and auto mode:

  • bench_chatty.jsonl, bench_quiet.jsonl: one run, chatty and quiet. Sub-benchmarks, a skip, stdout between a row's halves, a row with no measurement. Both count 4 rows.
  • bench_fuzz.jsonl: -fuzz with -bench, one package heading.
  • bench_count.jsonl: chatty -count=6 -benchtime=1x: 6 benchmarks, two of them byte-identical.
  • bench_print_n.jsonl: quiet, the benchmark prints 100 … 468991822; only the real row is kept.
  • testmain_stdout.jsonl: plain go test -json, TestMain prints Benchmarks: 3: 1 passed in 1 packages.

Unit tests in src/cmds/go/go_cmd.rs pin the row layout, -benchmem, a failing benchmark, unit-test stdout, and package order.

  • cargo fmt --all --check, cargo clippy --all-targets (0 warnings)
  • cargo test (full suite)

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working go testing labels Nov 1, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes Go test JSON filtering so benchmark result lines (from go test -json/-bench output) are buffered and preserved instead of being dropped or leaked as raw JSON. Adds logic to detect benchmark test names/result lines, flush trailing buffered output, and format benchmark summaries alongside passing, failing, and fuzz results.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4862


Analyzed automatically by wshm · This is an automated analysis, not a human review.

Benchmark result lines were deduplicated per package, so a -count=N run
that printed identical samples collapsed them into one and dropped
benchmark output. Keep every result line, and add regression tests for
-count samples and the issue rtk-ai#4862 quiet-mode b.Log stream.
@TechWizard9999

Copy link
Copy Markdown
Contributor Author

Hi @KuSh @aeppling, could you please review this PR when you have a chance? Thank you!

@KuSh KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Round 1 — CHANGES REQUESTED

Claim: rtk pipe -f go-test keeps the benchmark result lines of a go test -json -bench stream instead of reducing it to test counts (#4862).
Scope: accept (round 1, frozen); maintainer PRs: none overlap.
Ran: real go test -json -bench streams from Go 1.24.6 and 1.26.1 (quiet -v=false and chatty; -benchmem; -count 2; sub-benchmarks; b.Skip; b.Log; a failing benchmark; failing unit test; 2 packages; -fuzz + -bench; a benchmark printing to stdout; 19 streams) and the captured fixtures in tests/fixtures/go_test_pipe/, through rtk pipe -f go-test and auto mode, develop vs this branch merged on develop; plain rtk go test on a package without benchmarks; invariants: every decision, merge with latest develop; savings 92–96% vs 20% floor (CONTRIBUTING.md).
Pins: 22 decisions, 22 mutated, 11 survived the suite (benchmark names added on fail/output/run, skipped benchmark counted as skip, each classifier branch alone, empty-rest guard, \r trim, fuzz-path append, package sort, empty-package filter); 3 more are killed only by tests built on benchmark pass events, which Go does not emit.
Matrix: executed this round; pin check: executed this round
CI @1d3e20f7: all green on the branch; 8 tests fail after merging develop (item 3).

Splitting the line across events is needed. With -v=false, Go prints BenchmarkX-24 \t before the run and 10\t 6719 ns/op\n after it, and test2json emits the two halves as separate output events (same on 1.24.6 and 1.26.1). In that mode the Test field is also wrong: BenchmarkSub results are tagged BenchmarkX-24, and BenchmarkGood results are tagged BenchmarkFail. So per-package reassembly is the right level. In chatty mode the line comes in one event.

Blocks merge (3, frozen at round 1)

  1. src/cmds/go/go_cmd.rs:592 — the benchmark count does not count benchmarks. benchmark_count is max(distinct Test names, result rows). Names come from run/output/skip/fail events, so they include the parent of sub-benchmarks and skipped benchmarks. Evidence: on Go 1.26.1, one run of BenchmarkX, BenchmarkSub/{n=0,n=1} and BenchmarkSkip prints 1 passed, 5 benchmarks above 3 rows in chatty mode, and 3 benchmarks in quiet mode. The skip arm also moves a skipped benchmark out of , N skipped. The pass arm never fires, because benchmarks get no pass event on either Go version. The result rows are already the summary, so the smallest fix is to drop benchmark_names and the five arms that feed it, and count rows. Test must: the quiet and chatty captures of the same run report the same count, equal to the number of rows.

  2. src/cmds/go/go_cmd.rs:569, :625 — every output byte of every package goes through benchmark reassembly, and a substring decides what counts as a result. This also runs on plain rtk go test, which never carries benchmarks: run_test sends -bench* runs through raw.

    • Regression on develop's main path: a passing test that prints Benchmarks loaded from /srv/data (matches /s) and BenchmarkConfig 3 2 gives Go test: 1 passed, 2 benchmarks in 1 packages and prints both lines as rows. Develop prints Go test: 1 passed in 1 packages.
    • The claimed quiet case is still lost when a benchmark writes to stdout during its run. Real 1.26.1 stream: BenchmarkPrint-24 \t, then warming cache\n, then 10\t 2663 ns/op\n. The name joins the unrelated line, and the output is still Go test: No tests found.
    • The buffer keeps a copy of all output and rescans it for each chunk: 9.7 MB of output without newlines takes 0.557 s against 0.021 s on develop.

    All 47 real result lines across both Go versions have the shape name, integer iterations, value, unit…, so the substring branch never accepts a real line that the numeric branch would refuse. What a fix has to satisfy: hold only a pending fragment that is a bare benchmark name (Benchmark… plus whitespace, no newline), and join it with the next fragment that starts with the iteration count. Classify by that layout, and only on events with no Test or a Benchmark… Test. Test must: the stdout lines above stay out of plain rtk go test output (mutation: drop the scope or layout check), and the BenchmarkPrint quiet stream keeps its row.

  3. Fails on develop after merge: develop's tests/go_test_pipe_test.rs (8 tests) pins the old output on real captured streams that contain a benchmark, for example captured_core_events_are_consumed_not_returned_raw expects Go test: 3 passed in 2 packages. The new output on those fixtures is correct (3 passed, 1 benchmark + the BenchmarkLogged-9 row). Please rebase and update those expectations, including the "the existing reducer counts tests only" assertion on legacy_bench.jsonl. Those fixtures and capture.sh are real producer output; a -v=false capture added there would cover the quiet case the issue is about.

Optional — will not hold merge

  • Tests: test_go_test_wrapper_benchmark_json (pipe_cmd.rs:374) and pipe_go_test_preserves_benchmark_split_chunks (tests/pipeline_stdin_test.rs:64) repeat the input of test_filter_go_test_bench_vfalse_split_chunks word for word. go_test_wrapper is a one-line call, and no mutation was caught by those two alone. Three more unit tests (…_bench_only_no_unit_tests, …_vfalse_with_b_log_issue_4862, …_vfalse_split_chunks) cover the same quiet split. …_suppresses_passing_b_log_noise, …_benchmark_pass_is_not_unit_test_pass and …_with_unit_failure_preserves_benchmark are built on benchmark pass events. With item 1 most of these go away; a few captured streams (quiet, chatty, -count, failing benchmark next to a passing one) would replace them.
  • …_buffers_and_sorts_packages passes with the sort removed (HashMap order), so it pins nothing. The fuzz-path append has no test, and 1.26.1 -fuzz + -bench prints the package heading twice with no blank line between the blocks.
  • .trim_end_matches('\r') is dead: add_go_benchmark_output_line trims the line again. The _ => {} // run, pause, cont, etc. comment still lists run.

Follow-ups

  • #5215 Quiet-mode failing benchmark shows goos: linux as failure context — pre-existing (v1.2.1 and develop), outside this PR's hunks; filed.

Checked and correct — no need to re-verify

  • Quiet and chatty, single and multiple packages, -benchmem, -count 2 (every sample kept), sub-benchmarks, final tail without a newline, failing benchmark next to a passing one, auto mode (rtk pipe without -f): rows match Go's output on both versions.
  • fmt and clippy are clean on the merge; apart from item 3, the full suite passes (5819 tests).

Next

Items 1–3: count result rows instead of names, scope the reassembly to a pending benchmark-name fragment classified by layout, rebase and update develop's pipe tests.
Rounds: 1. Threads: 3 open (waiting on the author), 0 resolved this round.

Comment thread src/cmds/go/go_cmd.rs Resolved
Comment thread src/cmds/go/go_cmd.rs Resolved
The benchmark count now counts result rows: Go sends no pass event for a
benchmark, a parent of sub-benchmarks prints no row, and a skipped benchmark
stays in the skip count. Instead of buffering every output byte, only a bare
benchmark name fragment is held and joined with the half that completes it
into a row, so stdout printed between the halves no longer loses the row.
Rows are classified by layout (name, integer iterations, value and unit
pairs, or no pairs when the run was too fast to time) and only from
package-level or Benchmark events, so a unit test's own stdout is never a
row. With -fuzz, the rows sit under the fuzzed package's heading.

Adds real Go 1.27.4 captures of one run in chatty and quiet mode and of
-fuzz with -bench, and updates the captured-stream tests whose benchmark
row is now kept.
@TechWizard9999

Copy link
Copy Markdown
Contributor Author

@KuSh Round 1 addressed in 11fd14f (develop merged in a63c8f8). Two Go behaviours from the new captures (Go 1.27.4, windows/arm64) differ from the round-1 notes:

  • Chatty mode also splits a row: "BenchmarkX-12 \t" then " 2\t 100.0 ns/op\n" arrive as two output events (bench_chatty.jsonl), so the pending-name join covers both modes.
  • A run shorter than the timer resolves prints no measurement: "BenchmarkSub/n=1-12 \t" then " 2\n". The layout check accepts name + integer iterations with no pairs; BenchmarkConfig 3 2 and BenchmarkConfig 3 2 1 are still rejected (test_go_benchmark_result_line_layout).

Also: the pending name is kept only when the next fragment completes a valid row, so stdout between the halves (warming cache, bench_quiet.jsonl) leaves it in place.

@KuSh KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Round 2 — CHANGES REQUESTED

Claim: rtk pipe -f go-test keeps the benchmark result lines of a go test -json -bench stream instead of reducing it to test counts (#4862).
Scope: accept (round 1, frozen); maintainer PRs: none overlap.
Ran: real go test -json -bench streams from Go 1.24.6 and 1.26.1, chatty and -v=false (sub-benchmarks, b.Skip, b.Log, -count with -benchmem, 2 packages, a failing benchmark beside a passing one, stdout during a run, ReportMetric/SetBytes, -benchtime 1x, -fuzz with -bench, fmt.Println(b.N), func Benchmark with -cpu 1; 36 streams) plus the PR's go_test_pipe fixtures, through rtk pipe -f go-test and auto mode, develop vs this branch merged on develop; plain rtk go test on packages whose tests or TestMain print Benchmark… lines; 9.6 MB of output without newlines. Invariants: split streams, every decision, merge with latest develop. Savings 90–97% vs the 20% floor (CONTRIBUTING.md).
Pins: 24 decisions mutated, 3 survived: keeping whole-row samples undeduplicated (item 2); the fuzz package sort (develop's existing behaviour, rewritten); replacing a pending name (no captured stream reaches it).
Matrix: executed this round; pin check: executed this round
CI @11fd14f4: all green (16/16); merged on develop: fmt, clippy, 5964 tests pass.

Round 1's three items are closed. The count now equals the rows: chatty and quiet captures of the same run agree (3 = 3, 6 = 6, 2 = 2). Unit-test stdout no longer becomes a row. The no-newline stream takes 0.024 s (develop 0.029 s). The develop tests carry real captures. Both Go behaviours from your comment are confirmed in bench_chatty.jsonl, and the new rows handle them.

Blocks merge (2: round 1 items closed; +1 new in 11fd14f, +1 unpinned)

  1. src/cmds/go/go_cmd.rs:737 — lines Go did not print as rows are read as rows (new in 11fd14f). Package-level output reaches is_go_benchmark_result_line (the Test-less scope I asked for in round 1), and name + integer is now a row. Two real Go 1.26.1 cases:

    • plain rtk go test on a package whose TestMain prints Benchmarks: 3: Go test: 1 passed, 1 benchmark in 1 packages with that line as a row; develop prints Go test: 1 passed in 1 packages.
    • quiet -bench on a benchmark that runs fmt.Println(b.N): the 10 it prints completes the pending name, so the output is the row BenchmarkN-24 \t10 and the real 10 1178 ns/op is dropped (with -benchtime 100ms: 100 instead of 573299739 0.2005 ns/op).

    What Go prints, measured on every captured row: the name is Benchmark alone or followed by an identifier whose first letter is not lowercase (cmd/go refuses Benchmarklower), then -N and /sub parts; the name ends with spaces and a tab; a row with no measurement has its count right-aligned in eight columns (%8d, 2). Rows a fix must pass: the two cases above give 1 passed in 1 packages and the real measurement; every other captured stream (the 36 above and the PR's fixtures) is unchanged, BenchmarkSub/n=1-12 \t 2 included.

  2. src/cmds/go/go_cmd.rs:713 — unpinned behaviour (whole rows keep identical samples). Evidence: adding a dedup to add_go_benchmark_row passes every test; the existing identical-sample test only goes through the quiet join. Real case: chatty -count 6 -benchtime 1x sends whole rows (the later ones at package level) and two are byte-identical (1 130.0 ns/op). Test must: that stream reports 6 benchmarks and both identical rows; the dedup mutation turns it red.

Follow-ups

  • #5215 (quiet failing benchmark shows goos: linux as failure context) — pre-existing, unchanged by this PR.

Checked and correct — no need to re-verify

  • Counts and rows on all 36 Go streams and the PR's fixtures, including -count repeats, -benchmem, MB/s and custom metrics, -cpu 1, the fuzz heading, the stdout line between a quiet row's halves.
  • Plain rtk go test with Benchmark…-shaped stdout from a test, in chatty mode.
  • The develop pipe tests updated to the new output; the duplicated wrapper and stdin tests removed.

Your questions

  • "Chatty mode also splits a row" → confirmed in your capture (Linux 1.24.6/1.26.1 send it whole); both forms are handled.
  • "A run shorter than the timer resolves prints no measurement" → confirmed; keep that form, with the eight-column count from item 1.

Next

Items 1 and 2; after that this merges.

Comment thread src/cmds/go/go_cmd.rs Resolved
Comment thread src/cmds/go/go_cmd.rs Resolved
A row is the name ended by a tab, the iteration count, then value and
unit pairs, or a count alone padded by %8d. A TestMain printing
"Benchmarks: 3" or a benchmark printing its own b.N is no longer a row.
Whole and rejoined rows share one push, so identical -count samples are
pinned by a real chatty -count=6 capture.
@TechWizard9999

Copy link
Copy Markdown
Contributor Author

@KuSh both round-2 items fixed in a58e1e8, with real Go 1.27.4 captures (bench_print_n, testmain_stdout, bench_count). One mutant survives: dropping the return after a rejoined row; no Go stream completes a name twice. Please review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rtk pipe -f go-test drops benchmark results

2 participants