go-ruby-benchmark

Ruby's Benchmark module in pure Go โ€” MRI-compatible, no cgo.

pure Go ยท zero cgo Benchmark ยท Tms memberwise arithmetic %u %y %t %r format bm / bmbm / benchmark CAPTION + tables injected clock MRI byte-exact 100% coverage 6 arches
Documentation GitHub
Documentation (MkDocs Material + mike) License: BSD-3-Clause Go 1.26.4+ Coverage 100%

go-ruby-benchmark is a pure-Go (no cgo) reimplementation of Ruby's standard-library Benchmark module โ€” the deterministic, interpreter-independent core of MRI 4.0.5: the Tms measurement value type, its memberwise arithmetic and %-directive formatting, and the report layout (CAPTION, label justification, the bm / bmbm / benchmark tables). It reproduces MRI's formatted output byte-for-byte. The one impure ingredient โ€” the clock โ€” is injected: MRI's Benchmark.measure reads Process.times and clock_gettime(CLOCK_MONOTONIC), and this library takes those readings through a small Clock interface, so every formatted line is reproducible in tests. It was extracted from rbgo's internals into a reusable standalone library: no dependency on the Ruby runtime, the dependency runs the other way. It is the Benchmark backend for go-embedded-ruby, bound by rbgo as a native module just like go-ruby-regexp and go-ruby-erb โ€” differential-tested against MRI, 100% coverage, CI green across 6 arches and 3 OSes.

Tms value type ready

Benchmark::Tms: utime, stime, cutime, cstime, real, the derived total = utime+stime+cutime+cstime, and a label, with ToA / ToS and MRI-exact constructor semantics (a sum’s label clears to "").

Memberwise & scalar arithmetic ready

Add / Sub / Mul / Div against another Tms, and the scalar forms applied to all five fields (real included) โ€” exactly as MRI’s Tms#+ - * / do.

Format directives ready

The Benchmark %-extensions on top of printf: %u user, %y system, %U / %Y children’s, %t total, %r real (parenthesised), %n label โ€” each preserving its flag/width/precision run, with %% collapse and surplus-arg drop matching String#%.

Report layout ready

CAPTION, label left-justification, and the full bm(width), bmbm (rehearsal banner + total footer + take table), and general benchmark(caption, width, format, *labels) tables with trailing summary lines โ€” all pure functions over Tms.

Injected clock ready

MeasureWith, RealtimeWith, MsWith, and Tms.AddWith take a Clock seam (Process.times + clock_gettime(CLOCK_MONOTONIC)): the host supplies the real process clock, tests a deterministic one, so every formatted line is reproducible.

Differential oracle & coverage ready

The same fixed numbers and a patched, deterministic clock fed to the system ruby’s Benchmark and compared byte-for-byte; 100% coverage, gofmt + go vet clean, green across all six 64-bit Go arches and three OSes.

A faithful port of Ruby's Benchmark module in pure Go, cgo disabled, so it cross-compiles and embeds anywhere. It implements the Tms value type, its memberwise and scalar arithmetic, the %u %y %t %r format directives, and the bm / bmbm / benchmark report tables โ€” all pure functions over Tms, with the one impure ingredient (the clock) injected through a Clock seam. Validated differentially against the system ruby binary โ€” the same fixed numbers and a deterministic clock compared byte-for-byte. It is a standalone, reusable module extracted from rbgo's internals, and the Benchmark backend for the sibling org github.com/go-embedded-ruby.