Skip to content

Commit

Permalink
add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchiqing committed Sep 17, 2024
1 parent f297689 commit ebb01d8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
44 changes: 44 additions & 0 deletions storage/operation/approvals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package operation_test

import (
"testing"

"github.com/stretchr/testify/require"

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

func BenchmarkRetrieveApprovals(b *testing.B) {
dbtest.BenchWithDB(b, func(b *testing.B, db storage.DB) {
b.Run("RetrieveApprovals", func(b *testing.B) {
approval := unittest.ResultApprovalFixture()
require.NoError(b, db.WithReaderBatchWriter(storage.OnlyWriter(operation.InsertResultApproval(approval))))

b.ResetTimer()

approvalID := approval.ID()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var stored flow.ResultApproval
require.NoError(b, operation.RetrieveResultApproval(approvalID, &stored)(db.Reader()))
}
})

})
})
}

func BenchmarkInsertApproval(b *testing.B) {
dbtest.BenchWithDB(b, func(b *testing.B, db storage.DB) {
b.Run("InsertApprovals", func(b *testing.B) {
for i := 0; i < b.N; i++ {
approval := unittest.ResultApprovalFixture()
require.NoError(b, db.WithReaderBatchWriter(storage.OnlyWriter(operation.InsertResultApproval(approval))))
}
})
})
}
14 changes: 14 additions & 0 deletions storage/operation/dbtest/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,17 @@ func RunWithDB(t *testing.T, fn func(t *testing.T, store storage.DB)) {
})
})
}

func BenchWithDB(b *testing.B, fn func(*testing.B, storage.DB)) {
b.Run("BadgerStorage", func(b *testing.B) {
unittest.RunWithBadgerDB(b, func(db *badger.DB) {
fn(b, badgerimpl.ToDB(db))
})
})

b.Run("PebbleStorage", func(b *testing.B) {
unittest.RunWithPebbleDB(b, func(db *pebble.DB) {
fn(b, pebbleimpl.ToDB(db))
})
})
}

0 comments on commit ebb01d8

Please sign in to comment.