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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
package _generated
import (
"bytes"
"github.com/tinylib/msgp/msgp"
"reflect"
"testing"
"time"
)
// benchmark encoding a small, "fast" type.
// the point here is to see how much garbage
// is generated intrinsically by the encoding/
// decoding process as opposed to the nature
// of the struct.
func BenchmarkFastEncode(b *testing.B) {
v := &TestFast{
Lat: 40.12398,
Long: -41.9082,
Alt: 201.08290,
Data: []byte("whaaaaargharbl"),
}
var buf bytes.Buffer
msgp.Encode(&buf, v)
en := msgp.NewWriter(msgp.Nowhere)
b.SetBytes(int64(buf.Len()))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
v.EncodeMsg(en)
}
en.Flush()
}
// benchmark decoding a small, "fast" type.
// the point here is to see how much garbage
// is generated intrinsically by the encoding/
// decoding process as opposed to the nature
// of the struct.
func BenchmarkFastDecode(b *testing.B) {
v := &TestFast{
Lat: 40.12398,
Long: -41.9082,
Alt: 201.08290,
Data: []byte("whaaaaargharbl"),
}
var buf bytes.Buffer
msgp.Encode(&buf, v)
dc := msgp.NewReader(msgp.NewEndlessReader(buf.Bytes(), b))
b.SetBytes(int64(buf.Len()))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
v.DecodeMsg(dc)
}
}
// This covers the following cases:
// - Recursive types
// - Non-builtin identifiers (and recursive types)
// - time.Time
// - map[string]string
// - anonymous structs
//
func Test1EncodeDecode(t *testing.T) {
f := 32.00
tt := &TestType{
F: &f,
Els: map[string]string{
"thing_one": "one",
"thing_two": "two",
},
Obj: struct {
ValueA string `msg:"value_a"`
ValueB []byte `msg:"value_b"`
}{
ValueA: "here's the first inner value",
ValueB: []byte("here's the second inner value"),
},
Child: nil,
Time: time.Now(),
Appended: msgp.Raw([]byte{0xc0}), // 'nil'
}
var buf bytes.Buffer
err := msgp.Encode(&buf, tt)
if err != nil {
t.Fatal(err)
}
tnew := new(TestType)
err = msgp.Decode(&buf, tnew)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(tt, tnew) {
t.Logf("in: %v", tt)
t.Logf("out: %v", tnew)
t.Fatal("objects not equal")
}
tanother := new(TestType)
buf.Reset()
msgp.Encode(&buf, tt)
var left []byte
left, err = tanother.UnmarshalMsg(buf.Bytes())
if err != nil {
t.Error(err)
}
if len(left) > 0 {
t.Errorf("%d bytes left", len(left))
}
if !reflect.DeepEqual(tt, tanother) {
t.Logf("in: %v", tt)
t.Logf("out: %v", tanother)
t.Fatal("objects not equal")
}
}
|