@@ -4,42 +4,51 @@ import (
4
4
"testing"
5
5
"time"
6
6
7
+ "github.com/Yiling-J/theine-go"
7
8
"github.com/dgraph-io/ristretto"
8
9
"github.com/lxzan/memorycache"
9
10
"github.com/lxzan/memorycache/internal/utils"
10
- "github.com/maypok86/otter"
11
11
)
12
12
13
- const benchcount = 1000000
13
+ const (
14
+ sharding = 128
15
+ benchcount = 1 << 20
16
+ )
17
+
18
+ var (
19
+ benchkeys = make ([]string , 0 , benchcount )
14
20
15
- var benchkeys = make ([]string , 0 , benchcount )
21
+ options = []memorycache.Option {
22
+ memorycache .WithBucketNum (sharding ),
23
+ memorycache .WithBucketSize (benchcount / sharding / 10 , benchcount / sharding ),
24
+ memorycache .WithSwissTable (),
25
+ }
26
+ )
16
27
17
28
func init () {
18
29
for i := 0 ; i < benchcount ; i ++ {
19
30
benchkeys = append (benchkeys , string (utils .AlphabetNumeric .Generate (16 )))
20
31
}
21
32
}
22
33
34
+ func getIndex (i int ) int {
35
+ return i & (len (benchkeys ) - 1 )
36
+ }
37
+
23
38
func BenchmarkMemoryCache_Set (b * testing.B ) {
24
- var mc = memorycache .New (
25
- memorycache .WithBucketNum (128 ),
26
- memorycache .WithBucketSize (1000 , 10000 ),
27
- )
39
+ var mc = memorycache .New [string , int ](options ... )
28
40
b .RunParallel (func (pb * testing.PB ) {
29
41
var i = 0
30
42
for pb .Next () {
31
- index := i % benchcount
43
+ index := getIndex ( i )
32
44
i ++
33
45
mc .Set (benchkeys [index ], 1 , time .Hour )
34
46
}
35
47
})
36
48
}
37
49
38
50
func BenchmarkMemoryCache_Get (b * testing.B ) {
39
- var mc = memorycache .New (
40
- memorycache .WithBucketNum (128 ),
41
- memorycache .WithBucketSize (1000 , 10000 ),
42
- )
51
+ var mc = memorycache .New [string , int ](options ... )
43
52
for i := 0 ; i < benchcount ; i ++ {
44
53
mc .Set (benchkeys [i % benchcount ], 1 , time .Hour )
45
54
}
@@ -48,18 +57,15 @@ func BenchmarkMemoryCache_Get(b *testing.B) {
48
57
b .RunParallel (func (pb * testing.PB ) {
49
58
var i = 0
50
59
for pb .Next () {
51
- index := i % benchcount
60
+ index := getIndex ( i )
52
61
i ++
53
62
mc .Get (benchkeys [index ])
54
63
}
55
64
})
56
65
}
57
66
58
67
func BenchmarkMemoryCache_SetAndGet (b * testing.B ) {
59
- var mc = memorycache .New (
60
- memorycache .WithBucketNum (128 ),
61
- memorycache .WithBucketSize (1000 , 10000 ),
62
- )
68
+ var mc = memorycache .New [string , int ](options ... )
63
69
for i := 0 ; i < benchcount ; i ++ {
64
70
mc .Set (benchkeys [i % benchcount ], 1 , time .Hour )
65
71
}
@@ -68,7 +74,7 @@ func BenchmarkMemoryCache_SetAndGet(b *testing.B) {
68
74
b .RunParallel (func (pb * testing.PB ) {
69
75
var i = 0
70
76
for pb .Next () {
71
- index := i % benchcount
77
+ index := getIndex ( i )
72
78
i ++
73
79
if index & 7 == 0 {
74
80
mc .Set (benchkeys [index ], 1 , time .Hour )
@@ -81,14 +87,14 @@ func BenchmarkMemoryCache_SetAndGet(b *testing.B) {
81
87
82
88
func BenchmarkRistretto_Set (b * testing.B ) {
83
89
var mc , _ = ristretto .NewCache (& ristretto.Config {
84
- NumCounters : 10000 * 128 * 10 , // number of keys to track frequency of (10M).
85
- MaxCost : 1 << 30 , // maximum cost of cache (1GB).
86
- BufferItems : 64 , // number of keys per Get buffer.
90
+ NumCounters : benchcount * 10 , // number of keys to track frequency of (10M).
91
+ MaxCost : 1 << 30 , // maximum cost of cache (1GB).
92
+ BufferItems : 64 , // number of keys per Get buffer.
87
93
})
88
94
b .RunParallel (func (pb * testing.PB ) {
89
95
var i = 0
90
96
for pb .Next () {
91
- index := i % benchcount
97
+ index := getIndex ( i )
92
98
i ++
93
99
mc .SetWithTTL (benchkeys [index ], 1 , 1 , time .Hour )
94
100
}
@@ -97,9 +103,9 @@ func BenchmarkRistretto_Set(b *testing.B) {
97
103
98
104
func BenchmarkRistretto_Get (b * testing.B ) {
99
105
var mc , _ = ristretto .NewCache (& ristretto.Config {
100
- NumCounters : 1e7 , // number of keys to track frequency of (10M).
101
- MaxCost : 1 << 30 , // maximum cost of cache (1GB).
102
- BufferItems : 64 , // number of keys per Get buffer.
106
+ NumCounters : benchcount * 10 , // number of keys to track frequency of (10M).
107
+ MaxCost : 1 << 30 , // maximum cost of cache (1GB).
108
+ BufferItems : 64 , // number of keys per Get buffer.
103
109
})
104
110
for i := 0 ; i < benchcount ; i ++ {
105
111
mc .SetWithTTL (benchkeys [i % benchcount ], 1 , 1 , time .Hour )
@@ -109,7 +115,7 @@ func BenchmarkRistretto_Get(b *testing.B) {
109
115
b .RunParallel (func (pb * testing.PB ) {
110
116
var i = 0
111
117
for pb .Next () {
112
- index := i % benchcount
118
+ index := getIndex ( i )
113
119
i ++
114
120
mc .Get (benchkeys [index ])
115
121
}
@@ -118,9 +124,9 @@ func BenchmarkRistretto_Get(b *testing.B) {
118
124
119
125
func BenchmarkRistretto_SetAndGet (b * testing.B ) {
120
126
var mc , _ = ristretto .NewCache (& ristretto.Config {
121
- NumCounters : 10000 * 128 * 10 , // number of keys to track frequency of (10M).
122
- MaxCost : 1 << 30 , // maximum cost of cache (1GB).
123
- BufferItems : 64 , // number of keys per Get buffer.
127
+ NumCounters : benchcount * 10 , // number of keys to track frequency of (10M).
128
+ MaxCost : 1 << 30 , // maximum cost of cache (1GB).
129
+ BufferItems : 64 , // number of keys per Get buffer.
124
130
})
125
131
for i := 0 ; i < benchcount ; i ++ {
126
132
mc .SetWithTTL (benchkeys [i % benchcount ], 1 , 1 , time .Hour )
@@ -130,7 +136,7 @@ func BenchmarkRistretto_SetAndGet(b *testing.B) {
130
136
b .RunParallel (func (pb * testing.PB ) {
131
137
var i = 0
132
138
for pb .Next () {
133
- index := i % benchcount
139
+ index := getIndex ( i )
134
140
i ++
135
141
if index & 7 == 0 {
136
142
mc .SetWithTTL (benchkeys [index ], 1 , 1 , time .Hour )
@@ -141,53 +147,49 @@ func BenchmarkRistretto_SetAndGet(b *testing.B) {
141
147
})
142
148
}
143
149
144
- func BenchmarkOtter_Set (b * testing.B ) {
145
- var mc , _ = otter. MustBuilder [string , int ](10000 * 128 ).Build ()
150
+ func BenchmarkTheine_Set (b * testing.B ) {
151
+ mc , _ := theine. NewBuilder [string , int ](benchcount ).Build ()
146
152
b .RunParallel (func (pb * testing.PB ) {
147
153
i := 0
148
154
for pb .Next () {
149
- index := i % benchcount
155
+ index := getIndex ( i )
150
156
i ++
151
- mc .SetWithTTL (benchkeys [index ], 1 , time .Hour )
157
+ mc .SetWithTTL (benchkeys [index ], 1 , 1 , time .Hour )
152
158
}
153
159
})
154
160
}
155
161
156
- func BenchmarkOtter_Get (b * testing.B ) {
157
- mc , _ := otter. MustBuilder [string , int ](10000 * 128 ).Build ()
162
+ func BenchmarkTheine_Get (b * testing.B ) {
163
+ mc , _ := theine. NewBuilder [string , int ](benchcount ).Build ()
158
164
for i := 0 ; i < benchcount ; i ++ {
159
- mc .SetWithTTL (benchkeys [i % benchcount ], 1 , time .Hour )
165
+ mc .SetWithTTL (benchkeys [i % benchcount ], 1 , 1 , time .Hour )
160
166
}
161
167
162
168
b .ResetTimer ()
163
169
b .RunParallel (func (pb * testing.PB ) {
164
170
i := 0
165
171
for pb .Next () {
166
- index := i % benchcount
172
+ index := getIndex ( i )
167
173
i ++
168
- if index & 7 == 0 {
169
- mc .SetWithTTL (benchkeys [index ], 1 , time .Hour )
170
- } else {
171
- mc .Get (benchkeys [index ])
172
- }
174
+ mc .Get (benchkeys [index ])
173
175
}
174
176
})
175
177
}
176
178
177
- func BenchmarkOtter_SetAndGet (b * testing.B ) {
178
- mc , _ := otter. MustBuilder [string , int ](10000 * 128 ).Build ()
179
+ func BenchmarkTheine_SetAndGet (b * testing.B ) {
180
+ mc , _ := theine. NewBuilder [string , int ](benchcount ).Build ()
179
181
for i := 0 ; i < benchcount ; i ++ {
180
- mc .SetWithTTL (benchkeys [i % benchcount ], 1 , time .Hour )
182
+ mc .SetWithTTL (benchkeys [i % benchcount ], 1 , 1 , time .Hour )
181
183
}
182
184
183
185
b .ResetTimer ()
184
186
b .RunParallel (func (pb * testing.PB ) {
185
187
i := 0
186
188
for pb .Next () {
187
- index := i % benchcount
189
+ index := getIndex ( i )
188
190
i ++
189
191
if index & 7 == 0 {
190
- mc .SetWithTTL (benchkeys [index ], 1 , time .Hour )
192
+ mc .SetWithTTL (benchkeys [index ], 1 , 1 , time .Hour )
191
193
} else {
192
194
mc .Get (benchkeys [index ])
193
195
}
0 commit comments