/
main.go
329 lines (293 loc) · 8.01 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
/*
* Copyright © 2018-2020 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.
*
*/
// Test suite application to read data in Adabas. It provides the possibility
// to define the Adabas file to be used.
// It can start a read using multiple threads and loops.
// A search string can restrict the query to a specific case.
// It can track CPU and memory profiler informations.
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"runtime"
"runtime/pprof"
"strings"
"sync"
"time"
"github.com/SoftwareAG/adabas-go-api/adabas"
"github.com/SoftwareAG/adabas-go-api/adatypes"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type caller struct {
url string
file uint32
counter int
name string
threadNr uint32
credential string
}
var wg sync.WaitGroup
var output = false
var close = false
func displayResult(isn adatypes.Isn, buffer []byte, received uint64) {
if !output {
return
}
helper := adatypes.NewHelper(buffer, int(received), adabas.Endian())
aa, err := helper.ReceiveString(8)
if err != nil {
fmt.Println("Error receiving AA", err)
return
}
ac, err := helper.ReceiveString(20)
if err != nil {
fmt.Println("Error receiving AC", err)
return
}
ad, err := helper.ReceiveString(20)
if err != nil {
fmt.Println("Error receiving AD", err)
return
}
ae, err := helper.ReceiveString(20)
if err != nil {
fmt.Println("Error receiving AE", err)
return
}
as, err := helper.ReceiveInt32()
if err != nil {
fmt.Println("Error receiving AS", err)
return
}
fmt.Printf("ISN=%-4d AA=%s AC=%s AD=%s AE=%s AS=%d\n", isn, aa, ac, ad, ae, as)
}
func (c caller) createConnection() (*adabas.Connection, error) {
connStr := fmt.Sprintf("acj;target=%s;auth=NONE,id=%d,user=user%04d", c.url, 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.credential != "" {
c := strings.Split(c.credential, ":")
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: %v\n", c.threadNr, err)
return
}
}
steps := c.counter / 10
if c.counter < 50 || steps == 0 {
steps = 50
}
maxTime := 1.0
last := time.Now()
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.credential != "" {
c := strings.Split(c.credential, ":")
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.CreateFileReadRequest(adabas.Fnr(c.file))
if rerr != nil {
fmt.Println("Error creating read reference of database:", rerr)
return
}
err = readRequest.QueryFields("AA,AB,AS[N]")
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
result, err = readRequest.ReadLogicalWith("AE=" + c.name)
if err != nil {
fmt.Printf("Error reading thread %d with %d loops: %v\n", c.threadNr, i, err)
return
}
if output {
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 {
panic(err)
}
cfg.Level.SetLevel(level)
cfg.OutputPaths = []string{name}
logger, err := cfg.Build()
if err != nil {
panic(err)
}
defer logger.Sync()
sugar := logger.Sugar()
sugar.Infof("Start logging with level", 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("testsuite.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 file int
var name string
var credential 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")
flag.IntVar(&threadValue, "t", 1, "Number of threads")
flag.StringVar(&name, "n", "SMITH", "Test search for employee names separated by ','")
flag.StringVar(&credential, "p", "", "Define user and password credentials of type 'user:password'")
flag.IntVar(&file, "f", 11, "Adabas file used to read, should be Employees file")
flag.BoolVar(&output, "o", false, "display output")
flag.BoolVar(&close, "C", false, "Close Adabas connection in each loop")
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Printf("Usage: %s <url>\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()
}
defer writeMemProfile(*memprofile)
names := strings.Split(name, ",")
wg.Add(threadValue)
for i := uint32(0); i < uint32(threadValue); i++ {
fmt.Printf("Start thread %d/%d\n", i+1, threadValue)
c := caller{url: args[0], counter: countValue, threadNr: i + 1,
name: names[int(i)%len(names)], file: uint32(file),
credential: credential}
go callAdabas(c)
}
wg.Wait()
}
func writeMemProfile(file string) {
if file != "" {
f, err := os.Create(file)
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("Memory profile written")
}
}
// TimeTrack logger
func TimeTrack(start time.Time, name string) {
elapsed := time.Since(start)
fmt.Printf("%s took %s\n", name, elapsed)
}