2014-07-12 22:45:33 +00:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
|
|
|
// All rights reserved. Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-06-30 10:56:09 +00:00
|
|
|
|
|
|
|
package xdr_test
|
|
|
|
|
|
|
|
import (
|
2014-06-30 11:33:52 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-06-30 10:56:09 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type XDRBenchStruct struct {
|
2014-06-30 11:33:52 +00:00
|
|
|
I1 uint64
|
|
|
|
I2 uint32
|
|
|
|
I3 uint16
|
|
|
|
Bs0 []byte // max:128
|
|
|
|
Bs1 []byte
|
|
|
|
S0 string // max:128
|
|
|
|
S1 string
|
2014-06-30 10:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var res []byte // no to be optimized away
|
|
|
|
var s = XDRBenchStruct{
|
2014-06-30 11:33:52 +00:00
|
|
|
I1: 42,
|
|
|
|
I2: 43,
|
|
|
|
I3: 44,
|
|
|
|
Bs0: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18},
|
|
|
|
Bs1: []byte{11, 12, 13, 14, 15, 16, 17, 18, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
|
|
S0: "Hello World! String one.",
|
|
|
|
S1: "Hello World! String two.",
|
2014-06-30 10:56:09 +00:00
|
|
|
}
|
|
|
|
var e = s.MarshalXDR()
|
|
|
|
|
|
|
|
func BenchmarkThisMarshal(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
res = s.MarshalXDR()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkThisUnmarshal(b *testing.B) {
|
|
|
|
var t XDRBenchStruct
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
err := t.UnmarshalXDR(e)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-30 11:33:52 +00:00
|
|
|
func BenchmarkThisEncode(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, err := s.EncodeXDR(ioutil.Discard)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type repeatReader struct {
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *repeatReader) Read(bs []byte) (n int, err error) {
|
|
|
|
if len(bs) > len(r.data) {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
n = copy(bs, r.data)
|
|
|
|
r.data = r.data[n:]
|
|
|
|
return n, err
|
|
|
|
}
|
2014-06-30 10:56:09 +00:00
|
|
|
|
2014-06-30 11:33:52 +00:00
|
|
|
func (r *repeatReader) Reset(bs []byte) {
|
|
|
|
r.data = bs
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkThisDecode(b *testing.B) {
|
|
|
|
rr := &repeatReader{e}
|
|
|
|
var t XDRBenchStruct
|
2014-06-30 10:56:09 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2014-06-30 11:33:52 +00:00
|
|
|
err := t.DecodeXDR(rr)
|
2014-06-30 10:56:09 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2014-06-30 11:33:52 +00:00
|
|
|
rr.Reset(e)
|
2014-06-30 10:56:09 +00:00
|
|
|
}
|
|
|
|
}
|