Skip to content

Commit

Permalink
Add bench test for reader collect methods (#2922)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed May 24, 2022
1 parent 39fe636 commit 46bf817
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sdk/metric/manual_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ import (
func TestManualReader(t *testing.T) {
suite.Run(t, &readerTestSuite{Factory: NewManualReader})
}

func BenchmarkManualReader(b *testing.B) {
b.Run("Collect", benchReaderCollectFunc(NewManualReader()))
}
6 changes: 6 additions & 0 deletions sdk/metric/periodic_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,9 @@ func TestPeriodicReaderRun(t *testing.T) {
// Ensure Reader is allowed clean up attempt.
_ = r.Shutdown(context.Background())
}

func BenchmarkPeriodicReader(b *testing.B) {
b.Run("Collect", benchReaderCollectFunc(
NewPeriodicReader(new(fnExporter)),
))
}
25 changes: 25 additions & 0 deletions sdk/metric/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package metric // import "go.opentelemetry.io/otel/sdk/metric/reader"
import (
"context"
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -153,3 +154,27 @@ func (p testProducer) produce(ctx context.Context) (export.Metrics, error) {
}
return testMetrics, nil
}

func benchReaderCollectFunc(r Reader) func(*testing.B) {
ctx := context.Background()
r.register(testProducer{})

// Store bechmark results in a closure to prevent the compiler from
// inlining and skipping the function.
var (
collectedMetrics export.Metrics
err error
)

return func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

for n := 0; n < b.N; n++ {
collectedMetrics, err = r.Collect(ctx)
if collectedMetrics != testMetrics || err != nil {
b.Errorf("unexpected Collect response: (%#v, %v)", collectedMetrics, err)
}
}
}
}

0 comments on commit 46bf817

Please sign in to comment.