f2eb379c2e4fca7325bd9b890bd5fd5f52911d51
[ric-plt/sdlgo.git] / cmd / sdltester / sdltester.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 main
24
25 import (
26         "fmt"
27         "os"
28         "time"
29
30         "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
31 )
32
33 /*
34  * This program demonsrates the basic usage of sdlgo module.
35  *
36  * The following scenarios are provided:
37  *
38  * - write: Write data. Performance is measured.
39  *
40  * - read: Read data. Performance is measured.
41  *
42  * - remove: Remove data. Performance is measured.
43  *
44  * - noexist: Read non-existing data. Performance is measured and empty container is returned as nothing was
45  *            found.
46  *
47  * - getall: Read all keys within a namespace. One can manually add keys under the used namespace and all
48  *           those keys should be returned here. Performance is measured.
49  *
50  * - removeall: Remove all keys within a namespace. Performance is measured.
51  *
52  * - emptymap: Write an empty container. Performance is measured.
53  *
54  * - multiple: Make two writes. Performance is measured.
55  *
56  * - emptydata: Write empty data for a key. Performance is measured.
57  *
58  * - writeif and writeifnot: Write if old data (written with the "write" option) has remained and remains
59  *                           unchanged during the function call. Do not write if data has changed. Performance
60  *                           is measured.
61  *
62  * - removeif: Remove if old data (written with the "write" option) has remained and remains
63  *             unchanged during the function call. Do not remove data if data has changed. Performance
64  *             is measured.
65  */
66
67 func main() {
68         sdl := sdlgo.NewSyncStorage()
69
70         if len(os.Args) > 1 {
71                 switch command := os.Args[1]; command {
72                 case "write":
73                         write(sdl)
74                 case "read":
75                         read(sdl)
76                 case "remove":
77                         remove(sdl)
78                 case "noexist":
79                         noexist(sdl)
80                 case "getall":
81                         getall(sdl)
82                 case "removeall":
83                         removeall(sdl)
84                 case "emptymap":
85                         emptymap(sdl)
86                 case "multiple":
87                         multiple(sdl)
88                 case "emptydata":
89                         emptydata(sdl)
90                 case "writeif":
91                         writeif(sdl)
92                 case "writeifnot":
93                         writeifnot(sdl)
94                 case "removeif":
95                         removeif(sdl)
96                 default:
97                         printHelp()
98                 }
99
100         } else {
101                 printHelp()
102         }
103 }
104
105 func printHelp() {
106         fmt.Println("Usage: sdltester <command>")
107         fmt.Println("Commands:")
108         fmt.Println("write        Write data. Performance is measured")
109         fmt.Println("read         Read data. Performance is measured")
110         fmt.Println("remove       Remove data. Performance is measured")
111         fmt.Println("noexist      Read non-existing data. Performance is measured and empty container is returned as nothing was")
112         fmt.Println("             found")
113         fmt.Println("getall       Read all keys within a namespace. One can manually add keys under the used namespace and all")
114         fmt.Println("             those keys should be returned here. Performance is measured")
115         fmt.Println("removeall    Remove all keys within a namespace. Performance is measured")
116         fmt.Println("emptymap     Write an empty container. Performance is measured")
117         fmt.Println("multiple     Make two writes. Performance is measured")
118         fmt.Println("emptydata    Write empty data for a key. Performance is measured")
119         fmt.Println("writeif      Write if old data (written with the \"write\" option) has remained and remains")
120         fmt.Println("             unchanged during the function call. Do not write if data has changed. Performance")
121         fmt.Println("             is measured")
122         fmt.Println("writeifnot   Write only if key is not set. Performance is measured")
123         fmt.Println("removeif     Remove if old data (written with the \"write\" option) has remained and remains")
124         fmt.Println("             unchanged during the function call. Do not remove data if data has changed. Performance")
125         fmt.Println("             is measured")
126 }
127
128 func write(sdl *sdlgo.SyncStorage) {
129         data := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
130                 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44}
131         start := time.Now()
132         err := sdl.Set("tag1", "key1", data)
133         elapsed := time.Since(start)
134         if err == nil {
135                 fmt.Printf("Write: %s\n", elapsed)
136         } else {
137                 fmt.Println(err)
138         }
139 }
140
141 func read(sdl *sdlgo.SyncStorage) {
142         k := []string{"key1"}
143         start := time.Now()
144         data, err := sdl.Get("tag1", k)
145         elapsed := time.Since(start)
146         if err == nil {
147                 value, ok := data["key1"]
148                 if ok && value != nil {
149                         fmt.Printf("Read: %s\n", elapsed)
150                 } else {
151                         fmt.Printf("Read, not found: %s\n", elapsed)
152                 }
153
154         } else {
155                 fmt.Println(err)
156         }
157 }
158
159 func remove(sdl *sdlgo.SyncStorage) {
160         k := []string{"key1"}
161         start := time.Now()
162         err := sdl.Remove("tag1", k)
163         elapsed := time.Since(start)
164         if err == nil {
165                 fmt.Printf("Remove: %s\n", elapsed)
166         } else {
167                 fmt.Println(err)
168         }
169 }
170
171 func noexist(sdl *sdlgo.SyncStorage) {
172         start := time.Now()
173         _, err := sdl.Get("tag1", []string{"no1", "no2"})
174         elapsed := time.Since(start)
175         if err == nil {
176                 fmt.Printf("Noexist: %s\n", elapsed)
177         } else {
178                 fmt.Println(err)
179         }
180 }
181
182 func getall(sdl *sdlgo.SyncStorage) {
183         start := time.Now()
184         keys, err := sdl.GetAll("tag1")
185         elapsed := time.Since(start)
186         if err == nil {
187                 fmt.Printf("Getall: %s\n", elapsed)
188                 for _, i := range keys {
189                         fmt.Println(i)
190                 }
191         } else {
192                 fmt.Println(err)
193         }
194 }
195
196 func removeall(sdl *sdlgo.SyncStorage) {
197         start := time.Now()
198         err := sdl.RemoveAll("tag1")
199         elapsed := time.Since(start)
200         if err == nil {
201                 fmt.Printf("Removeall: %s\n", elapsed)
202         } else {
203                 fmt.Println(err)
204         }
205 }
206
207 func emptymap(sdl *sdlgo.SyncStorage) {
208         start := time.Now()
209         err := sdl.Set("tag1", "", "")
210         elapsed := time.Since(start)
211         if err == nil {
212                 fmt.Printf("Emptymap: %s\n", elapsed)
213         } else {
214                 fmt.Println(err)
215         }
216 }
217
218 func multiple(sdl *sdlgo.SyncStorage) {
219         data := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
220                 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44}
221         start := time.Now()
222         err := sdl.Set("tag1", "key1m", data)
223         elapsed := time.Since(start)
224         if err == nil {
225                 fmt.Printf("Multiple: %s ", elapsed)
226         } else {
227                 fmt.Println(err)
228         }
229         start = time.Now()
230         err = sdl.Set("tag1", "key2m", data)
231         elapsed = time.Since(start)
232         if err == nil {
233                 fmt.Printf(" %s \n", elapsed)
234         } else {
235                 fmt.Println(err)
236         }
237 }
238
239 func emptydata(sdl *sdlgo.SyncStorage) {
240         data := []byte{}
241         start := time.Now()
242         err := sdl.Set("tag1", "key1", data)
243         elapsed := time.Since(start)
244         if err == nil {
245                 fmt.Printf("Emptydata: %s\n", elapsed)
246         } else {
247                 fmt.Println(err)
248         }
249 }
250
251 func writeif(sdl *sdlgo.SyncStorage) {
252         oldVec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
253                 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44}
254         newVec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
255                 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x66}
256         start := time.Now()
257         _, err := sdl.SetIf("tag1", "key1", oldVec, newVec)
258         elapsed := time.Since(start)
259         if err == nil {
260                 fmt.Printf("Writeif: %s\n", elapsed)
261         } else {
262                 fmt.Println(err)
263         }
264 }
265
266 func writeifnot(sdl *sdlgo.SyncStorage) {
267         vec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
268                 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x88}
269         start := time.Now()
270         _, err := sdl.SetIfNotExists("tag1", "key1", vec)
271         elapsed := time.Since(start)
272         if err == nil {
273                 fmt.Printf("Writeifnot: %s\n", elapsed)
274         } else {
275                 fmt.Println(err)
276         }
277 }
278
279 func removeif(sdl *sdlgo.SyncStorage) {
280         vec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
281                 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x88}
282         start := time.Now()
283         _, err := sdl.RemoveIf("tag1", "key1", vec)
284         elapsed := time.Since(start)
285         if err == nil {
286                 fmt.Printf("Removeif: %s\n", elapsed)
287         } else {
288                 fmt.Println(err)
289         }
290 }