grafana/pkg/util/ring/adaptive_chan_bench_test.go
Diego Augusto Molina e6ead667b3
Unified Storage: added pkg/util/ring package to handle queueing of notifications (#84657)
* added pkg/util/rinq package to handle queueing of notifications

* fix linters

* Fix typo in comment

Co-authored-by: Dan Cech <dcech@grafana.com>

* improve allocation strategy for Enqueue; remove unnecessary clearing of slice

* Update pkg/util/ringq/dyn_chan_bench_test.go

Co-authored-by: Dan Cech <dcech@grafana.com>

* Update pkg/util/ringq/ringq.go

Co-authored-by: Dan Cech <dcech@grafana.com>

* refactor to move stats and shrinking into Ring

* add missing error assertions in tests

* add missing error assertions in tests and linting issues

* simplify controller closed check

* improve encapsulation of internal state in Ring

* use (*Ring).Len for clarity instead of stats

---------

Co-authored-by: Dan Cech <dcech@grafana.com>
2024-04-11 19:32:31 -03:00

74 lines
1.1 KiB
Go

package ring
import (
"context"
"testing"
)
func BenchmarkAdaptiveChanBaseline(b *testing.B) {
in, out, _ := AdaptiveChan[int]()
in <- 1
<-out
b.Cleanup(func() {
close(in)
})
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
in <- i
val := <-out
if val != i {
b.Fatalf("expected 1, got %d", val)
}
}
}
func BenchmarkAdaptiveChanWithStatsRead(b *testing.B) {
var stats AdaptiveChanStats
in, out, sr := AdaptiveChan[int]()
in <- 1
<-out
ctx := context.Background()
b.Cleanup(func() {
close(in)
})
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
in <- 1
val := <-out
if val != 1 {
b.Fatalf("expected 1, got %d", val)
}
err := sr.WriteStats(ctx, &stats)
if err != nil {
b.Fatalf("unexpected error: %v", err)
}
if stats.Enqueued == 0 {
b.Fatalf("unexpected stats: %v", stats)
}
}
}
func BenchmarkGoChanBaseline(b *testing.B) {
c := make(chan int, 1)
b.Cleanup(func() {
close(c)
})
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
c <- 1
val := <-c
if val != 1 {
b.Fatalf("expected 1, got %d", val)
}
}
}