-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheasyproto_timing_test.go
230 lines (219 loc) · 4.99 KB
/
easyproto_timing_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
package easyproto
import (
"fmt"
"sync"
"testing"
)
func BenchmarkMarshalComplexMessage(b *testing.B) {
const seriesCount = 1_000
b.ReportAllocs()
b.SetBytes(seriesCount)
b.RunParallel(func(pb *testing.PB) {
var buf []byte
for pb.Next() {
buf = marshalComplexMessage(buf[:0], seriesCount)
}
})
}
func BenchmarkUnmarshalComplexMessage(b *testing.B) {
const seriesCount = 1_000
data := marshalComplexMessage(nil, seriesCount)
b.ReportAllocs()
b.SetBytes(seriesCount)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
unmarshalComplexMessage(data)
}
})
}
func marshalComplexMessage(dst []byte, seriesCount int) []byte {
m := mp.Get()
mm := m.MessageMarshaler()
for i := 0; i < seriesCount; i++ {
mmTimeseries := mm.AppendMessage(1)
for j := 0; j < 20; j++ {
mmLabel := mmTimeseries.AppendMessage(1)
mmLabel.AppendString(1, "instance")
mmLabel.AppendString(2, "foo-bar-baz-aaa-bbb")
}
for j := 0; j < 1; j++ {
mmSample := mmTimeseries.AppendMessage(2)
mmSample.AppendDouble(1, float64(j)+1.23)
mmSample.AppendInt64(2, int64(j)*73287)
}
}
dst = m.Marshal(dst)
mp.Put(m)
return dst
}
func unmarshalComplexMessage(src []byte) {
type label struct {
name string
value string
}
type sample struct {
value float64
timestamp int64
}
type timeseries struct {
labels []label
samples []sample
}
type writeRequest struct {
tss []timeseries
}
resetTimeseries := func(ts *timeseries) {
labels := ts.labels
for i := range labels {
labels[i] = label{}
}
ts.labels = labels[:0]
samples := ts.samples
for i := range samples {
samples[i] = sample{}
}
ts.samples = samples[:0]
}
resetWriteRequest := func(wr *writeRequest) {
tss := wr.tss
for i := range tss {
resetTimeseries(&tss[i])
}
wr.tss = tss[:0]
}
unmarshalSample := func(smpl *sample, src []byte) error {
var fc FieldContext
for len(src) > 0 {
tail, err := fc.NextField(src)
if err != nil {
return fmt.Errorf("cannot get next field: %w", err)
}
switch fc.FieldNum {
case 1:
value, ok := fc.Double()
if !ok {
return fmt.Errorf("cannot unmarshal sample value")
}
smpl.value = value
case 2:
timestamp, ok := fc.Int64()
if !ok {
return fmt.Errorf("cannot unmarshal timestamp")
}
smpl.timestamp = timestamp
}
src = tail
}
return nil
}
unmarshalLabel := func(lbl *label, src []byte) error {
var fc FieldContext
for len(src) > 0 {
tail, err := fc.NextField(src)
if err != nil {
return fmt.Errorf("cannot get next field: %w", err)
}
switch fc.FieldNum {
case 1:
name, ok := fc.String()
if !ok {
return fmt.Errorf("cannot unmarshal label name")
}
lbl.name = name
case 2:
value, ok := fc.String()
if !ok {
return fmt.Errorf("cannot unmarshal label value")
}
lbl.value = value
}
src = tail
}
return nil
}
unmarshalTimeseries := func(ts *timeseries, src []byte) error {
labels := ts.labels
samples := ts.samples
var fc FieldContext
for len(src) > 0 {
tail, err := fc.NextField(src)
if err != nil {
return fmt.Errorf("cannot get next field: %w", err)
}
switch fc.FieldNum {
case 1:
data, ok := fc.MessageData()
if !ok {
return fmt.Errorf("cannot get label data")
}
if len(labels) < cap(labels) {
labels = labels[:len(labels)+1]
} else {
labels = append(labels, label{})
}
label := &labels[len(labels)-1]
if err := unmarshalLabel(label, data); err != nil {
return fmt.Errorf("cannot unmarshal label: %w", err)
}
case 2:
data, ok := fc.MessageData()
if !ok {
return fmt.Errorf("cannot get sample data")
}
if len(samples) < cap(samples) {
samples = samples[:len(samples)+1]
} else {
samples = append(samples, sample{})
}
sample := &samples[len(samples)-1]
if err := unmarshalSample(sample, data); err != nil {
return fmt.Errorf("cannot unmarshal sample: %w", err)
}
}
src = tail
}
ts.labels = labels
ts.samples = samples
return nil
}
unmarshalWriteRequest := func(wr *writeRequest, src []byte) error {
tss := wr.tss
var fc FieldContext
for len(src) > 0 {
tail, err := fc.NextField(src)
if err != nil {
return fmt.Errorf("cannot get next field: %w", err)
}
data, ok := fc.MessageData()
if !ok {
return fmt.Errorf("cannot get message data")
}
switch fc.FieldNum {
case 1:
if len(tss) < cap(tss) {
tss = tss[:len(tss)+1]
} else {
tss = append(tss, timeseries{})
}
ts := &tss[len(tss)-1]
if err := unmarshalTimeseries(ts, data); err != nil {
return fmt.Errorf("cannot unmarshal timeseries: %w", err)
}
}
src = tail
}
wr.tss = tss
return nil
}
v := writeRequestPool.Get()
if v == nil {
v = &writeRequest{}
}
wr := v.(*writeRequest)
if err := unmarshalWriteRequest(wr, src); err != nil {
panic(fmt.Errorf("cannot unmarshal writeRequest: %w", err))
}
resetWriteRequest(wr)
writeRequestPool.Put(wr)
}
var writeRequestPool sync.Pool