Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate bench to a cobra style command #680

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
541 changes: 541 additions & 0 deletions cmd/bbolt/command_bench.go

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions cmd/bbolt/command_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main_test

import (
"bytes"
"fmt"
"io"
"strings"
"testing"

main "go.etcd.io/bbolt/cmd/bbolt"
)

// Ensure the "bench" command runs and exits without errors
func TestBenchCommand_Run(t *testing.T) {
tests := map[string]struct {
args []string
}{
"no-args": {},
"100k count": {[]string{"--count", "100000"}},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
// Run the command.
buf := new(bytes.Buffer)
rootCmd := main.NewRootCommand()
rootCmd.SetArgs(append([]string{"bench"}, test.args...))
rootCmd.SetOut(buf)
if err := rootCmd.Execute(); err != nil {
t.Fatal(err)
}

output, err := io.ReadAll(buf)
if err != nil {
t.Fatal(err)
}
stderr := string(output)

if !strings.Contains(stderr, "starting write benchmark.") || !strings.Contains(stderr, "starting read benchmark.") {
t.Fatal(fmt.Errorf("benchmark result does not contain read/write start output:\n%s", stderr))
}

if strings.Contains(stderr, "iter mismatch") {
t.Fatal(fmt.Errorf("found iter mismatch in stdout:\n%s", stderr))
}

if !strings.Contains(stderr, "# Write") || !strings.Contains(stderr, "# Read") {
t.Fatal(fmt.Errorf("benchmark result does not contain read/write output:\n%s", stderr))
}
})
}
}
1 change: 1 addition & 0 deletions cmd/bbolt/command_root.go
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ func NewRootCommand() *cobra.Command {
newVersionCobraCommand(),
newSurgeryCobraCommand(),
newInspectCobraCommand(),
newBenchCobraCommand(),
)

return rootCmd
495 changes: 0 additions & 495 deletions cmd/bbolt/main.go

Large diffs are not rendered by default.

34 changes: 0 additions & 34 deletions cmd/bbolt/main_test.go
Original file line number Diff line number Diff line change
@@ -479,40 +479,6 @@ func TestPagesCommand_Run(t *testing.T) {
require.NoError(t, err)
}

// Ensure the "bench" command runs and exits without errors
func TestBenchCommand_Run(t *testing.T) {
tests := map[string]struct {
args []string
}{
"no-args": {},
"100k count": {[]string{"-count", "100000"}},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
// Run the command.
m := NewMain()
args := append([]string{"bench"}, test.args...)
if err := m.Run(args...); err != nil {
t.Fatal(err)
}

stderr := m.Stderr.String()
if !strings.Contains(stderr, "starting write benchmark.") || !strings.Contains(stderr, "starting read benchmark.") {
t.Fatal(fmt.Errorf("benchmark result does not contain read/write start output:\n%s", stderr))
}

if strings.Contains(stderr, "iter mismatch") {
t.Fatal(fmt.Errorf("found iter mismatch in stdout:\n%s", stderr))
}

if !strings.Contains(stderr, "# Write") || !strings.Contains(stderr, "# Read") {
t.Fatal(fmt.Errorf("benchmark result does not contain read/write output:\n%s", stderr))
}
})
}
}

type ConcurrentBuffer struct {
m sync.Mutex
buf bytes.Buffer
10 changes: 5 additions & 5 deletions tests/robustness/powerfailure_test.go
Original file line number Diff line number Diff line change
@@ -90,11 +90,11 @@ func doPowerFailure(t *testing.T, du time.Duration, fsMountOpt string, useFailpo
dbPath := filepath.Join(root, "boltdb")

args := []string{"bbolt", "bench",
"-work", // keep the database
"-path", dbPath,
"-count=1000000000",
"-batch-size=5", // separate total count into multiple truncation
"-value-size=512",
"--work", // keep the database
"--path", dbPath,
"--count=1000000000",
"--batch-size=5", // separate total count into multiple truncation
"--value-size=512",
}

logPath := filepath.Join(t.TempDir(), fmt.Sprintf("%s.log", t.Name()))