Codebase list golang-github-influxdata-line-protocol / upstream/0.0_git20210311.9aa0e37 encoder_test.go
upstream/0.0_git20210311.9aa0e37

Tree @upstream/0.0_git20210311.9aa0e37 (Download .tar.gz)

encoder_test.go @upstream/0.0_git20210311.9aa0e37raw · history · blame

  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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
package protocol

import (
	"bytes"
	"math"
	"sort"
	"testing"
	"time"
)

type MockMetric struct {
	name   string
	tags   []*Tag
	fields []*Field
	t      time.Time
}

func (m *MockMetric) Name() string {
	return m.name
}
func (m *MockMetric) TagList() []*Tag {
	return m.tags
}

func (m *MockMetric) FieldList() []*Field {
	return m.fields
}

func (m *MockMetric) Time() time.Time {
	return m.t
}

func (m *MockMetric) AddTag(k, v string) {
	for i, tag := range m.tags {
		if k == tag.Key {
			m.tags[i].Value = v
			return
		}
	}
	m.tags = append(m.tags, &Tag{Key: k, Value: v})
}

func (m *MockMetric) AddField(k string, v interface{}) {
	for i, field := range m.fields {
		if k == field.Key {
			m.fields[i].Value = v
			return
		}
	}
	m.fields = append(m.fields, &Field{Key: k, Value: convertField(v)})
	sort.Slice(m.fields, func(i, j int) bool {
		return string(m.fields[i].Key) < string(m.fields[j].Key)
	})
}

func NewMockMetric(name string,
	tags map[string]string,
	fields map[string]interface{},
	tm time.Time,
) Metric {
	m := &MockMetric{
		name:   name,
		tags:   nil,
		fields: nil,
		t:      tm,
	}

	if len(tags) > 0 {
		m.tags = make([]*Tag, 0, len(tags))
		for k, v := range tags {
			m.tags = append(m.tags,
				&Tag{Key: k, Value: v})
		}
		sort.Slice(m.tags, func(i, j int) bool { return m.tags[i].Key < m.tags[j].Key })
	}

	m.fields = make([]*Field, 0, len(fields))
	for k, v := range fields {
		v := convertField(v)
		if v == nil {
			continue
		}
		m.AddField(k, v)
	}

	return m
}

