Running only one sub-benchmark

81 views
Skip to first unread message

Jochen Voss

unread,
Aug 6, 2024, 7:49:45 AM8/6/24
to golang-nuts
Hello,

In a Go benchmark I use b.Run() to define a number of sub-benchmarks.  This seems to work:

> go test -run=^$ -bench '^BenchmarkTextLayout'
goos: darwin
goarch: arm64
pkg: seehuhn.de/go/pdf/graphics
BenchmarkTextLayout/CFFSimple1-8               49   24061184 ns/op
BenchmarkTextLayout/CFFSimple2-8               48   24036174 ns/op
BenchmarkTextLayout/OpenTypeCFFSimple1-8       49   24045372 ns/op
BenchmarkTextLayout/OpenTypeCFFSimple2-8       48   24147282 ns/op
BenchmarkTextLayout/TrueTypeSimple-8           51   23030744 ns/op
BenchmarkTextLayout/OpenTypeGlyfSimple-8       51   22865664 ns/op
BenchmarkTextLayout/BuiltIn-8                   43   27654625 ns/op
...

Now I just want to run one of these sub-benchmarks.  I tried -bench '^BenchmarkTextLayout/CFFSimple1$' but this runs two of the benchmarks:

> go test -run=^$ -bench '^BenchmarkTextLayout/CFFSimple1$'
goos: darwin
goarch: arm64
pkg: seehuhn.de/go/pdf/graphics
BenchmarkTextLayout/CFFSimple1-8               49   24198452 ns/op
BenchmarkTextLayout/OpenTypeCFFSimple1-8       48   24209569 ns/op
PASS
ok   seehuhn.de/go/pdf/graphics 3.804s

How do I just run "BenchmarkTextLayout/CFFSimple1"?


Many thanks,
Jochen

wagner riffel

unread,
Aug 7, 2024, 2:10:55 AM8/7/24
to Jochen Voss, golang-nuts
Jochen Voss <joche...@gmail.com> wrote:
> Now I just want to run one of these sub-benchmarks. I tried -bench
> '^BenchmarkTextLayout/CFFSimple1$' but this runs two of the benchmarks:
>
> > go test -run=^$ -bench '^BenchmarkTextLayout/CFFSimple1$'
> goos: darwin
> goarch: arm64
> pkg: seehuhn.de/go/pdf/graphics
> BenchmarkTextLayout/CFFSimple1-8 49 24198452 ns/op
> BenchmarkTextLayout/OpenTypeCFFSimple1-8 48 24209569 ns/op
> PASS
> ok seehuhn.de/go/pdf/graphics 3.804s
>
> How do I just run "BenchmarkTextLayout/CFFSimple1"?
>

The regexp matches against the benchmark name, not the "full name". Slashes are
interpreted as a separator for names to be matched against, thus your regexp
first matches "^BenchmarkTextLayout" against all top-level benchmarks, then
matches "CFFSimple1$" against all sub-benchmarks names found in
"BenchmarkTextLayout" which indeed "CFFSimple1" and "OpenTypeCFFSimple1" are
matches of.

So the following regexp should only match "BenchmarkTextLayout/CFFSimple1".

$ go test -bench='^BenchmarkTextLayout/^CFFSimple1$'

Note the '^' before CFF.

BR.

-w

Jochen Voss

unread,
Aug 7, 2024, 3:04:47 AM8/7/24
to golang-nuts
Thank you!
Reply all
Reply to author
Forward
0 new messages