Update cursor benchmark.

pull/34/head
Ben Johnson 2014-04-04 13:08:40 -06:00
parent 3f7dbffa2e
commit feb84e39be
1 changed files with 19 additions and 15 deletions

View File

@ -514,33 +514,37 @@ func TestTx_OnCommit_Rollback(t *testing.T) {
// Benchmark the performance iterating over a cursor. // Benchmark the performance iterating over a cursor.
func BenchmarkTxCursor(b *testing.B) { func BenchmarkTxCursor(b *testing.B) {
indexes := rand.Perm(b.N) var total = 50000
value := []byte(strings.Repeat("0", 64)) indexes := rand.Perm(total)
value := []byte(strings.Repeat("0", 100))
warn("X", b.N)
withOpenDB(func(db *DB, path string) { withOpenDB(func(db *DB, path string) {
// Write data to bucket. // Write data to bucket.
db.Update(func(tx *Tx) error { db.Update(func(tx *Tx) error {
tx.CreateBucket("widgets") tx.CreateBucket("widgets")
bucket := tx.Bucket("widgets") bucket := tx.Bucket("widgets")
for i := 0; i < b.N; i++ { for i := 0; i < total; i++ {
bucket.Put([]byte(strconv.Itoa(indexes[i])), value) bucket.Put([]byte(fmt.Sprintf("%016d", indexes[i])), value)
} }
return nil return nil
}) })
b.ResetTimer() b.ResetTimer()
// Iterate over bucket using cursor. // Iterate over bucket using cursor.
for i := 0; i < b.N; i++ {
db.View(func(tx *Tx) error { db.View(func(tx *Tx) error {
count := 0 count := 0
c := tx.Bucket("widgets").Cursor() c := tx.Bucket("widgets").Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() { for k, _ := c.First(); k != nil; k, _ = c.Next() {
count++ count++
} }
if count != b.N { if count != total {
b.Fatalf("wrong count: %d; expected: %d", count, b.N) b.Fatalf("wrong count: %d; expected: %d", count, total)
} }
return nil return nil
}) })
}
}) })
} }