var tests = []struct {
	name           string
	maxBytes       int
	typeSupport    FieldTypeSupport
	input          Metric
	output         []byte
	failOnFieldErr bool
	err            error
	precision      time.Duration
}{
	{
		name: "minimal",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"value": 42.0,
			},
			time.Unix(0, 0),
		),
		output: []byte("cpu value=42 0\n"),
	},
	{
		name: "multiple tags",
		input: NewMockMetric(
			"cpu",
			map[string]string{
				"host": "localhost",
				"cpu":  "CPU0",
			},
			map[string]interface{}{
				"value": 42.0,
			},
			time.Unix(0, 0),
		),
		output: []byte("cpu,cpu=CPU0,host=localhost value=42 0\n"),
	},
	{
		name: "multiple fields",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"x": 42.0,
				"y": 42.0,
			},
			time.Unix(0, 0),
		),
		output: []byte("cpu x=42,y=42 0\n"),
	},
	{
		name: "float NaN",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"x": math.NaN(),
				"y": 42,
			},
			time.Unix(0, 0),
		),
		output: []byte("cpu y=42i 0\n"),
	},
	{
		name: "float NaN only",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"value": math.NaN(),
			},
			time.Unix(0, 0),
		),
		err: ErrNoFields,
	},
	{
		name: "float Inf",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"value": math.Inf(1),
				"y":     42,
			},
			time.Unix(0, 0),
		),
		output: []byte("cpu y=42i 0\n"),
	},
	{
		name: "integer field",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"value": 42,
			},
			time.Unix(0, 0),
		),
		output: []byte("cpu value=42i 0\n"),
	},
	{
		name: "integer field 64-bit",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"value": int64(123456789012345),
			},
			time.Unix(0, 0),
		),
		output: []byte("cpu value=123456789012345i 0\n"),
	},
	{
		name: "uint field",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"value": uint64(42),
			},
			time.Unix(0, 0),
		),
		output:      []byte("cpu value=42u 0\n"),
		typeSupport: UintSupport,
	},
	{
		name: "uint field max value",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"value": uint64(18446744073709551615),
			},
			time.Unix(0, 0),
		),
		output:      []byte("cpu value=18446744073709551615u 0\n"),
		typeSupport: UintSupport,
	},
	{
		name: "uint field no uint support",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"value": uint64(42),
			},
			time.Unix(0, 0),
		),
		output: []byte("cpu value=42i 0\n"),
	},
	{
		name: "uint field no uint support overflow",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"value": uint64(18446744073709551615),
			},
			time.Unix(0, 0),
		),
		output: []byte("cpu value=9223372036854775807i 0\n"),
	},
	{
		name: "bool field",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"value": true,
			},
			time.Unix(0, 0),
		),
		output: []byte("cpu value=true 0\n"),
	},
	{
		name: "string field",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"value": "howdy",
			},
			time.Unix(0, 0),
		),
		output: []byte("cpu value=\"howdy\" 0\n"),
	},
	{
		name: "timestamp",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"value": 42.0,
			},
			time.Unix(1519194109, 42),
		),
		output: []byte("cpu value=42 1519194109000000042\n"),
	},
	{
		name:     "split fields exact",
		maxBytes: 33,
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"abc": 123,
				"def": 456,
			},
			time.Unix(1519194109, 42),
		),
		output: []byte("cpu abc=123i 1519194109000000042\ncpu def=456i 1519194109000000042\n"),
	},
	{
		name:     "split fields extra",
		maxBytes: 34,
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"abc": 123,
				"def": 456,
			},
			time.Unix(1519194109, 42),
		),
		output: []byte("cpu abc=123i 1519194109000000042\ncpu def=456i 1519194109000000042\n"),
	},
	{
		name:     "split_fields_overflow",
		maxBytes: 43,
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"abc": 123,
				"def": 456,
				"ghi": 789,
				"jkl": 123,
			},
			time.Unix(1519194109, 42),
		),
		output: []byte("cpu abc=123i,def=456i 1519194109000000042\ncpu ghi=789i,jkl=123i 1519194109000000042\n"),
	},
	{
		name: "name newline",
		input: NewMockMetric(
			"c\npu",
			map[string]string{},
			map[string]interface{}{
				"value": 42,
			},
			time.Unix(0, 0),
		),
		output: []byte("c\\npu value=42i 0\n"),
	},
	{
		name: "tag newline",
		input: NewMockMetric(
			"cpu",
			map[string]string{
				"host": "x\ny",
			},
			map[string]interface{}{
				"value": 42,
			},
			time.Unix(0, 0),
		),
		output: []byte("cpu,host=x\\ny value=42i 0\n"),
	},
	{
		name: "string newline",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"value": "x\ny",
			},
			time.Unix(0, 0),
		),
		output: []byte("cpu value=\"x\\ny\" 0\n"),
	},
	{
		name:     "need more space",
		maxBytes: 32,
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"abc": 123,
				"def": 456,
			},
			time.Unix(1519194109, 42),
		),
		output: nil,
		err:    ErrNeedMoreSpace,
	},
	{
		name: "no fields",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{},
			time.Unix(0, 0),
		),
		err: ErrNoFields,
	},
	{
		name: "procstat",
		input: NewMockMetric(
			"procstat",
			map[string]string{
				"exe":          "bash",
				"process_name": "bash",
			},
			map[string]interface{}{
				"cpu_time":                      0,
				"cpu_time_guest":                float64(0),
				"cpu_time_guest_nice":           float64(0),
				"cpu_time_idle":                 float64(0),
				"cpu_time_iowait":               float64(0),
				"cpu_time_irq":                  float64(0),
				"cpu_time_nice":                 float64(0),
				"cpu_time_soft_irq":             float64(0),
				"cpu_time_steal":                float64(0),
				"cpu_time_stolen":               float64(0),
				"cpu_time_system":               float64(0),
				"cpu_time_user":                 float64(0.02),
				"cpu_usage":                     float64(0),
				"involuntary_context_switches":  2,
				"memory_data":                   1576960,
				"memory_locked":                 0,
				"memory_rss":                    5103616,
				"memory_stack":                  139264,
				"memory_swap":                   0,
				"memory_vms":                    21659648,
				"nice_priority":                 20,
				"num_fds":                       4,
				"num_threads":                   1,
				"pid":                           29417,
				"read_bytes":                    0,
				"read_count":                    259,
				"realtime_priority":             0,
				"rlimit_cpu_time_hard":          2147483647,
				"rlimit_cpu_time_soft":          2147483647,
				"rlimit_file_locks_hard":        2147483647,
				"rlimit_file_locks_soft":        2147483647,
				"rlimit_memory_data_hard":       2147483647,
				"rlimit_memory_data_soft":       2147483647,
				"rlimit_memory_locked_hard":     65536,
				"rlimit_memory_locked_soft":     65536,
				"rlimit_memory_rss_hard":        2147483647,
				"rlimit_memory_rss_soft":        2147483647,
				"rlimit_memory_stack_hard":      2147483647,
				"rlimit_memory_stack_soft":      8388608,
				"rlimit_memory_vms_hard":        2147483647,
				"rlimit_memory_vms_soft":        2147483647,
				"rlimit_nice_priority_hard":     0,
				"rlimit_nice_priority_soft":     0,
				"rlimit_num_fds_hard":           4096,
				"rlimit_num_fds_soft":           1024,
				"rlimit_realtime_priority_hard": 0,
				"rlimit_realtime_priority_soft": 0,
				"rlimit_signals_pending_hard":   78994,
				"rlimit_signals_pending_soft":   78994,
				"signals_pending":               0,
				"voluntary_context_switches":    42,
				"write_bytes":                   106496,
				"write_count":                   35,
			},
			time.Unix(0, 1517620624000000000),
		),
		output: []byte("procstat,exe=bash,process_name=bash cpu_time=0i,cpu_time_guest=0,cpu_time_guest_nice=0,cpu_time_idle=0,cpu_time_iowait=0,cpu_time_irq=0,cpu_time_nice=0,cpu_time_soft_irq=0,cpu_time_steal=0,cpu_time_stolen=0,cpu_time_system=0,cpu_time_user=0.02,cpu_usage=0,involuntary_context_switches=2i,memory_data=1576960i,memory_locked=0i,memory_rss=5103616i,memory_stack=139264i,memory_swap=0i,memory_vms=21659648i,nice_priority=20i,num_fds=4i,num_threads=1i,pid=29417i,read_bytes=0i,read_count=259i,realtime_priority=0i,rlimit_cpu_time_hard=2147483647i,rlimit_cpu_time_soft=2147483647i,rlimit_file_locks_hard=2147483647i,rlimit_file_locks_soft=2147483647i,rlimit_memory_data_hard=2147483647i,rlimit_memory_data_soft=2147483647i,rlimit_memory_locked_hard=65536i,rlimit_memory_locked_soft=65536i,rlimit_memory_rss_hard=2147483647i,rlimit_memory_rss_soft=2147483647i,rlimit_memory_stack_hard=2147483647i,rlimit_memory_stack_soft=8388608i,rlimit_memory_vms_hard=2147483647i,rlimit_memory_vms_soft=2147483647i,rlimit_nice_priority_hard=0i,rlimit_nice_priority_soft=0i,rlimit_num_fds_hard=4096i,rlimit_num_fds_soft=1024i,rlimit_realtime_priority_hard=0i,rlimit_realtime_priority_soft=0i,rlimit_signals_pending_hard=78994i,rlimit_signals_pending_soft=78994i,signals_pending=0i,voluntary_context_switches=42i,write_bytes=106496i,write_count=35i 1517620624000000000\n"),
	},
	{
		name: "error out on field error",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"x": math.NaN(),
				"y": 42,
			},
			time.Unix(0, 0),
		),
		failOnFieldErr: true,
		err:            ErrIsNaN,
	},
	{
		name: "explicit nanoseconds precision",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"x": 3,
				"y": 42.3,
			},
			time.Unix(123456789, 123456789),
		),
		precision: time.Nanosecond,
		output:    []byte("cpu x=3i,y=42.3 123456789123456789\n"),
	},
	{
		name: "microseconds precision",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"x": 3,
				"y": 42.3,
			},
			time.Unix(123456789, 123456789),
		),
		precision: time.Microsecond,
		output:    []byte("cpu x=3i,y=42.3 123456789123456\n"),
	},
	{
		name: "milliseconds precision",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"x": 3,
				"y": 42.3,
			},
			time.Unix(123456789, 123456789),
		),
		precision: time.Millisecond,
		output:    []byte("cpu x=3i,y=42.3 123456789123\n"),
	},
	{
		name: "seconds precision",
		input: NewMockMetric(
			"cpu",
			map[string]string{},
			map[string]interface{}{
				"x": 3,
				"y": 42.3,
			},
			time.Unix(123456789, 123456789),
		),
		precision: time.Second,
		output:    []byte("cpu x=3i,y=42.3 123456789\n"),
	},
}

