Skip to content

Commit 91dbc23

Browse files
andotalecthomas
authored andcommitted
Added benchmarks for hprose 2.0 (alecthomas#60)
1 parent 61a5420 commit 91dbc23

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

serialization_benchmarks_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/gogo/protobuf/proto"
2121
flatbuffers "github.com/google/flatbuffers/go"
2222
"github.com/hprose/hprose-go"
23+
hprose2 "github.com/hprose/hprose-golang/io"
2324
jsoniter "github.com/json-iterator/go"
2425
"github.com/ikkerens/ikeapack"
2526
"github.com/tinylib/msgp/msgp"
@@ -667,6 +668,54 @@ func BenchmarkHproseUnmarshal(b *testing.B) {
667668
benchUnmarshal(b, &HproseSerializer{writer: writer, reader: reader})
668669
}
669670

671+
// github.com/hprose/hprose-golang/io
672+
673+
type Hprose2Serializer struct {
674+
writer *hprose2.Writer
675+
reader *hprose2.Reader
676+
}
677+
678+
func (s Hprose2Serializer) Marshal(o interface{}) []byte {
679+
a := o.(*A)
680+
writer := s.writer
681+
writer.Clear()
682+
writer.WriteString(a.Name)
683+
writer.WriteTime(&a.BirthDay)
684+
writer.WriteString(a.Phone)
685+
writer.WriteInt(int64(a.Siblings))
686+
writer.WriteBool(a.Spouse)
687+
writer.WriteFloat(a.Money, 64)
688+
return writer.Bytes()
689+
}
690+
691+
func (s Hprose2Serializer) Unmarshal(d []byte, i interface{}) error {
692+
o := i.(*A)
693+
reader := s.reader
694+
reader.Init(d)
695+
o.Name = reader.ReadString()
696+
o.BirthDay = reader.ReadTime()
697+
o.Phone = reader.ReadString()
698+
o.Siblings = int(reader.ReadInt())
699+
o.Spouse = reader.ReadBool()
700+
o.Money = reader.ReadFloat64()
701+
return nil
702+
}
703+
704+
func (s Hprose2Serializer) String() string {
705+
return "Hprose2"
706+
}
707+
708+
func BenchmarkHprose2Marshal(b *testing.B) {
709+
writer := hprose2.NewWriter(true)
710+
benchMarshal(b, Hprose2Serializer{writer: writer})
711+
}
712+
713+
func BenchmarkHprose2Unmarshal(b *testing.B) {
714+
writer := hprose2.NewWriter(true)
715+
reader := hprose2.NewReader(nil, true)
716+
benchUnmarshal(b, &Hprose2Serializer{writer: writer, reader: reader})
717+
}
718+
670719
// github.com/DeDiS/protobuf
671720

672721
type ProtobufSerializer struct{}

0 commit comments

Comments
 (0)