diff options
Diffstat (limited to 'transaction_test.go')
-rw-r--r-- | transaction_test.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/transaction_test.go b/transaction_test.go index 8cf9dfa..04cb44b 100644 --- a/transaction_test.go +++ b/transaction_test.go @@ -2,8 +2,11 @@ package bolt import ( "fmt" + "math/rand" "os" "sort" + "strconv" + "strings" "testing" "testing/quick" @@ -228,3 +231,35 @@ func TestTransactionCursorIterateReverse(t *testing.T) { } fmt.Fprint(os.Stderr, "\n") } + +// Benchmark the performance iterating over a cursor. +func BenchmarkTransactionCursor(b *testing.B) { + indexes := rand.Perm(b.N) + value := []byte(strings.Repeat("0", 64)) + + withOpenDB(func(db *DB, path string) { + // Write data to bucket. + db.CreateBucket("widgets") + db.Do(func(txn *RWTransaction) error { + bucket := txn.Bucket("widgets") + for i := 0; i < b.N; i++ { + bucket.Put([]byte(strconv.Itoa(indexes[i])), value) + } + return nil + }) + b.ResetTimer() + + // Iterate over bucket using cursor. + db.With(func(txn *Transaction) error { + count := 0 + c := txn.Bucket("widgets").Cursor() + for k, _ := c.First(); k != nil; k, _ = c.Next() { + count++ + } + if count != b.N { + b.Fatalf("wrong count: %d; expected: %d", count, b.N) + } + return nil + }) + }) +} |