From 138cbb3bc16e4404883b59a69a6d33acf5682cd2 Mon Sep 17 00:00:00 2001 From: Thomas Jungblut Date: Wed, 31 Jul 2024 13:28:14 +0200 Subject: [PATCH] ensure build and bench output in 3.5 manual backport of #765 and partially #664, to make the nightly benchmark work Signed-off-by: Thomas Jungblut --- Makefile | 9 +++++++++ cmd/bbolt/main.go | 26 +++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 18154c638..498e3412c 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,15 @@ coverage: TEST_FREELIST_TYPE=array go test -v -timeout 30m \ -coverprofile cover-freelist-array.out -covermode atomic +BOLT_CMD=bbolt + +build: + go build -o bin/${BOLT_CMD} ./cmd/${BOLT_CMD} + +.PHONY: clean +clean: # Clean binaries + rm -f ./bin/${BOLT_CMD} + .PHONY: gofail-enable gofail-enable: install-gofail gofail enable . diff --git a/cmd/bbolt/main.go b/cmd/bbolt/main.go index 5bc0161e2..ed0d89e28 100644 --- a/cmd/bbolt/main.go +++ b/cmd/bbolt/main.go @@ -15,6 +15,7 @@ import ( "runtime/pprof" "strconv" "strings" + "testing" "time" "unicode" "unicode/utf8" @@ -1114,9 +1115,26 @@ func (cmd *benchCommand) Run(args ...string) error { } // Print results. - fmt.Fprintf(os.Stderr, "# Write\t%v\t(%v/op)\t(%v op/sec)\n", results.WriteDuration, results.WriteOpDuration(), results.WriteOpsPerSecond()) - fmt.Fprintf(os.Stderr, "# Read\t%v\t(%v/op)\t(%v op/sec)\n", results.ReadDuration, results.ReadOpDuration(), results.ReadOpsPerSecond()) - fmt.Fprintln(os.Stderr, "") + if options.GoBenchOutput { + // below replicates the output of testing.B benchmarks, e.g. for external tooling + benchWriteName := "BenchmarkWrite" + benchReadName := "BenchmarkRead" + maxLen := max(len(benchReadName), len(benchWriteName)) + gobenchResult := testing.BenchmarkResult{} + gobenchResult.T = results.WriteDuration + gobenchResult.N = results.WriteOps + fmt.Fprintf(os.Stderr, "%-*s\t%s\n", maxLen, benchWriteName, gobenchResult.String()) + + gobenchResult = testing.BenchmarkResult{} + gobenchResult.T = results.ReadDuration + gobenchResult.N = results.ReadOps + fmt.Fprintf(os.Stderr, "%-*s\t%s\n", maxLen, benchReadName, gobenchResult.String()) + } else { + fmt.Fprintf(os.Stderr, "# Write\t%v\t(%v/op)\t(%v op/sec)\n", results.WriteDuration, results.WriteOpDuration(), results.WriteOpsPerSecond()) + fmt.Fprintf(os.Stderr, "# Read\t%v\t(%v/op)\t(%v op/sec)\n", results.ReadDuration, results.ReadOpDuration(), results.ReadOpsPerSecond()) + } + fmt.Fprintln(cmd.Stderr, "") + return nil } @@ -1140,6 +1158,7 @@ func (cmd *benchCommand) ParseFlags(args []string) (*BenchOptions, error) { fs.BoolVar(&options.NoSync, "no-sync", false, "") fs.BoolVar(&options.Work, "work", false, "") fs.StringVar(&options.Path, "path", "", "") + fs.BoolVar(&options.GoBenchOutput, "gobench-output", false, "") fs.SetOutput(cmd.Stderr) if err := fs.Parse(args); err != nil { return nil, err @@ -1482,6 +1501,7 @@ type BenchOptions struct { NoSync bool Work bool Path string + GoBenchOutput bool } // BenchResults represents the performance results of the benchmark.