Skip to content

Commit b929e59

Browse files
niubaoshualecthomas
authored andcommitted
add gotiny benchmark and some samll change (alecthomas#71)
1 parent 9c78534 commit b929e59

13 files changed

+308
-118
lines changed

Colfer.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ func (i ColferError) Error() string {
2828
}
2929

3030
type ColferA struct {
31-
Name string
32-
BirthDay time.Time
33-
Phone string
34-
Siblings int32
35-
Spouse bool
36-
Money float64
31+
Name string
32+
BirthDay time.Time
33+
Phone string
34+
Siblings int32
35+
Spouse bool
36+
Money float64
3737
}
3838

3939
// MarshalTo encodes o as Colfer into buf and returns the number of bytes written.

FlatBufferA.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package goserbench
55
import (
66
flatbuffers "github.com/google/flatbuffers/go"
77
)
8+
89
type FlatBufferA struct {
910
_tab flatbuffers.Table
1011
}
@@ -63,10 +64,22 @@ func (rcv *FlatBufferA) Money() float64 {
6364
}
6465

6566
func FlatBufferAStart(builder *flatbuffers.Builder) { builder.StartObject(6) }
66-
func FlatBufferAAddName(builder *flatbuffers.Builder, name flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(name), 0) }
67-
func FlatBufferAAddBirthDay(builder *flatbuffers.Builder, birthDay int64) { builder.PrependInt64Slot(1, birthDay, 0) }
68-
func FlatBufferAAddPhone(builder *flatbuffers.Builder, phone flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(2, flatbuffers.UOffsetT(phone), 0) }
69-
func FlatBufferAAddSiblings(builder *flatbuffers.Builder, siblings int32) { builder.PrependInt32Slot(3, siblings, 0) }
70-
func FlatBufferAAddSpouse(builder *flatbuffers.Builder, spouse byte) { builder.PrependByteSlot(4, spouse, 0) }
71-
func FlatBufferAAddMoney(builder *flatbuffers.Builder, money float64) { builder.PrependFloat64Slot(5, money, 0) }
67+
func FlatBufferAAddName(builder *flatbuffers.Builder, name flatbuffers.UOffsetT) {
68+
builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(name), 0)
69+
}
70+
func FlatBufferAAddBirthDay(builder *flatbuffers.Builder, birthDay int64) {
71+
builder.PrependInt64Slot(1, birthDay, 0)
72+
}
73+
func FlatBufferAAddPhone(builder *flatbuffers.Builder, phone flatbuffers.UOffsetT) {
74+
builder.PrependUOffsetTSlot(2, flatbuffers.UOffsetT(phone), 0)
75+
}
76+
func FlatBufferAAddSiblings(builder *flatbuffers.Builder, siblings int32) {
77+
builder.PrependInt32Slot(3, siblings, 0)
78+
}
79+
func FlatBufferAAddSpouse(builder *flatbuffers.Builder, spouse byte) {
80+
builder.PrependByteSlot(4, spouse, 0)
81+
}
82+
func FlatBufferAAddMoney(builder *flatbuffers.Builder, money float64) {
83+
builder.PrependFloat64Slot(5, money, 0)
84+
}
7285
func FlatBufferAEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() }

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@ install:
6969
go get -u github.com/hprose/hprose-go/io
7070
go get -u github.com/pascaldekloe/colfer/cmd/colf
7171
go get -u github.com/calmh/xdr
72+
go get -u github.com/niubaoshu/gotiny

README.md

Lines changed: 96 additions & 59 deletions
Large diffs are not rendered by default.

