-
Notifications
You must be signed in to change notification settings - Fork 13
/
gogo_benchmarks_test.go
103 lines (95 loc) · 2.35 KB
/
gogo_benchmarks_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
package proto2_test
import (
"testing"
"time"
"github.com/gofrs/uuid"
"github.com/gogo/protobuf/proto"
"github.com/CrowdStrike/csproto"
"github.com/CrowdStrike/csproto/example/proto2/gogo"
)
func BenchmarkEncodeGogo(b *testing.B) {
var (
evt = createGogoEvent()
buf []byte
)
b.ResetTimer()
for n := 0; n < b.N; n++ {
buf, _ = proto.Marshal(evt)
_ = buf
}
_ = buf
}
func BenchmarkCustomEncodeGogo(b *testing.B) {
var (
evt = createGogoEvent()
buf []byte
)
b.ResetTimer()
for n := 0; n < b.N; n++ {
buf, _ = csproto.Marshal(evt)
_ = buf
}
_ = buf
}
func BenchmarkDecodeGogo(b *testing.B) {
var (
evt = createGogoEvent()
evt2 gogo.BaseEvent
ext *gogo.TestEvent
)
data, _ := proto.Marshal(evt)
b.ResetTimer()
for n := 0; n < b.N; n++ {
evt2.Reset()
_ = proto.Unmarshal(data, &evt2)
// also extract the proto2 extension to ensure we fully unmarshal *all* the data
extval, _ := proto.GetExtension(&evt2, gogo.E_TestEvent_EventExt)
ext = extval.(*gogo.TestEvent)
}
_ = ext
}
func BenchmarkCustomDecodeGogo(b *testing.B) {
var (
evt = createGogoEvent()
evt2 gogo.BaseEvent
ext *gogo.TestEvent
)
data, _ := proto.Marshal(evt)
b.ResetTimer()
for n := 0; n < b.N; n++ {
evt2.Reset()
_ = csproto.Unmarshal(data, &evt2)
// also extract the proto2 extension to ensure we fully unmarshal *all* the data
extval, _ := csproto.GetExtension(&evt2, gogo.E_TestEvent_EventExt)
ext = extval.(*gogo.TestEvent)
}
_ = ext
}
func createGogoEvent() *gogo.BaseEvent {
eventType := gogo.EventType_EVENT_TYPE_ONE
baseEvent := gogo.BaseEvent{
EventID: csproto.String(uuid.Must(uuid.NewV4()).String()),
SourceID: csproto.String(uuid.Must(uuid.NewV4()).String()),
Timestamp: csproto.Uint64(uint64(time.Now().UTC().Unix())),
EventType: &eventType,
}
extEvent := gogo.TestEvent{
Name: csproto.String("test-event"),
Info: csproto.String("blah blah blah"),
IsAwesome: csproto.Bool(true),
Labels: []string{"one", "two", "three"},
Embedded: &gogo.EmbeddedEvent{
ID: csproto.Int32(1),
FavoriteNumbers: []int32{42, 1138},
RandomThings: [][]byte{
{0x0, 0x1, 0x2, 0x3, 0x4, 0x5},
{0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf},
},
},
Path: &gogo.TestEvent_Jedi{
Jedi: true,
},
}
_ = csproto.SetExtension(&baseEvent, gogo.E_TestEvent_EventExt, &extEvent)
return &baseEvent
}