forked from SoftwareAG/adabas-go-api
/
main.go
349 lines (316 loc) · 8.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright © 2018-2022 Software AG, Darmstadt, Germany and/or its licensors
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// Query application to read data in Adabas using Adabas Map references.
// It can start a read using multiple threads and loops.
// A search string can restrict the query to a specific case.
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"runtime"
"runtime/pprof"
"strings"
"sync"
"time"
"github.com/tknie/adabas-go-api/adabas"
"github.com/tknie/adabas-go-api/adatypes"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type caller struct {
url string
counter int
mapName string
name string
search string
fields string
threadNr uint32
limit uint64
credentials string
}
var wg sync.WaitGroup
var output = false
var close = false
func (c caller) createConnection() (*adabas.Connection, error) {
connStr := fmt.Sprintf("acj;map;auth=NONE,id=%d,user=user%04d", c.threadNr, c.threadNr)
connection, err := adabas.NewConnection(connStr)
if err != nil {
fmt.Println("Open connection error", err)
return nil, err
}
return connection, nil
}
func callAdabas(c caller) {
defer wg.Done()
var connection *adabas.Connection
var err error
if !close {
connection, err = c.createConnection()
if err != nil {
fmt.Printf("Error create connection to thread %d\n", c.threadNr)
return
}
defer connection.Close()
if c.credentials != "" {
c := strings.Split(c.credentials, ":")
if len(c) != 2 {
fmt.Printf("User credentials invalid format")
return
}
connection.AddCredential(c[0], c[1])
}
}
steps := c.counter / 10
if c.counter < 50 || steps == 0 {
steps = 50
}
maxTime := 1.0
last := time.Now()
dumpMessage := true
for i := 0; i < c.counter; i++ {
if close {
connection, err = c.createConnection()
if err != nil {
fmt.Printf("Error create connection to thread %d\n", c.threadNr)
return
}
if c.credentials != "" {
c := strings.Split(c.credentials, ":")
if len(c) != 2 {
fmt.Printf("User credentials invalid format")
return
}
connection.AddCredential(c[0], c[1])
}
err = connection.Open()
if err != nil {
fmt.Printf("Error opening connection to thread %d\n", c.threadNr)
return
}
}
readRequest, rerr := connection.CreateMapReadRequest(c.mapName)
if rerr != nil {
fmt.Println("Error creating read reference of database:", rerr)
return
}
readRequest.Limit = c.limit
err = readRequest.QueryFields(c.fields)
if err != nil {
fmt.Println("Error query fields of database file:", err)
return
}
newTime := time.Now()
diff := newTime.Sub(last)
//fmt.Println(diff.Minutes())
if (i > 0 && i%steps == 0) || (diff.Minutes() > maxTime) {
if diff.Minutes() > maxTime {
maxTime += 1.0
}
fmt.Printf("Call thread %d counter %d query for %s used %v\n", c.threadNr,
i, c.name, diff)
// last = newTime
}
var result *adabas.Response
switch {
case c.search != "":
if dumpMessage {
fmt.Println("Search for ", c.search)
dumpMessage = false
}
result, err = readRequest.ReadLogicalWith(c.search)
if err != nil {
fmt.Printf("Error reading thread %d with %d loops: %v\n", c.threadNr, i, err)
return
}
case c.name != "":
if dumpMessage {
fmt.Println("Order by ", c.name)
dumpMessage = false
}
result, err = readRequest.ReadLogicalBy(c.name)
if err != nil {
fmt.Printf("Error reading thread %d with %d loops: %v\n", c.threadNr, i, err)
return
}
default:
if dumpMessage {
fmt.Println("Physical read")
dumpMessage = false
}
result, err = readRequest.ReadPhysicalSequence()
if err != nil {
fmt.Printf("Error reading thread %d with %d loops: %v\n", c.threadNr, i, err)
return
}
}
if output {
fmt.Printf("Result of query search=%s descriptor=%s and fields=%s\n", c.search, c.name, c.fields)
result.DumpValues()
}
if close {
connection.Close()
} else {
connection.Release()
}
}
fmt.Printf("Finish thread %d with %d loops\n", c.threadNr, c.counter)
}
func initLogLevelWithFile(fileName string, level zapcore.Level) (err error) {
p := os.Getenv("LOGPATH")
if p == "" {
p = "."
}
name := p + string(os.PathSeparator) + fileName
rawJSON := []byte(`{
"level": "error",
"encoding": "console",
"outputPaths": [ "XXX"],
"errorOutputPaths": ["stderr"],
"encoderConfig": {
"messageKey": "message",
"levelKey": "level",
"levelEncoder": "lowercase"
}
}`)
var cfg zap.Config
if err := json.Unmarshal(rawJSON, &cfg); err != nil {
fmt.Printf("Initial logging JSON configuration error: %v\n", err)
os.Exit(1)
}
cfg.Level.SetLevel(level)
cfg.OutputPaths = []string{name}
logger, err := cfg.Build()
if err != nil {
fmt.Printf("Initial logging error: %v\n", err)
os.Exit(1)
}
defer logger.Sync()
sugar := logger.Sugar()
sugar.Infof("Start logging with level %v", level)
adatypes.Central.Log = sugar
return
}
func main() {
level := zapcore.ErrorLevel
ed := os.Getenv("ENABLE_DEBUG")
switch ed {
case "1":
level = zapcore.DebugLevel
adatypes.Central.SetDebugLevel(true)
case "2":
level = zapcore.InfoLevel
}
err := initLogLevelWithFile("querym.log", level)
if err != nil {
fmt.Printf("Error opening log file: %v\n", err)
return
}
defer TimeTrack(time.Now(), "Done testsuite test")
var countValue int
var threadValue int
var repository string
var limit int
var name string
var search string
var fields string
var showMaps bool
var showMap bool
var credentials string
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
//flag.StringVar(&gopherType, "gopher_type", defaultGopher, usage)
flag.IntVar(&countValue, "c", 1, "Number of loops [default 1]")
flag.IntVar(&threadValue, "t", 1, "Number of threads [default 1]")
flag.IntVar(&limit, "l", 10, "Number of records maximal read [default 10]")
flag.StringVar(&name, "n", "", "Read descriptor order")
flag.StringVar(&search, "s", "", "Search request like 'name=SMITH'")
flag.StringVar(&credentials, "p", "", "Define user and password credentials of type 'user:password'")
flag.StringVar(&fields, "d", "", "Query field list like 'name,personnel-id', '*' queries all fields, [default is '']")
flag.StringVar(&repository, "r", "", "Adabas map repository used for search. Need to be defined using '<db target>,fnr'")
flag.BoolVar(&output, "o", false, "display output")
flag.BoolVar(&showMaps, "M", false, "List all maps available")
flag.BoolVar(&showMap, "m", false, "List all map fields of given map")
flag.BoolVar(&close, "C", false, "Close Adabas connection in each loop")
flag.Parse()
args := flag.Args()
if !showMaps && len(args) < 1 {
fmt.Printf("Usage: %s <map name>\n", os.Args[0])
flag.PrintDefaults()
return
}
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
panic("could not create CPU profile: " + err.Error())
}
if err := pprof.StartCPUProfile(f); err != nil {
panic("could not start CPU profile: " + err.Error())
}
defer pprof.StopCPUProfile()
}
names := strings.Split(name, ",")
err = adabas.AddGlobalMapRepositoryReference(repository)
if err != nil {
fmt.Printf("Error repository: %v\n", err)
os.Exit(1)
}
if showMaps {
adabas.DumpGlobalMapRepositories()
if len(args) < 1 {
return
}
}
if showMap {
id := adabas.NewAdabasID()
m, _, merr := adabas.SearchMapRepository(id, args[0])
if merr != nil {
fmt.Println("Searched map not found", merr)
return
}
fmt.Println(m.String())
}
wg.Add(threadValue)
for i := uint32(0); i < uint32(threadValue); i++ {
fmt.Printf("Start thread %d/%d\n", i+1, threadValue)
c := caller{mapName: args[0], counter: countValue, threadNr: i + 1,
name: names[int(i)%len(names)], limit: uint64(limit), search: search,
fields: fields, credentials: credentials}
go callAdabas(c)
}
wg.Wait()
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
panic("could not create memory profile: " + err.Error())
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
panic("could not write memory profile: " + err.Error())
}
defer f.Close()
fmt.Println("Wrote memory profiler data")
}
}
// TimeTrack logger
func TimeTrack(start time.Time, name string) {
elapsed := time.Since(start)
fmt.Printf("%s took %s\n", name, elapsed)
}