gencode-unsafe.schema.gen.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (d *GencodeUnsafeA) Size() (s uint64) {
3030

3131
t := l
3232
for t >= 0x80 {
33-
t <<= 7
33+
t >>= 7
3434
s++
3535
}
3636
s++
@@ -45,7 +45,7 @@ func (d *GencodeUnsafeA) Size() (s uint64) {
4545

4646
t := l
4747
for t >= 0x80 {
48-
t <<= 7
48+
t >>= 7
4949
s++
5050
}
5151
s++
@@ -61,7 +61,7 @@ func (d *GencodeUnsafeA) Size() (s uint64) {
6161
t = ^t
6262
}
6363
for t >= 0x80 {
64-
t <<= 7
64+
t >>= 7
6565
s++
6666
}
6767
s++

msgp_gen.go

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

serialization_benchmarks_test.go

Lines changed: 102 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@ import (
88
"math"
99
"math/rand"
1010
"os"
11+
"reflect"
1112
"testing"
1213
"time"
1314

15+
"github.com/niubaoshu/gotiny"
1416
"zombiezen.com/go/capnproto2"
1517

1618
"github.com/DeDiS/protobuf"
1719
"github.com/Sereal/Sereal/Go/sereal"
1820
"github.com/davecgh/go-xdr/xdr"
1921
"github.com/glycerine/go-capnproto"
2022
"github.com/gogo/protobuf/proto"
21-
flatbuffers "github.com/google/flatbuffers/go"
23+
"github.com/google/flatbuffers/go"
2224
"github.com/hprose/hprose-go"
2325
hprose2 "github.com/hprose/hprose-golang/io"
24-
jsoniter "github.com/json-iterator/go"
2526
"github.com/ikkerens/ikeapack"
27+
"github.com/json-iterator/go"
2628
"github.com/tinylib/msgp/msgp"
2729
"github.com/ugorji/go/codec"
2830
"gopkg.in/mgo.v2/bson"
@@ -32,7 +34,7 @@ import (
3234
)
3335

3436
var (
35-
validate = os.Getenv("VALIDATE")
37+
validate = os.Getenv("VALIDATE")
3638
jsoniterFast = jsoniter.ConfigFastest
3739
)
3840

@@ -121,7 +123,7 @@ func benchUnmarshal(b *testing.B, s Serializer) {
121123
// Validate unmarshalled data.
122124
if validate != "" {
123125
i := data[n]
124-
correct := o.Name == i.Name && o.Phone == i.Phone && o.Siblings == i.Siblings && o.Spouse == i.Spouse && o.Money == i.Money && o.BirthDay.String() == i.BirthDay.String() //&& cmpTags(o.Tags, i.Tags) && cmpAliases(o.Aliases, i.Aliases)
126+
correct := o.Name == i.Name && o.Phone == i.Phone && o.Siblings == i.Siblings && o.Spouse == i.Spouse && o.Money == i.Money && o.BirthDay.Equal(i.BirthDay) //&& cmpTags(o.Tags, i.Tags) && cmpAliases(o.Aliases, i.Aliases)
125127
if !correct {
126128
b.Fatalf("unmarshaled object differed:\n%v\n%v", i, o)
127129
}
@@ -138,6 +140,97 @@ See README.md for details on running the benchmarks.
138140

139141
}
140142

143+
// github.com/niubaoshu/gotiny
144+
145+
type GotinySerializer struct {
146+
enc *gotiny.Encoder
147+
dec *gotiny.Decoder
148+
}
149+
150+
func (g GotinySerializer) Marshal(o interface{}) []byte {
151+
return g.enc.Encode(o)
152+
}
153+
154+
func (g GotinySerializer) Unmarshal(d []byte, o interface{}) error {
155+
g.dec.Decode(d, o)
156+
return nil
157+
}
158+
159+
func (GotinySerializer) String() string { return "gotiny" }
160+
161+
func NewGotinySerializer(o interface{}) Serializer {
162+
ot := reflect.TypeOf(o)
163+
return GotinySerializer{
164+
enc: gotiny.NewEncoderWithType(ot),
165+
dec: gotiny.NewDecoderWithType(ot),
166+
}
167+
}
168+
169+
func BenchmarkGotinyMarshal(b *testing.B) {
170+
benchMarshal(b, NewGotinySerializer(A{}))
171+
}
172+
173+
func BenchmarkGotinyUnmarshal(b *testing.B) {
174+
benchUnmarshal(b, NewGotinySerializer(A{}))
175+
}
176+
177+
func generateNoTimeA() []*NoTimeA {
178+
a := make([]*NoTimeA, 0, 1000)
179+
for i := 0; i < 1000; i++ {
180+
a = append(a, &NoTimeA{
181+
Name: randString(16),
182+
BirthDay: time.Now().UnixNano(),
183+
Phone: randString(10),
184+
Siblings: rand.Intn(5),
185+
Spouse: rand.Intn(2) == 1,
186+
Money: rand.Float64(),
187+
})
188+
}
189+
return a
190+
}
191+
192+
func BenchmarkGotinyNoTimeMarshal(b *testing.B) {
193+
b.StopTimer()
194+
s := NewGotinySerializer(NoTimeA{})
195+
data := generateNoTimeA()
196+
b.ReportAllocs()
197+
b.StartTimer()
198+
for i := 0; i < b.N; i++ {
199+
s.Marshal(data[rand.Intn(len(data))])
200+
}
201+
}
202+
203+
func BenchmarkGotinyNoTimeUnmarshal(b *testing.B) {
204+
b.StopTimer()
205+
s := NewGotinySerializer(NoTimeA{})
206+
data := generateNoTimeA()
207+
ser := make([][]byte, len(data))
208+
for i, d := range data {
209+
o := s.Marshal(d)
210+
t := make([]byte, len(o))
211+
copy(t, o)
212+
ser[i] = t
213+
}
214+
b.ReportAllocs()
215+
b.StartTimer()
216+
for i := 0; i < b.N; i++ {
217+
n := rand.Intn(len(ser))
218+
o := &NoTimeA{}
219+
err := s.Unmarshal(ser[n], o)
220+
if err != nil {
221+
b.Fatalf("%s failed to unmarshal: %s (%s)", s, err, ser[n])
222+
}
223+
// Validate unmarshalled data.
224+
if validate != "" {
225+
i := data[n]
226+
correct := o.Name == i.Name && o.Phone == i.Phone && o.Siblings == i.Siblings && o.Spouse == i.Spouse && o.Money == i.Money && o.BirthDay == i.BirthDay //&& cmpTags(o.Tags, i.Tags) && cmpAliases(o.Aliases, i.Aliases)
227+
if !correct {
228+
b.Fatalf("unmarshaled object differed:\n%v\n%v", i, o)
229+
}
230+
}
231+
}
232+
}
233+
141234
// github.com/tinylib/msgp
142235

143236
type MsgpSerializer struct{}
@@ -504,7 +597,7 @@ func (s *FlatBufferSerializer) Unmarshal(d []byte, i interface{}) error {
504597
o := FlatBufferA{}
505598
o.Init(d, flatbuffers.GetUOffsetT(d))
506599
a.Name = string(o.Name())
507-
a.BirthDay = time.Unix(o.BirthDay(), 0)
600+
a.BirthDay = time.Unix(0, o.BirthDay())
508601
a.Phone = string(o.Phone())
509602
a.Siblings = int(o.Siblings())
510603
a.Spouse = o.Spouse() == byte(1)
@@ -552,7 +645,7 @@ func (x *CapNProtoSerializer) Unmarshal(d []byte, i interface{}) error {
552645
s, _, _ := capn.ReadFromMemoryZeroCopy(d)
553646
o := ReadRootCapnpA(s)
554647
a.Name = string(o.NameBytes())
555-
a.BirthDay = time.Unix(o.BirthDay(), 0)
648+
a.BirthDay = time.Unix(0, o.BirthDay())
556649
a.Phone = string(o.PhoneBytes())
557650
a.Siblings = int(o.Siblings())
558651
a.Spouse = o.Spouse()
@@ -597,7 +690,7 @@ func (x *CapNProto2Serializer) Unmarshal(d []byte, i interface{}) error {
597690
m, _ := capnp.Unmarshal(d)
598691
o, _ := ReadRootCapnp2A(m)
599692
a.Name, _ = o.Name()
600-
a.BirthDay = time.Unix(o.BirthDay(), 0)
693+
a.BirthDay = time.Unix(0, o.BirthDay())
601694
a.Phone, _ = o.Phone()
602695
a.Siblings = int(o.Siblings())
603696
a.Spouse = o.Spouse()
@@ -900,7 +993,7 @@ func BenchmarkColferUnmarshal(b *testing.B) {
900993
}
901994
if validate != "" {
902995
i := data[n]
903-
correct := o.Name == i.Name && o.Phone == i.Phone && o.Siblings == i.Siblings && o.Spouse == i.Spouse && o.Money == i.Money && o.BirthDay == i.BirthDay
996+
correct := o.Name == i.Name && o.Phone == i.Phone && o.Siblings == i.Siblings && o.Spouse == i.Spouse && o.Money == i.Money && o.BirthDay.Equal(i.BirthDay)
904997
if !correct {
905998
b.Fatalf("unmarshaled object differed:\n%v\n%v", i, o)
906999
}
@@ -954,7 +1047,7 @@ func BenchmarkGencodeUnmarshal(b *testing.B) {
9541047
// Validate unmarshalled data.
9551048
if validate != "" {
9561049
i := data[n]
957-
correct := o.Name == i.Name && o.Phone == i.Phone && o.Siblings == i.Siblings && o.Spouse == i.Spouse && o.Money == i.Money && o.BirthDay == i.BirthDay //&& cmpTags(o.Tags, i.Tags) && cmpAliases(o.Aliases, i.Aliases)
1050+
correct := o.Name == i.Name && o.Phone == i.Phone && o.Siblings == i.Siblings && o.Spouse == i.Spouse && o.Money == i.Money && o.BirthDay.Equal(i.BirthDay) //&& cmpTags(o.Tags, i.Tags) && cmpAliases(o.Aliases, i.Aliases)
9581051
if !correct {
9591052
b.Fatalf("unmarshaled object differed:\n%v\n%v", i, o)
9601053
}

stats

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)