{"article":{"slug":"benchfmt-rs-bringing-gos-benchmarking-and-benchstat-workflow-to-rust","title":"benchfmt.rs: Bringing Go's Benchmarking and benchstat Workflow to Rust","subtitle":null,"summary":"Bartek Płotka introduces benchfmt.rs, a zero-dependency Rust benchmark runner and Criterion converter for Go's benchstat, and walks through why Go's benchmarking workflow works well, plus key=value projections, per-op heap allocation tracking and pprof profiling for Rust benchmarks.","content_type":"tutorial","language":"en","canonical_url":"https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/","author":{"name":"Bartek Płotka","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"bwplotka.dev","url":null,"listing_slug":null,"listing":null},"topics":[{"name":"rust","slug":"rust","url":"https://listedarticles.com/topics/rust"},{"name":"go","slug":"go","url":"https://listedarticles.com/topics/go"},{"name":"performance","slug":"performance","url":"https://listedarticles.com/topics/performance"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":4799,"reading_minutes":21,"published_at":"2026-11-03T00:00:00.000Z","added_at":"2026-11-04T20:13:58.680Z","updated_at":"2026-11-04T20:13:58.680Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/benchfmt-rs-bringing-gos-benchmarking-and-benchstat-workflow-to-rust","markdown_url":"https://listedarticles.com/articles/benchfmt-rs-bringing-gos-benchmarking-and-benchstat-workflow-to-rust.md","example":false,"citation":"Bartek Płotka, bwplotka.dev. \"benchfmt.rs: Bringing Go's Benchmarking and benchstat Workflow to Rust.\" 3 Nov 2026. https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/ (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/"},"body_markdown":"# benchfmt.rs: Bringing Go's Benchmarking and benchstat Workflow to Rust\n\n[Bartek Płotka](https://www.bwplotka.dev/ \"Author\")· 2026-11-03·\n 20 minutes\n·\n 4164 words\n\n\n\n> **TL;DR:** I built [`benchfmt.rs`](https://github.com/bwplotka/benchfmt.rs), a zero-dependency Rust benchmark runner and [`Criterion`](https://crates.io/crates/criterion) converter for Go’s [`benchstat`](https://pkg.go.dev/golang.org/x/perf/cmd/benchstat). It adds `/key=value` projections, heap tracking for allocated bytes (`B/op`) and objects (`allocs/op`) per op, and `pprof` profiling.\n>\n> In this post, I am benchmarking `varint` and [Prometheus Remote Write 2.0](https://prometheus.io/docs/specs/prw/remote_write_spec_2_0/) decoding across Go and Rust to showcase the benefits of `benchstat`-like flow in Rust. I’m also sharing some fun learnings on the way (e.g., yes [Rust is faster](#comparing-go-vs-rust)).\n\nRecently, I’ve been working on a few Rust projects across Google and [open source](https://github.com/prometheus/expfmt-rs). I came to Rust after years of writing performance-sensitive Go in [Prometheus](https://prometheus.io/), [Thanos](https://thanos.io/), and teaching Go in the [“Efficient Go” book](https://www.bwplotka.dev/book). For me, much of Rust’s performance story is really good, until…. **I wanted to run A/B microbenchmarks**.\n\nPerhaps I was spoiled by the Go benchmarking ecosystem, with [the `go tool -bench` and `benchstat`](https://www.bwplotka.dev/2024/go-microbenchmarks-benchstat/) in the center. To me, Rust standard benchmarking tools are lacking:\n\n- Standard format for case comparison projections.\n- Standard heap allocation measurements.\n- Standard `pprof` support.\n- Simple to understand reliability scoring.\n\n> **DISCLAIMER:** A `standard` here means something that will be familiar to large number of developers, and I biased on Go experience for sure!\n\nAs a result, I asked myself: can we get [`benchstat`](https://pkg.go.dev/golang.org/x/perf/cmd/benchstat)-compatible output for Rust microbenchmarks? Wouldn’t that make A/B testing easier and unlock fun Go vs Rust comparisons?\n\n**Here we are!** I built [`benchfmt.rs`](https://github.com/bwplotka/benchfmt.rs), a zero-dependency Rust benchmark runner with a thread-local heap allocation counter, `pprof` profiler integration, and the same CLI flags as `go test -bench`. For those who don’t trust a new benchmarking runtime (you shouldn’t from the start!), it also ships a [`Criterion`](https://crates.io/crates/criterion) and `libtest` converter that emits Go’s [`benchfmt`](https://go.googlesource.com/proposal/+/master/design/14313-benchmark-format.md) format directly.\n\nIn this blog post, we will implement the same pieces of code in Go and Rust, benchmark them across different Rust frameworks, and compare the results. We might learn something new along the way (I did when building this!). Enjoy!\n\nAll benchmark code and raw outputs live in [`bwplotka/benchmarks/benchmarks/rust-go-decode`](https://github.com/bwplotka/benchmarks/tree/c55bd342d9ac23e788d06327dc7973951a5eeb82/benchmarks/rust-go-decode).\n\n## Benchmarking two workloads\n\nLet’s pick two workloads to implement and benchmark in both Go and Rust. I am particularly curious about protobuf decoding performance, which, funny enough, is often one of the main costs of running distributed systems these days. Thus, let’s decode our [Prometheus Remote Write 2.0 protocol](https://prometheus.io/docs/specs/prw/remote_write_spec_2_0/). Because protobuf code generators have different maturity and trade-offs across languages, let’s also measure something simpler alongside it, a standalone `varint` decoder.\n\n### Workload details:\n\n1. **`DecodeVarints`.** A 36-line unsigned LEB128 varint stream decoder. Varints are the integer encoding underneath Protocol Buffers. Our fixture `varints_2000.bin` is `4,798` bytes and packs `2,000` `uint64` values mixing 1-byte symbol indices, 2-byte bucket offsets, 4-byte deltas, and 6-byte millisecond Unix timestamps. The function writes into a preallocated `2,000`-element slice with zero heap allocations.\n2. **`DecodeRemoteWrite2`.** A full HTTP request body decoder for Snappy-compressed Remote Write 2.0 (`io.prometheus.write.v2.Request`) payloads. We test batches of `200` and `2,000` series, where `2,000` matches Prometheus’s default `max_samples_per_send`. The `2,000`-series fixture mixes counters, gauges, classic and native histograms, `1,000` trace exemplars, and metric metadata. Remote Write 2.0 interns all strings into a top-level `symbols` table and stores packed varint `labels_refs` per series. That shrinks the `2,000`-series batch to `244.9 KiB` uncompressed and `57.0 KiB` Snappy-compressed.\n\n## Implementing and benchmarking in Go\n\nLet’s start with Go. First, `DecodeVarints` writes straight into a caller-provided slice, with a fast path for single-byte values below `0x80`:\n\n```\nfunc DecodeVarints(src []byte, dst []uint64) (int, error) {\n\tn := 0\n\ti := 0\n\tfor i < len(src) {\n\t\tif n >= len(dst) {\n\t\t\treturn n, ErrDstTooSmall\n\t\t}\n\t\tb := src[i]\n\t\ti++\n\t\tif b < 0x80 {\n\t\t\tdst[n] = uint64(b)\n\t\t\tn++\n\t\t\tcontinue\n\t\t}\n\t\tx := uint64(b & 0x7f)\n\t\tshift := uint(7)\n\t\tfor {\n\t\t\tif i >= len(src) {\n\t\t\t\treturn n, ErrUnexpectedEOF\n\t\t\t}\n\t\t\tb = src[i]\n\t\t\ti++\n\t\t\tx |= uint64(b&0x7f) << shift\n\t\t\tif b < 0x80 {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tshift += 7\n\t\t\tif shift >= 64 {\n\t\t\t\treturn n, ErrOverflow\n\t\t\t}\n\t\t}\n\t\tdst[n] = x\n\t\tn++\n\t}\n\treturn n, nil\n}\n```\n\nSecond, for `DecodeRemoteWrite2`, we tune the Go implementation the way Prometheus runs in production rather than benchmarking slow reflection-based unmarshaling. We pair [`github.com/golang/snappy`](https://github.com/golang/snappy), which uses x86-64 assembly on `linux/amd64`, with [`vtprotobuf`](https://github.com/planetscale/vtprotobuf) and its generated, optimized `UnmarshalVT` decoder:\n\n```\nfunc DecodeRemoteWrite2(compressed []byte) (*writev2.Request, error) {\n\traw, err := snappy.Decode(nil, compressed)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treq := &writev2.Request{}\n\tif err := req.UnmarshalVT(raw); err != nil {\n\t\treturn nil, err\n\t}\n\treturn req, nil\n}\n```\n\nNow let’s measure the efficiency of our Go code!\n\nNotice Go’s `/key=value` sub-benchmark names (`samples=2000/lang=go`), which let Go’s official [`benchstat`](https://pkg.go.dev/golang.org/x/perf/cmd/benchstat) tool pivot across `/lang` or `/samples` later, along with `b.SetBytes`, `b.ReportAllocs`, and `for b.Loop()`:\n\n```\nfunc BenchmarkDecodeVarints(b *testing.B) {\n\tmanifest := ensureFixtures(b)\n\traw, err := os.ReadFile(filepath.Join(\"testdata\", \"varints_2000.bin\"))\n\tif err != nil {\n\t\tb.Fatalf(\"read varints: %v\", err)\n\t}\n\tdst := make([]uint64, manifest.VarintsCount)\n\n\t// Encode dimensions as /key=value so benchstat can pivot across /samples or /lang.\n\tb.Run(\"samples=2000/lang=go\", func(b *testing.B) {\n\t\tb.SetBytes(int64(len(raw)))\n\t\tb.ReportAllocs()\n\t\t// NOTE: b.Loop() resets the timer on its first call, stops it on exit, and keeps\n\t\t// call arguments and results alive without package-level sink variables.\n\t\tfor b.Loop() {\n\t\t\tn, err := DecodeVarints(raw, dst)\n\t\t\tif err != nil || n != manifest.VarintsCount {\n\t\t\t\tb.Fatalf(\"unexpected decode result: n=%d err=%v\", n, err)\n\t\t\t}\n\t\t}\n\t})\n}\n\nfunc BenchmarkDecodeRemoteWrite2(b *testing.B) {\n\tensureFixtures(b)\n\n\tfor _, samples := range []int{200, 2000} {\n\t\tfile := fmt.Sprintf(\"rw2_%d.snappy\", samples)\n\t\tcompressed, err := os.ReadFile(filepath.Join(\"testdata\", file))\n\t\tif err != nil {\n\t\t\tb.Fatalf(\"read %s: %v\", file, err)\n\t\t}\n\n\t\tb.Run(fmt.Sprintf(\"samples=%d/lang=go\", samples), func(b *testing.B) {\n\t\t\tb.SetBytes(int64(len(compressed)))\n\t\t\tb.ReportAllocs()\n\t\t\tfor b.Loop() {\n\t\t\t\treq, err := DecodeRemoteWrite2(compressed)\n\t\t\t\tif err != nil || len(req.Timeseries) != samples {\n\t\t\t\t\tb.Fatalf(\"unexpected v2 decode: err=%v\", err)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n```\n\nFinally, let’s run `6` benchmark runs (“epochs”) for `2s` each at `-cpu 2` on a 24-core Intel Xeon 2.20GHz Linux workstation (Go 1.26.8, Rust 1.99.1):\n\n```\ngo test -run '^$' -bench '^BenchmarkDecode' \\\n    -benchtime 2s -count 6 -cpu 2 -timeout 999m -benchmem \\\n| tee results/go.txt\n```\n\nOne epoch looks as follows:\n\n```\ngoos: linux\ngoarch: amd64\npkg: github.com/bwplotka/benchmarks/benchmarks/rust-go-decode\ncpu: Intel(R) Xeon(R) CPU @ 2.20GHz\nBenchmarkDecodeVarints/samples=2000/lang=go-2          279861      8514 ns/op   563.52 MB/s        0 B/op        0 allocs/op\nBenchmarkDecodeRemoteWrite2/samples=200/lang=go-2        8467    264321 ns/op    31.43 MB/s   195296 B/op     2189 allocs/op\nBenchmarkDecodeRemoteWrite2/samples=2000/lang=go-2        976   2448633 ns/op    23.84 MB/s  1643970 B/op    19297 allocs/op\n```\n\n## Implementing and benchmarking in Rust with Criterion\n\nNow let’s port both decoders and benchmarks to Rust. First, `decode_varints` mirrors the Go loop line for line:\n\n```\npub fn decode_varints(src: &[u8], dst: &mut [u64]) -> Result<usize, VarintError> {\n    let mut n = 0usize;\n    let mut i = 0usize;\n    while i < src.len() {\n        if n >= dst.len() {\n            return Err(VarintError::DstTooSmall);\n        }\n        let mut b = src[i];\n        i += 1;\n        if b < 0x80 {\n            dst[n] = u64::from(b);\n            n += 1;\n            continue;\n        }\n        let mut x = u64::from(b & 0x7f);\n        let mut shift = 7u32;\n        loop {\n            if i >= src.len() {\n                return Err(VarintError::UnexpectedEof);\n            }\n            b = src[i];\n            i += 1;\n            x |= u64::from(b & 0x7f) << shift;\n            if b < 0x80 {\n                break;\n            }\n            shift += 7;\n            if shift >= 64 {\n                return Err(VarintError::Overflow);\n            }\n        }\n        dst[n] = x;\n        n += 1;\n    }\n    Ok(n)\n}\n```\n\nSecond, `decode_remote_write2` pairs the [`snap`](https://crates.io/crates/snap) crate with [`prost`](https://crates.io/crates/prost) compile-time generated `Message` structs:\n\n```\npub fn decode_remote_write2(compressed: &[u8]) -> Result<proto::v2::Request, String> {\n    let raw = snap::raw::Decoder::new()\n        .decompress_vec(compressed)\n        .map_err(|e| e.to_string())?;\n    proto::v2::Request::decode(raw.as_slice()).map_err(|e| e.to_string())\n}\n```\n\nNow, let’s write both Rust benchmarks using [`Criterion`](https://crates.io/crates/criterion), the default choice for stable-Rust microbenchmarking. We keep the same `/key=value` benchmark IDs and configure `Throughput::BytesDecimal` to match Go’s decimal `MB/s`:\n\n```\nfn bench_varints(c: &mut Criterion) {\n    // Resolve testdata/ from the crate directory baked in at compile time.\n    let root = Path::new(env!(\"CARGO_MANIFEST_DIR\")).join(\"testdata\");\n    let raw = fs::read(root.join(\"varints_2000.bin\")).unwrap();\n    // Preallocate the output buffer outside the timed loop so each op does 0 heap allocs.\n    let mut dst = vec![0u64; 2000];\n\n    // Group + function ID join into \"DecodeVarints/samples=2000/lang=rust\" to match Go's b.Run.\n    let mut group = c.benchmark_group(\"DecodeVarints\");\n    // Report decimal MB/s (1e6 B/s) to match Go's b.SetBytes; Throughput::Bytes prints MiB/s.\n    group.throughput(Throughput::BytesDecimal(raw.len() as u64));\n    group.bench_function(\"samples=2000/lang=rust\", |b| {\n        // b.iter runs the closure across Criterion's warmup and 100 linear-ramp samples.\n        b.iter(|| {\n            // std::hint::black_box is an LLVM optimization barrier: Unlike Go's b.Loop(), Rust\n            // would constant-fold known inputs or delete unused work without black_box hints.\n            let n = decode_varints(black_box(&raw), black_box(&mut dst)).unwrap();\n            assert_eq!(n, 2000);\n            // Observe the written buffer so LLVM cannot optimize away stores into dst.\n            black_box(dst[n - 1]);\n        });\n    });\n    group.finish();\n}\n\nfn bench_remote_write2(c: &mut Criterion) {\n    let root = Path::new(env!(\"CARGO_MANIFEST_DIR\")).join(\"testdata\");\n    let mut group = c.benchmark_group(\"DecodeRemoteWrite2\");\n\n    for samples in [200usize, 2000usize] {\n        let file = format!(\"rw2_{samples}.snappy\");\n        let compressed = fs::read(root.join(file)).unwrap();\n\n        group.throughput(Throughput::BytesDecimal(compressed.len() as u64));\n        let id = format!(\"samples={samples}/lang=rust\");\n        group.bench_function(&id, |b| {\n            b.iter(|| {\n                let req = decode_remote_write2(black_box(&compressed)).unwrap();\n                assert_eq!(req.timeseries.len(), samples);\n                // Force req to materialize before dropping at the end of the iteration.\n                black_box(req);\n            });\n        });\n    }\n    group.finish();\n}\n\n// Generate the benchmark group runner and custom main() required by harness = false in Cargo.toml.\ncriterion_group!(benches, bench_varints, bench_remote_write2);\ncriterion_main!(benches);\n```\n\nFinally, let’s run the Criterion benchmark suite:\n\n```\ncargo bench --bench criterion_decode\n```\n\n```\nDecodeVarints/samples=2000/lang=rust\n                        time:   [5.2789 µs 5.3369 µs 5.4014 µs]\n                        thrpt:  [888.29 MB/s 899.02 MB/s 908.90 MB/s]\n\nDecodeRemoteWrite2/samples=200/lang=rust\n                        time:   [237.09 µs 238.48 µs 239.95 µs]\n                        thrpt:  [34.620 MB/s 34.833 MB/s 35.037 MB/s]\n\nDecodeRemoteWrite2/samples=2000/lang=rust\n                        time:   [2.0712 ms 2.0831 ms 2.0968 ms]\n                        thrpt:  [27.835 MB/s 28.018 MB/s 28.179 MB/s]\n```\n\n### Criterion downsides\n\nCriterion is convenient when you want confidence intervals and HTML plots from a single `cargo bench` command without external tools. However, compared to `go test` and `benchstat`, four design choices get in the way:\n\n1. **Compile weight.** Pulling in `criterion` with `default-features = false` still compiles `clap`, `regex-automata`, `ciborium`, `oorandom`, `itertools`, `tinytemplate`, and `anes`. That adds 42 transitive crates and ~19s to a cold `cargo bench` build.\n2. **Linear-ramp sampling vs steady-state epochs.**\n\n   - Go’s `testing.B` calibrates a fixed iteration count `N` for `-benchtime 2s` and runs `-count 6` equal steady-state epochs, which `benchstat` summarizes using the outlier-resistant median and Mann-Whitney U test.\n   - Criterion defaults to `Linear` sampling: after a 3-second warmup, it collects 100 batches of `[1d, 2d, ..., 100d]` iterations and fits a least-squares line `y = m * x` through the origin. Because the line is forced through zero, it does not subtract a fixed timer-overhead intercept. Instead, it computes a `k^2`-weighted average where batch 100 carries `10,000x` the weight of batch 1 and late OS spikes pull the slope up.\n\n   Comparing those two models directly introduces two measurable distortions:\n\n   - First, grouping Criterion’s 100 ramp samples into 6 `benchstat` epochs gives epoch 1 `11.5x` fewer iterations than epoch 6 (`24,888` vs `286,212` on `DecodeVarints`). Mixing short bursts with long runs inflates variance to `± 5%`, compared to `± 1-2%` in steady-state epochs.\n   - Second, timing 100 separate closure batches adds `+8.6%` overhead on microsecond loops (`5.36 µs` vs `4.90 µs`, `p=0.002`) and skews early allocating samples with cold allocator caches (`+2.3%` on `DecodeRemoteWrite2`).\n3. **No heap allocation tracking.** Criterion reports wall-clock time and throughput only. On `DecodeVarints` at `0 allocs/op`, that is fine. On `DecodeRemoteWrite2`, we cannot tell whether `prost` made 500 heap allocations or 500,000, or whether a refactor doubled heap churn.\n4. **Pairwise baseline folders vs multi-dimensional tables.** Criterion compares `target/criterion/<id>/base` against `new`. You cannot feed a single run file into a table that [pivots `-col /samples` or `-col /lang`](https://www.bwplotka.dev/2024/go-microbenchmarks-benchstat/) across Go and Rust.\n\n## Converting Criterion output with the `benchfmt` CLI\n\nIf a Rust codebase already uses Criterion, rewriting benchmarks just to get a `benchstat` table is not ideal. That is why `benchfmt.rs` includes a standalone `benchfmt` CLI binary. It reads `benchmark.json` and `sample.json` directly from `target/criterion`, extracts `Throughput::BytesDecimal` (or `Throughput::Bytes`) as decimal `MB/s`, and groups Criterion’s 100 samples into `6` contiguous epochs via `--epochs 6`:\n\n```\ncargo install --git https://github.com/bwplotka/benchfmt.rs\n\nbenchfmt criterion target/criterion \\\n    --pkg github.com/bwplotka/benchmarks/benchmarks/rust-go-decode \\\n    --cpu 2 \\\n> results/rust_criterion.txt\n\nbenchstat -col /lang results/go.txt results/rust_criterion.txt\n```\n\nWithout touching the benchmark implementation, `benchstat` gives us a side-by-side Go vs Rust table:\n\n```\ngoos: linux\ngoarch: amd64\npkg: github.com/bwplotka/benchmarks/benchmarks/rust-go-decode\ncpu: Intel(R) Xeon(R) CPU @ 2.20GHz\n                                  │     go      │                rust                │\n                                  │   sec/op    │   sec/op     vs base               │\nDecodeVarints/samples=2000-2        8.536µ ± 1%   5.364µ ± 5%  -37.16% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=200-2    271.4µ ± 3%   235.1µ ± 4%  -13.37% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=2000-2   2.470m ± 5%   2.082m ± 1%  -15.72% (p=0.002 n=6)\ngeomean                             178.9µ        138.0µ       -22.87%\n\n                                  │      go      │                rust                 │\n                                  │     B/s      │     B/s       vs base               │\nDecodeVarints/samples=2000-2        536.1Mi ± 1%   853.2Mi ± 5%  +59.15% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=200-2    29.19Mi ± 3%   33.70Mi ± 4%  +15.44% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=2000-2   22.53Mi ± 5%   26.74Mi ± 1%  +18.67% (p=0.002 n=6)\ngeomean                             70.65Mi        91.60Mi       +29.67%\n\n                                  │       go       │\n                                  │      B/op      │\nDecodeVarints/samples=2000-2          0.000 ± 0%\nDecodeRemoteWrite2/samples=200-2    190.7Ki ± 0%\nDecodeRemoteWrite2/samples=2000-2   1.568Mi ± 0%\ngeomean                                          ¹\n¹ summaries must be >0 to compute geomean\n\n                                  │      go       │\n                                  │   allocs/op   │\nDecodeVarints/samples=2000-2         0.000 ± 0%\nDecodeRemoteWrite2/samples=200-2    2.189k ± 0%\nDecodeRemoteWrite2/samples=2000-2   19.30k ± 0%\ngeomean                                         ¹\n¹ summaries must be >0 to compute geomean\n```\n\nNow, with this view we finally have a readable, standard `benchstat` output Go developers love. It clearly shows the Go vs Rust efficiency for our example functions. Rust is `37.16%` faster on `DecodeVarints` (`5.36 µs` vs `8.54 µs`) and `13.37%` to `15.72%` faster on `DecodeRemoteWrite2`.\n\nUnfortunately, there are some gaps:\n\n1. The `B/op` and `allocs/op` tables only have a `go` column because Criterion never recorded allocations\n2. Criterion’s linear-ramp epochs still leave `± 5%` variance on `DecodeVarints`.\n\n## Native `benchfmt` runs with `benchfmt::Suite` and `CountingAlloc`\n\nFor new Rust benchmarks, we can skip Criterion and use `benchfmt::Suite` with `harness = false` in `Cargo.toml`. Registering `CountingAlloc` as `#[global_allocator]` tracks every `alloc`, `alloc_zeroed`, and `realloc` in a `thread_local!` `Cell<(u64, u64)>` with zero atomic contention and zero external dependencies:\n\n```\n// Wrap Rust's System allocator globally so every heap alloc/realloc on the benchmark thread\n// increments thread-local (allocs, bytes) counters for B/op and allocs/op.\n#[global_allocator]\nstatic ALLOC: CountingAlloc = CountingAlloc::new();\n\nfn main() {\n    let root = Path::new(env!(\"CARGO_MANIFEST_DIR\")).join(\"testdata\");\n    let raw_varints = fs::read(root.join(\"varints_2000.bin\")).unwrap();\n    let rw2_200 = fs::read(root.join(\"rw2_200.snappy\")).unwrap();\n    let rw2_2000 = fs::read(root.join(\"rw2_2000.snappy\")).unwrap();\n    let mut dst = vec![0u64; 2000];\n\n    // Parse go-test-compatible CLI flags (-bench, -benchtime, -count, -cpu, -benchmem,\n    // -cpuprofile, -memprofile) from std::env::args() and set the default \"pkg:\" header.\n    // Using the same pkg path as Go's go.mod lets `benchstat -col /lang` join Go and Rust rows.\n    Suite::from_env(\"github.com/bwplotka/benchmarks/benchmarks/rust-go-decode\")\n        // Attach ALLOC so each benchmark snapshots thread-local allocation deltas per epoch.\n        .with_alloc(&ALLOC)\n        // Suite prepends \"Benchmark\" in benchfmt output, matching Go's BenchmarkDecodeVarints.\n        .bench(\"DecodeVarints/samples=2000/lang=rust\", |b| {\n            b.set_bytes(raw_varints.len() as u64);\n            b.report_allocs();\n            // Go b.Loop() equivalent: starts timing, alloc snapshots, and any pprof capture on the\n            // first call, runs the calibrated N steady-state iterations, then stops all three.\n            while b.keep_running() {\n                let n = decode_varints(black_box(&raw_varints), black_box(&mut dst)).unwrap();\n                assert_eq!(n, 2000);\n                black_box(dst[n - 1]);\n            }\n        })\n        .bench(\"DecodeRemoteWrite2/samples=200/lang=rust\", |b| {\n            b.set_bytes(rw2_200.len() as u64);\n            b.report_allocs();\n            while b.keep_running() {\n                let req = decode_remote_write2(black_box(&rw2_200)).unwrap();\n                assert_eq!(req.timeseries.len(), 200);\n                black_box(req);\n            }\n        })\n        .bench(\"DecodeRemoteWrite2/samples=2000/lang=rust\", |b| {\n            b.set_bytes(rw2_2000.len() as u64);\n            b.report_allocs();\n            while b.keep_running() {\n                let req = decode_remote_write2(black_box(&rw2_2000)).unwrap();\n                assert_eq!(req.timeseries.len(), 2000);\n                black_box(req);\n            }\n        })\n        .run();\n}\n```\n\n> **Does `CountingAlloc` work with Tokio and custom allocators?** Mostly yes. You can wrap custom allocators like `jemalloc` or `mimalloc` via `CountingAlloc::with_allocator(Jemalloc)`. Because `CountingAlloc` uses thread-local counters on the benchmark thread, single-threaded Tokio `current_thread` works out of the box, while multi-threaded pools like `tokio::spawn` or `rayon` need a process-wide atomic [`AllocTracker`](https://github.com/bwplotka/benchfmt.rs/blob/main/src/alloc.rs) passed to `Suite::with_alloc`.\n\n`Suite::from_env` reads `std::env::args()`, auto-detects `goos`, `goarch`, and `cpu`, and writes the passed string as the `pkg:` metadata line (matching Go’s module path so `benchstat` joins Go and Rust rows into one table). Passing flags after `--` mirrors `go test`:\n\n```\ncargo bench --bench suite_decode -- \\\n    -bench '^BenchmarkDecode' -benchtime 2s -count 6 -cpu 2 -benchmem \\\n| tee results/rust_suite.txt\n```\n\nFirst, let’s verify that `benchfmt::Suite` agrees with Criterion on timing, even with `CountingAlloc` active on every allocation:\n\n```\nbenchstat criterion=results/rust_criterion.txt suite=results/rust_suite.txt\n```\n\n```\n                                            │  criterion  │               suite               │\n                                            │   sec/op    │   sec/op     vs base              │\nDecodeRemoteWrite2/samples=2000/lang=rust-2   2.082m ± 1%   2.033m ± 2%  -2.33% (p=0.015 n=6)\nDecodeRemoteWrite2/samples=200/lang=rust-2    235.1µ ± 4%   231.5µ ± 1%  -1.55% (p=0.004 n=6)\nDecodeVarints/samples=2000/lang=rust-2        5.364µ ± 5%   4.902µ ± 2%  -8.60% (p=0.002 n=6)\ngeomean                                       138.0µ        132.1µ       -4.21%\n```\n\nOn `DecodeRemoteWrite2`, Criterion and `benchfmt::Suite` agree within `1.6%` to `2.3%`, while `Suite`’s steady-state `2s` epochs tighten worst-case variance from `± 4-5%` down to `± 1-2%`. On `DecodeVarints`, `while b.keep_running()` runs `490,000` back-to-back iterations in one tight loop instead of 100 short ramp batches, clocking `4.90 µs` vs `5.36 µs`.\n\n### Comparing Go vs Rust\n\nNow let’s run the full Go vs Rust comparison with `results/rust_suite.txt`:\n\n```\nbenchstat -col /lang results/go.txt results/rust_suite.txt\n```\n\n```\ngoos: linux\ngoarch: amd64\npkg: github.com/bwplotka/benchmarks/benchmarks/rust-go-decode\ncpu: Intel(R) Xeon(R) CPU @ 2.20GHz\n                                  │     go      │                rust                │\n                                  │   sec/op    │   sec/op     vs base               │\nDecodeVarints/samples=2000-2        8.536µ ± 1%   4.902µ ± 2%  -42.56% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=200-2    271.4µ ± 3%   231.5µ ± 1%  -14.71% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=2000-2   2.470m ± 5%   2.033m ± 2%  -17.68% (p=0.002 n=6)\ngeomean                             178.9µ        132.1µ       -26.12%\n\n                                  │      go      │                rust                 │\n                                  │     B/s      │     B/s       vs base               │\nDecodeVarints/samples=2000-2        536.1Mi ± 1%   933.3Mi ± 2%  +74.09% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=200-2    29.19Mi ± 3%   34.22Mi ± 1%  +17.23% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=2000-2   22.53Mi ± 5%   27.37Mi ± 2%  +21.48% (p=0.002 n=6)\ngeomean                             70.65Mi        95.62Mi       +35.35%\n\n                                  │       go       │                 rust                  │\n                                  │      B/op      │     B/op      vs base                 │\nDecodeVarints/samples=2000-2          0.000 ± 0%       0.000 ± 0%        ~ (p=1.000 n=6) ¹\nDecodeRemoteWrite2/samples=200-2    190.7Ki ± 0%     271.9Ki ± 0%  +42.59% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=2000-2   1.568Mi ± 0%     2.053Mi ± 0%  +30.98% (p=0.002 n=6)\ngeomean                                          ²                 +23.15%               ²\n¹ all samples are equal\n² summaries must be >0 to compute geomean\n\n                                  │      go       │                 rust                 │\n                                  │   allocs/op   │  allocs/op   vs base                 │\nDecodeVarints/samples=2000-2         0.000 ± 0%      0.000 ± 0%        ~ (p=1.000 n=6) ¹\nDecodeRemoteWrite2/samples=200-2    2.189k ± 0%     1.905k ± 0%  -12.97% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=2000-2   19.30k ± 0%     16.49k ± 0%  -14.55% (p=0.002 n=6)\ngeomean                                         ²                 -9.40%               ²\n¹ all samples are equal\n² summaries must be >0 to compute geomean\n```\n\nLook closely at the `B/op` and `allocs/op` tables. That is the kind of surprise you miss without allocation counters:\n\n- Rust makes **14.55% fewer heap allocations** at `2,000` series (`16,490` vs Go’s `19,297 allocs/op`). Go’s `protoc-gen-go` generates pointer slices such as `[]*TimeSeries` and `[]*Sample`, allocating every nested struct separately on the heap. Rust’s `prost` stores `Vec<TimeSeries>` and `Vec<Sample>` inline by value.\n- Yet Rust requests **30.98% more heap bytes** at `2,000` series (`2.05 MiB` vs Go’s `1.57 MiB`) and **42.59% more** at `200` series (`271.9 KiB` vs `190.7 KiB`)!\n\n## Bonus: `go tool pprof` profiling to tell WHERE certain overhead exists benchmarks in Go and Rust\n\n`benchstat` tells us *what* changed, while [`pprof`](https://github.com/google/pprof) tells us *why*. With `features = [\"pprof\"]` and `[profile.bench] debug = 1` in `Cargo.toml`, `benchfmt::Suite` supports the same `-cpuprofile`, `-memprofile`, and `-memprofilerate` flags as `go test`. Every `.pb.gz` file opens directly in `go tool pprof -http=:8080` or [`pprof.me`](https://pprof.me).\n\n### CPU profiling `DecodeVarints`\n\n`DecodeVarints` does zero heap allocations in both languages, yet Rust finishes in `4.90 µs` while Go takes `8.54 µs` for the same loop. Let’s capture single-threaded CPU profiles from both runners:\n\n```\ngo test -run '^$' -bench '^BenchmarkDecodeVarints$' -benchtime 5s -count 1 -cpu 1 \\\n    -cpuprofile /tmp/go_varints_cpu.pb.gz\n\ncargo bench --bench suite_decode -- \\\n    -bench '^BenchmarkDecodeVarints/' -benchtime 3s -count 1 -cpu 1 \\\n    -cpuprofile /tmp/rust_varints_cpu.pb.gz\n```\n\nOpening the source view in `pprof` shows where each compiler spends its cycles:\n\n[![/2026/rust-microbenchmarks-benchstat/pprof_go_varints_cpu.png](https://www.bwplotka.dev/svg/loading.min.svg)](https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/pprof_go_varints_cpu.png \"Go DecodeVarints CPU source profile\")\n\nGo `DecodeVarints` CPU source profile. Explore the [interactive profile on pprof.me](https://pprof.me/8f4cc5b0654098a24105f5dc43f18531).\n\n[![/2026/rust-microbenchmarks-benchstat/pprof_rust_varints_cpu.png](https://www.bwplotka.dev/svg/loading.min.svg)](https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/pprof_rust_varints_cpu.png \"Rust decode_varints CPU source profile\")\n\nRust `decode_varints` CPU source profile. Explore the [interactive profile on pprof.me](https://pprof.me/ff159d82dab52a12795ed08c8bf93240).\n\nBoth languages spend over `99%` of CPU inside the varint loop. Go, however, burns **`1.74s` (`32.3%`)** on `x |= uint64(b&0x7f) << shift` alone and **`1.24s` (`23.0%`)** on the outer loop checks. Inspecting `go tool objdump` and `objdump -d` shows two codegen differences behind the `42.56%` gap:\n\n1. **Loop unrolling and 64-bit shift masking.** Because `shift` steps by `7` up to `64` (at most 9 bytes), LLVM unrolls Rust’s inner continuation loop. Every variable `<< shift` becomes a constant shift (`shl $0x7`, `shl $0xe`, `shl $0x15`, …) with no `shift` counter or `shift >= 64` branch. Go keeps the inner loop rolled and defines `x << shift` as `0` when `shift >= 64`, whereas x86 `SHLQ CL` masks modulo 64. That forces Go to emit `SHLQ CL`, `CMPQ $64`, `SBBQ`, and `ANDQ` on every continuation byte.\n2. **Signed `int` vs unsigned `usize` indices.** Go’s `i` and `n` are signed `int`, so `i < len(src)` and `n >= len(dst)` do not prove non-negativity across the loop back-edge. Go emits duplicate unsigned `CMPQ`/`JBE` slice bounds checks before `src[i]` and `dst[n]`, while LLVM removes the inner checks on Rust’s unsigned `usize` indices and inlines `decode_varints` into `while b.keep_running()`.\n\n### CPU and heap profiling `DecodeRemoteWrite2`\n\nNext, let’s collect `-cpuprofile` and exact `-memprofile` captures on the `2,000`-series Remote Write 2.0 benchmark:\n\n```\n# CPU profiles: 3s at -cpu 1.\ngo test -run '^$' -bench '^BenchmarkDecodeRemoteWrite2/samples=2000/' -benchtime 3s -count 1 -cpu 1 \\\n    -cpuprofile /tmp/go_rw2_cpu.pb.gz\ncargo bench --bench suite_decode -- \\\n    -bench 'DecodeRemoteWrite2/samples=2000' -benchtime 3s -count 1 -cpu 1 \\\n    -cpuprofile /tmp/rust_rw2_cpu.pb.gz\n\n# Exact heap profiles: 50 iterations with every allocation sampled.\ngo test -run '^$' -bench '^BenchmarkDecodeRemoteWrite2/samples=2000/' -benchtime 50x -count 1 -cpu 1 \\\n    -memprofile /tmp/go_rw2_mem.pb.gz -memprofilerate 1\ncargo bench --bench suite_decode -- \\\n    -bench 'DecodeRemoteWrite2/samples=2000' -benchtime 50x -count 1 -cpu 1 \\\n    -memprofile /tmp/rust_rw2_mem.pb.gz -memprofilerate 1\n```\n\nThe two CPU flamegraphs show very different bottlenecks:\n\n[![/2026/rust-microbenchmarks-benchstat/pprof_go_rw2_cpu.png](https://www.bwplotka.dev/svg/loading.min.svg)](https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/pprof_go_rw2_cpu.png \"Go DecodeRemoteWrite2 CPU flamegraph\")\n\nGo `DecodeRemoteWrite2` CPU flamegraph (`samples=2000`). Explore the [interactive profile on pprof.me](https://pprof.me/7af23a8fbe6409728965b2db5072fb87).\n\n[![/2026/rust-microbenchmarks-benchstat/pprof_rust_rw2_cpu.png](https://www.bwplotka.dev/svg/loading.min.svg)](https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/pprof_rust_rw2_cpu.png \"Rust DecodeRemoteWrite2 CPU flamegraph\")\n\nRust `DecodeRemoteWrite2` CPU flamegraph (`samples=2000`). Explore the [interactive profile on pprof.me](https://pprof.me/9041b44c87f5861a25f8cc5a280c47a3).\n\nGo’s x86-assembly `snappy.Decode` at `~0.12 ms/op` actually beats pure-Rust `snap` at `~0.18 ms/op`, and `vtprotobuf`’s own wire parsing takes only `~0.95 ms/op`. Go still loses overall because `runtime.mallocgc` and GC assist take `54.1%` of all on-CPU time, or `~1.34 ms/op`, allocating and scanning `19,297` heap objects.\n\nIn Rust, `71.1%` of CPU time goes to `prost` field decoding, `8.9%` to `snap` decompression, and `18.7%` to `RawVecInner::grow_amortized` under `Vec::push` and `Vec::reserve`. The `alloc_space` heap profiles show why `RawVecInner::grow_amortized` is so busy and why Rust allocates `30.98%` more `B/op` than Go:\n\n[![/2026/rust-microbenchmarks-benchstat/pprof_go_rw2_mem.png](https://www.bwplotka.dev/svg/loading.min.svg)](https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/pprof_go_rw2_mem.png \"Go DecodeRemoteWrite2 heap alloc_space flamegraph\")\n\nGo `DecodeRemoteWrite2` heap `alloc_space` flamegraph (`50x`). Explore the [interactive profile on pprof.me](https://pprof.me/60e14eabe540995ce6cc1cabd0d6f0e2).\n\n[![/2026/rust-microbenchmarks-benchstat/pprof_rust_rw2_mem.png](https://www.bwplotka.dev/svg/loading.min.svg)](https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/pprof_rust_rw2_mem.png \"Rust DecodeRemoteWrite2 heap alloc_space flamegraph\")\n\nRust `DecodeRemoteWrite2` heap `alloc_space` flamegraph (`50x`). Explore the [interactive profile on pprof.me](https://pprof.me/1fb6663440810f6deb67f24c7829a079).\n\nGo’s `UnmarshalVT` preallocates slices and structs close to their final size, totaling `1.57 MiB/op`. Rust allocates `2.05 MiB/op`, and `88.4%` of those bytes come from `RawVec` capacity doubling inside `prost`:\n\n- `prost` decodes packed `repeated uint32 labels_refs` one `u32` at a time with `Vec::push` instead of pre-sizing from the wire byte length. Every series grows its `Vec<u32>` through capacities `4 -> 8 -> 16 -> 32`, accounting for `23.0%` of all allocated bytes.\n- Growing `Vec<v2::TimeSeries>` from `0` to `2,048` copies inline 112-byte structs at each power of two, which is `21.3%` of allocated bytes. Go only grows an 8-byte `[]*TimeSeries` pointer slice.\n- Pushing one 24-byte `Sample` into an empty `Vec<Sample>` jumps straight to capacity `4` due to `RawVec::MIN_NON_ZERO_CAP`, adding another `8.5%` of allocated bytes across `1,900` series.\n\nHowever, **even with that extra `Vec` churn**, `glibc` thread-cache reallocations in Rust **cost `~3.5x` less CPU than Go’s `mallocgc` and GC assist**, so `-memprofile` points straight to a `Vec::with_capacity` fix.\n\n## Checking our microbenchmarks with a standalone binary and `/usr/bin/time -v`\n\nOk, those microbenchmarks are great, but let’s verify our efficiency numbers against the real program!\n\nMicrobenchmarks run inside an artificial loop with a hot instruction cache, a warm allocator, and a custom `#[global_allocator]` wrapper in `suite_decode`. Do those `ns/op` and `B/op` numbers hold up in an unmodified release binary?\n\nTo check, I wrote two standalone CLI binaries:\n\n1. [One](https://github.com/bwplotka/benchmarks/blob/c55bd342d9ac23e788d06327dc7973951a5eeb82/benchmarks/rust-go-decode/src/bin/verify_decode.rs) using Rust’s default `System` allocator with no benchmark library.\n2. [Second](https://github.com/bwplotka/benchmarks/blob/c55bd342d9ac23e788d06327dc7973951a5eeb82/benchmarks/rust-go-decode/cmd/verify_decode/main.go) in Go.\n\nEach binary runs `N` iterations, accumulates a checksum, and optionally keeps `K` decoded messages alive via `--retain K` to simulate an ingestion queue. I ran both under GNU `/usr/bin/time -v` with `GOMAXPROCS=2` on Go to match `-cpu 2`:\n\n```\ncargo build --release --bin verify_decode\n/usr/bin/time -v ./target/release/verify_decode --case rw2_2000 --iters 1000 --retain 0\n/usr/bin/time -v ./target/release/verify_decode --case rw2_2000 --iters 1000 --retain 1000\n```\n\nHere is what `/usr/bin/time -v` reported across all runs:\n\n| Run | Iters | Microbench | Binary wall | Total CPU | CPU % | Max RSS |\n| --- | --- | --- | --- | --- | --- | --- |\n| Rust `varints` | `500,000` | `4.90 µs` | `5.74 µs` | `2.89s (5.78 µs/op)` | `99%` | `2.44 MiB` |\n| Go `varints` | `500,000` | `8.54 µs` | `8.64 µs` | `4.34s (8.68 µs/op)` | `100%` | `6.11 MiB` |\n| Rust `rw2_2000` | `1,000` | `2.03 ms` | `2.22 ms` | `2.22s (2.22 ms/op)` | `99%` | `4.21 MiB` |\n| Go `rw2_2000` | `1,000` | `2.47 ms` | `2.97 ms` | `4.28s (4.28 ms/op)` | `144%` | `15.98 MiB` |\n| Rust `rw2_2000 --retain 1000` | `1,000` | `2.03 ms` | `2.75 ms` | `3.18s (3.18 ms/op)` | `99%` | `1,292.4 MiB` |\n\nThe process-level numbers show a few important details:\n\n- **`CountingAlloc` did not distort latency.** The standalone Rust binary ran `rw2_2000` at `2.22 ms/op` across `1,000` cold-start iterations, close to `2.03 ms/op` in `benchfmt::Suite` while counting `16,490` allocations per iteration.\n- **`B/op` measures allocator churn, not live memory.** Over `1,000` iterations of `rw2_2000`, Rust requested `2.00 GiB` from the allocator, yet peak RSS with `retain=0` stayed at `4.21 MiB`. Dropping each `Request` at the end of the loop lets `glibc` reuse the freed thread-cache chunks on the next iteration without asking Linux for new pages. Keeping all `1,000` batches alive with `--retain 1000` raised RSS to `1,292.4 MiB` (`1.29 MiB` per retained message after temporary Snappy and `Vec`-doubling buffers were freed) and split CPU into `2.12s` user + `1.06s` kernel `sys` time for page faults.\n- **Wall-clock `ns/op` hides Go’s background GC cost.** In `benchstat`, Rust was `17.68%` faster than Go on `rw2_2000`. Under `/usr/bin/time -v` at `GOMAXPROCS=2`, Rust used `99%` of one core and `4.21 MiB` RSS, while Go hit `144%` CPU, `4.28 ms` of CPU time per op, and `15.98 MiB` RSS. Leaving `GOMAXPROCS` unrestricted on all 24 cores pushed Go to `220%` CPU, or `7.13 ms` of CPU time per op, and `18.53 MiB` RSS as background GC threads swept heap objects. Across the whole process, Rust used **1.9x to 3.2x less CPU** and **3.8x to 4.4x less RAM** per request.\n\n## Summary\n\nIn general I think Rust has real potential in this AI era:\n\n- It has extreme [compile-time verification](https://doc.rust-lang.org/book/ch10-02-traits.html), including strict [ownership and memory safety checks](https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html), which is ideal when AI generates code.\n- It takes ages and can be annoying to program in by hand (sorry!), but AI writes a lot of code nowadays. Code mostly has to be readable for reviews, and Rust is good enough at that.\n- It has a strong columnar storage ecosystem around crates like [`arrow-rs`](https://github.com/apache/arrow-rs).\n- It has no garbage collector, gives more control over memory allocations, and is [clearly faster](#comparing-go-vs-rust)!\n\nIn terms of benchmarking, having both `benchfmt` tools in Rust lets you match the workflow to the job:\n\n1. Run **`benchfmt criterion target/criterion`** on existing Criterion suites when you want `benchstat` A/B tables across commits or languages without editing benchmark code.\n2. Use **`benchfmt::Suite` and `CountingAlloc`** with `features = [\"pprof\"]` for new Rust microbenchmarks to get fast `std`-only compiles, steady-state epochs, `B/op` and `allocs/op` tracking, and `pprof` profiles.\n\nGive [`benchfmt.rs`](https://github.com/bwplotka/benchfmt.rs) a spin and let me know on GitHub what breaks or what is missing!\n","body_html":"<h1 id=\"benchfmt-rs-bringing-go-s-benchmarking-and-benchstat-workflow-to\">benchfmt.rs: Bringing Go&#39;s Benchmarking and benchstat Workflow to Rust</h1>\n<p><a href=\"https://www.bwplotka.dev/\" title=\"Author\" rel=\"nofollow ugc noopener\">Bartek Płotka</a>· 2026-11-03·\n 20 minutes\n·\n 4164 words</p>\n<blockquote><p><strong>TL;DR:</strong> I built <a href=\"https://github.com/bwplotka/benchfmt.rs\" rel=\"nofollow ugc noopener\"><code>benchfmt.rs</code></a>, a zero-dependency Rust benchmark runner and <a href=\"https://crates.io/crates/criterion\" rel=\"nofollow ugc noopener\"><code>Criterion</code></a> converter for Go’s <a href=\"https://pkg.go.dev/golang.org/x/perf/cmd/benchstat\" rel=\"nofollow ugc noopener\"><code>benchstat</code></a>. It adds <code>/key=value</code> projections, heap tracking for allocated bytes (<code>B/op</code>) and objects (<code>allocs/op</code>) per op, and <code>pprof</code> profiling.</p>\n<p>In this post, I am benchmarking <code>varint</code> and <a href=\"https://prometheus.io/docs/specs/prw/remote_write_spec_2_0/\" rel=\"nofollow ugc noopener\">Prometheus Remote Write 2.0</a> decoding across Go and Rust to showcase the benefits of <code>benchstat</code>-like flow in Rust. I’m also sharing some fun learnings on the way (e.g., yes <a href=\"#comparing-go-vs-rust\">Rust is faster</a>).</p></blockquote>\n<p>Recently, I’ve been working on a few Rust projects across Google and <a href=\"https://github.com/prometheus/expfmt-rs\" rel=\"nofollow ugc noopener\">open source</a>. I came to Rust after years of writing performance-sensitive Go in <a href=\"https://prometheus.io/\" rel=\"nofollow ugc noopener\">Prometheus</a>, <a href=\"https://thanos.io/\" rel=\"nofollow ugc noopener\">Thanos</a>, and teaching Go in the <a href=\"https://www.bwplotka.dev/book\" rel=\"nofollow ugc noopener\">“Efficient Go” book</a>. For me, much of Rust’s performance story is really good, until…. <strong>I wanted to run A/B microbenchmarks</strong>.</p>\n<p>Perhaps I was spoiled by the Go benchmarking ecosystem, with <a href=\"https://www.bwplotka.dev/2024/go-microbenchmarks-benchstat/\" rel=\"nofollow ugc noopener\">the <code>go tool -bench</code> and <code>benchstat</code></a> in the center. To me, Rust standard benchmarking tools are lacking:</p>\n<ul><li>Standard format for case comparison projections.</li><li>Standard heap allocation measurements.</li><li>Standard <code>pprof</code> support.</li><li>Simple to understand reliability scoring.</li></ul>\n<blockquote><p><strong>DISCLAIMER:</strong> A <code>standard</code> here means something that will be familiar to large number of developers, and I biased on Go experience for sure!</p></blockquote>\n<p>As a result, I asked myself: can we get <a href=\"https://pkg.go.dev/golang.org/x/perf/cmd/benchstat\" rel=\"nofollow ugc noopener\"><code>benchstat</code></a>-compatible output for Rust microbenchmarks? Wouldn’t that make A/B testing easier and unlock fun Go vs Rust comparisons?</p>\n<p><strong>Here we are!</strong> I built <a href=\"https://github.com/bwplotka/benchfmt.rs\" rel=\"nofollow ugc noopener\"><code>benchfmt.rs</code></a>, a zero-dependency Rust benchmark runner with a thread-local heap allocation counter, <code>pprof</code> profiler integration, and the same CLI flags as <code>go test -bench</code>. For those who don’t trust a new benchmarking runtime (you shouldn’t from the start!), it also ships a <a href=\"https://crates.io/crates/criterion\" rel=\"nofollow ugc noopener\"><code>Criterion</code></a> and <code>libtest</code> converter that emits Go’s <a href=\"https://go.googlesource.com/proposal/+/master/design/14313-benchmark-format.md\" rel=\"nofollow ugc noopener\"><code>benchfmt</code></a> format directly.</p>\n<p>In this blog post, we will implement the same pieces of code in Go and Rust, benchmark them across different Rust frameworks, and compare the results. We might learn something new along the way (I did when building this!). Enjoy!</p>\n<p>All benchmark code and raw outputs live in <a href=\"https://github.com/bwplotka/benchmarks/tree/c55bd342d9ac23e788d06327dc7973951a5eeb82/benchmarks/rust-go-decode\" rel=\"nofollow ugc noopener\"><code>bwplotka/benchmarks/benchmarks/rust-go-decode</code></a>.</p>\n<h2 id=\"benchmarking-two-workloads\">Benchmarking two workloads</h2>\n<p>Let’s pick two workloads to implement and benchmark in both Go and Rust. I am particularly curious about protobuf decoding performance, which, funny enough, is often one of the main costs of running distributed systems these days. Thus, let’s decode our <a href=\"https://prometheus.io/docs/specs/prw/remote_write_spec_2_0/\" rel=\"nofollow ugc noopener\">Prometheus Remote Write 2.0 protocol</a>. Because protobuf code generators have different maturity and trade-offs across languages, let’s also measure something simpler alongside it, a standalone <code>varint</code> decoder.</p>\n<h3 id=\"workload-details\">Workload details:</h3>\n<ol><li><strong><code>DecodeVarints</code>.</strong> A 36-line unsigned LEB128 varint stream decoder. Varints are the integer encoding underneath Protocol Buffers. Our fixture <code>varints_2000.bin</code> is <code>4,798</code> bytes and packs <code>2,000</code> <code>uint64</code> values mixing 1-byte symbol indices, 2-byte bucket offsets, 4-byte deltas, and 6-byte millisecond Unix timestamps. The function writes into a preallocated <code>2,000</code>-element slice with zero heap allocations.</li><li><strong><code>DecodeRemoteWrite2</code>.</strong> A full HTTP request body decoder for Snappy-compressed Remote Write 2.0 (<code>io.prometheus.write.v2.Request</code>) payloads. We test batches of <code>200</code> and <code>2,000</code> series, where <code>2,000</code> matches Prometheus’s default <code>max_samples_per_send</code>. The <code>2,000</code>-series fixture mixes counters, gauges, classic and native histograms, <code>1,000</code> trace exemplars, and metric metadata. Remote Write 2.0 interns all strings into a top-level <code>symbols</code> table and stores packed varint <code>labels_refs</code> per series. That shrinks the <code>2,000</code>-series batch to <code>244.9 KiB</code> uncompressed and <code>57.0 KiB</code> Snappy-compressed.</li></ol>\n<h2 id=\"implementing-and-benchmarking-in-go\">Implementing and benchmarking in Go</h2>\n<p>Let’s start with Go. First, <code>DecodeVarints</code> writes straight into a caller-provided slice, with a fast path for single-byte values below <code>0x80</code>:</p>\n<pre><code>func DecodeVarints(src []byte, dst []uint64) (int, error) {\n    n := 0\n    i := 0\n    for i &lt; len(src) {\n        if n &gt;= len(dst) {\n            return n, ErrDstTooSmall\n        }\n        b := src[i]\n        i++\n        if b &lt; 0x80 {\n            dst[n] = uint64(b)\n            n++\n            continue\n        }\n        x := uint64(b &amp; 0x7f)\n        shift := uint(7)\n        for {\n            if i &gt;= len(src) {\n                return n, ErrUnexpectedEOF\n            }\n            b = src[i]\n            i++\n            x |= uint64(b&amp;0x7f) &lt;&lt; shift\n            if b &lt; 0x80 {\n                break\n            }\n            shift += 7\n            if shift &gt;= 64 {\n                return n, ErrOverflow\n            }\n        }\n        dst[n] = x\n        n++\n    }\n    return n, nil\n}</code></pre>\n<p>Second, for <code>DecodeRemoteWrite2</code>, we tune the Go implementation the way Prometheus runs in production rather than benchmarking slow reflection-based unmarshaling. We pair <a href=\"https://github.com/golang/snappy\" rel=\"nofollow ugc noopener\"><code>github.com/golang/snappy</code></a>, which uses x86-64 assembly on <code>linux/amd64</code>, with <a href=\"https://github.com/planetscale/vtprotobuf\" rel=\"nofollow ugc noopener\"><code>vtprotobuf</code></a> and its generated, optimized <code>UnmarshalVT</code> decoder:</p>\n<pre><code>func DecodeRemoteWrite2(compressed []byte) (*writev2.Request, error) {\n    raw, err := snappy.Decode(nil, compressed)\n    if err != nil {\n        return nil, err\n    }\n    req := &amp;writev2.Request{}\n    if err := req.UnmarshalVT(raw); err != nil {\n        return nil, err\n    }\n    return req, nil\n}</code></pre>\n<p>Now let’s measure the efficiency of our Go code!</p>\n<p>Notice Go’s <code>/key=value</code> sub-benchmark names (<code>samples=2000/lang=go</code>), which let Go’s official <a href=\"https://pkg.go.dev/golang.org/x/perf/cmd/benchstat\" rel=\"nofollow ugc noopener\"><code>benchstat</code></a> tool pivot across <code>/lang</code> or <code>/samples</code> later, along with <code>b.SetBytes</code>, <code>b.ReportAllocs</code>, and <code>for b.Loop()</code>:</p>\n<pre><code>func BenchmarkDecodeVarints(b *testing.B) {\n    manifest := ensureFixtures(b)\n    raw, err := os.ReadFile(filepath.Join(&quot;testdata&quot;, &quot;varints_2000.bin&quot;))\n    if err != nil {\n        b.Fatalf(&quot;read varints: %v&quot;, err)\n    }\n    dst := make([]uint64, manifest.VarintsCount)\n\n    // Encode dimensions as /key=value so benchstat can pivot across /samples or /lang.\n    b.Run(&quot;samples=2000/lang=go&quot;, func(b *testing.B) {\n        b.SetBytes(int64(len(raw)))\n        b.ReportAllocs()\n        // NOTE: b.Loop() resets the timer on its first call, stops it on exit, and keeps\n        // call arguments and results alive without package-level sink variables.\n        for b.Loop() {\n            n, err := DecodeVarints(raw, dst)\n            if err != nil || n != manifest.VarintsCount {\n                b.Fatalf(&quot;unexpected decode result: n=%d err=%v&quot;, n, err)\n            }\n        }\n    })\n}\n\nfunc BenchmarkDecodeRemoteWrite2(b *testing.B) {\n    ensureFixtures(b)\n\n    for _, samples := range []int{200, 2000} {\n        file := fmt.Sprintf(&quot;rw2_%d.snappy&quot;, samples)\n        compressed, err := os.ReadFile(filepath.Join(&quot;testdata&quot;, file))\n        if err != nil {\n            b.Fatalf(&quot;read %s: %v&quot;, file, err)\n        }\n\n        b.Run(fmt.Sprintf(&quot;samples=%d/lang=go&quot;, samples), func(b *testing.B) {\n            b.SetBytes(int64(len(compressed)))\n            b.ReportAllocs()\n            for b.Loop() {\n                req, err := DecodeRemoteWrite2(compressed)\n                if err != nil || len(req.Timeseries) != samples {\n                    b.Fatalf(&quot;unexpected v2 decode: err=%v&quot;, err)\n                }\n            }\n        })\n    }\n}</code></pre>\n<p>Finally, let’s run <code>6</code> benchmark runs (“epochs”) for <code>2s</code> each at <code>-cpu 2</code> on a 24-core Intel Xeon 2.20GHz Linux workstation (Go 1.26.8, Rust 1.99.1):</p>\n<pre><code>go test -run &#39;^$&#39; -bench &#39;^BenchmarkDecode&#39; \\\n    -benchtime 2s -count 6 -cpu 2 -timeout 999m -benchmem \\\n| tee results/go.txt</code></pre>\n<p>One epoch looks as follows:</p>\n<pre><code>goos: linux\ngoarch: amd64\npkg: github.com/bwplotka/benchmarks/benchmarks/rust-go-decode\ncpu: Intel(R) Xeon(R) CPU @ 2.20GHz\nBenchmarkDecodeVarints/samples=2000/lang=go-2          279861      8514 ns/op   563.52 MB/s        0 B/op        0 allocs/op\nBenchmarkDecodeRemoteWrite2/samples=200/lang=go-2        8467    264321 ns/op    31.43 MB/s   195296 B/op     2189 allocs/op\nBenchmarkDecodeRemoteWrite2/samples=2000/lang=go-2        976   2448633 ns/op    23.84 MB/s  1643970 B/op    19297 allocs/op</code></pre>\n<h2 id=\"implementing-and-benchmarking-in-rust-with-criterion\">Implementing and benchmarking in Rust with Criterion</h2>\n<p>Now let’s port both decoders and benchmarks to Rust. First, <code>decode_varints</code> mirrors the Go loop line for line:</p>\n<pre><code>pub fn decode_varints(src: &amp;[u8], dst: &amp;mut [u64]) -&gt; Result&lt;usize, VarintError&gt; {\n    let mut n = 0usize;\n    let mut i = 0usize;\n    while i &lt; src.len() {\n        if n &gt;= dst.len() {\n            return Err(VarintError::DstTooSmall);\n        }\n        let mut b = src[i];\n        i += 1;\n        if b &lt; 0x80 {\n            dst[n] = u64::from(b);\n            n += 1;\n            continue;\n        }\n        let mut x = u64::from(b &amp; 0x7f);\n        let mut shift = 7u32;\n        loop {\n            if i &gt;= src.len() {\n                return Err(VarintError::UnexpectedEof);\n            }\n            b = src[i];\n            i += 1;\n            x |= u64::from(b &amp; 0x7f) &lt;&lt; shift;\n            if b &lt; 0x80 {\n                break;\n            }\n            shift += 7;\n            if shift &gt;= 64 {\n                return Err(VarintError::Overflow);\n            }\n        }\n        dst[n] = x;\n        n += 1;\n    }\n    Ok(n)\n}</code></pre>\n<p>Second, <code>decode_remote_write2</code> pairs the <a href=\"https://crates.io/crates/snap\" rel=\"nofollow ugc noopener\"><code>snap</code></a> crate with <a href=\"https://crates.io/crates/prost\" rel=\"nofollow ugc noopener\"><code>prost</code></a> compile-time generated <code>Message</code> structs:</p>\n<pre><code>pub fn decode_remote_write2(compressed: &amp;[u8]) -&gt; Result&lt;proto::v2::Request, String&gt; {\n    let raw = snap::raw::Decoder::new()\n        .decompress_vec(compressed)\n        .map_err(|e| e.to_string())?;\n    proto::v2::Request::decode(raw.as_slice()).map_err(|e| e.to_string())\n}</code></pre>\n<p>Now, let’s write both Rust benchmarks using <a href=\"https://crates.io/crates/criterion\" rel=\"nofollow ugc noopener\"><code>Criterion</code></a>, the default choice for stable-Rust microbenchmarking. We keep the same <code>/key=value</code> benchmark IDs and configure <code>Throughput::BytesDecimal</code> to match Go’s decimal <code>MB/s</code>:</p>\n<pre><code>fn bench_varints(c: &amp;mut Criterion) {\n    // Resolve testdata/ from the crate directory baked in at compile time.\n    let root = Path::new(env!(&quot;CARGO_MANIFEST_DIR&quot;)).join(&quot;testdata&quot;);\n    let raw = fs::read(root.join(&quot;varints_2000.bin&quot;)).unwrap();\n    // Preallocate the output buffer outside the timed loop so each op does 0 heap allocs.\n    let mut dst = vec![0u64; 2000];\n\n    // Group + function ID join into &quot;DecodeVarints/samples=2000/lang=rust&quot; to match Go&#39;s b.Run.\n    let mut group = c.benchmark_group(&quot;DecodeVarints&quot;);\n    // Report decimal MB/s (1e6 B/s) to match Go&#39;s b.SetBytes; Throughput::Bytes prints MiB/s.\n    group.throughput(Throughput::BytesDecimal(raw.len() as u64));\n    group.bench_function(&quot;samples=2000/lang=rust&quot;, |b| {\n        // b.iter runs the closure across Criterion&#39;s warmup and 100 linear-ramp samples.\n        b.iter(|| {\n            // std::hint::black_box is an LLVM optimization barrier: Unlike Go&#39;s b.Loop(), Rust\n            // would constant-fold known inputs or delete unused work without black_box hints.\n            let n = decode_varints(black_box(&amp;raw), black_box(&amp;mut dst)).unwrap();\n            assert_eq!(n, 2000);\n            // Observe the written buffer so LLVM cannot optimize away stores into dst.\n            black_box(dst[n - 1]);\n        });\n    });\n    group.finish();\n}\n\nfn bench_remote_write2(c: &amp;mut Criterion) {\n    let root = Path::new(env!(&quot;CARGO_MANIFEST_DIR&quot;)).join(&quot;testdata&quot;);\n    let mut group = c.benchmark_group(&quot;DecodeRemoteWrite2&quot;);\n\n    for samples in [200usize, 2000usize] {\n        let file = format!(&quot;rw2_{samples}.snappy&quot;);\n        let compressed = fs::read(root.join(file)).unwrap();\n\n        group.throughput(Throughput::BytesDecimal(compressed.len() as u64));\n        let id = format!(&quot;samples={samples}/lang=rust&quot;);\n        group.bench_function(&amp;id, |b| {\n            b.iter(|| {\n                let req = decode_remote_write2(black_box(&amp;compressed)).unwrap();\n                assert_eq!(req.timeseries.len(), samples);\n                // Force req to materialize before dropping at the end of the iteration.\n                black_box(req);\n            });\n        });\n    }\n    group.finish();\n}\n\n// Generate the benchmark group runner and custom main() required by harness = false in Cargo.toml.\ncriterion_group!(benches, bench_varints, bench_remote_write2);\ncriterion_main!(benches);</code></pre>\n<p>Finally, let’s run the Criterion benchmark suite:</p>\n<pre><code>cargo bench --bench criterion_decode</code></pre>\n<pre><code>DecodeVarints/samples=2000/lang=rust\n                        time:   [5.2789 µs 5.3369 µs 5.4014 µs]\n                        thrpt:  [888.29 MB/s 899.02 MB/s 908.90 MB/s]\n\nDecodeRemoteWrite2/samples=200/lang=rust\n                        time:   [237.09 µs 238.48 µs 239.95 µs]\n                        thrpt:  [34.620 MB/s 34.833 MB/s 35.037 MB/s]\n\nDecodeRemoteWrite2/samples=2000/lang=rust\n                        time:   [2.0712 ms 2.0831 ms 2.0968 ms]\n                        thrpt:  [27.835 MB/s 28.018 MB/s 28.179 MB/s]</code></pre>\n<h3 id=\"criterion-downsides\">Criterion downsides</h3>\n<p>Criterion is convenient when you want confidence intervals and HTML plots from a single <code>cargo bench</code> command without external tools. However, compared to <code>go test</code> and <code>benchstat</code>, four design choices get in the way:</p>\n<ol><li><strong>Compile weight.</strong> Pulling in <code>criterion</code> with <code>default-features = false</code> still compiles <code>clap</code>, <code>regex-automata</code>, <code>ciborium</code>, <code>oorandom</code>, <code>itertools</code>, <code>tinytemplate</code>, and <code>anes</code>. That adds 42 transitive crates and ~19s to a cold <code>cargo bench</code> build.</li><li><p><strong>Linear-ramp sampling vs steady-state epochs.</strong></p><ul><li>Go’s <code>testing.B</code> calibrates a fixed iteration count <code>N</code> for <code>-benchtime 2s</code> and runs <code>-count 6</code> equal steady-state epochs, which <code>benchstat</code> summarizes using the outlier-resistant median and Mann-Whitney U test.</li><li>Criterion defaults to <code>Linear</code> sampling: after a 3-second warmup, it collects 100 batches of <code>[1d, 2d, ..., 100d]</code> iterations and fits a least-squares line <code>y = m * x</code> through the origin. Because the line is forced through zero, it does not subtract a fixed timer-overhead intercept. Instead, it computes a <code>k^2</code>-weighted average where batch 100 carries <code>10,000x</code> the weight of batch 1 and late OS spikes pull the slope up.</li></ul>\n<p> Comparing those two models directly introduces two measurable distortions:</p>\n<ul><li>First, grouping Criterion’s 100 ramp samples into 6 <code>benchstat</code> epochs gives epoch 1 <code>11.5x</code> fewer iterations than epoch 6 (<code>24,888</code> vs <code>286,212</code> on <code>DecodeVarints</code>). Mixing short bursts with long runs inflates variance to <code>± 5%</code>, compared to <code>± 1-2%</code> in steady-state epochs.</li><li>Second, timing 100 separate closure batches adds <code>+8.6%</code> overhead on microsecond loops (<code>5.36 µs</code> vs <code>4.90 µs</code>, <code>p=0.002</code>) and skews early allocating samples with cold allocator caches (<code>+2.3%</code> on <code>DecodeRemoteWrite2</code>).</li></ul></li><li><strong>No heap allocation tracking.</strong> Criterion reports wall-clock time and throughput only. On <code>DecodeVarints</code> at <code>0 allocs/op</code>, that is fine. On <code>DecodeRemoteWrite2</code>, we cannot tell whether <code>prost</code> made 500 heap allocations or 500,000, or whether a refactor doubled heap churn.</li><li><strong>Pairwise baseline folders vs multi-dimensional tables.</strong> Criterion compares <code>target/criterion/&lt;id&gt;/base</code> against <code>new</code>. You cannot feed a single run file into a table that <a href=\"https://www.bwplotka.dev/2024/go-microbenchmarks-benchstat/\" rel=\"nofollow ugc noopener\">pivots <code>-col /samples</code> or <code>-col /lang</code></a> across Go and Rust.</li></ol>\n<h2 id=\"converting-criterion-output-with-the-benchfmt-cli\">Converting Criterion output with the <code>benchfmt</code> CLI</h2>\n<p>If a Rust codebase already uses Criterion, rewriting benchmarks just to get a <code>benchstat</code> table is not ideal. That is why <code>benchfmt.rs</code> includes a standalone <code>benchfmt</code> CLI binary. It reads <code>benchmark.json</code> and <code>sample.json</code> directly from <code>target/criterion</code>, extracts <code>Throughput::BytesDecimal</code> (or <code>Throughput::Bytes</code>) as decimal <code>MB/s</code>, and groups Criterion’s 100 samples into <code>6</code> contiguous epochs via <code>--epochs 6</code>:</p>\n<pre><code>cargo install --git https://github.com/bwplotka/benchfmt.rs\n\nbenchfmt criterion target/criterion \\\n    --pkg github.com/bwplotka/benchmarks/benchmarks/rust-go-decode \\\n    --cpu 2 \\\n&gt; results/rust_criterion.txt\n\nbenchstat -col /lang results/go.txt results/rust_criterion.txt</code></pre>\n<p>Without touching the benchmark implementation, <code>benchstat</code> gives us a side-by-side Go vs Rust table:</p>\n<pre><code>goos: linux\ngoarch: amd64\npkg: github.com/bwplotka/benchmarks/benchmarks/rust-go-decode\ncpu: Intel(R) Xeon(R) CPU @ 2.20GHz\n                                  │     go      │                rust                │\n                                  │   sec/op    │   sec/op     vs base               │\nDecodeVarints/samples=2000-2        8.536µ ± 1%   5.364µ ± 5%  -37.16% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=200-2    271.4µ ± 3%   235.1µ ± 4%  -13.37% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=2000-2   2.470m ± 5%   2.082m ± 1%  -15.72% (p=0.002 n=6)\ngeomean                             178.9µ        138.0µ       -22.87%\n\n                                  │      go      │                rust                 │\n                                  │     B/s      │     B/s       vs base               │\nDecodeVarints/samples=2000-2        536.1Mi ± 1%   853.2Mi ± 5%  +59.15% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=200-2    29.19Mi ± 3%   33.70Mi ± 4%  +15.44% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=2000-2   22.53Mi ± 5%   26.74Mi ± 1%  +18.67% (p=0.002 n=6)\ngeomean                             70.65Mi        91.60Mi       +29.67%\n\n                                  │       go       │\n                                  │      B/op      │\nDecodeVarints/samples=2000-2          0.000 ± 0%\nDecodeRemoteWrite2/samples=200-2    190.7Ki ± 0%\nDecodeRemoteWrite2/samples=2000-2   1.568Mi ± 0%\ngeomean                                          ¹\n¹ summaries must be &gt;0 to compute geomean\n\n                                  │      go       │\n                                  │   allocs/op   │\nDecodeVarints/samples=2000-2         0.000 ± 0%\nDecodeRemoteWrite2/samples=200-2    2.189k ± 0%\nDecodeRemoteWrite2/samples=2000-2   19.30k ± 0%\ngeomean                                         ¹\n¹ summaries must be &gt;0 to compute geomean</code></pre>\n<p>Now, with this view we finally have a readable, standard <code>benchstat</code> output Go developers love. It clearly shows the Go vs Rust efficiency for our example functions. Rust is <code>37.16%</code> faster on <code>DecodeVarints</code> (<code>5.36 µs</code> vs <code>8.54 µs</code>) and <code>13.37%</code> to <code>15.72%</code> faster on <code>DecodeRemoteWrite2</code>.</p>\n<p>Unfortunately, there are some gaps:</p>\n<ol><li>The <code>B/op</code> and <code>allocs/op</code> tables only have a <code>go</code> column because Criterion never recorded allocations</li><li>Criterion’s linear-ramp epochs still leave <code>± 5%</code> variance on <code>DecodeVarints</code>.</li></ol>\n<h2 id=\"native-benchfmt-runs-with-benchfmt-suite-and-countingalloc\">Native <code>benchfmt</code> runs with <code>benchfmt::Suite</code> and <code>CountingAlloc</code></h2>\n<p>For new Rust benchmarks, we can skip Criterion and use <code>benchfmt::Suite</code> with <code>harness = false</code> in <code>Cargo.toml</code>. Registering <code>CountingAlloc</code> as <code>#[global_allocator]</code> tracks every <code>alloc</code>, <code>alloc_zeroed</code>, and <code>realloc</code> in a <code>thread_local!</code> <code>Cell&lt;(u64, u64)&gt;</code> with zero atomic contention and zero external dependencies:</p>\n<pre><code>// Wrap Rust&#39;s System allocator globally so every heap alloc/realloc on the benchmark thread\n// increments thread-local (allocs, bytes) counters for B/op and allocs/op.\n#[global_allocator]\nstatic ALLOC: CountingAlloc = CountingAlloc::new();\n\nfn main() {\n    let root = Path::new(env!(&quot;CARGO_MANIFEST_DIR&quot;)).join(&quot;testdata&quot;);\n    let raw_varints = fs::read(root.join(&quot;varints_2000.bin&quot;)).unwrap();\n    let rw2_200 = fs::read(root.join(&quot;rw2_200.snappy&quot;)).unwrap();\n    let rw2_2000 = fs::read(root.join(&quot;rw2_2000.snappy&quot;)).unwrap();\n    let mut dst = vec![0u64; 2000];\n\n    // Parse go-test-compatible CLI flags (-bench, -benchtime, -count, -cpu, -benchmem,\n    // -cpuprofile, -memprofile) from std::env::args() and set the default &quot;pkg:&quot; header.\n    // Using the same pkg path as Go&#39;s go.mod lets `benchstat -col /lang` join Go and Rust rows.\n    Suite::from_env(&quot;github.com/bwplotka/benchmarks/benchmarks/rust-go-decode&quot;)\n        // Attach ALLOC so each benchmark snapshots thread-local allocation deltas per epoch.\n        .with_alloc(&amp;ALLOC)\n        // Suite prepends &quot;Benchmark&quot; in benchfmt output, matching Go&#39;s BenchmarkDecodeVarints.\n        .bench(&quot;DecodeVarints/samples=2000/lang=rust&quot;, |b| {\n            b.set_bytes(raw_varints.len() as u64);\n            b.report_allocs();\n            // Go b.Loop() equivalent: starts timing, alloc snapshots, and any pprof capture on the\n            // first call, runs the calibrated N steady-state iterations, then stops all three.\n            while b.keep_running() {\n                let n = decode_varints(black_box(&amp;raw_varints), black_box(&amp;mut dst)).unwrap();\n                assert_eq!(n, 2000);\n                black_box(dst[n - 1]);\n            }\n        })\n        .bench(&quot;DecodeRemoteWrite2/samples=200/lang=rust&quot;, |b| {\n            b.set_bytes(rw2_200.len() as u64);\n            b.report_allocs();\n            while b.keep_running() {\n                let req = decode_remote_write2(black_box(&amp;rw2_200)).unwrap();\n                assert_eq!(req.timeseries.len(), 200);\n                black_box(req);\n            }\n        })\n        .bench(&quot;DecodeRemoteWrite2/samples=2000/lang=rust&quot;, |b| {\n            b.set_bytes(rw2_2000.len() as u64);\n            b.report_allocs();\n            while b.keep_running() {\n                let req = decode_remote_write2(black_box(&amp;rw2_2000)).unwrap();\n                assert_eq!(req.timeseries.len(), 2000);\n                black_box(req);\n            }\n        })\n        .run();\n}</code></pre>\n<blockquote><p><strong>Does <code>CountingAlloc</code> work with Tokio and custom allocators?</strong> Mostly yes. You can wrap custom allocators like <code>jemalloc</code> or <code>mimalloc</code> via <code>CountingAlloc::with_allocator(Jemalloc)</code>. Because <code>CountingAlloc</code> uses thread-local counters on the benchmark thread, single-threaded Tokio <code>current_thread</code> works out of the box, while multi-threaded pools like <code>tokio::spawn</code> or <code>rayon</code> need a process-wide atomic <a href=\"https://github.com/bwplotka/benchfmt.rs/blob/main/src/alloc.rs\" rel=\"nofollow ugc noopener\"><code>AllocTracker</code></a> passed to <code>Suite::with_alloc</code>.</p></blockquote>\n<p><code>Suite::from_env</code> reads <code>std::env::args()</code>, auto-detects <code>goos</code>, <code>goarch</code>, and <code>cpu</code>, and writes the passed string as the <code>pkg:</code> metadata line (matching Go’s module path so <code>benchstat</code> joins Go and Rust rows into one table). Passing flags after <code>--</code> mirrors <code>go test</code>:</p>\n<pre><code>cargo bench --bench suite_decode -- \\\n    -bench &#39;^BenchmarkDecode&#39; -benchtime 2s -count 6 -cpu 2 -benchmem \\\n| tee results/rust_suite.txt</code></pre>\n<p>First, let’s verify that <code>benchfmt::Suite</code> agrees with Criterion on timing, even with <code>CountingAlloc</code> active on every allocation:</p>\n<pre><code>benchstat criterion=results/rust_criterion.txt suite=results/rust_suite.txt</code></pre>\n<pre><code>                                            │  criterion  │               suite               │\n                                            │   sec/op    │   sec/op     vs base              │\nDecodeRemoteWrite2/samples=2000/lang=rust-2   2.082m ± 1%   2.033m ± 2%  -2.33% (p=0.015 n=6)\nDecodeRemoteWrite2/samples=200/lang=rust-2    235.1µ ± 4%   231.5µ ± 1%  -1.55% (p=0.004 n=6)\nDecodeVarints/samples=2000/lang=rust-2        5.364µ ± 5%   4.902µ ± 2%  -8.60% (p=0.002 n=6)\ngeomean                                       138.0µ        132.1µ       -4.21%</code></pre>\n<p>On <code>DecodeRemoteWrite2</code>, Criterion and <code>benchfmt::Suite</code> agree within <code>1.6%</code> to <code>2.3%</code>, while <code>Suite</code>’s steady-state <code>2s</code> epochs tighten worst-case variance from <code>± 4-5%</code> down to <code>± 1-2%</code>. On <code>DecodeVarints</code>, <code>while b.keep_running()</code> runs <code>490,000</code> back-to-back iterations in one tight loop instead of 100 short ramp batches, clocking <code>4.90 µs</code> vs <code>5.36 µs</code>.</p>\n<h3 id=\"comparing-go-vs-rust\">Comparing Go vs Rust</h3>\n<p>Now let’s run the full Go vs Rust comparison with <code>results/rust_suite.txt</code>:</p>\n<pre><code>benchstat -col /lang results/go.txt results/rust_suite.txt</code></pre>\n<pre><code>goos: linux\ngoarch: amd64\npkg: github.com/bwplotka/benchmarks/benchmarks/rust-go-decode\ncpu: Intel(R) Xeon(R) CPU @ 2.20GHz\n                                  │     go      │                rust                │\n                                  │   sec/op    │   sec/op     vs base               │\nDecodeVarints/samples=2000-2        8.536µ ± 1%   4.902µ ± 2%  -42.56% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=200-2    271.4µ ± 3%   231.5µ ± 1%  -14.71% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=2000-2   2.470m ± 5%   2.033m ± 2%  -17.68% (p=0.002 n=6)\ngeomean                             178.9µ        132.1µ       -26.12%\n\n                                  │      go      │                rust                 │\n                                  │     B/s      │     B/s       vs base               │\nDecodeVarints/samples=2000-2        536.1Mi ± 1%   933.3Mi ± 2%  +74.09% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=200-2    29.19Mi ± 3%   34.22Mi ± 1%  +17.23% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=2000-2   22.53Mi ± 5%   27.37Mi ± 2%  +21.48% (p=0.002 n=6)\ngeomean                             70.65Mi        95.62Mi       +35.35%\n\n                                  │       go       │                 rust                  │\n                                  │      B/op      │     B/op      vs base                 │\nDecodeVarints/samples=2000-2          0.000 ± 0%       0.000 ± 0%        ~ (p=1.000 n=6) ¹\nDecodeRemoteWrite2/samples=200-2    190.7Ki ± 0%     271.9Ki ± 0%  +42.59% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=2000-2   1.568Mi ± 0%     2.053Mi ± 0%  +30.98% (p=0.002 n=6)\ngeomean                                          ²                 +23.15%               ²\n¹ all samples are equal\n² summaries must be &gt;0 to compute geomean\n\n                                  │      go       │                 rust                 │\n                                  │   allocs/op   │  allocs/op   vs base                 │\nDecodeVarints/samples=2000-2         0.000 ± 0%      0.000 ± 0%        ~ (p=1.000 n=6) ¹\nDecodeRemoteWrite2/samples=200-2    2.189k ± 0%     1.905k ± 0%  -12.97% (p=0.002 n=6)\nDecodeRemoteWrite2/samples=2000-2   19.30k ± 0%     16.49k ± 0%  -14.55% (p=0.002 n=6)\ngeomean                                         ²                 -9.40%               ²\n¹ all samples are equal\n² summaries must be &gt;0 to compute geomean</code></pre>\n<p>Look closely at the <code>B/op</code> and <code>allocs/op</code> tables. That is the kind of surprise you miss without allocation counters:</p>\n<ul><li>Rust makes <strong>14.55% fewer heap allocations</strong> at <code>2,000</code> series (<code>16,490</code> vs Go’s <code>19,297 allocs/op</code>). Go’s <code>protoc-gen-go</code> generates pointer slices such as <code>[]*TimeSeries</code> and <code>[]*Sample</code>, allocating every nested struct separately on the heap. Rust’s <code>prost</code> stores <code>Vec&lt;TimeSeries&gt;</code> and <code>Vec&lt;Sample&gt;</code> inline by value.</li><li>Yet Rust requests <strong>30.98% more heap bytes</strong> at <code>2,000</code> series (<code>2.05 MiB</code> vs Go’s <code>1.57 MiB</code>) and <strong>42.59% more</strong> at <code>200</code> series (<code>271.9 KiB</code> vs <code>190.7 KiB</code>)!</li></ul>\n<h2 id=\"bonus-go-tool-pprof-profiling-to-tell-where-certain-overhead-exi\">Bonus: <code>go tool pprof</code> profiling to tell WHERE certain overhead exists benchmarks in Go and Rust</h2>\n<p><code>benchstat</code> tells us <em>what</em> changed, while <a href=\"https://github.com/google/pprof\" rel=\"nofollow ugc noopener\"><code>pprof</code></a> tells us <em>why</em>. With <code>features = [&quot;pprof&quot;]</code> and <code>[profile.bench] debug = 1</code> in <code>Cargo.toml</code>, <code>benchfmt::Suite</code> supports the same <code>-cpuprofile</code>, <code>-memprofile</code>, and <code>-memprofilerate</code> flags as <code>go test</code>. Every <code>.pb.gz</code> file opens directly in <code>go tool pprof -http=:8080</code> or <a href=\"https://pprof.me\" rel=\"nofollow ugc noopener\"><code>pprof.me</code></a>.</p>\n<h3 id=\"cpu-profiling-decodevarints\">CPU profiling <code>DecodeVarints</code></h3>\n<p><code>DecodeVarints</code> does zero heap allocations in both languages, yet Rust finishes in <code>4.90 µs</code> while Go takes <code>8.54 µs</code> for the same loop. Let’s capture single-threaded CPU profiles from both runners:</p>\n<pre><code>go test -run &#39;^$&#39; -bench &#39;^BenchmarkDecodeVarints$&#39; -benchtime 5s -count 1 -cpu 1 \\\n    -cpuprofile /tmp/go_varints_cpu.pb.gz\n\ncargo bench --bench suite_decode -- \\\n    -bench &#39;^BenchmarkDecodeVarints/&#39; -benchtime 3s -count 1 -cpu 1 \\\n    -cpuprofile /tmp/rust_varints_cpu.pb.gz</code></pre>\n<p>Opening the source view in <code>pprof</code> shows where each compiler spends its cycles:</p>\n<p><a href=\"https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/pprof_go_varints_cpu.png\" title=\"Go DecodeVarints CPU source profile\" rel=\"nofollow ugc noopener\"><img src=\"https://www.bwplotka.dev/svg/loading.min.svg\" alt=\"/2026/rust-microbenchmarks-benchstat/pprof_go_varints_cpu.png\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></a></p>\n<p>Go <code>DecodeVarints</code> CPU source profile. Explore the <a href=\"https://pprof.me/8f4cc5b0654098a24105f5dc43f18531\" rel=\"nofollow ugc noopener\">interactive profile on pprof.me</a>.</p>\n<p><a href=\"https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/pprof_rust_varints_cpu.png\" title=\"Rust decode_varints CPU source profile\" rel=\"nofollow ugc noopener\"><img src=\"https://www.bwplotka.dev/svg/loading.min.svg\" alt=\"/2026/rust-microbenchmarks-benchstat/pprof_rust_varints_cpu.png\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></a></p>\n<p>Rust <code>decode_varints</code> CPU source profile. Explore the <a href=\"https://pprof.me/ff159d82dab52a12795ed08c8bf93240\" rel=\"nofollow ugc noopener\">interactive profile on pprof.me</a>.</p>\n<p>Both languages spend over <code>99%</code> of CPU inside the varint loop. Go, however, burns <strong><code>1.74s</code> (<code>32.3%</code>)</strong> on <code>x |= uint64(b&amp;0x7f) &lt;&lt; shift</code> alone and <strong><code>1.24s</code> (<code>23.0%</code>)</strong> on the outer loop checks. Inspecting <code>go tool objdump</code> and <code>objdump -d</code> shows two codegen differences behind the <code>42.56%</code> gap:</p>\n<ol><li><strong>Loop unrolling and 64-bit shift masking.</strong> Because <code>shift</code> steps by <code>7</code> up to <code>64</code> (at most 9 bytes), LLVM unrolls Rust’s inner continuation loop. Every variable <code>&lt;&lt; shift</code> becomes a constant shift (<code>shl $0x7</code>, <code>shl $0xe</code>, <code>shl $0x15</code>, …) with no <code>shift</code> counter or <code>shift &gt;= 64</code> branch. Go keeps the inner loop rolled and defines <code>x &lt;&lt; shift</code> as <code>0</code> when <code>shift &gt;= 64</code>, whereas x86 <code>SHLQ CL</code> masks modulo 64. That forces Go to emit <code>SHLQ CL</code>, <code>CMPQ $64</code>, <code>SBBQ</code>, and <code>ANDQ</code> on every continuation byte.</li><li><strong>Signed <code>int</code> vs unsigned <code>usize</code> indices.</strong> Go’s <code>i</code> and <code>n</code> are signed <code>int</code>, so <code>i &lt; len(src)</code> and <code>n &gt;= len(dst)</code> do not prove non-negativity across the loop back-edge. Go emits duplicate unsigned <code>CMPQ</code>/<code>JBE</code> slice bounds checks before <code>src[i]</code> and <code>dst[n]</code>, while LLVM removes the inner checks on Rust’s unsigned <code>usize</code> indices and inlines <code>decode_varints</code> into <code>while b.keep_running()</code>.</li></ol>\n<h3 id=\"cpu-and-heap-profiling-decoderemotewrite2\">CPU and heap profiling <code>DecodeRemoteWrite2</code></h3>\n<p>Next, let’s collect <code>-cpuprofile</code> and exact <code>-memprofile</code> captures on the <code>2,000</code>-series Remote Write 2.0 benchmark:</p>\n<pre><code># CPU profiles: 3s at -cpu 1.\ngo test -run &#39;^$&#39; -bench &#39;^BenchmarkDecodeRemoteWrite2/samples=2000/&#39; -benchtime 3s -count 1 -cpu 1 \\\n    -cpuprofile /tmp/go_rw2_cpu.pb.gz\ncargo bench --bench suite_decode -- \\\n    -bench &#39;DecodeRemoteWrite2/samples=2000&#39; -benchtime 3s -count 1 -cpu 1 \\\n    -cpuprofile /tmp/rust_rw2_cpu.pb.gz\n\n# Exact heap profiles: 50 iterations with every allocation sampled.\ngo test -run &#39;^$&#39; -bench &#39;^BenchmarkDecodeRemoteWrite2/samples=2000/&#39; -benchtime 50x -count 1 -cpu 1 \\\n    -memprofile /tmp/go_rw2_mem.pb.gz -memprofilerate 1\ncargo bench --bench suite_decode -- \\\n    -bench &#39;DecodeRemoteWrite2/samples=2000&#39; -benchtime 50x -count 1 -cpu 1 \\\n    -memprofile /tmp/rust_rw2_mem.pb.gz -memprofilerate 1</code></pre>\n<p>The two CPU flamegraphs show very different bottlenecks:</p>\n<p><a href=\"https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/pprof_go_rw2_cpu.png\" title=\"Go DecodeRemoteWrite2 CPU flamegraph\" rel=\"nofollow ugc noopener\"><img src=\"https://www.bwplotka.dev/svg/loading.min.svg\" alt=\"/2026/rust-microbenchmarks-benchstat/pprof_go_rw2_cpu.png\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></a></p>\n<p>Go <code>DecodeRemoteWrite2</code> CPU flamegraph (<code>samples=2000</code>). Explore the <a href=\"https://pprof.me/7af23a8fbe6409728965b2db5072fb87\" rel=\"nofollow ugc noopener\">interactive profile on pprof.me</a>.</p>\n<p><a href=\"https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/pprof_rust_rw2_cpu.png\" title=\"Rust DecodeRemoteWrite2 CPU flamegraph\" rel=\"nofollow ugc noopener\"><img src=\"https://www.bwplotka.dev/svg/loading.min.svg\" alt=\"/2026/rust-microbenchmarks-benchstat/pprof_rust_rw2_cpu.png\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></a></p>\n<p>Rust <code>DecodeRemoteWrite2</code> CPU flamegraph (<code>samples=2000</code>). Explore the <a href=\"https://pprof.me/9041b44c87f5861a25f8cc5a280c47a3\" rel=\"nofollow ugc noopener\">interactive profile on pprof.me</a>.</p>\n<p>Go’s x86-assembly <code>snappy.Decode</code> at <code>~0.12 ms/op</code> actually beats pure-Rust <code>snap</code> at <code>~0.18 ms/op</code>, and <code>vtprotobuf</code>’s own wire parsing takes only <code>~0.95 ms/op</code>. Go still loses overall because <code>runtime.mallocgc</code> and GC assist take <code>54.1%</code> of all on-CPU time, or <code>~1.34 ms/op</code>, allocating and scanning <code>19,297</code> heap objects.</p>\n<p>In Rust, <code>71.1%</code> of CPU time goes to <code>prost</code> field decoding, <code>8.9%</code> to <code>snap</code> decompression, and <code>18.7%</code> to <code>RawVecInner::grow_amortized</code> under <code>Vec::push</code> and <code>Vec::reserve</code>. The <code>alloc_space</code> heap profiles show why <code>RawVecInner::grow_amortized</code> is so busy and why Rust allocates <code>30.98%</code> more <code>B/op</code> than Go:</p>\n<p><a href=\"https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/pprof_go_rw2_mem.png\" title=\"Go DecodeRemoteWrite2 heap alloc_space flamegraph\" rel=\"nofollow ugc noopener\"><img src=\"https://www.bwplotka.dev/svg/loading.min.svg\" alt=\"/2026/rust-microbenchmarks-benchstat/pprof_go_rw2_mem.png\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></a></p>\n<p>Go <code>DecodeRemoteWrite2</code> heap <code>alloc_space</code> flamegraph (<code>50x</code>). Explore the <a href=\"https://pprof.me/60e14eabe540995ce6cc1cabd0d6f0e2\" rel=\"nofollow ugc noopener\">interactive profile on pprof.me</a>.</p>\n<p><a href=\"https://www.bwplotka.dev/2026/rust-microbenchmarks-benchstat/pprof_rust_rw2_mem.png\" title=\"Rust DecodeRemoteWrite2 heap alloc_space flamegraph\" rel=\"nofollow ugc noopener\"><img src=\"https://www.bwplotka.dev/svg/loading.min.svg\" alt=\"/2026/rust-microbenchmarks-benchstat/pprof_rust_rw2_mem.png\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></a></p>\n<p>Rust <code>DecodeRemoteWrite2</code> heap <code>alloc_space</code> flamegraph (<code>50x</code>). Explore the <a href=\"https://pprof.me/1fb6663440810f6deb67f24c7829a079\" rel=\"nofollow ugc noopener\">interactive profile on pprof.me</a>.</p>\n<p>Go’s <code>UnmarshalVT</code> preallocates slices and structs close to their final size, totaling <code>1.57 MiB/op</code>. Rust allocates <code>2.05 MiB/op</code>, and <code>88.4%</code> of those bytes come from <code>RawVec</code> capacity doubling inside <code>prost</code>:</p>\n<ul><li><code>prost</code> decodes packed <code>repeated uint32 labels_refs</code> one <code>u32</code> at a time with <code>Vec::push</code> instead of pre-sizing from the wire byte length. Every series grows its <code>Vec&lt;u32&gt;</code> through capacities <code>4 -&gt; 8 -&gt; 16 -&gt; 32</code>, accounting for <code>23.0%</code> of all allocated bytes.</li><li>Growing <code>Vec&lt;v2::TimeSeries&gt;</code> from <code>0</code> to <code>2,048</code> copies inline 112-byte structs at each power of two, which is <code>21.3%</code> of allocated bytes. Go only grows an 8-byte <code>[]*TimeSeries</code> pointer slice.</li><li>Pushing one 24-byte <code>Sample</code> into an empty <code>Vec&lt;Sample&gt;</code> jumps straight to capacity <code>4</code> due to <code>RawVec::MIN_NON_ZERO_CAP</code>, adding another <code>8.5%</code> of allocated bytes across <code>1,900</code> series.</li></ul>\n<p>However, <strong>even with that extra <code>Vec</code> churn</strong>, <code>glibc</code> thread-cache reallocations in Rust <strong>cost <code>~3.5x</code> less CPU than Go’s <code>mallocgc</code> and GC assist</strong>, so <code>-memprofile</code> points straight to a <code>Vec::with_capacity</code> fix.</p>\n<h2 id=\"checking-our-microbenchmarks-with-a-standalone-binary-and-usr-bi\">Checking our microbenchmarks with a standalone binary and <code>/usr/bin/time -v</code></h2>\n<p>Ok, those microbenchmarks are great, but let’s verify our efficiency numbers against the real program!</p>\n<p>Microbenchmarks run inside an artificial loop with a hot instruction cache, a warm allocator, and a custom <code>#[global_allocator]</code> wrapper in <code>suite_decode</code>. Do those <code>ns/op</code> and <code>B/op</code> numbers hold up in an unmodified release binary?</p>\n<p>To check, I wrote two standalone CLI binaries:</p>\n<ol><li><a href=\"https://github.com/bwplotka/benchmarks/blob/c55bd342d9ac23e788d06327dc7973951a5eeb82/benchmarks/rust-go-decode/src/bin/verify_decode.rs\" rel=\"nofollow ugc noopener\">One</a> using Rust’s default <code>System</code> allocator with no benchmark library.</li><li><a href=\"https://github.com/bwplotka/benchmarks/blob/c55bd342d9ac23e788d06327dc7973951a5eeb82/benchmarks/rust-go-decode/cmd/verify_decode/main.go\" rel=\"nofollow ugc noopener\">Second</a> in Go.</li></ol>\n<p>Each binary runs <code>N</code> iterations, accumulates a checksum, and optionally keeps <code>K</code> decoded messages alive via <code>--retain K</code> to simulate an ingestion queue. I ran both under GNU <code>/usr/bin/time -v</code> with <code>GOMAXPROCS=2</code> on Go to match <code>-cpu 2</code>:</p>\n<pre><code>cargo build --release --bin verify_decode\n/usr/bin/time -v ./target/release/verify_decode --case rw2_2000 --iters 1000 --retain 0\n/usr/bin/time -v ./target/release/verify_decode --case rw2_2000 --iters 1000 --retain 1000</code></pre>\n<p>Here is what <code>/usr/bin/time -v</code> reported across all runs:</p>\n<div class=\"table-wrap\"><table><thead><tr><th>Run</th><th>Iters</th><th>Microbench</th><th>Binary wall</th><th>Total CPU</th><th>CPU %</th><th>Max RSS</th></tr></thead><tbody><tr><td>Rust <code>varints</code></td><td><code>500,000</code></td><td><code>4.90 µs</code></td><td><code>5.74 µs</code></td><td><code>2.89s (5.78 µs/op)</code></td><td><code>99%</code></td><td><code>2.44 MiB</code></td></tr><tr><td>Go <code>varints</code></td><td><code>500,000</code></td><td><code>8.54 µs</code></td><td><code>8.64 µs</code></td><td><code>4.34s (8.68 µs/op)</code></td><td><code>100%</code></td><td><code>6.11 MiB</code></td></tr><tr><td>Rust <code>rw2_2000</code></td><td><code>1,000</code></td><td><code>2.03 ms</code></td><td><code>2.22 ms</code></td><td><code>2.22s (2.22 ms/op)</code></td><td><code>99%</code></td><td><code>4.21 MiB</code></td></tr><tr><td>Go <code>rw2_2000</code></td><td><code>1,000</code></td><td><code>2.47 ms</code></td><td><code>2.97 ms</code></td><td><code>4.28s (4.28 ms/op)</code></td><td><code>144%</code></td><td><code>15.98 MiB</code></td></tr><tr><td>Rust <code>rw2_2000 --retain 1000</code></td><td><code>1,000</code></td><td><code>2.03 ms</code></td><td><code>2.75 ms</code></td><td><code>3.18s (3.18 ms/op)</code></td><td><code>99%</code></td><td><code>1,292.4 MiB</code></td></tr></tbody></table></div>\n<p>The process-level numbers show a few important details:</p>\n<ul><li><strong><code>CountingAlloc</code> did not distort latency.</strong> The standalone Rust binary ran <code>rw2_2000</code> at <code>2.22 ms/op</code> across <code>1,000</code> cold-start iterations, close to <code>2.03 ms/op</code> in <code>benchfmt::Suite</code> while counting <code>16,490</code> allocations per iteration.</li><li><strong><code>B/op</code> measures allocator churn, not live memory.</strong> Over <code>1,000</code> iterations of <code>rw2_2000</code>, Rust requested <code>2.00 GiB</code> from the allocator, yet peak RSS with <code>retain=0</code> stayed at <code>4.21 MiB</code>. Dropping each <code>Request</code> at the end of the loop lets <code>glibc</code> reuse the freed thread-cache chunks on the next iteration without asking Linux for new pages. Keeping all <code>1,000</code> batches alive with <code>--retain 1000</code> raised RSS to <code>1,292.4 MiB</code> (<code>1.29 MiB</code> per retained message after temporary Snappy and <code>Vec</code>-doubling buffers were freed) and split CPU into <code>2.12s</code> user + <code>1.06s</code> kernel <code>sys</code> time for page faults.</li><li><strong>Wall-clock <code>ns/op</code> hides Go’s background GC cost.</strong> In <code>benchstat</code>, Rust was <code>17.68%</code> faster than Go on <code>rw2_2000</code>. Under <code>/usr/bin/time -v</code> at <code>GOMAXPROCS=2</code>, Rust used <code>99%</code> of one core and <code>4.21 MiB</code> RSS, while Go hit <code>144%</code> CPU, <code>4.28 ms</code> of CPU time per op, and <code>15.98 MiB</code> RSS. Leaving <code>GOMAXPROCS</code> unrestricted on all 24 cores pushed Go to <code>220%</code> CPU, or <code>7.13 ms</code> of CPU time per op, and <code>18.53 MiB</code> RSS as background GC threads swept heap objects. Across the whole process, Rust used <strong>1.9x to 3.2x less CPU</strong> and <strong>3.8x to 4.4x less RAM</strong> per request.</li></ul>\n<h2 id=\"summary\">Summary</h2>\n<p>In general I think Rust has real potential in this AI era:</p>\n<ul><li>It has extreme <a href=\"https://doc.rust-lang.org/book/ch10-02-traits.html\" rel=\"nofollow ugc noopener\">compile-time verification</a>, including strict <a href=\"https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html\" rel=\"nofollow ugc noopener\">ownership and memory safety checks</a>, which is ideal when AI generates code.</li><li>It takes ages and can be annoying to program in by hand (sorry!), but AI writes a lot of code nowadays. Code mostly has to be readable for reviews, and Rust is good enough at that.</li><li>It has a strong columnar storage ecosystem around crates like <a href=\"https://github.com/apache/arrow-rs\" rel=\"nofollow ugc noopener\"><code>arrow-rs</code></a>.</li><li>It has no garbage collector, gives more control over memory allocations, and is <a href=\"#comparing-go-vs-rust\">clearly faster</a>!</li></ul>\n<p>In terms of benchmarking, having both <code>benchfmt</code> tools in Rust lets you match the workflow to the job:</p>\n<ol><li>Run <strong><code>benchfmt criterion target/criterion</code></strong> on existing Criterion suites when you want <code>benchstat</code> A/B tables across commits or languages without editing benchmark code.</li><li>Use <strong><code>benchfmt::Suite</code> and <code>CountingAlloc</code></strong> with <code>features = [&quot;pprof&quot;]</code> for new Rust microbenchmarks to get fast <code>std</code>-only compiles, steady-state epochs, <code>B/op</code> and <code>allocs/op</code> tracking, and <code>pprof</code> profiles.</li></ol>\n<p>Give <a href=\"https://github.com/bwplotka/benchfmt.rs\" rel=\"nofollow ugc noopener\"><code>benchfmt.rs</code></a> a spin and let me know on GitHub what breaks or what is missing!</p>","headings":[{"level":1,"text":"benchfmt.rs: Bringing Go's Benchmarking and benchstat Workflow to Rust","id":"benchfmt-rs-bringing-go-s-benchmarking-and-benchstat-workflow-to"},{"level":2,"text":"Benchmarking two workloads","id":"benchmarking-two-workloads"},{"level":3,"text":"Workload details:","id":"workload-details"},{"level":2,"text":"Implementing and benchmarking in Go","id":"implementing-and-benchmarking-in-go"},{"level":2,"text":"Implementing and benchmarking in Rust with Criterion","id":"implementing-and-benchmarking-in-rust-with-criterion"},{"level":3,"text":"Criterion downsides","id":"criterion-downsides"},{"level":2,"text":"Converting Criterion output with the benchfmt CLI","id":"converting-criterion-output-with-the-benchfmt-cli"},{"level":2,"text":"Native benchfmt runs with benchfmt::Suite and CountingAlloc","id":"native-benchfmt-runs-with-benchfmt-suite-and-countingalloc"},{"level":3,"text":"Comparing Go vs Rust","id":"comparing-go-vs-rust"},{"level":2,"text":"Bonus: go tool pprof profiling to tell WHERE certain overhead exists benchmarks in Go and Rust","id":"bonus-go-tool-pprof-profiling-to-tell-where-certain-overhead-exi"},{"level":3,"text":"CPU profiling DecodeVarints","id":"cpu-profiling-decodevarints"},{"level":3,"text":"CPU and heap profiling DecodeRemoteWrite2","id":"cpu-and-heap-profiling-decoderemotewrite2"},{"level":2,"text":"Checking our microbenchmarks with a standalone binary and /usr/bin/time -v","id":"checking-our-microbenchmarks-with-a-standalone-binary-and-usr-bi"},{"level":2,"text":"Summary","id":"summary"}]}}