Skip to content

Commit

Permalink
add iterator benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchiqing committed Oct 8, 2024
1 parent 7334a0b commit b82106a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
63 changes: 63 additions & 0 deletions storage/operation/reads_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package operation_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/storage/operation"
"github.com/onflow/flow-go/storage/operation/dbtest"
)

func BenchmarkRetrieve(t *testing.B) {
dbtest.BenchWithStorages(t, func(t *testing.B, r storage.Reader, withWriter dbtest.WithWriter) {
e := Entity{ID: 1337}
require.NoError(t, withWriter(operation.Upsert(e.Key(), e)))

t.ResetTimer()

for i := 0; i < t.N; i++ {
var readBack Entity
require.NoError(t, operation.Retrieve(e.Key(), &readBack)(r))
}
})
}

func BenchmarkNonExist(t *testing.B) {
dbtest.BenchWithStorages(t, func(t *testing.B, r storage.Reader, withWriter dbtest.WithWriter) {
for i := 0; i < t.N; i++ {
e := Entity{ID: uint64(i)}
require.NoError(t, withWriter(operation.Upsert(e.Key(), e)))
}

t.ResetTimer()
nonExist := Entity{ID: uint64(t.N + 1)}
for i := 0; i < t.N; i++ {
var exists bool
require.NoError(t, operation.Exists(nonExist.Key(), &exists)(r))
}
})
}

func BenchmarkIterate(t *testing.B) {
dbtest.BenchWithStorages(t, func(t *testing.B, r storage.Reader, withWriter dbtest.WithWriter) {
prefix1 := []byte("prefix-1")
prefix2 := []byte("prefix-2")
for i := 0; i < t.N; i++ {
e := Entity{ID: uint64(i)}
key1 := append(prefix1, e.Key()...)
key2 := append(prefix2, e.Key()...)

require.NoError(t, withWriter(operation.Upsert(key1, e)))
require.NoError(t, withWriter(operation.Upsert(key2, e)))
}

t.ResetTimer()
var found [][]byte
require.NoError(t, operation.IterateKeysInPrefixRange(prefix1, prefix2, func(key []byte) error {
found = append(found, key)
return nil
})(r), "should iterate forward without error")
})
}
14 changes: 0 additions & 14 deletions storage/operation/writes_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ import (
"github.com/onflow/flow-go/storage/operation/dbtest"
)

func BenchmarkRetrieve(t *testing.B) {
dbtest.BenchWithStorages(t, func(t *testing.B, r storage.Reader, withWriter dbtest.WithWriter) {
e := Entity{ID: 1337}
require.NoError(t, withWriter(operation.Upsert(e.Key(), e)))

t.ResetTimer()

for i := 0; i < t.N; i++ {
var readBack Entity
require.NoError(t, operation.Retrieve(e.Key(), &readBack)(r))
}
})
}

func BenchmarkUpsert(t *testing.B) {
dbtest.BenchWithStorages(t, func(t *testing.B, r storage.Reader, withWriter dbtest.WithWriter) {
for i := 0; i < t.N; i++ {
Expand Down

0 comments on commit b82106a

Please sign in to comment.