-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter_test.go
51 lines (40 loc) · 926 Bytes
/
counter_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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package ratecount
import (
"fmt"
"testing"
"time"
)
func ExampleCounter() {
c := NewAvgCounter(100*time.Millisecond, 3)
for i := 0; i < 51; i++ {
c.Incr(1)
time.Sleep(10 * time.Millisecond)
}
// should be 9 or 10
fmt.Println(c.Rate() == 9 || c.Rate() == 10)
// output: true
}
func BenchmarkIncrSmall(b *testing.B) {
c := NewAvgCounter(time.Millisecond, 5)
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Incr(1)
}
}
func BenchmarkIncrMid(b *testing.B) {
c := NewAvgCounter(500*time.Millisecond, 5)
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Incr(1)
}
}
func BenchmarkIncrLarge(b *testing.B) {
c := NewAvgCounter(time.Minute, 5)
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Incr(1)
}
}