Revert "Support for XApp configuration update"
[ric-plt/appmgr.git] / internal / sdlgo / bench_test.go
1 package sdlgo_test
2
3 import (
4         "fmt"
5         "strconv"
6         "strings"
7         "testing"
8
9         "gitlabe1.ext.net.nokia.com/ric_dev/sdlgo"
10 )
11
12 type singleBenchmark struct {
13         key       string
14         keySize   int
15         valueSize int
16 }
17
18 type multiBenchmark struct {
19         keyBase   string
20         keyCount  int
21         keySize   int
22         valueSize int
23 }
24
25 func (bm singleBenchmark) String(oper string) string {
26         return fmt.Sprintf("op = %s key=%d value=%d", oper, bm.keySize, bm.valueSize)
27 }
28
29 func (bm multiBenchmark) String(oper string) string {
30         return fmt.Sprintf("op = %s keycnt=%d key=%d value=%d", oper, bm.keyCount, bm.keySize, bm.valueSize)
31 }
32 func BenchmarkSet(b *testing.B) {
33         benchmarks := []singleBenchmark{
34                 {"a", 10, 64},
35                 {"b", 10, 1024},
36                 {"c", 10, 64 * 1024},
37                 {"d", 10, 1024 * 1024},
38                 {"e", 10, 10 * 1024 * 1024},
39
40                 {"f", 100, 64},
41                 {"g", 100, 1024},
42                 {"h", 100, 64 * 1024},
43                 {"i", 100, 1024 * 1024},
44                 {"j", 100, 10 * 1024 * 1024},
45         }
46
47         for _, bm := range benchmarks {
48                 b.Run(bm.String("set"), func(b *testing.B) {
49                         key := strings.Repeat(bm.key, bm.keySize)
50                         value := strings.Repeat("1", bm.valueSize)
51                         sdl := sdlgo.Create("namespace")
52
53                         b.ResetTimer()
54                         b.RunParallel(func(pb *testing.PB) {
55                                 for pb.Next() {
56                                         err := sdl.Set(key, value)
57                                         if err != nil {
58                                                 b.Fatal(err)
59                                         }
60                                 }
61                         })
62                 })
63         }
64 }
65
66 func BenchmarkGet(b *testing.B) {
67         benchmarks := []singleBenchmark{
68                 {"a", 10, 64},
69                 {"b", 10, 1024},
70                 {"c", 10, 64 * 1024},
71                 {"d", 10, 1024 * 1024},
72                 {"e", 10, 10 * 1024 * 1024},
73
74                 {"f", 100, 64},
75                 {"g", 100, 1024},
76                 {"h", 100, 64 * 1024},
77                 {"i", 100, 1024 * 1024},
78                 {"j", 100, 10 * 1024 * 1024},
79         }
80
81         for _, bm := range benchmarks {
82                 b.Run(bm.String("Get"), func(b *testing.B) {
83                         key := strings.Repeat(bm.key, bm.keySize)
84                         value := strings.Repeat("1", bm.valueSize)
85                         sdl := sdlgo.Create("namespace")
86                         if err := sdl.Set(key, value); err != nil {
87                                 b.Fatal(err)
88                         }
89                         b.ResetTimer()
90                         b.RunParallel(func(pb *testing.PB) {
91                                 for pb.Next() {
92                                         _, err := sdl.Get([]string{key})
93                                         if err != nil {
94                                                 b.Fatal(err)
95                                         }
96                                 }
97                         })
98                 })
99         }
100 }
101
102 func BenchmarkMultiSet(b *testing.B) {
103         benchmarks := []multiBenchmark{
104                 {"a", 2, 10, 64},
105                 {"b", 10, 10, 64},
106                 {"c", 100, 10, 64},
107                 {"d", 1000, 10, 64},
108                 {"e", 5000, 10, 64},
109
110                 {"f", 2, 100, 64},
111                 {"g", 10, 100, 64},
112                 {"h", 100, 100, 64},
113                 {"i", 1000, 100, 64},
114                 {"j", 5000, 100, 64},
115         }
116
117         for _, bm := range benchmarks {
118                 b.Run(bm.String("mset"), func(b *testing.B) {
119                         sdl := sdlgo.Create("namespace")
120                         value := strings.Repeat("1", bm.valueSize)
121                         keyVals := make([]string, 0)
122                         for i := 0; i < bm.keyCount; i++ {
123                                 key := strings.Repeat(bm.keyBase+strconv.Itoa(i), bm.keySize)
124                                 keyVals = append(keyVals, key, value)
125                         }
126                         b.ResetTimer()
127                         b.RunParallel(func(pb *testing.PB) {
128                                 for pb.Next() {
129                                         err := sdl.Set(keyVals)
130                                         if err != nil {
131                                                 b.Fatal(err)
132                                         }
133                                 }
134                         })
135                 })
136         }
137 }
138
139 func BenchmarkMultiGet(b *testing.B) {
140         benchmarks := []multiBenchmark{
141                 {"a", 2, 10, 64},
142                 {"b", 10, 10, 64},
143                 {"c", 100, 10, 64},
144                 {"d", 1000, 10, 64},
145                 {"e", 5000, 10, 64},
146
147                 {"f", 2, 100, 64},
148                 {"g", 10, 100, 64},
149                 {"h", 100, 100, 64},
150                 {"i", 1000, 100, 64},
151                 {"j", 5000, 100, 64},
152         }
153
154         for _, bm := range benchmarks {
155                 b.Run(bm.String("gset"), func(b *testing.B) {
156                         sdl := sdlgo.Create("namespace")
157                         keyVals := make([]string, 0)
158                         for i := 0; i < bm.keyCount; i++ {
159                                 key := strings.Repeat(bm.keyBase+strconv.Itoa(i), bm.keySize)
160                                 keyVals = append(keyVals, key)
161                         }
162                         b.ResetTimer()
163                         b.RunParallel(func(pb *testing.PB) {
164                                 for pb.Next() {
165                                         _, err := sdl.Get(keyVals)
166                                         if err != nil {
167                                                 b.Fatal(err)
168                                         }
169                                 }
170                         })
171                 })
172         }
173 }