forked from SAP/go-hdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testhandler.go
261 lines (214 loc) · 5.29 KB
/
testhandler.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
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"net/http"
"runtime"
"sync"
"time"
"github.com/SAP/go-hdb/driver"
)
// Test URL paths.
const (
testSeq = "/test/Seq"
testPar = "/test/Par"
)
// testResult is the structure used to provide the JSON based test result response.
type testResult struct {
Test string
Seconds float64
BatchCount int
BatchSize int
BulkSize int
Duration time.Duration
Error string
}
func (r *testResult) String() string {
if r.Error != "" {
return r.Error
}
return fmt.Sprintf("%s: insert of %d rows in %f seconds (batchCount %d batchSize %d bulkSize %d)", r.Test, r.BatchCount*r.BatchSize, r.Duration.Seconds(), r.BatchCount, r.BatchSize, r.BulkSize)
}
// newTestResult returns a TestResult based on the JSON load provided by a HTTP response.
func newTestResult(r *http.Response) (*testResult, error) {
result := &testResult{}
if err := json.NewDecoder(r.Body).Decode(result); err != nil {
return nil, err
}
return result, nil
}
type testFunc func(db *sql.DB, batchCount, batchSize int, drop bool, wait time.Duration) (time.Duration, error)
// testHandler implements the http.Handler interface for the tests.
type testHandler struct {
log func(format string, v ...any)
testFuncs map[string]testFunc
}
// newTestHandler returns a new TestHandler instance.
func newTestHandler(log func(format string, v ...any)) (*testHandler, error) {
h := &testHandler{log: log}
h.testFuncs = map[string]testFunc{
testSeq: h.testSeq,
testPar: h.testPar,
}
return h, nil
}
func (h *testHandler) tests() []string {
// need correct sort order
return []string{testSeq, testPar}
}
const (
defBatchCount = 10
defBatchSize = 10000
)
func (h *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Try to get a comparable environment for each run
// by clearing garbage from previous runs.
runtime.GC()
q := newURLQuery(r)
batchCount := q.getInt(urlQueryBatchCount, defBatchCount)
batchSize := q.getInt(urlQueryBatchSize, defBatchSize)
waitDuration := time.Duration(wait) * time.Second
test := r.URL.Path
result := &testResult{Test: test, BatchCount: batchCount, BatchSize: batchSize}
defer func() {
h.log("%s", result)
e := json.NewEncoder(w)
e.Encode(result) // ignore error
}()
db, bulkSize, err := h.setup(batchSize)
if err != nil {
result.Error = err.Error()
return
}
defer h.teardown(db)
var d time.Duration
if f, ok := h.testFuncs[test]; ok {
if err = ensureSchemaTable(db); err == nil {
d, err = f(db, batchCount, batchSize, drop, waitDuration)
}
} else {
err = fmt.Errorf("invalid test %s", test)
}
result.BulkSize = bulkSize
result.Duration = d
result.Seconds = d.Seconds()
if err != nil {
result.Error = err.Error()
}
}
func (h *testHandler) testSeq(db *sql.DB, batchCount, batchSize int, drop bool, wait time.Duration) (time.Duration, error) {
numRow := batchCount * batchSize
if wait > 0 {
time.Sleep(wait)
}
conn, err := db.Conn(context.Background())
if err != nil {
return 0, err
}
stmt, err := conn.PrepareContext(context.Background(), prepareQuery)
if err != nil {
return 0, err
}
defer stmt.Close()
var d time.Duration
i := 0
t := time.Now()
_, err = stmt.Exec(func(args []any) error {
if i >= numRow {
return driver.ErrEndOfRows
}
fillRow(i, args)
i++
return nil
})
d += time.Since(t)
if err != nil {
return d, err
}
return d, nil
}
type task struct {
conn *sql.Conn
stmt *sql.Stmt
size int
err error
}
func (t *task) close() {
t.stmt.Close()
t.conn.Close()
}
func (h *testHandler) createTasks(db *sql.DB, batchCount, batchSize int, bulk, drop bool) ([]*task, error) {
var err error
tasks := make([]*task, batchCount)
for i := 0; i < batchCount; i++ {
conn, err := db.Conn(context.Background())
if err != nil {
return nil, err
}
stmt, err := conn.PrepareContext(context.Background(), prepareQuery)
if err != nil {
return nil, err
}
tasks[i] = &task{conn: conn, stmt: stmt, size: batchSize}
}
return tasks, err
}
func (h *testHandler) testPar(db *sql.DB, batchCount, batchSize int, drop bool, wait time.Duration) (time.Duration, error) {
var wg sync.WaitGroup
tasks, err := h.createTasks(db, batchCount, batchSize, true, drop)
if err != nil {
return 0, err
}
if wait > 0 {
time.Sleep(wait)
}
t := time.Now() // Start time.
for i, t := range tasks { // Start one worker per task.
wg.Add(1)
go func(worker int, t *task) {
defer wg.Done()
j := 0
if _, err = t.stmt.Exec(func(args []any) error {
if j >= t.size {
return driver.ErrEndOfRows
}
fillRow(worker*t.size+j, args)
j++
return nil
}); err != nil {
t.err = err
}
}(i, t)
}
wg.Wait()
d := time.Since(t) // Duration.
for _, t := range tasks {
// return last error
err = t.err
t.close()
}
return d, err
}
func (h *testHandler) setup(batchSize int) (*sql.DB, int, error) {
// Set bulk size to batchSize.
ctr, err := driver.NewDSNConnector(dsn)
if err != nil {
return nil, 0, err
}
ctr.SetBulkSize(batchSize)
ctr.SetBufferSize(bufferSize)
if err != nil {
return nil, 0, err
}
return sql.OpenDB(ctr), ctr.BulkSize(), nil
}
func (h *testHandler) teardown(db *sql.DB) {
db.Close()
}
// fillRow fills a table row with values.
func fillRow(idx int, args []any) {
args[0] = idx
args[1] = float64(idx)
}