Skip to content

Commit 82f7993

Browse files
introduce msgpack codec
1 parent 158b4ac commit 82f7993

File tree

8 files changed

+85
-0
lines changed

8 files changed

+85
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ in [examples/tcp](./examples/tcp).
116116
| Benchmark_OneRouteJsonCodec-8 | 176893 | 6413 ns/op | 1618 B/op | 32 allocs/op | build with `encoding/json` |
117117
| Benchmark_OneRouteJsonCodec-8 | 189723 | 5985 ns/op | 1347 B/op | 27 allocs/op | build with `json-jsoniter/go` |
118118
| Benchmark_OneRouteProtobufCodec-8 | 210532 | 6346 ns/op | 708 B/op | 19 allocs/op | |
119+
| Benchmark_OneRouteMsgpackCodec-8 | 181495 | 6196 ns/op | 868 B/op | 22 allocs/op | |
119120

120121
## Architecture
121122

benchmarks_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package easytcp
22

33
import (
44
"github.com/DarthPestilane/easytcp/message"
5+
"github.com/DarthPestilane/easytcp/test_data/msgpack"
56
"github.com/DarthPestilane/easytcp/test_data/pb"
67
"net"
78
"testing"
@@ -206,6 +207,37 @@ func Benchmark_OneRouteProtobufCodec(b *testing.B) {
206207
}
207208
}
208209

210+
func Benchmark_OneRouteMsgpackCodec(b *testing.B) {
211+
s := NewServer(&ServerOption{
212+
Codec: &MsgpackCodec{},
213+
DoNotPrintRoutes: true,
214+
})
215+
s.AddRoute(uint32(1), func(ctx *Context) (*message.Entry, error) {
216+
var req msgpack.Sample
217+
if err := ctx.Bind(&req); err != nil {
218+
panic(err)
219+
}
220+
return ctx.Response(2, &msgpack.Sample{Foo: "test-resp", Bar: req.Bar + 1})
221+
})
222+
go s.Serve(":0") // nolint
223+
224+
<-s.accepting
225+
226+
// client
227+
client, err := net.Dial("tcp", s.Listener.Addr().String())
228+
if err != nil {
229+
panic(err)
230+
}
231+
defer client.Close() // nolint
232+
233+
data, _ := s.Codec.Encode(&msgpack.Sample{Foo: "test", Bar: 1})
234+
packedMsg, _ := s.Packer.Pack(&message.Entry{ID: uint32(1), Data: data})
235+
beforeBench(b)
236+
for i := 0; i < b.N; i++ {
237+
_, _ = client.Write(packedMsg)
238+
}
239+
}
240+
209241
func beforeBench(b *testing.B) {
210242
Log = &mutedLogger{}
211243
b.ReportAllocs()

codec_msgpack.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package easytcp
2+
3+
import (
4+
"github.com/vmihailenco/msgpack/v5"
5+
)
6+
7+
// MsgpackCodec implements the Codec interface.
8+
type MsgpackCodec struct{}
9+
10+
// Encode implements the Codec Encode method.
11+
func (m *MsgpackCodec) Encode(v interface{}) ([]byte, error) {
12+
return msgpack.Marshal(v)
13+
}
14+
15+
// Decode implements the Codec Decode method.
16+
func (m *MsgpackCodec) Decode(data []byte, v interface{}) error {
17+
return msgpack.Unmarshal(data, v)
18+
}

codec_pb.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"google.golang.org/protobuf/proto"
66
)
77

8+
// ProtobufCodec implements the Codec interface.
89
type ProtobufCodec struct{}
910

11+
// Encode implements the Codec Encode method.
1012
func (p *ProtobufCodec) Encode(v interface{}) ([]byte, error) {
1113
m, ok := v.(proto.Message)
1214
if !ok {
@@ -15,6 +17,7 @@ func (p *ProtobufCodec) Encode(v interface{}) ([]byte, error) {
1517
return proto.Marshal(m)
1618
}
1719

20+
// Decode implements the Codec Decode method.
1821
func (p *ProtobufCodec) Decode(data []byte, v interface{}) error {
1922
m, ok := v.(proto.Message)
2023
if !ok {

codec_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package easytcp
22

33
import (
4+
"github.com/DarthPestilane/easytcp/test_data/msgpack"
45
"github.com/DarthPestilane/easytcp/test_data/pb"
56
"github.com/stretchr/testify/assert"
67
"google.golang.org/protobuf/proto"
@@ -54,3 +55,19 @@ func TestProtobufCodec(t *testing.T) {
5455
assert.True(t, proto.Equal(v, sample))
5556
})
5657
}
58+
59+
func TestMsgpackCodec(t *testing.T) {
60+
item := msgpack.Sample{
61+
Foo: "test",
62+
Bar: 1,
63+
Baz: map[int]string{2: "2"},
64+
}
65+
c := &MsgpackCodec{}
66+
b, err := c.Encode(item)
67+
assert.NoError(t, err)
68+
assert.NotNil(t, b)
69+
70+
var itemDec msgpack.Sample
71+
assert.NoError(t, c.Decode(b, &itemDec))
72+
assert.Equal(t, item, itemDec)
73+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/olekukonko/tablewriter v0.0.5
1313
github.com/sirupsen/logrus v1.8.1
1414
github.com/stretchr/testify v1.7.0
15+
github.com/vmihailenco/msgpack/v5 v5.3.4
1516
github.com/zhuangsirui/binpacker v1.0.0
1617
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7 // indirect
1718
google.golang.org/protobuf v1.26.0

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
3636
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
3737
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
3838
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
39+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3940
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
4041
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
42+
github.com/vmihailenco/msgpack/v5 v5.3.4 h1:qMKAwOV+meBw2Y8k9cVwAy7qErtYCwBzZ2ellBfvnqc=
43+
github.com/vmihailenco/msgpack/v5 v5.3.4/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
44+
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
45+
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
4146
github.com/zhuangsirui/binpacker v1.0.0 h1:kgziCOrgYftUXSwSqQDvXM42LRaEBDt0fqTj2BGZ1mQ=
4247
github.com/zhuangsirui/binpacker v1.0.0/go.mod h1:TdE7uEZ8Q7sMzbCpk2Y+ksFB8yA5AErPz0meDB612rU=
4348
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

test_data/msgpack/sample.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package msgpack
2+
3+
// Sample is a sample struct for test only.
4+
type Sample struct {
5+
Foo string `msgpack:"foo"`
6+
Bar int64 `msgpack:"bar"`
7+
Baz map[int]string `msgpack:"baz"`
8+
}

0 commit comments

Comments
 (0)