forked from CiscoDevNet/bigmuddy-network-telemetry-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_gpb_test.go
602 lines (503 loc) · 13.7 KB
/
codec_gpb_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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//
// August 2016, cisco
//
// Copyright (c) 2016 by cisco Systems, Inc.
// All rights reserved.
//
//
package main
import (
"bytes"
"encoding/json"
"fmt"
samples "github.com/cisco/bigmuddy-network-telemetry-pipeline/mdt_msg_samples"
telem "github.com/cisco/bigmuddy-network-telemetry-proto/proto_go"
"github.com/dlintw/goconf"
"github.com/golang/protobuf/jsonpb"
"os"
"reflect"
"strings"
"testing"
)
func TestCodecGPB2JSON(t *testing.T) {
var targetString, targetNumber telem.Telemetry
var generic interface{}
source := &telem.Telemetry{
CollectionId: 1234567890,
}
marshallerEmitNumber := &jsonpb.Marshaler{
EmitUInt64Unquoted: true,
EmitDefaults: true,
OrigName: true,
}
marshallerEmitString := &jsonpb.Marshaler{
EmitUInt64Unquoted: false,
EmitDefaults: true,
OrigName: true,
}
jsonNumber, err := marshallerEmitNumber.MarshalToString(source)
if err != nil {
t.Fatalf("Failed to jsonify (uint64 -> number) [%v]", err)
}
fmt.Println("jsonNumber: ", jsonNumber)
jsonString, err := marshallerEmitString.MarshalToString(source)
if err != nil {
t.Fatalf("Failed to jsonify (uint64 -> string) [%v]", err)
}
fmt.Println("jsonString: ", jsonString)
Unmarshaler := &jsonpb.Unmarshaler{AllowUnknownFields: true}
err = Unmarshaler.Unmarshal(bytes.NewBuffer([]byte(jsonString)),
&targetString)
if err != nil {
t.Fatalf("Failed to unjsonify (uint64 -> string) [%v]", err)
}
err = Unmarshaler.Unmarshal(bytes.NewBuffer([]byte(jsonNumber)),
&targetNumber)
if err != nil {
t.Fatalf("Failed to unjsonify (uint64 -> number) [%v]", err)
}
//
// Compare source and targets
if targetNumber.CollectionId != targetString.CollectionId ||
targetString.CollectionId != source.CollectionId {
t.Fatalf("Extracted differences between source, target String/Number")
}
//
// Generic decoding is where EmitUInt64Unquoted is useful
err = json.Unmarshal([]byte(jsonString), &generic)
if err != nil {
t.Fatalf("Failed generic unmarshal (uint64 -> string) [%v]", err)
}
genericMap := generic.(map[string]interface{})
v := genericMap["collection_id"]
switch v.(type) {
case string:
//
// as expected
default:
t.Fatalf("Failed unmarshal unexpected type (uint64 -> string): %T [%v]",
v, err)
}
//
// Generic decoding is where EmitUInt64Unquoted is useful
err = json.Unmarshal([]byte(jsonNumber), &generic)
if err != nil {
t.Fatalf("Failed generic unmarshal (uint64 -> number) [%v]", err)
}
genericMap = generic.(map[string]interface{})
v = genericMap["collection_id"]
switch v.(type) {
case float64:
//
// as expected
default:
t.Fatalf("Failed unmarshal unexpected type (uint64 -> number): %T", v)
}
}
type gpbTestControlTemplate struct {
t *testing.T
codec codec
streamSpec *dataMsgStreamSpec
}
func (m *gpbTestControlTemplate) String() string {
return "codec_gpb_test_template"
}
func codecGPBProcessSampleTemplate(
sample *samples.SampleTelemetryTableEntry,
context samples.MDTContext) (abort bool) {
control := context.(gpbTestControlTemplate)
err, msgs := control.codec.blockToDataMsgs(&control, sample.SampleStreamGPB)
if err != nil {
fmt.Printf("Failed to extract messages from GPB stream: [%v]\n", err)
}
for _, msg := range msgs {
err, b := msg.produceByteStream(control.streamSpec)
if err != nil {
control.t.Fatal(err)
}
fmt.Printf("%s", string(b))
}
return false
}
func TestCodecGPBTemplate(t *testing.T) {
var nc nodeConfig
err, codec := getNewCodecGPB("test", ENCODING_GPB)
if err != nil {
t.Fatalf("Failed to get a GPB codec to test [%v]", err)
}
nc.config, err = goconf.ReadConfigFile("pipeline_test.conf")
if err != nil {
t.Fatalf("Failed to get template config [%v]", err)
}
err, streamSpec := dataMsgStreamSpecFromConfig(nc, "templatetest")
if err != nil {
t.Fatalf("Failed to parse template [%v]", err)
}
control := gpbTestControlTemplate{
t: t,
codec: codec,
streamSpec: streamSpec,
}
count := samples.MDTSampleTelemetryTableIterate(
samples.SAMPLE_TELEMETRY_DATABASE_BASIC,
codecGPBProcessSampleTemplate,
control)
if count == 0 {
t.Errorf("No iterations run")
}
}
type metricsTestOutputHandler struct {
t *testing.T
b *testing.B
buf *bytes.Buffer
codec codec
spec *metricsSpec
}
// Issue test or benchmark failure
func (m *metricsTestOutputHandler) errorFn(failure string) {
if m.t != nil {
m.t.Errorf(failure)
} else if m.b != nil {
m.b.Errorf(failure)
}
}
//
// Test constrains symbols in sensor name
func (p *metricsTestOutputHandler) adaptSensorName(name string) string {
return name
}
//
// Test constrains symbols in tag names
func (p *metricsTestOutputHandler) adaptTagName(name string) string {
return name
}
func (p *metricsTestOutputHandler) flushMetric(
tags []metricsAtom,
ts uint64,
buf metricsOutputContext) {
}
func (p *metricsTestOutputHandler) buildMetric(
tags []metricsAtom,
sensor metricsAtom,
ts uint64,
context metricsOutputContext) {
if p.buf == nil {
// Benchmarking. Bail out asap
return
}
buf := context.(*bytes.Buffer)
buf.WriteString(
fmt.Sprintf("{\"tags\": \"%v\", \"metrics\":\"%v\",\"ts\":\"%v\"}\n",
tags, sensor, ts))
}
func (p *metricsTestOutputHandler) setupWorkers(m *metricsOutputModule) {
}
func (m *metricsTestOutputHandler) String() string {
return "codec_gpb_benchmark"
}
func codecGPBExtractMetrics(
sample *samples.SampleTelemetryTableEntry,
context samples.MDTContext) (abort bool) {
c := context.(*metricsTestOutputHandler)
err, msgs := c.codec.blockToDataMsgs(c, sample.SampleStreamGPB)
if err != nil {
c.errorFn(
fmt.Sprintf(
"Failed to extract messages from GPB metrics: [%v]\n", err))
}
for _, msg := range msgs {
err = msg.produceMetrics(c.spec, c, c.buf)
if err != nil {
c.errorFn(
fmt.Sprintf(
"Error extract metric: %s [%v]\n",
msg.getDataMsgDescription(), err))
}
}
return false
}
func TestCodecGPBMetrics(t *testing.T) {
var nc nodeConfig
err, codec := getNewCodecGPB("test", ENCODING_GPB)
if err != nil {
t.Errorf("Failed to get a GPB codec to test")
}
//
// Will produce metrics from samples, into buf and then compare
// output against expected outcome.
c := &metricsTestOutputHandler{
t: t,
buf: new(bytes.Buffer),
codec: codec,
}
testOutputHandler = c
nc.config, err = goconf.ReadConfigFile("pipeline_test.conf")
if err != nil {
t.Errorf("Metrics GPB codec, pipeline test config missing\n")
}
m := &metricsOutputModule{}
m.configure("mymetricstest", nc)
c.spec = &m.inputSpec
samples.MDTSampleTelemetryTableIterate(
samples.SAMPLE_TELEMETRY_DATABASE_BASIC,
codecGPBExtractMetrics, c)
expect := samples.MDTLoadMetrics()
if len(expect) == 0 {
t.Errorf("Failed to load expected results\n")
}
cmp := strings.Compare(c.buf.String(), expect)
if cmp != 0 {
t.Errorf("Metrics GPB codec, pipeline test comp failed\n")
t.Errorf("Found:\n%v\n", c.buf.String())
t.Errorf("Expected:\n%v\n", samples.MDTLoadMetrics())
// If you are sure results are good... rewrite results file
//ioutil.WriteFile("mdt_msg_samples/dump.metrics",
// c.buf.Bytes(), os.ModePerm)
}
}
type gpbTestControl struct {
t *testing.T
b *testing.B
codec codec
produceByteStream bool
validate bool
streamType dataMsgStreamType
}
// compareJSON - compares content by unmarshaling to a map and using
// reflection to deep compare the map.
func compareJSON(a []byte, b []byte) bool {
type mapping map[string]interface{}
var map_a = make(mapping)
var map_b = make(mapping)
err := json.Unmarshal(a, &map_a)
if err != nil {
return false
}
err = json.Unmarshal(b, &map_b)
if err != nil {
return false
}
fmt.Printf("\n===A===\n%+v\n", map_a)
fmt.Printf("\n===B===\n%+v\n", map_b)
return reflect.DeepEqual(map_a, map_b)
}
// Issue test or benchmark failure
func (g *gpbTestControl) errorFn(failure string) {
if g.t != nil {
g.t.Errorf(failure)
} else if g.b != nil {
g.b.Errorf(failure)
}
}
func (g *gpbTestControl) validateFn(
sample *samples.SampleTelemetryTableEntry,
b []byte) error {
switch g.streamType {
case dMStreamGPB, dMStreamMsgDefault:
// Compare b to sample
if !bytes.Equal(b, sample.SampleStreamGPB) {
return fmt.Errorf("Original stream and produced stream did not match")
}
case dMStreamJSON:
if false {
//
// If we are sure things are working as expected write the
// content.
f, err := os.OpenFile("mdt_msg_samples/dump.json",
os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer f.Close()
if _, err = f.Write(b); err != nil {
panic(err)
}
} else {
if sample.SampleStreamJSON != nil {
if !strings.Contains(string(b), string(sample.SampleStreamJSON)) &&
!compareJSON(b, sample.SampleStreamJSON) {
return fmt.Errorf("JSON stream and produced JSON did not match")
}
}
}
case dMStreamJSONEvents:
events := strings.Count(string(b), "ontent")
if events != sample.Events {
return fmt.Errorf("Count of events [%d] does not match original [%d]",
events, sample.Events)
}
default:
}
return nil
}
func (m *gpbTestControl) String() string {
return "codec_gpb_test"
}
func codecGPBProcessSampleDescribe(
sample *samples.SampleTelemetryTableEntry,
context samples.MDTContext) (abort bool) {
control := context.(gpbTestControl)
fmt.Printf("===Sample start:\n Objects %d, Leaves %d\n", sample.Events, sample.Leaves)
err, msgs := control.codec.blockToDataMsgs(&control, sample.SampleStreamGPB)
if err != nil {
fmt.Printf("Failed to extract messages from GPB stream: [%v]\n", err)
}
for _, msg := range msgs {
fmt.Printf(" Msg: %s\n", msg.getDataMsgDescription())
}
fmt.Printf("===Sample End\n")
return false
}
func codecGPBProcessSample(
sample *samples.SampleTelemetryTableEntry,
context samples.MDTContext) (abort bool) {
control := context.(gpbTestControl)
err, msgs := control.codec.blockToDataMsgs(&control, sample.SampleStreamGPB)
if err != nil {
fmt.Printf("Failed to extract messages from GPB stream: [%v]\n", err)
}
if control.produceByteStream {
for _, msg := range msgs {
err, b := msg.produceByteStream(
&dataMsgStreamSpec{streamType: control.streamType})
if err != nil {
if control.t != nil {
control.errorFn(
fmt.Sprintf("Failed to produce byte stream for msg of type %v: [%v]\n",
control.streamType,
err))
}
}
if len(b) == 0 {
control.t.Errorf("Zero length byte stream\n")
}
if control.validate {
err = control.validateFn(sample, b)
if err != nil {
control.errorFn(fmt.Sprintf("Validation failed: [%v]", err))
}
}
}
}
return false
}
func TestCodecGPBDescribe(t *testing.T) {
err, codec := getNewCodecGPB("test", ENCODING_GPB)
if err != nil {
t.Errorf("Failed to get a GPB codec to test")
}
control := gpbTestControl{
t: t,
codec: codec,
produceByteStream: false,
}
samples.MDTSampleTelemetryTableIterate(
samples.SAMPLE_TELEMETRY_DATABASE_BASIC,
codecGPBProcessSampleDescribe,
control)
}
func TestCodecGPBBasic(t *testing.T) {
err, codec := getNewCodecGPB("test", ENCODING_GPB)
if err != nil {
t.Errorf("Failed to get a GPB codec to test")
}
var permutations = []struct {
produce bool
validate bool
streamType dataMsgStreamType
}{
{false, false, dMStreamGPB},
{true, false, dMStreamGPB},
{true, true, dMStreamGPB},
{true, true, dMStreamJSONEvents},
{true, true, dMStreamJSON},
}
for _, p := range permutations {
control := gpbTestControl{
t: t,
codec: codec,
produceByteStream: p.produce,
validate: p.validate,
streamType: p.streamType,
}
count := samples.MDTSampleTelemetryTableIterate(
samples.SAMPLE_TELEMETRY_DATABASE_BASIC,
codecGPBProcessSample,
control)
if count == 0 {
t.Errorf("No iterations run")
}
}
}
func codecGPBBenchmarkHelper(b *testing.B, control gpbTestControl) {
var err error
err, control.codec = getNewCodecGPB("test", ENCODING_GPB)
if err != nil {
b.Errorf("Failed to get a GPB codec to test")
}
control.b = b
//
// Start benchmark clock now
b.ResetTimer()
for n := 0; n < b.N; n++ {
count := samples.MDTSampleTelemetryTableIterate(
samples.SAMPLE_TELEMETRY_DATABASE_BASIC,
codecGPBProcessSample,
control)
if count == 0 {
b.Errorf("No iterations run")
}
}
}
func BenchmarkCodecGPBCreateDM(b *testing.B) {
codecGPBBenchmarkHelper(b, gpbTestControl{
produceByteStream: false,
validate: false,
streamType: dMStreamGPB,
})
}
func BenchmarkCodecGPBCreateDMProduceGPB(b *testing.B) {
codecGPBBenchmarkHelper(b, gpbTestControl{
produceByteStream: true,
validate: false,
streamType: dMStreamGPB,
})
}
func BenchmarkCodecGPBCreateDMProduceJSON(b *testing.B) {
codecGPBBenchmarkHelper(b, gpbTestControl{
produceByteStream: true,
validate: false,
streamType: dMStreamJSON,
})
}
func BenchmarkCodecGPBCreateDMProduceJSONEvents(b *testing.B) {
codecGPBBenchmarkHelper(b, gpbTestControl{
produceByteStream: true,
validate: false,
streamType: dMStreamJSONEvents,
})
}
func BenchmarkCodecGPBMetrics(b *testing.B) {
var nc nodeConfig
err, codec := getNewCodecGPB("test", ENCODING_GPB)
if err != nil {
b.Errorf("Failed to get a GPB codec to test")
}
c := &metricsTestOutputHandler{
codec: codec,
}
testOutputHandler = c
nc.config, err = goconf.ReadConfigFile("pipeline_test.conf")
if err != nil {
b.Errorf("Metrics GPB codec, pipeline test config missing\n")
}
m := &metricsOutputModule{}
m.configure("mymetricstest", nc)
c.spec = &m.inputSpec
b.ResetTimer()
for n := 0; n < b.N; n++ {
samples.MDTSampleTelemetryTableIterate(
samples.SAMPLE_TELEMETRY_DATABASE_BASIC,
codecGPBExtractMetrics, c)
}
}