Sentinel check to SDL CLI 'healthcheck' -command
[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 /*
19  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20  * platform project (RICP).
21  */
22
23 package sdlgo_test
24
25 import (
26         "fmt"
27         "strconv"
28         "strings"
29         "testing"
30
31         "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
32 )
33
34 type nsKeysBenchmark struct {
35         ns        string
36         nsCount   int
37         keyName   string
38         keyCount  int
39         keySize   int
40         valueSize int
41 }
42
43 type keysBenchmark struct {
44         keyName   string
45         keyCount  int
46         keySize   int
47         valueSize int
48 }
49
50 type groupBenchmark struct {
51         key         string
52         member      string
53         memberCount int
54 }
55
56 func (bm nsKeysBenchmark) String(oper string) string {
57         return fmt.Sprintf("op=%s ns-cnt=%d key-cnt=%d key-sz=%d value-sz=%d",
58                 oper, bm.nsCount, bm.keyCount, bm.keySize, bm.valueSize)
59 }
60
61 func (bm keysBenchmark) String(oper string) string {
62         return fmt.Sprintf("op=%s key-cnt=%d key-sz=%d value-sz=%d",
63                 oper, bm.keyCount, bm.keySize, bm.valueSize)
64 }
65
66 func (bm groupBenchmark) String(oper string) string {
67         return fmt.Sprintf("op=%s, mbr-cnt=%d", oper, bm.memberCount)
68 }
69
70 func getNsKeyBenchmarkInput() []nsKeysBenchmark {
71         return []nsKeysBenchmark{
72                 {"ns-a", 1, "a", 100, 10, 64},
73                 {"ns-b", 10, "b", 100, 10, 64},
74                 {"ns-c", 100, "c", 100, 10, 64},
75                 {"ns-d", 1000, "d", 100, 10, 64},
76                 {"ns-e", 10000, "e", 100, 10, 64},
77         }
78 }
79
80 func getKeyBenchmarkInput() []keysBenchmark {
81         return []keysBenchmark{
82                 {"a", 1, 10, 64},
83                 {"b", 1, 10, 1024},
84                 {"c", 1, 10, 64 * 1024},
85                 {"d", 1, 10, 1024 * 1024},
86                 {"e", 1, 10, 10 * 1024 * 1024},
87
88                 {"f", 1, 100, 64},
89                 {"g", 1, 100, 1024},
90                 {"h", 1, 100, 64 * 1024},
91                 {"i", 1, 100, 1024 * 1024},
92                 {"j", 1, 100, 10 * 1024 * 1024},
93
94                 {"k", 2, 10, 64},
95                 {"l", 10, 10, 64},
96                 {"m", 100, 10, 64},
97                 {"n", 1000, 10, 64},
98                 {"r", 5000, 10, 64},
99
100                 {"s", 2, 100, 64},
101                 {"t", 10, 100, 64},
102                 {"u", 100, 100, 64},
103                 {"v", 1000, 100, 64},
104                 {"x", 5000, 100, 64},
105         }
106 }
107
108 func BenchmarkMultiNamespaceKeysWrite(b *testing.B) {
109         benchmarks := getNsKeyBenchmarkInput()
110
111         for _, bm := range benchmarks {
112                 b.Run(bm.String("ns-keys-set"), func(b *testing.B) {
113                         sdl := sdlgo.NewSyncStorage()
114                         value := strings.Repeat("1", bm.valueSize)
115                         keyVals := make([]string, 0)
116                         for i := 0; i < bm.keyCount; i++ {
117                                 key := strings.Repeat(bm.keyName+strconv.Itoa(i), bm.keySize)
118                                 keyVals = append(keyVals, key, value)
119                         }
120                         b.ResetTimer()
121                         b.RunParallel(func(pb *testing.PB) {
122                                 for pb.Next() {
123                                         for n := 0; n < bm.nsCount; n++ {
124                                                 err := sdl.Set(bm.ns+strconv.Itoa(n), keyVals)
125                                                 if err != nil {
126                                                         b.Fatal(err)
127                                                 }
128                                         }
129                                 }
130                         })
131                 })
132         }
133 }
134
135 func BenchmarkMultiNamespaceKeysRead(b *testing.B) {
136         benchmarks := getNsKeyBenchmarkInput()
137
138         for _, bm := range benchmarks {
139                 b.Run(bm.String("keys-get"), func(b *testing.B) {
140                         sdl := sdlgo.NewSyncStorage()
141                         keys := make([]string, 0)
142                         for i := 0; i < bm.keyCount; i++ {
143                                 key := strings.Repeat(bm.keyName+strconv.Itoa(i), bm.keySize)
144                                 keys = append(keys, key)
145                         }
146                         b.ResetTimer()
147                         b.RunParallel(func(pb *testing.PB) {
148                                 for pb.Next() {
149                                         for n := 0; n < bm.nsCount; n++ {
150                                                 _, err := sdl.Get(bm.ns+strconv.Itoa(n), keys)
151                                                 if err != nil {
152                                                         b.Fatal(err)
153                                                 }
154                                         }
155                                 }
156                         })
157                 })
158         }
159 }
160
161 func BenchmarkKeysWrite(b *testing.B) {
162         benchmarks := getKeyBenchmarkInput()
163
164         for _, bm := range benchmarks {
165                 b.Run(bm.String("keys-set"), func(b *testing.B) {
166                         sdl := sdlgo.NewSdlInstance("namespace", sdlgo.NewDatabase())
167                         value := strings.Repeat("1", bm.valueSize)
168                         keyVals := make([]string, 0)
169                         for i := 0; i < bm.keyCount; i++ {
170                                 key := strings.Repeat(bm.keyName+strconv.Itoa(i), bm.keySize)
171                                 keyVals = append(keyVals, key, value)
172                         }
173                         b.ResetTimer()
174                         b.RunParallel(func(pb *testing.PB) {
175                                 for pb.Next() {
176                                         err := sdl.Set(keyVals)
177                                         if err != nil {
178                                                 b.Fatal(err)
179                                         }
180                                 }
181                         })
182                 })
183         }
184 }
185
186 func BenchmarkKeysRead(b *testing.B) {
187         benchmarks := getKeyBenchmarkInput()
188
189         for _, bm := range benchmarks {
190                 b.Run(bm.String("keys-get"), func(b *testing.B) {
191                         sdl := sdlgo.NewSdlInstance("namespace", sdlgo.NewDatabase())
192                         keys := make([]string, 0)
193                         for i := 0; i < bm.keyCount; i++ {
194                                 key := strings.Repeat(bm.keyName+strconv.Itoa(i), bm.keySize)
195                                 keys = append(keys, key)
196                         }
197                         b.ResetTimer()
198                         b.RunParallel(func(pb *testing.PB) {
199                                 for pb.Next() {
200                                         _, err := sdl.Get(keys)
201                                         if err != nil {
202                                                 b.Fatal(err)
203                                         }
204                                 }
205                         })
206                 })
207         }
208 }
209
210 func BenchmarkGroupMemberAdd(b *testing.B) {
211         benchmarks := []groupBenchmark{
212                 {"a", "x", 1},
213                 {"b", "x", 100},
214                 {"c", "x", 10000},
215                 {"d", "x", 1000000},
216         }
217
218         for _, bm := range benchmarks {
219                 b.Run(bm.String("group-add-member"), func(b *testing.B) {
220                         sdl := sdlgo.NewSdlInstance("namespace", sdlgo.NewDatabase())
221                         members := make([]string, 0)
222                         for i := 0; i < bm.memberCount; i++ {
223                                 member := bm.member + strconv.Itoa(i)
224                                 members = append(members, member)
225                         }
226                         b.ResetTimer()
227                         b.RunParallel(func(pb *testing.PB) {
228                                 for pb.Next() {
229                                         err := sdl.AddMember(bm.key, members)
230                                         if err != nil {
231                                                 b.Fatal(err)
232                                         }
233                                 }
234                         })
235                 })
236         }
237 }