forked from aerospike/aerospike-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_bench_test.go
297 lines (251 loc) · 6.82 KB
/
key_bench_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2013-2016 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aerospike
import (
"math/rand"
"reflect"
"strings"
"testing"
"github.com/aerospike/aerospike-client-go/pkg/ripemd160"
)
var res []byte
func doTheHash(buf []byte, b *testing.B) {
hash := ripemd160.New()
for i := 0; i < b.N; i++ {
hash.Reset()
hash.Write(buf)
res = hash.Sum(nil)
}
}
func Benchmark_K_Hash_S_______1(b *testing.B) {
buffer := []byte(strings.Repeat("s", 1))
doTheHash(buffer, b)
}
func Benchmark_K_Hash_S______10(b *testing.B) {
buffer := []byte(strings.Repeat("s", 10))
doTheHash(buffer, b)
}
func Benchmark_K_Hash_S_____100(b *testing.B) {
buffer := []byte(strings.Repeat("s", 100))
doTheHash(buffer, b)
}
func Benchmark_K_Hash_S____1000(b *testing.B) {
buffer := []byte(strings.Repeat("s", 1000))
doTheHash(buffer, b)
}
func Benchmark_K_Hash_S__10_000(b *testing.B) {
buffer := []byte(strings.Repeat("s", 10000))
doTheHash(buffer, b)
}
func Benchmark_K_Hash_S_100_000(b *testing.B) {
buffer := []byte(strings.Repeat("s", 100000))
doTheHash(buffer, b)
}
func makeKeys(val interface{}, b *testing.B) {
for i := 0; i < b.N; i++ {
key, _ := NewKey("ns", "set", val)
res = key.Digest()
}
}
func Benchmark_NewKey_String______1(b *testing.B) {
buffer := strings.Repeat("s", 1)
makeKeys(buffer, b)
}
func Benchmark_NewKey_String_____10(b *testing.B) {
buffer := strings.Repeat("s", 10)
makeKeys(buffer, b)
}
func Benchmark_NewKey_String____100(b *testing.B) {
buffer := strings.Repeat("s", 100)
makeKeys(buffer, b)
}
func Benchmark_NewKey_String___1000(b *testing.B) {
buffer := strings.Repeat("s", 1000)
makeKeys(buffer, b)
}
func Benchmark_NewKey_String__10000(b *testing.B) {
buffer := strings.Repeat("s", 10000)
makeKeys(buffer, b)
}
func Benchmark_NewKey_String_100000(b *testing.B) {
buffer := strings.Repeat("s", 100000)
makeKeys(buffer, b)
}
func Benchmark_NewKey_Byte______1(b *testing.B) {
buffer := []byte(strings.Repeat("s", 1))
makeKeys(buffer, b)
}
func Benchmark_NewKey_Byte_____10(b *testing.B) {
buffer := []byte(strings.Repeat("s", 10))
makeKeys(buffer, b)
}
func Benchmark_NewKey_Byte____100(b *testing.B) {
buffer := []byte(strings.Repeat("s", 100))
makeKeys(buffer, b)
}
func Benchmark_NewKey_Byte___1000(b *testing.B) {
buffer := []byte(strings.Repeat("s", 1000))
makeKeys(buffer, b)
}
func Benchmark_NewKey_Byte__10000(b *testing.B) {
buffer := []byte(strings.Repeat("s", 10000))
makeKeys(buffer, b)
}
func Benchmark_NewKey_Byte_100000(b *testing.B) {
buffer := []byte(strings.Repeat("s", 100000))
makeKeys(buffer, b)
}
func Benchmark_NewKey_________Int(b *testing.B) {
makeKeys(rand.Int63(), b)
}
func Benchmark_NewKey_List_No_Reflect(b *testing.B) {
list := []interface{}{
strings.Repeat("s", 1),
strings.Repeat("s", 10),
strings.Repeat("s", 100),
strings.Repeat("s", 1000),
strings.Repeat("s", 10000),
strings.Repeat("s", 100000),
}
makeKeys(list, b)
}
func Benchmark_NewKey_List_With_Reflect(b *testing.B) {
list := []string{
strings.Repeat("s", 1),
strings.Repeat("s", 10),
strings.Repeat("s", 100),
strings.Repeat("s", 1000),
strings.Repeat("s", 10000),
strings.Repeat("s", 100000),
}
makeKeys(list, b)
}
func Benchmark_NewKey_Map_No_Reflect(b *testing.B) {
theMap := map[interface{}]interface{}{
1: strings.Repeat("s", 1),
2: strings.Repeat("s", 10),
3: strings.Repeat("s", 100),
4: strings.Repeat("s", 1000),
5: strings.Repeat("s", 10000),
6: strings.Repeat("s", 100000),
7: rand.Int63(),
}
makeKeys(theMap, b)
}
func Benchmark_NewKey_Map_With_Reflect(b *testing.B) {
theMap := map[int]interface{}{
1: strings.Repeat("s", 1),
2: strings.Repeat("s", 10),
3: strings.Repeat("s", 100),
4: strings.Repeat("s", 1000),
5: strings.Repeat("s", 10000),
6: strings.Repeat("s", 100000),
7: rand.Int63(),
}
makeKeys(theMap, b)
}
var _k, _v interface{}
func Benchmark_Map_Native_Iterate(b *testing.B) {
theMap := map[interface{}]interface{}{
1: strings.Repeat("s", 1),
7: rand.Int63(),
}
for i := 0; i < b.N; i++ {
for k, v := range theMap {
_k, _v = k, v
}
}
}
func Benchmark_Map_Reflect_Iterate(b *testing.B) {
theMap := map[interface{}]interface{}{
1: strings.Repeat("s", 1),
7: rand.Int63(),
}
var interfaceMap interface{} = theMap
for i := 0; i < b.N; i++ {
s := reflect.ValueOf(interfaceMap)
for _, k := range s.MapKeys() {
_k, _v = k.Interface(), s.MapIndex(k).Interface()
}
}
}
// func Benchmark_Key_New(b *testing.B) {
// for i := 0; i < b.N; i++ {
// key, _ = NewKeyNew(str, str, str)
// res = key.Digest()
// }
// }
// func Benchmark_K_ComputeDigest_Orig(b *testing.B) {
// key, _ := NewKey(str, str, str)
// b.ResetTimer()
// for i := 0; i < b.N; i++ {
// res, _ = computeDigest(key)
// }
// }
// func Benchmark_K_ComputeDigest_Verify(b *testing.B) {
// var res1, res2 []byte
// var key *Key
// for i := 0; i < b.N; i++ {
// key, _ = NewKey(str, str, i)
// res1, _ = computeDigest(key)
// res2, _ = computeDigestNew(key)
// if !bytes.Equal(res1, res2) {
// panic("Oh oh!")
// }
// key, _ = NewKey(str, str, fmt.Sprintf("%s%d", str, i))
// res1, _ = computeDigest(key)
// res2, _ = computeDigestNew(key)
// if !bytes.Equal(res1, res2) {
// panic(fmt.Sprintf("Oh oh!\n%v\n%v", res1, res2))
// }
// }
// }
// func Benchmark_K_ComputeDigest_Raw(b *testing.B) {
// h := ripemd160.New()
// setName := []byte(str)
// keyType := []byte{byte(ParticleType.STRING)}
// keyVal := []byte(str)
// for i := 0; i < b.N; i++ {
// h.Reset()
// // write will not fail; no error checking necessary
// h.Write(setName)
// h.Write(keyType)
// h.Write(keyVal)
// res = h.Sum(nil)
// }
// }
// func Benchmark_ComputeDigest_New(b *testing.B) {
// for i := 0; i < b.N; i++ {
// res, _ = ComputeDigestNew(str, strVal)
// }
// }
// func Test_Compare_ComputeDigests(t *testing.T) {
// for i := 0; i < 100000; i++ {
// str := randString(rand.Intn(100))
// // v := str
// // v := rand.Int()
// // v := rand.Int63()
// // v := []byte{}
// // v := []byte{17, 191, 241}
// // v := []int{17, 191, 241}
// // v := []interface{}{17, "191", nil}
// v := []Value{NewValue(17), NewValue("191"), NewValue(nil)}
// val := NewValue(v)
// a, _ := ComputeDigest(str, val)
// b, _ := ComputeDigestNew(str, val)
// if !bytes.Equal(a, b) {
// panic("Digests are not the same")
// }
// }
// }