Add support for SDL groups
[ric-plt/sdlgo.git] / bench_test.go
1 /*
2    Copyright (c) 2019 AT&T Intellectual Property.
3    Copyright (c) 2018-2019 Nokia.
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 */
17
18 package sdlgo_test
19
20 import (
21         "fmt"
22         "strconv"
23         "strings"
24         "testing"
25
26         "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
27 )
28
29 type singleBenchmark struct {
30         key       string
31         keySize   int
32         valueSize int
33 }
34
35 type multiBenchmark struct {
36         keyBase   string
37         keyCount  int
38         keySize   int
39         valueSize int
40 }
41
42 type setBenchmark struct {
43         key         string
44         member      string
45         memberCount int
46 }
47
48 func (bm singleBenchmark) String(oper string) string {
49         return fmt.Sprintf("op = %s key=%d value=%d", oper, bm.keySize, bm.valueSize)
50 }
51
52 func (bm multiBenchmark) String(oper string) string {
53         return fmt.Sprintf("op = %s keycnt=%d key=%d value=%d", oper, bm.keyCount, bm.keySize, bm.valueSize)
54 }
55
56 func (bm setBenchmark) String(oper string) string {
57         return fmt.Sprintf("op = %s, memberCount=%d", oper, bm.memberCount)
58 }
59 func BenchmarkSet(b *testing.B) {
60         benchmarks := []singleBenchmark{
61                 {"a", 10, 64},
62                 {"b", 10, 1024},
63                 {"c", 10, 64 * 1024},
64                 {"d", 10, 1024 * 1024},
65                 {"e", 10, 10 * 1024 * 1024},
66
67                 {"f", 100, 64},
68                 {"g", 100, 1024},
69                 {"h", 100, 64 * 1024},
70                 {"i", 100, 1024 * 1024},
71                 {"j", 100, 10 * 1024 * 1024},
72         }
73
74         for _, bm := range benchmarks {
75                 b.Run(bm.String("set"), func(b *testing.B) {
76                         key := strings.Repeat(bm.key, bm.keySize)
77                         value := strings.Repeat("1", bm.valueSize)
78                         sdl := sdlgo.NewSdlInstance("namespace", sdlgo.NewDatabase())
79
80                         b.ResetTimer()
81                         b.RunParallel(func(pb *testing.PB) {
82                                 for pb.Next() {
83                                         err := sdl.Set(key, value)
84                                         if err != nil {
85                                                 b.Fatal(err)
86                                         }
87                                 }
88                         })
89                 })
90         }
91 }
92
93 func BenchmarkGet(b *testing.B) {
94         benchmarks := []singleBenchmark{
95                 {"a", 10, 64},
96                 {"b", 10, 1024},
97                 {"c", 10, 64 * 1024},
98                 {"d", 10, 1024 * 1024},
99                 {"e", 10, 10 * 1024 * 1024},
100
101                 {"f", 100, 64},
102                 {"g", 100, 1024},
103                 {"h", 100, 64 * 1024},
104                 {"i", 100, 1024 * 1024},
105                 {"j", 100, 10 * 1024 * 1024},
106         }
107
108         for _, bm := range benchmarks {
109                 b.Run(bm.String("Get"), func(b *testing.B) {
110                         key := strings.Repeat(bm.key, bm.keySize)
111                         value := strings.Repeat("1", bm.valueSize)
112                         sdl := sdlgo.NewSdlInstance("namespace", sdlgo.NewDatabase())
113                         if err := sdl.Set(key, value); err != nil {
114                                 b.Fatal(err)
115                         }
116                         b.ResetTimer()
117                         b.RunParallel(func(pb *testing.PB) {
118                                 for pb.Next() {
119                                         _, err := sdl.Get([]string{key})
120                                         if err != nil {
121                                                 b.Fatal(err)
122                                         }
123                                 }
124                         })
125                 })
126         }
127 }
128
129 func BenchmarkMultiSet(b *testing.B) {
130         benchmarks := []multiBenchmark{
131                 {"a", 2, 10, 64},
132                 {"b", 10, 10, 64},
133                 {"c", 100, 10, 64},
134                 {"d", 1000, 10, 64},
135                 {"e", 5000, 10, 64},
136
137                 {"f", 2, 100, 64},
138                 {"g", 10, 100, 64},
139                 {"h", 100, 100, 64},
140                 {"i", 1000, 100, 64},
141                 {"j", 5000, 100, 64},
142         }
143
144         for _, bm := range benchmarks {
145                 b.Run(bm.String("mset"), func(b *testing.B) {
146                         sdl := sdlgo.NewSdlInstance("namespace", sdlgo.NewDatabase())
147                         value := strings.Repeat("1", bm.valueSize)
148                         keyVals := make([]string, 0)
149                         for i := 0; i < bm.keyCount; i++ {
150                                 key := strings.Repeat(bm.keyBase+strconv.Itoa(i), bm.keySize)
151                                 keyVals = append(keyVals, key, value)
152                         }
153                         b.ResetTimer()
154                         b.RunParallel(func(pb *testing.PB) {
155                                 for pb.Next() {
156                                         err := sdl.Set(keyVals)
157                                         if err != nil {
158                                                 b.Fatal(err)
159                                         }
160                                 }
161                         })
162                 })
163         }
164 }
165
166 func BenchmarkMultiGet(b *testing.B) {
167         benchmarks := []multiBenchmark{
168                 {"a", 2, 10, 64},
169                 {"b", 10, 10, 64},
170                 {"c", 100, 10, 64},
171                 {"d", 1000, 10, 64},
172                 {"e", 5000, 10, 64},
173
174                 {"f", 2, 100, 64},
175                 {"g", 10, 100, 64},
176                 {"h", 100, 100, 64},
177                 {"i", 1000, 100, 64},
178                 {"j", 5000, 100, 64},
179         }
180
181         for _, bm := range benchmarks {
182                 b.Run(bm.String("gset"), func(b *testing.B) {
183                         sdl := sdlgo.NewSdlInstance("namespace", sdlgo.NewDatabase())
184                         keyVals := make([]string, 0)
185                         for i := 0; i < bm.keyCount; i++ {
186                                 key := strings.Repeat(bm.keyBase+strconv.Itoa(i), bm.keySize)
187                                 keyVals = append(keyVals, key)
188                         }
189                         b.ResetTimer()
190                         b.RunParallel(func(pb *testing.PB) {
191                                 for pb.Next() {
192                                         _, err := sdl.Get(keyVals)
193                                         if err != nil {
194                                                 b.Fatal(err)
195                                         }
196                                 }
197                         })
198                 })
199         }
200 }
201
202 func BenchmarkSetAddMember(b *testing.B) {
203         benchmarks := []setBenchmark{
204                 {"a", "x", 1},
205                 {"b", "x", 100},
206                 {"c", "x", 10000},
207                 {"d", "x", 1000000},
208         }
209
210         for _, bm := range benchmarks {
211                 b.Run(bm.String("AddMember"), func(b *testing.B) {
212                         sdl := sdlgo.NewSdlInstance("namespace", sdlgo.NewDatabase())
213                         members := make([]string, 0)
214                         for i := 0; i < bm.memberCount; i++ {
215                                 member := bm.member + strconv.Itoa(i)
216                                 members = append(members, member)
217                         }
218                         b.ResetTimer()
219                         b.RunParallel(func(pb *testing.PB) {
220                                 for pb.Next() {
221                                         err := sdl.AddMember(bm.key, members)
222                                         if err != nil {
223                                                 b.Fatal(err)
224                                         }
225                                 }
226                         })
227                 })
228         }
229 }