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

Fix progress reporting in bench cmd #434

Merged
merged 1 commit into from
Mar 24, 2023
Merged
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
43 changes: 16 additions & 27 deletions cmd/bbolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"runtime/pprof"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"unicode"
"unicode/utf8"
Expand Down Expand Up @@ -1223,6 +1223,7 @@ func (cmd *benchCommand) runWritesWithSource(db *bolt.DB, options *BenchOptions,
b, _ := tx.CreateBucketIfNotExists(benchBucketName)
b.FillPercent = options.FillPercent

fmt.Fprintf(cmd.Stderr, "Starting write iteration %d\n", i)
for j := int64(0); j < options.BatchSize; j++ {
key := make([]byte, options.KeySize)
value := make([]byte, options.ValueSize)
Expand All @@ -1234,13 +1235,14 @@ func (cmd *benchCommand) runWritesWithSource(db *bolt.DB, options *BenchOptions,
if err := b.Put(key, value); err != nil {
return err
}

results.AddCompletedOps(1)
cenkalti marked this conversation as resolved.
Show resolved Hide resolved
}
fmt.Fprintf(cmd.Stderr, "Finished write iteration %d\n", i)

return nil
}); err != nil {
return err
} else {
results.AddCompletedOps(options.BatchSize)
}
}
return nil
Expand All @@ -1266,6 +1268,7 @@ func (cmd *benchCommand) runWritesNestedWithSource(db *bolt.DB, options *BenchOp
}
b.FillPercent = options.FillPercent

fmt.Fprintf(cmd.Stderr, "Starting write iteration %d\n", i)
for j := int64(0); j < options.BatchSize; j++ {
var key = make([]byte, options.KeySize)
var value = make([]byte, options.ValueSize)
Expand All @@ -1277,13 +1280,14 @@ func (cmd *benchCommand) runWritesNestedWithSource(db *bolt.DB, options *BenchOp
if err := b.Put(key, value); err != nil {
return err
}

results.AddCompletedOps(1)
}
fmt.Fprintf(cmd.Stderr, "Finished write iteration %d\n", i)

return nil
}); err != nil {
return err
} else {
results.AddCompletedOps(options.BatchSize)
}
}
return nil
Expand Down Expand Up @@ -1335,6 +1339,7 @@ func (cmd *benchCommand) runReadsSequential(db *bolt.DB, options *BenchOptions,
c := tx.Bucket(benchBucketName).Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
numReads++
results.AddCompletedOps(1)
if v == nil {
return errors.New("invalid value")
}
Expand All @@ -1344,8 +1349,6 @@ func (cmd *benchCommand) runReadsSequential(db *bolt.DB, options *BenchOptions,
return fmt.Errorf("read seq: iter mismatch: expected %d, got %d", options.Iterations, numReads)
}

results.AddCompletedOps(numReads)

// Make sure we do this for at least a second.
if time.Since(t) >= time.Second {
break
Expand All @@ -1368,6 +1371,7 @@ func (cmd *benchCommand) runReadsSequentialNested(db *bolt.DB, options *BenchOpt
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
numReads++
results.AddCompletedOps(1)
if v == nil {
return ErrInvalidValue
}
Expand All @@ -1382,8 +1386,6 @@ func (cmd *benchCommand) runReadsSequentialNested(db *bolt.DB, options *BenchOpt
return fmt.Errorf("read seq-nest: iter mismatch: expected %d, got %d", options.Iterations, numReads)
}

results.AddCompletedOps(numReads)

// Make sure we do this for at least a second.
if time.Since(t) >= time.Second {
break
Expand Down Expand Up @@ -1502,37 +1504,24 @@ type BenchOptions struct {

// BenchResults represents the performance results of the benchmark and is thread-safe.
type BenchResults struct {
m sync.Mutex
completedOps int64
duration time.Duration
duration int64
}

func (r *BenchResults) AddCompletedOps(amount int64) {
r.m.Lock()
defer r.m.Unlock()

r.completedOps += amount
atomic.AddInt64(&r.completedOps, amount)
}

func (r *BenchResults) CompletedOps() int64 {
r.m.Lock()
defer r.m.Unlock()

return r.completedOps
return atomic.LoadInt64(&r.completedOps)
}

func (r *BenchResults) SetDuration(dur time.Duration) {
r.m.Lock()
defer r.m.Unlock()

r.duration = dur
atomic.StoreInt64(&r.duration, int64(dur))
}

func (r *BenchResults) Duration() time.Duration {
r.m.Lock()
defer r.m.Unlock()

return r.duration
return time.Duration(atomic.LoadInt64(&r.duration))
cenkalti marked this conversation as resolved.
Show resolved Hide resolved
}

// Returns the duration for a single read/write operation.
Expand Down