github.com/pinpoint-apm/pinpoint-go-agent@v1.4.1-0.20240110120318-a50c2eb18c8c/sampler_test.go (about) 1 package pinpoint 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func Test_rateSampler_isSampled(t *testing.T) { 11 type fields struct { 12 rate uint64 13 counter uint64 14 } 15 tests := []struct { 16 name string 17 fields fields 18 want bool 19 }{ 20 {"1", fields{1, 0}, true}, 21 {"2", fields{10, 0}, false}, 22 {"3", fields{10, 9}, true}, 23 } 24 for _, tt := range tests { 25 t.Run(tt.name, func(t *testing.T) { 26 s := &rateSampler{ 27 rate: tt.fields.rate, 28 counter: tt.fields.counter, 29 } 30 if got := s.isSampled(); got != tt.want { 31 t.Errorf("rateSampler.isSampled() = %v, want %v", got, tt.want) 32 } 33 }) 34 } 35 } 36 37 func Test_percentSampler_isSampled(t *testing.T) { 38 type fields struct { 39 percent float64 40 counter uint64 41 } 42 tests := []struct { 43 name string 44 fields fields 45 want bool 46 }{ 47 {"1", fields{100, 0}, true}, 48 {"2", fields{50, 0}, false}, 49 {"3", fields{50, 5000}, true}, 50 {"4", fields{1, 0}, false}, 51 {"5", fields{1, 9900}, true}, 52 {"6", fields{1, 10000}, false}, 53 } 54 for _, tt := range tests { 55 t.Run(tt.name, func(t *testing.T) { 56 s := &percentSampler{ 57 rate: uint64(tt.fields.percent * 100), 58 counter: tt.fields.counter, 59 } 60 if got := s.isSampled(); got != tt.want { 61 t.Errorf("rateSampler.isSampled() = %v, want %v", got, tt.want) 62 } 63 }) 64 } 65 } 66 67 func Test_basicTraceSampler_isNewSampled(t *testing.T) { 68 type fields struct { 69 baseSampler sampler 70 } 71 tests := []struct { 72 name string 73 fields fields 74 want bool 75 }{ 76 {"1", fields{newRateSampler(1)}, true}, 77 {"2", fields{newRateSampler(10)}, false}, 78 } 79 for _, tt := range tests { 80 t.Run(tt.name, func(t *testing.T) { 81 s := &basicTraceSampler{ 82 baseSampler: tt.fields.baseSampler, 83 } 84 if got := s.isNewSampled(); got != tt.want { 85 t.Errorf("basicTraceSampler.isNewSampled() = %v, want %v", got, tt.want) 86 } 87 }) 88 } 89 } 90 91 func Test_basicTraceSampler_isContinueSampled(t *testing.T) { 92 type fields struct { 93 baseSampler sampler 94 } 95 tests := []struct { 96 name string 97 fields fields 98 want bool 99 }{ 100 {"1", fields{newRateSampler(1)}, true}, 101 {"2", fields{newPercentSampler(10)}, true}, 102 } 103 for _, tt := range tests { 104 t.Run(tt.name, func(t *testing.T) { 105 s := &basicTraceSampler{ 106 baseSampler: tt.fields.baseSampler, 107 } 108 if got := s.isContinueSampled(); got != tt.want { 109 t.Errorf("basicTraceSampler.isNewSampled() = %v, want %v", got, tt.want) 110 } 111 }) 112 } 113 } 114 115 func Test_throughputLimitTraceSampler_isNewSampled(t *testing.T) { 116 type fields struct { 117 sampler traceSampler 118 } 119 tests := []struct { 120 name string 121 fields fields 122 want bool 123 }{ 124 {"1", fields{newThroughputLimitTraceSampler(newRateSampler(1), 10, 10)}, true}, 125 {"2", fields{newThroughputLimitTraceSampler(newRateSampler(10), 10, 10)}, false}, 126 } 127 for _, tt := range tests { 128 t.Run(tt.name, func(t *testing.T) { 129 s := tt.fields.sampler 130 if got := s.isNewSampled(); got != tt.want { 131 t.Errorf("throughputLimitTraceSampler.isNewSampled() = %v, want %v", got, tt.want) 132 } 133 }) 134 } 135 } 136 137 func Test_throughputLimitTraceSampler_skipNew(t *testing.T) { 138 type fields struct { 139 sampler traceSampler 140 } 141 tests := []struct { 142 name string 143 fields fields 144 want bool 145 }{ 146 {"1", fields{newThroughputLimitTraceSampler(newRateSampler(1), 1, 10)}, true}, 147 } 148 for _, tt := range tests { 149 t.Run(tt.name, func(t *testing.T) { 150 s := tt.fields.sampler 151 resetResponseTime() 152 153 for i := 0; i < 100; i++ { 154 s.isNewSampled() 155 } 156 assert.Equal(t, int64(1), sampleNew, "sampleNew") 157 assert.Equal(t, int64(99), skipNew, "skipNew") 158 159 time.Sleep(1 * time.Second) 160 161 for i := 0; i < 100; i++ { 162 s.isNewSampled() 163 } 164 assert.Equal(t, int64(1*2), sampleNew, "sampleNew") 165 assert.Equal(t, int64(99*2), skipNew, "skipNew") 166 }) 167 } 168 } 169 170 func Test_throughputLimitTraceSampler_isContinueSampled(t *testing.T) { 171 type fields struct { 172 sampler traceSampler 173 } 174 tests := []struct { 175 name string 176 fields fields 177 want bool 178 }{ 179 {"1", fields{newThroughputLimitTraceSampler(newRateSampler(1), 10, 10)}, true}, 180 {"2", fields{newThroughputLimitTraceSampler(newRateSampler(100), 10, 10)}, true}, 181 } 182 for _, tt := range tests { 183 t.Run(tt.name, func(t *testing.T) { 184 s := tt.fields.sampler 185 if got := s.isContinueSampled(); got != tt.want { 186 t.Errorf("throughputLimitTraceSampler.isNewSampled() = %v, want %v", got, tt.want) 187 } 188 }) 189 } 190 } 191 192 func Test_throughputLimitTraceSampler_skipContinue(t *testing.T) { 193 type fields struct { 194 sampler traceSampler 195 } 196 tests := []struct { 197 name string 198 fields fields 199 want bool 200 }{ 201 {"1", fields{newThroughputLimitTraceSampler(newRateSampler(100), 10, 1)}, true}, 202 } 203 for _, tt := range tests { 204 t.Run(tt.name, func(t *testing.T) { 205 s := tt.fields.sampler 206 resetResponseTime() 207 208 for i := 0; i < 100; i++ { 209 s.isContinueSampled() 210 } 211 assert.Equal(t, int64(1), sampleCont, "sampleCont") 212 assert.Equal(t, int64(99), skipCont, "skipCont") 213 214 time.Sleep(1 * time.Second) 215 216 for i := 0; i < 100; i++ { 217 s.isContinueSampled() 218 } 219 assert.Equal(t, int64(1*2), sampleCont, "sampleCont") 220 assert.Equal(t, int64(99*2), skipCont, "skipCont") 221 }) 222 } 223 }