func TestEncoder(t *testing.T) {
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			buf := &bytes.Buffer{}
			serializer := NewEncoder(buf)
			serializer.SetMaxLineBytes(tt.maxBytes)
			serializer.SetFieldSortOrder(SortFields)
			serializer.SetFieldTypeSupport(tt.typeSupport)
			serializer.FailOnFieldErr(tt.failOnFieldErr)
			serializer.SetPrecision(tt.precision)
			i, err := serializer.Encode(tt.input)
			if tt.err != err {
				t.Fatalf("expected error %v, but got %v", tt.err, err)
			}
			if i != len(buf.Bytes()) {
				t.Fatalf("expected i: %v, but got: %v", len(buf.Bytes()), i)
			}
			if string(tt.output) != buf.String() {
				t.Fatalf("expected output %v, but got %v", tt.output, buf.String())
			}
		})
	}
}

func TestWriter(t *testing.T) {
	type args struct {
		name                        []byte
		ts                          time.Time
		tagKeys, tagVals, fieldKeys [][]byte
		fieldVals                   []interface{}
	}
	btests := make([]struct {
		name           string
		maxBytes       int
		typeSupport    FieldTypeSupport
		failOnFieldErr bool
		fields         args
		err            error
		output         []byte
		precision      time.Duration
	}, len(tests))
	for i, tt := range tests {
		btests[i].name = tt.name
		btests[i].maxBytes = tt.maxBytes
		btests[i].typeSupport = tt.typeSupport
		btests[i].failOnFieldErr = tt.failOnFieldErr
		btests[i].err = tt.err
		btests[i].output = tt.output
		btests[i].precision = tt.precision
		btests[i].fields.name = []byte(tt.input.Name())
		btests[i].fields.ts = tt.input.Time()
		btests[i].fields.fieldKeys, btests[i].fields.fieldVals = fieldsToBytes(tt.input.FieldList())
		btests[i].fields.tagKeys, btests[i].fields.tagVals = tagsToBytes(tt.input.TagList())
	}
	for _, tt := range btests {
		t.Run(tt.name, func(t *testing.T) {
			if t.Name() == "TestWriter/split_fields_overflow" {
				t.Skip("https://github.com/influxdata/line-protocol/issues/9")
			}
			buf := &bytes.Buffer{}
			serializer := NewEncoder(buf)
			serializer.SetMaxLineBytes(tt.maxBytes)
			serializer.SetFieldSortOrder(SortFields)
			serializer.SetFieldTypeSupport(tt.typeSupport)
			serializer.FailOnFieldErr(tt.failOnFieldErr)
			serializer.SetPrecision(tt.precision)
			_, err := serializer.Write(tt.fields.name, tt.fields.ts, tt.fields.tagKeys, tt.fields.tagVals, tt.fields.fieldKeys, tt.fields.fieldVals)
			if tt.err != err {
				t.Fatalf("expected error %v, but got %v", tt.err, err)
			}
			if string(tt.output) != buf.String() {
				t.Fatalf("expected output %s, but got %s", tt.output, buf.String())
			}
		})
	}
}

