sample_test.go 825 B

1234567891011121314151617181920212223242526272829303132333435
  1. package trace
  2. import (
  3. "testing"
  4. )
  5. func TestProbabilitySampling(t *testing.T) {
  6. sampler := newSampler(0.001)
  7. t.Run("test one operationName", func(t *testing.T) {
  8. sampled, probability := sampler.IsSampled(0, "test123")
  9. if !sampled || probability != 1 {
  10. t.Errorf("expect sampled and probability == 1 get: %v %f", sampled, probability)
  11. }
  12. })
  13. t.Run("test probability", func(t *testing.T) {
  14. sampler.IsSampled(0, "test_opt_2")
  15. count := 0
  16. for i := 0; i < 100000; i++ {
  17. sampled, _ := sampler.IsSampled(0, "test_opt_2")
  18. if sampled {
  19. count++
  20. }
  21. }
  22. if count < 80 || count > 120 {
  23. t.Errorf("expect count between 80~120 get %d", count)
  24. }
  25. })
  26. }
  27. func BenchmarkProbabilitySampling(b *testing.B) {
  28. sampler := newSampler(0.001)
  29. for i := 0; i < b.N; i++ {
  30. sampler.IsSampled(0, "test_opt_xxx")
  31. }
  32. }