-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreader_test.go
82 lines (69 loc) · 1.55 KB
/
reader_test.go
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
package restlicodec
import (
"encoding/json"
"testing"
)
var (
Err error
unmarshalData = []byte(`{"status":500}`)
Obj = &Object{Status: 10}
)
func BenchmarkMarshal(b *testing.B) {
for i := 0; i < b.N; i++ {
Err = Obj.MarshalRestLi(NoopWriter)
}
}
func BenchmarkUnmarshal(b *testing.B) {
for i := 0; i < b.N; i++ {
reader, _ := NewJsonReader(unmarshalData)
Err = new(Object).UnmarshalRestLi(reader)
}
}
func BenchmarkReflectMarshal(b *testing.B) {
for i := 0; i < b.N; i++ {
Err = MarshalRestLi[*Object](Obj, NoopWriter)
}
}
func BenchmarkReflectUnmarshal(b *testing.B) {
for i := 0; i < b.N; i++ {
reader, _ := NewJsonReader(unmarshalData)
_, Err = UnmarshalRestLi[*Object](reader)
}
}
func BenchmarkMarshalJSON(b *testing.B) {
for i := 0; i < b.N; i++ {
_, Err = json.Marshal(Obj)
}
}
func BenchmarkUnmarshalJSON(b *testing.B) {
for i := 0; i < b.N; i++ {
Err = json.Unmarshal(unmarshalData, new(Object))
}
}
type Object struct {
Status int32
}
func (e *Object) NewInstance() *Object {
return new(Object)
}
func (e *Object) MarshalRestLi(writer Writer) (err error) {
return writer.WriteMap(func(keyWriter func(string) Writer) (err error) {
keyWriter("status").WriteInt32(e.Status)
return nil
})
}
func (e *Object) UnmarshalRestLi(reader Reader) (err error) {
err = reader.ReadRecord(nil, func(reader Reader, field string) (err error) {
switch field {
case "status":
e.Status, err = reader.ReadInt32()
default:
err = reader.Skip()
}
return err
})
if err != nil {
return err
}
return err
}