From 64fcacedfa3e59e9dbb0cc0d2b3de71cae3290df Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Tue, 4 Mar 2014 13:02:17 -0700 Subject: Add benchmarks. --- rwtransaction_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'rwtransaction_test.go') diff --git a/rwtransaction_test.go b/rwtransaction_test.go index 1635b45..e45ec10 100644 --- a/rwtransaction_test.go +++ b/rwtransaction_test.go @@ -1,6 +1,8 @@ package bolt import ( + "math/rand" + "strconv" "strings" "testing" @@ -140,3 +142,40 @@ func TestRWTransactionDeleteBucketNotFound(t *testing.T) { assert.Equal(t, err, ErrBucketNotFound) }) } + +// Benchmark the performance of bulk put transactions in random order. +func BenchmarkRWTransactionPutRandom(b *testing.B) { + indexes := rand.Perm(b.N) + value := []byte(strings.Repeat("0", 64)) + withOpenDB(func(db *DB, path string) { + db.CreateBucket("widgets") + var txn *RWTransaction + var bucket *Bucket + for i := 0; i < b.N; i++ { + if i%1000 == 0 { + if txn != nil { + txn.Commit() + } + txn, _ = db.RWTransaction() + bucket = txn.Bucket("widgets") + } + bucket.Put([]byte(strconv.Itoa(indexes[i])), value) + } + txn.Commit() + }) +} + +// Benchmark the performance of bulk put transactions in sequential order. +func BenchmarkRWTransactionPutSequential(b *testing.B) { + value := []byte(strings.Repeat("0", 64)) + withOpenDB(func(db *DB, path string) { + 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(i)), value) + } + return nil + }) + }) +} -- cgit v1.2.3