forked from Telefonica/prometheus-kafka-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializers_test.go
104 lines (85 loc) · 2.88 KB
/
serializers_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
package main
import (
"math"
"testing"
"github.com/prometheus/prometheus/prompb"
"github.com/stretchr/testify/assert"
)
func NewWriteRequest() *prompb.WriteRequest {
return &prompb.WriteRequest{
Timeseries: []*prompb.TimeSeries{
&prompb.TimeSeries{
Labels: []*prompb.Label{
&prompb.Label{Name: "__name__", Value: "foo"},
&prompb.Label{Name: "labelfoo", Value: "label-bar"},
},
Samples: []*prompb.Sample{
&prompb.Sample{Timestamp: 0, Value: 456},
&prompb.Sample{Timestamp: 10000, Value: math.Inf(1)},
},
},
},
}
}
func TestSerializeEmptyTimeseriesToJSON(t *testing.T) {
request := &prompb.WriteRequest{}
serializer, err := NewJSONSerializer()
assert.Nil(t, err)
data, err := Serialize(serializer, request)
assert.Nil(t, err)
assert.Len(t, data, 0)
assert.NotNil(t, data)
}
func TestSerializeToJSON(t *testing.T) {
serializer, err := NewJSONSerializer()
assert.Nil(t, err)
writeRequest := NewWriteRequest()
output, err := Serialize(serializer, writeRequest)
assert.Len(t, output, 2)
assert.Nil(t, err)
expectedSamples := []string{
"{\"value\":\"456\",\"timestamp\":\"1970-01-01T00:00:00Z\",\"name\":\"foo\",\"labels\":{\"__name__\":\"foo\",\"labelfoo\":\"label-bar\"}}",
"{\"value\":\"+Inf\",\"timestamp\":\"1970-01-01T00:00:10Z\",\"name\":\"foo\",\"labels\":{\"__name__\":\"foo\",\"labelfoo\":\"label-bar\"}}",
}
for i, metric := range output {
assert.JSONEqf(t, expectedSamples[i], string(metric[:]), "wrong json serialization found")
}
}
func TestSerializeEmptyTimeseriesToAvroJSON(t *testing.T) {
request := &prompb.WriteRequest{}
serializer, err := NewAvroJSONSerializer("schemas/metric.avsc")
assert.Nil(t, err)
data, err := Serialize(serializer, request)
assert.Nil(t, err)
assert.Len(t, data, 0)
assert.NotNil(t, data)
}
func TestSerializeToAvro(t *testing.T) {
serializer, err := NewAvroJSONSerializer("schemas/metric.avsc")
assert.Nil(t, err)
writeRequest := NewWriteRequest()
output, err := Serialize(serializer, writeRequest)
assert.Len(t, output, 2)
assert.Nil(t, err)
expectedSamples := []string{
"{\"value\":\"456\",\"timestamp\":\"1970-01-01T00:00:00Z\",\"name\":\"foo\",\"labels\":{\"__name__\":\"foo\",\"labelfoo\":\"label-bar\"}}",
"{\"value\":\"+Inf\",\"timestamp\":\"1970-01-01T00:00:10Z\",\"name\":\"foo\",\"labels\":{\"__name__\":\"foo\",\"labelfoo\":\"label-bar\"}}",
}
for i, metric := range output {
assert.JSONEqf(t, expectedSamples[i], string(metric[:]), "wrong json serialization found")
}
}
func BenchmarkSerializeToAvroJSON(b *testing.B) {
serializer, _ := NewAvroJSONSerializer("schemas/metric.avsc")
writeRequest := NewWriteRequest()
for n := 0; n < 20000; n++ {
Serialize(serializer, writeRequest)
}
}
func BenchmarkSerializeToJSON(b *testing.B) {
serializer, _ := NewJSONSerializer()
writeRequest := NewWriteRequest()
for n := 0; n < 20000; n++ {
Serialize(serializer, writeRequest)
}
}