-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_test.go
350 lines (323 loc) · 8.75 KB
/
main_test.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
350
package main
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"sync"
"testing"
"time"
"github.com/globusdigital/logbench/benchmark"
"github.com/stretchr/testify/require"
)
// SyncBuffer is a thread-safe buffer implementing the io.ReadWriter interface
type SyncBuffer struct {
m sync.Mutex
b bytes.Buffer
}
func (b *SyncBuffer) Read(p []byte) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.Read(p)
}
func (b *SyncBuffer) Write(p []byte) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.Write(p)
}
func (b *SyncBuffer) String() string {
b.m.Lock()
defer b.m.Unlock()
return b.b.String()
}
// FV represents a mapping between field names
// and the according list validators
type FV map[string]func(interface{}) error
func expectString(actual interface{}) (string, error) {
val, ok := actual.(string)
if !ok {
return "", fmt.Errorf(
"unexpected field type (expected: string; got: %s)",
reflect.TypeOf(actual),
)
}
return val, nil
}
func expectBool(actual interface{}) (bool, error) {
val, ok := actual.(bool)
if !ok {
return false, fmt.Errorf(
"unexpected field type (expected: bool; got: %s)",
reflect.TypeOf(actual),
)
}
return val, nil
}
func expectFloat64(actual interface{}) (float64, error) {
val, ok := actual.(float64)
if !ok {
return 0, fmt.Errorf(
"unexpected field type (expected: int; got: %s)",
reflect.TypeOf(actual),
)
}
return val, nil
}
func validateTime(actual interface{}) error {
val, err := expectString(actual)
if err != nil {
return err
}
_, err = time.Parse(time.RFC3339, val)
return err
}
func newValidatorLevel(expected string) func(interface{}) error {
return func(actual interface{}) error {
val, err := expectString(actual)
if err != nil {
return err
}
if val != expected {
return fmt.Errorf(
"mismatching level: (expected: %q, got: %q)",
expected,
val,
)
}
return nil
}
}
func newValidatorText(expected string) func(interface{}) error {
return func(actual interface{}) error {
val, err := expectString(actual)
if err != nil {
return err
}
if val != expected {
return fmt.Errorf(
"mismatching text: (expected: %q, got: %q)",
expected,
val,
)
}
return nil
}
}
func newValidatorBool(expected bool) func(interface{}) error {
return func(actual interface{}) error {
val, err := expectBool(actual)
if err != nil {
return err
}
if val != expected {
return fmt.Errorf("mismatching bool: (expected: %t)", expected)
}
return nil
}
}
func newValidatorInt(expected int) func(interface{}) error {
return func(actual interface{}) error {
val, err := expectFloat64(actual)
if err != nil {
return err
}
if val != float64(expected) {
return fmt.Errorf(
"mismatching int: (expected: %d, got: %f)",
expected,
val,
)
}
return nil
}
}
func newValidatorFloat64(expected float64) func(interface{}) error {
return func(actual interface{}) error {
val, err := expectFloat64(actual)
if err != nil {
return err
}
if val != expected {
return fmt.Errorf(
"mismatching float64: (expected: %f, got: %f)",
expected,
val,
)
}
return nil
}
}
func newValidatorStrings(expected []string) func(interface{}) error {
return func(actual interface{}) error {
val, ok := actual.([]interface{})
if !ok {
return fmt.Errorf(
"unexpected field type (expected: []string; got: %s)",
reflect.TypeOf(actual),
)
}
for i, val := range val {
val, err := expectString(val)
if err != nil {
return err
}
expected := expected[i]
if expected != val {
return fmt.Errorf(
"mismatching array item: (expected: %s, got: %s)",
expected,
val,
)
}
}
return nil
}
}
func newValidatorInts(expected []int) func(interface{}) error {
return func(actual interface{}) error {
val, ok := actual.([]interface{})
if !ok {
return fmt.Errorf(
"unexpected field type (expected: []int; got: %s)",
reflect.TypeOf(actual),
)
}
for i, val := range val {
val, err := expectFloat64(val)
if err != nil {
return err
}
expected := expected[i]
if float64(expected) != val {
return fmt.Errorf(
"mismatching array item: (expected: %d, got: %f)",
expected,
val,
)
}
}
return nil
}
}
func newValidatorFloat64s(expected []float64) func(interface{}) error {
return func(actual interface{}) error {
val, ok := actual.([]interface{})
if !ok {
return fmt.Errorf(
"unexpected field type (expected: []interface{}; got: %s)",
reflect.TypeOf(actual),
)
}
for i, val := range val {
val, err := expectFloat64(val)
if err != nil {
return err
}
expected := expected[i]
if float64(expected) != val {
return fmt.Errorf(
"mismatching array item: (expected: %f, got: %f)",
expected,
val,
)
}
}
return nil
}
}
func TestFormat(t *testing.T) {
fields3 := benchmark.NewFields3()
fields10 := benchmark.NewFields10()
fieldValidators := map[string]FV{
benchmark.LogOperationInfo: {
benchmark.FieldTime: validateTime,
benchmark.FieldLevel: newValidatorLevel(benchmark.LevelInfo),
benchmark.FieldMessage: newValidatorText("information"),
},
benchmark.LogOperationInfoFmt: {
benchmark.FieldTime: validateTime,
benchmark.FieldLevel: newValidatorLevel(benchmark.LevelInfo),
benchmark.FieldMessage: newValidatorText("information 42"),
},
benchmark.LogOperationInfoWithErrorStack: {
benchmark.FieldTime: validateTime,
benchmark.FieldLevel: newValidatorLevel(benchmark.LevelInfo),
benchmark.FieldMessage: newValidatorText("information"),
benchmark.FieldError: newValidatorText("error with stack trace"),
},
benchmark.LogOperationError: {
benchmark.FieldTime: validateTime,
benchmark.FieldLevel: newValidatorLevel(benchmark.LevelError),
benchmark.FieldMessage: newValidatorText("error message"),
},
benchmark.LogOperationInfoWith3: {
benchmark.FieldTime: validateTime,
benchmark.FieldLevel: newValidatorLevel(benchmark.LevelInfo),
benchmark.FieldMessage: newValidatorText("information"),
fields3.Name1: newValidatorText(fields3.Value1),
fields3.Name2: newValidatorInt(fields3.Value2),
fields3.Name3: newValidatorFloat64(fields3.Value3),
},
benchmark.LogOperationInfoWith10: {
benchmark.FieldTime: validateTime,
benchmark.FieldLevel: newValidatorLevel(benchmark.LevelInfo),
benchmark.FieldMessage: newValidatorText("information"),
fields10.Name1: newValidatorText(fields10.Value1),
fields10.Name2: newValidatorText(fields10.Value2),
fields10.Name3: newValidatorText(fields10.Value3),
fields10.Name4: newValidatorBool(fields10.Value4),
fields10.Name5: newValidatorText(fields10.Value5),
fields10.Name6: newValidatorInt(fields10.Value6),
fields10.Name7: newValidatorFloat64(fields10.Value7),
fields10.Name8: newValidatorStrings(fields10.Value8),
fields10.Name9: newValidatorInts(fields10.Value9),
fields10.Name10: newValidatorFloat64s(fields10.Value10),
},
benchmark.LogOperationInfoWith10Exist: {
benchmark.FieldTime: validateTime,
benchmark.FieldLevel: newValidatorLevel(benchmark.LevelInfo),
benchmark.FieldMessage: newValidatorText("information"),
fields10.Name1: newValidatorText(fields10.Value1),
fields10.Name2: newValidatorText(fields10.Value2),
fields10.Name3: newValidatorText(fields10.Value3),
fields10.Name4: newValidatorBool(fields10.Value4),
fields10.Name5: newValidatorText(fields10.Value5),
fields10.Name6: newValidatorInt(fields10.Value6),
fields10.Name7: newValidatorFloat64(fields10.Value7),
fields10.Name8: newValidatorStrings(fields10.Value8),
fields10.Name9: newValidatorInts(fields10.Value9),
fields10.Name10: newValidatorFloat64s(fields10.Value10),
},
}
for loggerName, initFn := range setups {
t.Run(loggerName, func(t *testing.T) {
for operationName, validators := range fieldValidators {
t.Run(operationName, func(t *testing.T) {
buf := new(SyncBuffer)
bench, err := benchmark.New(buf, operationName, initFn)
require.NoError(t, err)
stats := bench.Run(1, 1, nil)
require.Equal(t, uint64(1), stats.TotalLogsWritten)
var fields map[string]interface{}
dec := json.NewDecoder(buf)
require.NoError(
t,
dec.Decode(&fields),
"decoding JSON for logger %q",
loggerName,
)
for requiredField, validate := range validators {
require.Contains(t, fields, requiredField)
require.NoError(
t,
validate(fields[requiredField]),
"invalid value for field %q of logger %q",
requiredField,
loggerName,
)
}
})
}
})
}
}