@@ -8,21 +8,23 @@ import (
8
8
"math"
9
9
"math/rand"
10
10
"os"
11
+ "reflect"
11
12
"testing"
12
13
"time"
13
14
15
+ "github.com/niubaoshu/gotiny"
14
16
"zombiezen.com/go/capnproto2"
15
17
16
18
"github.com/DeDiS/protobuf"
17
19
"github.com/Sereal/Sereal/Go/sereal"
18
20
"github.com/davecgh/go-xdr/xdr"
19
21
"github.com/glycerine/go-capnproto"
20
22
"github.com/gogo/protobuf/proto"
21
- flatbuffers "github.com/google/flatbuffers/go"
23
+ "github.com/google/flatbuffers/go"
22
24
"github.com/hprose/hprose-go"
23
25
hprose2 "github.com/hprose/hprose-golang/io"
24
- jsoniter "github.com/json-iterator/go"
25
26
"github.com/ikkerens/ikeapack"
27
+ "github.com/json-iterator/go"
26
28
"github.com/tinylib/msgp/msgp"
27
29
"github.com/ugorji/go/codec"
28
30
"gopkg.in/mgo.v2/bson"
@@ -32,7 +34,7 @@ import (
32
34
)
33
35
34
36
var (
35
- validate = os .Getenv ("VALIDATE" )
37
+ validate = os .Getenv ("VALIDATE" )
36
38
jsoniterFast = jsoniter .ConfigFastest
37
39
)
38
40
@@ -121,7 +123,7 @@ func benchUnmarshal(b *testing.B, s Serializer) {
121
123
// Validate unmarshalled data.
122
124
if validate != "" {
123
125
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)
125
127
if ! correct {
126
128
b .Fatalf ("unmarshaled object differed:\n %v\n %v" , i , o )
127
129
}
@@ -138,6 +140,97 @@ See README.md for details on running the benchmarks.
138
140
139
141
}
140
142
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
+
141
234
// github.com/tinylib/msgp
142
235
143
236
type MsgpSerializer struct {}
@@ -504,7 +597,7 @@ func (s *FlatBufferSerializer) Unmarshal(d []byte, i interface{}) error {
504
597
o := FlatBufferA {}
505
598
o .Init (d , flatbuffers .GetUOffsetT (d ))
506
599
a .Name = string (o .Name ())
507
- a .BirthDay = time .Unix (o .BirthDay (), 0 )
600
+ a .BirthDay = time .Unix (0 , o .BirthDay ())
508
601
a .Phone = string (o .Phone ())
509
602
a .Siblings = int (o .Siblings ())
510
603
a .Spouse = o .Spouse () == byte (1 )
@@ -552,7 +645,7 @@ func (x *CapNProtoSerializer) Unmarshal(d []byte, i interface{}) error {
552
645
s , _ , _ := capn .ReadFromMemoryZeroCopy (d )
553
646
o := ReadRootCapnpA (s )
554
647
a .Name = string (o .NameBytes ())
555
- a .BirthDay = time .Unix (o .BirthDay (), 0 )
648
+ a .BirthDay = time .Unix (0 , o .BirthDay ())
556
649
a .Phone = string (o .PhoneBytes ())
557
650
a .Siblings = int (o .Siblings ())
558
651
a .Spouse = o .Spouse ()
@@ -597,7 +690,7 @@ func (x *CapNProto2Serializer) Unmarshal(d []byte, i interface{}) error {
597
690
m , _ := capnp .Unmarshal (d )
598
691
o , _ := ReadRootCapnp2A (m )
599
692
a .Name , _ = o .Name ()
600
- a .BirthDay = time .Unix (o .BirthDay (), 0 )
693
+ a .BirthDay = time .Unix (0 , o .BirthDay ())
601
694
a .Phone , _ = o .Phone ()
602
695
a .Siblings = int (o .Siblings ())
603
696
a .Spouse = o .Spouse ()
@@ -900,7 +993,7 @@ func BenchmarkColferUnmarshal(b *testing.B) {
900
993
}
901
994
if validate != "" {
902
995
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 )
904
997
if ! correct {
905
998
b .Fatalf ("unmarshaled object differed:\n %v\n %v" , i , o )
906
999
}
@@ -954,7 +1047,7 @@ func BenchmarkGencodeUnmarshal(b *testing.B) {
954
1047
// Validate unmarshalled data.
955
1048
if validate != "" {
956
1049
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)
958
1051
if ! correct {
959
1052
b .Fatalf ("unmarshaled object differed:\n %v\n %v" , i , o )
960
1053
}
0 commit comments