1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-10 00:29:01 +02:00
kratos/pkg/net/trace/sample_test.go
2019-02-01 15:57:43 +08:00

36 lines
825 B
Go

package trace
import (
"testing"
)
func TestProbabilitySampling(t *testing.T) {
sampler := newSampler(0.001)
t.Run("test one operationName", func(t *testing.T) {
sampled, probability := sampler.IsSampled(0, "test123")
if !sampled || probability != 1 {
t.Errorf("expect sampled and probability == 1 get: %v %f", sampled, probability)
}
})
t.Run("test probability", func(t *testing.T) {
sampler.IsSampled(0, "test_opt_2")
count := 0
for i := 0; i < 100000; i++ {
sampled, _ := sampler.IsSampled(0, "test_opt_2")
if sampled {
count++
}
}
if count < 80 || count > 120 {
t.Errorf("expect count between 80~120 get %d", count)
}
})
}
func BenchmarkProbabilitySampling(b *testing.B) {
sampler := newSampler(0.001)
for i := 0; i < b.N; i++ {
sampler.IsSampled(0, "test_opt_xxx")
}
}