-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_test.go
121 lines (101 loc) · 2.52 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
package polo
import "testing"
type MixedObject struct {
A string
B int32
C []string
D map[string]string
E float64
}
func BenchmarkEncoding(b *testing.B) {
b.Run("Reflection Encoding", func(b *testing.B) {
object := MixedObject{
A: "Sins & Virtues",
B: 567822,
C: []string{"pride", "greed", "lust", "gluttony", "envy", "wrath", "sloth"},
D: map[string]string{"bravery": "piety", "friendship": "chastity"},
E: 45.23,
}
wire, _ := Polorize(object)
newObject := new(MixedObject)
b.ResetTimer()
b.Run("Polorize", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Polorize(object)
}
})
b.Run("Depolorize", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Depolorize(newObject, wire)
}
})
})
b.Run("Custom Encoding", func(b *testing.B) {
object := CustomEncodeObject{
A: "Sins & Virtues",
B: 567822,
C: []string{"pride", "greed", "lust", "gluttony", "envy", "wrath", "sloth"},
D: map[string]string{"bravery": "piety", "friendship": "chastity"},
E: 45.23,
}
wire, _ := Polorize(object)
newObject := new(CustomEncodeObject)
b.ResetTimer()
b.Run("Reflective Invoke", func(b *testing.B) {
b.Run("Polorize", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Polorize(object)
}
})
b.Run("Depolorize", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Depolorize(newObject, wire)
}
})
})
b.Run("Direct Invoke", func(b *testing.B) {
b.Run("Polorize", func(b *testing.B) {
for i := 0; i < b.N; i++ {
p, _ := object.Polorize()
_ = p.Bytes()
}
})
b.Run("Depolorize", func(b *testing.B) {
for i := 0; i < b.N; i++ {
d, _ := NewDepolorizer(wire)
_ = newObject.Depolorize(d)
}
})
})
})
}
func BenchmarkDocument(b *testing.B) {
object := MixedObject{
A: "Sins & Virtues",
B: 567822,
C: []string{"pride", "greed", "lust", "gluttony", "envy", "wrath", "sloth"},
D: map[string]string{"bravery": "piety", "friendship": "chastity"},
E: 45.23,
}
document, _ := PolorizeDocument(object)
docwire := document.Bytes()
newObject := new(MixedObject)
newDocument := make(Document)
b.Run("Doc Encode", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = PolorizeDocument(object)
}
})
b.Run("Doc Decode", func(b *testing.B) {
b.Run("Struct", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Depolorize(newObject, docwire)
}
})
b.Run("Document", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Depolorize(&newDocument, docwire)
}
})
})
}