-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom.go
104 lines (88 loc) · 2.6 KB
/
custom.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 v2testcsproto
import (
"testing"
"github.com/CrowdStrike/csproto"
"github.com/bwplotka/go-proto-bench/prw/internal/base"
)
type b struct {
tb testing.TB
src *WriteRequest
}
func NewBenchmarkable(tb testing.TB, wr *base.WriteRequest) base.Benchmarkable {
return &b{tb: tb, src: fromBase(wr)}
}
func (b *b) Serialize() []byte {
// csproto will use generated fastmarshal methods.
out, err := csproto.Marshal(b.src)
if err != nil {
b.tb.Fatal(err)
}
return out
}
func (b *b) Deserialize(in []byte) base.Sizer {
obj := &WriteRequest{}
// csproto will use generated fastunmarshal methods.
if err := csproto.Unmarshal(in, obj); err != nil {
b.tb.Fatal(err)
}
return obj
}
func (b *b) DeserializeToBase(in []byte) *base.WriteRequest {
obj := &WriteRequest{}
if err := csproto.Unmarshal(in, obj); err != nil {
b.tb.Fatal(err)
}
return toBase(obj)
}
func fromBase(wr *base.WriteRequest) *WriteRequest {
symbolsMap := map[string]uint32{}
ret := &WriteRequest{Timeseries: make([]*TimeSeries, len(wr.Timeseries))}
for i := range wr.Timeseries {
lbls := make([]uint32, 0, 2*len(wr.Timeseries[i].Labels))
for j := range wr.Timeseries[i].Labels {
str := wr.Timeseries[i].Labels[j].Name
ref, ok := symbolsMap[str]
if !ok {
ret.Symbols = append(ret.Symbols, str)
ref = uint32(len(ret.Symbols) - 1)
symbolsMap[str] = ref
}
lbls = append(lbls, ref)
str2 := wr.Timeseries[i].Labels[j].Value
ref2, ok := symbolsMap[str2]
if !ok {
ret.Symbols = append(ret.Symbols, str2)
ref2 = uint32(len(ret.Symbols) - 1)
symbolsMap[str2] = ref2
}
lbls = append(lbls, ref2)
}
s := make([]*Sample, len(wr.Timeseries[i].Samples))
for j := range wr.Timeseries[i].Samples {
s[j] = &Sample{Value: wr.Timeseries[i].Samples[j].Value, Timestamp: wr.Timeseries[i].Samples[j].Timestamp}
}
ret.Timeseries[i] = &TimeSeries{LabelSymbols: lbls, Samples: s}
}
return ret
}
func toBase(wr *WriteRequest) *base.WriteRequest {
ret := &base.WriteRequest{Timeseries: make([]base.TimeSeries, len(wr.Timeseries))}
for i := range wr.Timeseries {
lbls := make([]base.Label, len(wr.Timeseries[i].LabelSymbols)/2)
sID := 0
for j := range lbls {
lbls[j].Name = wr.Symbols[wr.Timeseries[i].LabelSymbols[sID]]
sID++
lbls[j].Value = wr.Symbols[wr.Timeseries[i].LabelSymbols[sID]]
sID++
}
ret.Timeseries[i].Labels = lbls
s := make([]base.Sample, len(wr.Timeseries[i].Samples))
for j := range wr.Timeseries[i].Samples {
s[j].Value = wr.Timeseries[i].Samples[j].Value
s[j].Timestamp = wr.Timeseries[i].Samples[j].Timestamp
}
ret.Timeseries[i].Samples = s
}
return ret
}