forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_test.go
144 lines (122 loc) · 3.22 KB
/
bench_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
package avro_test
import (
"io/ioutil"
"testing"
"github.com/hamba/avro"
)
type Superhero struct {
ID int `avro:"id"`
AffiliationID int `avro:"affiliation_id"`
Name string `avro:"name"`
Life float32 `avro:"life"`
Energy float32 `avro:"energy"`
Powers []*Superpower `avro:"powers"`
}
type Superpower struct {
ID int `avro:"id"`
Name string `avro:"name"`
Damage float32 `avro:"damage"`
Energy float32 `avro:"energy"`
Passive bool `avro:"passive"`
}
type PartialSuperhero struct {
ID int `avro:"id"`
AffiliationID int `avro:"affiliation_id"`
Name string `avro:"name"`
Life float32 `avro:"life"`
Energy float32 `avro:"energy"`
}
func BenchmarkSuperheroDecode(b *testing.B) {
data, err := ioutil.ReadFile("testdata/superhero.bin")
if err != nil {
panic(err)
}
schema, err := avro.ParseFiles("testdata/superhero.avsc")
if err != nil {
panic(err)
}
super := &Superhero{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = avro.Unmarshal(schema, data, super)
}
}
func BenchmarkSuperheroEncode(b *testing.B) {
schema, err := avro.ParseFiles("testdata/superhero.avsc")
if err != nil {
panic(err)
}
super := &Superhero{
ID: 234765,
AffiliationID: 9867,
Name: "Wolverine",
Life: 85.25,
Energy: 32.75,
Powers: []*Superpower{
{ID: 2345, Name: "Bone Claws", Damage: 5, Energy: 1.15, Passive: false},
{ID: 2346, Name: "Regeneration", Damage: -2, Energy: 0.55, Passive: true},
{ID: 2347, Name: "Adamant skeleton", Damage: -10, Energy: 0, Passive: true},
},
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = avro.Marshal(schema, super)
}
}
func BenchmarkPartialSuperheroDecode(b *testing.B) {
data, err := ioutil.ReadFile("testdata/superhero.bin")
if err != nil {
panic(err)
}
schema, err := avro.ParseFiles("testdata/superhero.avsc")
if err != nil {
panic(err)
}
super := &PartialSuperhero{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = avro.Unmarshal(schema, data, super)
}
}
func BenchmarkSuperheroGenericDecode(b *testing.B) {
data, err := ioutil.ReadFile("testdata/superhero.bin")
if err != nil {
panic(err)
}
schema, err := avro.ParseFiles("testdata/superhero.avsc")
if err != nil {
panic(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var m interface{}
_ = avro.Unmarshal(schema, data, &m)
}
}
func BenchmarkSuperheroGenericEncode(b *testing.B) {
schema, err := avro.ParseFiles("testdata/superhero.avsc")
if err != nil {
panic(err)
}
super := map[string]interface{}{
"id": 234765,
"affiliation_id": 9867,
"Name": "Wolverine",
"life": 85.25,
"energy": 32.75,
"powers": []map[string]interface{}{
{"id": 2345, "name": "Bone Claws", "damage": 5, "energy": 1.15, "passive": false},
{"id": 2346, "name": "Regeneration", "damage": -2, "energy": 0.55, "passive": true},
{"id": 2347, "name": "Adamant skeleton", "damage": -10, "energy": 0, "passive": true},
},
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = avro.Marshal(schema, super)
}
}