-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks_test.go
127 lines (111 loc) · 2.7 KB
/
benchmarks_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
125
126
127
package sketchy
import (
"encoding/binary"
"math/rand"
"net"
"runtime"
"testing"
"time"
)
// a set of keys to test with
var ips [][]byte
func init() {
// Precompute the keys we'll choose from.
for i := 0; i < 50; i++ {
bytes := make([]byte, 4)
binary.BigEndian.PutUint32(bytes, rand.Uint32())
ip := []byte(net.IP(bytes).String())
for j := 0; j < i; j++ {
ips = append(ips, ip)
}
}
}
type event struct {
ip []byte
ts time.Time
}
func BenchmarkSketch(b *testing.B) {
b.StopTimer()
runtime.GC()
// Precompute the choices we'll make.
indexes := make([]int, b.N)
for i := 0; i < b.N; i++ {
indexes[i] = rand.Intn(len(ips))
}
// Initialize counter and info state.
bucket := NewSketch(0.9957, 0.993)
maxIP := []byte{}
maxIPCount := uint64(0)
// Run the benchmark.
b.StartTimer()
for _, i := range indexes {
ip := ips[i]
if v := bucket.Count(ip, 1); v > maxIPCount {
maxIPCount = v
maxIP = ip
}
}
b.StopTimer()
b.Logf("max IP for n=%d: %s (%d)", b.N, string(maxIP), maxIPCount)
runtime.GC()
}
func BenchmarkRollingCounter(b *testing.B) {
b.StopTimer()
runtime.GC()
// Precompute the events we'll track.
mean := float64(b.N) / float64(24*time.Hour)
events := make([]event, b.N)
ts := time.Now()
for i := 0; i < b.N; i++ {
events[i].ip = ips[rand.Intn(len(ips))]
ts = ts.Add(time.Duration(rand.ExpFloat64() / mean))
events[i].ts = ts
}
// Initialize counter and info state.
counter := RollingCounter(0, 0, 5*time.Minute, 12).(*rollingCounter)
counter.clock = func() time.Time { return ts }
maxIP := []byte{}
maxIPRate := 0.0
// Run the benchmark.
b.StartTimer()
for _, e := range events {
ts = e.ts
if rate := counter.Count(e.ip, 1, time.Minute); rate > maxIPRate {
maxIPRate = rate
maxIP = e.ip
}
}
b.StopTimer()
b.Logf("max IP for n=%d: %s (%f)", b.N, string(maxIP), maxIPRate)
runtime.GC()
}
func BenchmarkRollupCounter(b *testing.B) {
b.StopTimer()
runtime.GC()
// Precompute the events we'll track.
mean := float64(b.N) / float64(24*time.Hour)
events := make([]event, b.N)
ts := time.Now()
for i := 0; i < b.N; i++ {
events[i].ip = ips[rand.Intn(len(ips))]
ts = ts.Add(time.Duration(rand.ExpFloat64() / mean))
events[i].ts = ts
}
// Initialize counter and info state.
counter := RollupCounter(0, 0, 15*time.Minute, time.Hour, 4*time.Hour, 24*time.Hour).(*rollupCounter)
counter.clock = func() time.Time { return ts }
maxIP := []byte{}
maxIPRate := 0.0
// Run the benchmark.
b.StartTimer()
for _, e := range events {
ts = e.ts
if rate := counter.Count(e.ip, 1, time.Hour); rate > maxIPRate {
maxIPRate = rate
maxIP = e.ip
}
}
b.StopTimer()
b.Logf("max IP for n=%d: %s (%f)", b.N, string(maxIP), maxIPRate)
runtime.GC()
}