func fieldsToBytes(tg []*Field) ([][]byte, []interface{}) {
	b := make([][]byte, len(tg))
	v := make([]interface{}, len(tg))
	for i := range tg {
		b[i] = []byte(tg[i].Key)
		v[i] = tg[i].Value
	}
	return b, v
}

func tagsToBytes(tg []*Tag) ([][]byte, [][]byte) {
	b := make([][]byte, len(tg))
	v := make([][]byte, len(tg))
	for i := range tg {
		b[i] = []byte(tg[i].Key)
		v[i] = []byte(tg[i].Value)
	}
	return b, v

}

func BenchmarkSerializer(b *testing.B) {
	for _, tt := range tests {
		b.Run(tt.name, func(b *testing.B) {
			buf := &bytes.Buffer{}
			serializer := NewEncoder(buf)
			serializer.SetMaxLineBytes(tt.maxBytes)
			serializer.SetFieldTypeSupport(tt.typeSupport)
			for n := 0; n < b.N; n++ {
				output, err := serializer.Encode(tt.input)
				_ = err
				_ = output
			}
		})
	}
}

func BenchmarkWriter(b *testing.B) {
	type fields struct {
		name                        []byte
		ts                          time.Time
		tagKeys, tagVals, fieldKeys [][]byte
		fieldVals                   []interface{}
	}
	benches := make([]struct {
		name           string
		maxBytes       int
		typeSupport    FieldTypeSupport
		failOnFieldErr bool
		fields         fields
	}, len(tests))
	for i, tt := range tests {
		benches[i].name = tt.name
		benches[i].maxBytes = tt.maxBytes
		benches[i].typeSupport = tt.typeSupport
		benches[i].failOnFieldErr = tt.failOnFieldErr
		benches[i].fields.name = []byte(tt.input.Name())
		benches[i].fields.ts = tt.input.Time()
		benches[i].fields.fieldKeys, benches[i].fields.fieldVals = fieldsToBytes(tt.input.FieldList())
		benches[i].fields.tagKeys, benches[i].fields.tagVals = tagsToBytes(tt.input.TagList())
	}
	b.ResetTimer()

	for _, tt := range benches {
		b.Run(tt.name, func(b *testing.B) {
			buf := &bytes.Buffer{}
			serializer := NewEncoder(buf)
			serializer.SetMaxLineBytes(tt.maxBytes)
			serializer.SetFieldTypeSupport(tt.typeSupport)
			var i int
			var err error
			for n := 0; n < b.N; n++ {
				i, err = serializer.Write(tt.fields.name, tt.fields.ts, tt.fields.tagKeys, tt.fields.tagVals, tt.fields.fieldKeys, tt.fields.fieldVals)
				_ = err
				_ = i
			}
			_ = buf
		})
	}
}