Skip to content

Commit

Permalink
Update cursor benchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
benbjohnson committed Apr 4, 2014
1 parent 3f7dbff commit feb84e3
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,33 +514,37 @@ func TestTx_OnCommit_Rollback(t *testing.T) {

// Benchmark the performance iterating over a cursor.
func BenchmarkTxCursor(b *testing.B) {
indexes := rand.Perm(b.N)
value := []byte(strings.Repeat("0", 64))
var total = 50000
indexes := rand.Perm(total)
value := []byte(strings.Repeat("0", 100))

warn("X", b.N)
withOpenDB(func(db *DB, path string) {
// Write data to bucket.
db.Update(func(tx *Tx) error {
tx.CreateBucket("widgets")
bucket := tx.Bucket("widgets")
for i := 0; i < b.N; i++ {
bucket.Put([]byte(strconv.Itoa(indexes[i])), value)
for i := 0; i < total; i++ {
bucket.Put([]byte(fmt.Sprintf("%016d", indexes[i])), value)
}
return nil
})
b.ResetTimer()

// Iterate over bucket using cursor.
db.View(func(tx *Tx) error {
count := 0
c := tx.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
})
for i := 0; i < b.N; i++ {
db.View(func(tx *Tx) error {
count := 0
c := tx.Bucket("widgets").Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
count++
}
if count != total {
b.Fatalf("wrong count: %d; expected: %d", count, total)
}
return nil
})
}
})
}

Expand Down

0 comments on commit feb84e3

Please sign in to comment.