Skip to content

Commit

Permalink
add gobench output option
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jungblut <[email protected]>
  • Loading branch information
tjungblu committed Jun 6, 2024
1 parent b342624 commit 1beca7c
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions cmd/bbolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
"unicode"
"unicode/utf8"
Expand Down Expand Up @@ -1050,9 +1051,23 @@ func (cmd *benchCommand) Run(args ...string) error {
}

// Print results.
fmt.Fprintf(cmd.Stderr, "# Write\t%v(ops)\t%v\t(%v/op)\t(%v op/sec)\n", writeResults.CompletedOps(), writeResults.Duration(), writeResults.OpDuration(), writeResults.OpsPerSecond())
fmt.Fprintf(cmd.Stderr, "# Read\t%v(ops)\t%v\t(%v/op)\t(%v op/sec)\n", readResults.CompletedOps(), readResults.Duration(), readResults.OpDuration(), readResults.OpsPerSecond())
if options.GoBenchOutput {
// below replicates the output of testing.B benchmarks, e.g. for external tooling
maxLen := 5 // the length of "Write"
gobenchResult := testing.BenchmarkResult{}
gobenchResult.T = writeResults.Duration()
gobenchResult.N = int(writeResults.CompletedOps())
fmt.Fprintf(cmd.Stderr, "%-*s\t%s\n", maxLen, "Write", gobenchResult.String())
gobenchResult = testing.BenchmarkResult{}
gobenchResult.T = readResults.Duration()
gobenchResult.N = int(readResults.CompletedOps())
fmt.Fprintf(cmd.Stderr, "%-*s\t%s\n", maxLen, "Read", gobenchResult.String())
} else {
fmt.Fprintf(cmd.Stderr, "# Write\t%v(ops)\t%v\t(%v/op)\t(%v op/sec)\n", writeResults.CompletedOps(), writeResults.Duration(), writeResults.OpDuration(), writeResults.OpsPerSecond())
fmt.Fprintf(cmd.Stderr, "# Read\t%v(ops)\t%v\t(%v/op)\t(%v op/sec)\n", readResults.CompletedOps(), readResults.Duration(), readResults.OpDuration(), readResults.OpsPerSecond())
}
fmt.Fprintln(cmd.Stderr, "")

return nil
}

Expand All @@ -1076,6 +1091,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
Expand Down Expand Up @@ -1555,6 +1571,7 @@ type BenchOptions struct {
NoSync bool
Work bool
Path string
GoBenchOutput bool
}

// BenchResults represents the performance results of the benchmark and is thread-safe.
Expand Down

0 comments on commit 1beca7c

Please sign in to comment.