-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_test.go
More file actions
208 lines (183 loc) · 3.96 KB
/
encoder_test.go
File metadata and controls
208 lines (183 loc) · 3.96 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package binary
import (
"bytes"
"encoding/json"
"reflect"
"testing"
"unsafe"
)
var testMsg = msg{
Name: "Roman",
Timestamp: 1242345235,
Payload: []byte("hi"),
Ssid: []uint32{1, 2, 3},
}
// Test_Full removed - uses map[string]column which is not supported
// Maps are intentionally not supported in Binary for WebAssembly optimization
// Use slice of structs instead: []struct{Key string; Value column}
/*
func Test_Full(t *testing.T) {
v := composite{}
v["a"] = column{
Varchar: columnVarchar{
Nulls: []bool{false, false, false, true, false},
Sizes: []uint32{2, 2, 2, 0, 2},
Bytes: []byte{10, 10, 10, 10, 10, 10, 10, 10},
},
}
v["b"] = column{
Float64: columnFloat64{
Nulls: []bool{false, false, false, true, false},
Floats: []float64{1.1, 2.2, 3.3, 0, 4.4},
},
}
b, err := Encode(&v)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if b == nil {
t.Error("Expected non-nil bytes")
}
var o composite
err = Decode(b, &o)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(v, o) {
t.Errorf("Expected %v, got %v", v, o)
}
}
*/
/*
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Benchmark_Binary/marshal-12 5074890 227.4 ns/op 112 B/op 2 allocs/op
Benchmark_Binary/marshal-to-12 7011523 162.3 ns/op 30 B/op 0 allocs/op
Benchmark_Binary/unmarshal-12 4224048 283.0 ns/op 72 B/op 5 allocs/op
*/
func BenchmarkBinary(b *testing.B) {
v := testMsg
var enc []byte
Encode(&v, &enc)
b.Run("marshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var out []byte
for n := 0; n < b.N; n++ {
Encode(&v, &out)
}
})
var buffer bytes.Buffer
b.Run("marshal-to", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
buffer.Reset()
Encode(&v, &buffer)
}
})
b.Run("unmarshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var out msg
for n := 0; n < b.N; n++ {
Decode(enc, &out)
}
})
}
func BenchmarkJSON(b *testing.B) {
v := testMsg
enc, _ := json.Marshal(&v)
b.Run("marshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
json.Marshal(&v)
}
})
b.Run("unmarshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var out msg
for n := 0; n < b.N; n++ {
json.Unmarshal(enc, &out)
}
})
}
type namedMsg struct {
msg
}
func (m *namedMsg) HandlerName() string { return "namedMsg" }
func BenchmarkNamedHandler(b *testing.B) {
v := testMsg
nv := namedMsg{msg: testMsg}
var enc []byte
Encode(&nv, &enc)
b.Run("regular/marshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var out []byte
for n := 0; n < b.N; n++ {
Encode(&v, &out)
}
})
b.Run("named/marshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var out []byte
for n := 0; n < b.N; n++ {
Encode(&nv, &out)
}
})
b.Run("regular/unmarshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var out msg
for n := 0; n < b.N; n++ {
Decode(enc, &out)
}
})
b.Run("named/unmarshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var out namedMsg
for n := 0; n < b.N; n++ {
Decode(enc, &out)
}
})
}
func TestBinaryEncodeStruct(t *testing.T) {
var b []byte
err := Encode(s0v, &b)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !bytes.Equal(s0b, b) {
t.Errorf("Expected %v, got %v", s0b, b)
}
}
func TestEncoderSizeOf(t *testing.T) {
var e encoder
size := int(unsafe.Sizeof(e))
if size != 56 {
t.Errorf("Expected %v, got %v", 56, size)
}
}
func TestMarshalWithCustomcodec(t *testing.T) {
v := testCustom("custom codec")
var b []byte
err := Encode(v, &b)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if b == nil {
t.Error("Expected non-nil bytes")
}
var out testCustom
err = Decode(b, &out)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(v, out) {
t.Errorf("Expected %v, got %v", v, out)
}
}