Initial commit
[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.oran-osc.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 func (bm singleBenchmark) String(oper string) string {
43         return fmt.Sprintf("op = %s key=%d value=%d", oper, bm.keySize, bm.valueSize)
44 }
45
46 func (bm multiBenchmark) String(oper string) string {
47         return fmt.Sprintf("op = %s keycnt=%d key=%d value=%d", oper, bm.keyCount, bm.keySize, bm.valueSize)
48 }
49 func BenchmarkSet(b *testing.B) {
50         benchmarks := []singleBenchmark{
51                 {"a", 10, 64},
52                 {"b", 10, 1024},
53                 {"c", 10, 64 * 1024},
54                 {"d", 10, 1024 * 1024},
55                 {"e", 10, 10 * 1024 * 1024},
56
57                 {"f", 100, 64},
58                 {"g", 100, 1024},
59                 {"h", 100, 64 * 1024},
60                 {"i", 100, 1024 * 1024},
61                 {"j", 100, 10 * 1024 * 1024},
62         }
63
64         for _, bm := range benchmarks {
65                 b.Run(bm.String("set"), func(b *testing.B) {
66                         key := strings.Repeat(bm.key, bm.keySize)
67                         value := strings.Repeat("1", bm.valueSize)
68                         sdl := sdlgo.NewSdlInstance("namespace", sdlgo.NewDatabase())
69
70                         b.ResetTimer()
71                         b.RunParallel(func(pb *testing.PB) {
72                                 for pb.Next() {
73                                         err := sdl.Set(key, value)
74                                         if err != nil {
75                                                 b.Fatal(err)
76                                         }
77                                 }
78                         })
79                 })
80         }
81 }
82
83 func BenchmarkGet(b *testing.B) {
84         benchmarks := []singleBenchmark{
85                 {"a", 10, 64},
86                 {"b", 10, 1024},
87                 {"c", 10, 64 * 1024},
88                 {"d", 10, 1024 * 1024},
89                 {"e", 10, 10 * 1024 * 1024},
90
91                 {"f", 100, 64},
92                 {"g", 100, 1024},
93                 {"h", 100, 64 * 1024},
94                 {"i", 100, 1024 * 1024},
95                 {"j", 100, 10 * 1024 * 1024},
96         }
97
98         for _, bm := range benchmarks {
99                 b.Run(bm.String("Get"), func(b *testing.B) {
100                         key := strings.Repeat(bm.key, bm.keySize)
101                         value := strings.Repeat("1", bm.valueSize)
102                         sdl := sdlgo.NewSdlInstance("namespace", sdlgo.NewDatabase())
103                         if err := sdl.Set(key, value); err != nil {
104                                 b.Fatal(err)
105                         }
106                         b.ResetTimer()
107                         b.RunParallel(func(pb *testing.PB) {
108                                 for pb.Next() {
109                                         _, err := sdl.Get([]string{key})
110                                         if err != nil {
111                                                 b.Fatal(err)
112                                         }
113                                 }
114                         })
115                 })
116         }
117 }
118
119 func BenchmarkMultiSet(b *testing.B) {
120         benchmarks := []multiBenchmark{
121                 {"a", 2, 10, 64},
122                 {"b", 10, 10, 64},
123                 {"c", 100, 10, 64},
124                 {"d", 1000, 10, 64},
125                 {"e", 5000, 10, 64},
126
127                 {"f", 2, 100, 64},
128                 {"g", 10, 100, 64},
129                 {"h", 100, 100, 64},
130                 {"i", 1000, 100, 64},
131                 {"j", 5000, 100, 64},
132         }
133
134         for _, bm := range benchmarks {
135                 b.Run(bm.String("mset"), func(b *testing.B) {
136                         sdl := sdlgo.NewSdlInstance("namespace", sdlgo.NewDatabase())
137                         value := strings.Repeat("1", bm.valueSize)
138                         keyVals := make([]string, 0)
139                         for i := 0; i < bm.keyCount; i++ {
140                                 key := strings.Repeat(bm.keyBase+strconv.Itoa(i), bm.keySize)
141                                 keyVals = append(keyVals, key, value)
142                         }
143                         b.ResetTimer()
144                         b.RunParallel(func(pb *testing.PB) {
145                                 for pb.Next() {
146                                         err := sdl.Set(keyVals)
147                                         if err != nil {
148                                                 b.Fatal(err)
149                                         }
150                                 }
151                         })
152                 })
153         }
154 }
155
156 func BenchmarkMultiGet(b *testing.B) {
157         benchmarks := []multiBenchmark{
158                 {"a", 2, 10, 64},
159                 {"b", 10, 10, 64},
160                 {"c", 100, 10, 64},
161                 {"d", 1000, 10, 64},
162                 {"e", 5000, 10, 64},
163
164                 {"f", 2, 100, 64},
165                 {"g", 10, 100, 64},
166                 {"h", 100, 100, 64},
167                 {"i", 1000, 100, 64},
168                 {"j", 5000, 100, 64},
169         }
170
171         for _, bm := range benchmarks {
172                 b.Run(bm.String("gset"), func(b *testing.B) {
173                         sdl := sdlgo.NewSdlInstance("namespace", sdlgo.NewDatabase())
174                         keyVals := make([]string, 0)
175                         for i := 0; i < bm.keyCount; i++ {
176                                 key := strings.Repeat(bm.keyBase+strconv.Itoa(i), bm.keySize)
177                                 keyVals = append(keyVals, key)
178                         }
179                         b.ResetTimer()
180                         b.RunParallel(func(pb *testing.PB) {
181                                 for pb.Next() {
182                                         _, err := sdl.Get(keyVals)
183                                         if err != nil {
184                                                 b.Fatal(err)
185                                         }
186                                 }
187                         })
188                 })
189         }
190 }