Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Add a benchmark for stats.WithMeter (but with no views registered)
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Anderson <evan.k.anderson@gmail.com>
  • Loading branch information
evankanderson committed Feb 13, 2020
1 parent e7bc6a0 commit 1f969d3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 16 deletions.
17 changes: 17 additions & 0 deletions stats/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
_ "go.opencensus.io/stats/view" // enable collection
"go.opencensus.io/tag"
)
Expand Down Expand Up @@ -52,6 +53,22 @@ func BenchmarkRecord8(b *testing.B) {
}
}

func BenchmarkRecord8_WithRecorder(b *testing.B) {
ctx := context.Background()
meter := view.NewMeter()
meter.Start()
defer meter.Stop()
b.ResetTimer()

for i := 0; i < b.N; i++ {
// Note that this benchmark has one extra allocation for stats.WithRecorder.
// If you cache the recorder option, this benchmark should be equally fast as BenchmarkRecord8
stats.RecordWithOptions(ctx, stats.WithRecorder(meter), stats.WithMeasurements(m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1)))
}

b.StopTimer()
}

func BenchmarkRecord8_Parallel(b *testing.B) {
ctx := context.Background()
b.ResetTimer()
Expand Down
64 changes: 48 additions & 16 deletions stats/view/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,20 @@ var (
// BenchmarkRecordReqCommand benchmarks calling the internal recording machinery
// directly.
func BenchmarkRecordReqCommand(b *testing.B) {
w := NewWorker().(*worker)
w := NewMeter().(*worker)

register := &registerViewReq{views: []*View{view}, err: make(chan error, 1)}
register.handleCommand(w)
if err := <-register.err; err != nil {
b.Fatal(err)
}
defer func() {
unregister := &unregisterFromViewReq{views: []string{view.Name}, done: make(chan struct{}, 1)}
unregister.handleCommand(w)
<-unregister.done
}()

const tagCount = 10
ctxs := make([]context.Context, 0, tagCount)
for i := 0; i < tagCount; i++ {
ctx, _ := tag.New(context.Background(),
tag.Upsert(k1, fmt.Sprintf("v%d", i)),
tag.Upsert(k2, fmt.Sprintf("v%d", i)),
tag.Upsert(k3, fmt.Sprintf("v%d", i)),
tag.Upsert(k4, fmt.Sprintf("v%d", i)),
tag.Upsert(k5, fmt.Sprintf("v%d", i)),
tag.Upsert(k6, fmt.Sprintf("v%d", i)),
tag.Upsert(k7, fmt.Sprintf("v%d", i)),
tag.Upsert(k8, fmt.Sprintf("v%d", i)),
)
ctxs = append(ctxs, ctx)
}
ctxs := prepareContexts(10)

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -91,3 +82,44 @@ func BenchmarkRecordReqCommand(b *testing.B) {
record.handleCommand(w)
}
}

func BenchmarkRecordViaStats(b *testing.B) {

meter := NewMeter()
meter.Start()
defer meter.Stop()
meter.Register(view)
defer meter.Unregister(view)

/* Register(view)
defer Unregister(view)*/
ctxs := prepareContexts(10)
rec := stats.WithRecorder(meter)
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
stats.RecordWithOptions(ctxs[i%len(ctxs)], rec, stats.WithMeasurements(m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1)))
//stats.Record(ctxs[i%len(ctxs)], m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1))
}

}

func prepareContexts(tagCount int) []context.Context {
ctxs := make([]context.Context, 0, tagCount)
for i := 0; i < tagCount; i++ {
ctx, _ := tag.New(context.Background(),
tag.Upsert(k1, fmt.Sprintf("v%d", i)),
tag.Upsert(k2, fmt.Sprintf("v%d", i)),
tag.Upsert(k3, fmt.Sprintf("v%d", i)),
tag.Upsert(k4, fmt.Sprintf("v%d", i)),
tag.Upsert(k5, fmt.Sprintf("v%d", i)),
tag.Upsert(k6, fmt.Sprintf("v%d", i)),
tag.Upsert(k7, fmt.Sprintf("v%d", i)),
tag.Upsert(k8, fmt.Sprintf("v%d", i)),
)
ctxs = append(ctxs, ctx)
}

return ctxs
}

0 comments on commit 1f969d3

Please sign in to comment.