forked from tracer/tracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampler_test.go
124 lines (110 loc) · 2.36 KB
/
sampler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package tracer
import (
"testing"
"time"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
)
const N = int(1e6)
func TestProbabilisticSampler(t *testing.T) {
s := NewProbabilisticSampler(1)
n := 0
for i := 0; i < N; i++ {
if s.Sample(1) {
n++
}
}
if n != N {
t.Errorf("got %d out of %d samples, expected %d", N, n, N)
}
s = NewProbabilisticSampler(0)
n = 0
for i := 0; i < N; i++ {
if s.Sample(1) {
n++
}
}
if n != 0 {
t.Errorf("got %d out of %d samples, expected 0", n, N)
}
s = NewProbabilisticSampler(0.25)
n = 0
for i := 0; i < N; i++ {
if s.Sample(1) {
n++
}
}
if n > N/4+N/100 || n < N/4-N {
t.Errorf("got %d out of %d samples, expected about %d", N, n, N/4)
}
}
func TestConstSampler(t *testing.T) {
s := NewConstSampler(true)
for i := 0; i < N; i++ {
if !s.Sample(1) {
t.Error("expected only true samples")
}
}
s = NewConstSampler(false)
for i := 0; i < N; i++ {
if s.Sample(1) {
t.Error("expected only false samples")
}
}
}
func TestRateSampler(t *testing.T) {
t1 := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
t2 := t1.Add(2 * time.Second)
s := NewRateSampler(1000)
s.(rateSampler).l.t = t1
s.(rateSampler).l.nowFn = func() time.Time { return t1 }
n := 0
for i := 0; i < N; i++ {
if s.Sample(1) {
n++
}
}
if n != 1000 {
t.Errorf("got %d samples, expected %d", n, 1000)
}
s = NewRateSampler(1000)
s.(rateSampler).l.t = t1
s.(rateSampler).l.nowFn = func() time.Time { return t1 }
n = 0
for i := 0; i < N; i++ {
if s.Sample(1) {
n++
}
if i == N/2 {
s.(rateSampler).l.nowFn = func() time.Time {
return t2
}
}
}
if n != 2000 {
t.Errorf("got %d samples, expected %d", n, 1000)
}
}
func TestForcedSample(t *testing.T) {
tr := &Tracer{}
tr.Sampler = NewConstSampler(false)
tr.idGenerator = RandomID{}
sp := tr.StartSpan("", opentracing.Tags{string(ext.SamplingPriority): uint16(1)})
if !sp.(*Span).Sampled() {
t.Errorf("span wasn't sampled but expected it to be")
}
}
func TestSamplerUse(t *testing.T) {
tr := &Tracer{}
tr.Sampler = NewConstSampler(true)
tr.idGenerator = RandomID{}
sp := tr.StartSpan("")
if !sp.(*Span).Sampled() {
t.Errorf("span wasn't sampled but expected it to be")
}
tr.Sampler = NewConstSampler(false)
sp = tr.StartSpan("")
if sp.(*Span).Sampled() {
t.Errorf("span was sampled but didn't expect it to be")
}
}