-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialize-benchmark_test.go
85 lines (66 loc) · 1.88 KB
/
serialize-benchmark_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
package kosuzu_test
import (
"testing"
"github.com/zergon321/kosuzu"
)
type Choice struct {
Parameter int32
Numbers []int64
Parameters []complex128
}
func BenchmarkSerializeReflect(b *testing.B) {
choice := &Choice{
Parameter: 34,
Numbers: []int64{13, 14, 15, 16, 18},
Parameters: []complex128{2 + 3i, 3 + 1i, 2 + 5i},
}
kosuzu.Serialize(18, choice)
}
func BenchmarkSerializeCustom(b *testing.B) {
serialize := func(opcode int32, choice *Choice) *kosuzu.Packet {
builder := kosuzu.NewPacketBuilder()
builder.AddInt32(choice.Parameter)
builder.AddInt64Array(choice.Numbers)
builder.AddComplex128Array(choice.Parameters)
return builder.BuildPacket(opcode)
}
choice := &Choice{
Parameter: 34,
Numbers: []int64{13, 14, 15, 16, 18},
Parameters: []complex128{2 + 3i, 3 + 1i, 2 + 5i},
}
serialize(18, choice)
}
func BenchmarkDeserializeReflect(b *testing.B) {
choice := &Choice{
Parameter: 34,
Numbers: []int64{13, 14, 15, 16, 18},
Parameters: []complex128{2 + 3i, 3 + 1i, 2 + 5i},
}
packet, _ := kosuzu.Serialize(18, choice)
restored := new(Choice)
kosuzu.Deserialize(packet, &restored)
}
func BenchmarkDeserializeCustom(b *testing.B) {
serialize := func(opcode int32, choice *Choice) *kosuzu.Packet {
builder := kosuzu.NewPacketBuilder()
builder.AddInt32(choice.Parameter)
builder.AddInt64Array(choice.Numbers)
return builder.BuildPacket(opcode)
}
deserialize := func(packet *kosuzu.Packet) *Choice {
decomposer := kosuzu.NewPacketDecomposer(packet)
choice := new(Choice)
choice.Parameter, _ = decomposer.ReadInt32()
choice.Numbers, _ = decomposer.ReadInt64Array()
choice.Parameters, _ = decomposer.ReadComplex128Array()
return choice
}
choice := &Choice{
Parameter: 34,
Numbers: []int64{13, 14, 15, 16, 18},
Parameters: []complex128{2 + 3i, 3 + 1i, 2 + 5i},
}
packet := serialize(18, choice)
deserialize(packet)
}