github.com/kayoticsully/syncthing@v0.8.9-0.20140724133906-c45a2fdc03f8/xdr/bench_test.go (about) 1 // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file). 2 // All rights reserved. Use of this source code is governed by an MIT-style 3 // license that can be found in the LICENSE file. 4 5 package xdr_test 6 7 import ( 8 "io" 9 "io/ioutil" 10 "testing" 11 ) 12 13 type XDRBenchStruct struct { 14 I1 uint64 15 I2 uint32 16 I3 uint16 17 Bs0 []byte // max:128 18 Bs1 []byte 19 S0 string // max:128 20 S1 string 21 } 22 23 var res []byte // no to be optimized away 24 var s = XDRBenchStruct{ 25 I1: 42, 26 I2: 43, 27 I3: 44, 28 Bs0: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}, 29 Bs1: []byte{11, 12, 13, 14, 15, 16, 17, 18, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 30 S0: "Hello World! String one.", 31 S1: "Hello World! String two.", 32 } 33 var e = s.MarshalXDR() 34 35 func BenchmarkThisMarshal(b *testing.B) { 36 for i := 0; i < b.N; i++ { 37 res = s.MarshalXDR() 38 } 39 } 40 41 func BenchmarkThisUnmarshal(b *testing.B) { 42 var t XDRBenchStruct 43 for i := 0; i < b.N; i++ { 44 err := t.UnmarshalXDR(e) 45 if err != nil { 46 b.Fatal(err) 47 } 48 } 49 } 50 51 func BenchmarkThisEncode(b *testing.B) { 52 for i := 0; i < b.N; i++ { 53 _, err := s.EncodeXDR(ioutil.Discard) 54 if err != nil { 55 b.Fatal(err) 56 } 57 } 58 } 59 60 type repeatReader struct { 61 data []byte 62 } 63 64 func (r *repeatReader) Read(bs []byte) (n int, err error) { 65 if len(bs) > len(r.data) { 66 err = io.EOF 67 } 68 n = copy(bs, r.data) 69 r.data = r.data[n:] 70 return n, err 71 } 72 73 func (r *repeatReader) Reset(bs []byte) { 74 r.data = bs 75 } 76 77 func BenchmarkThisDecode(b *testing.B) { 78 rr := &repeatReader{e} 79 var t XDRBenchStruct 80 for i := 0; i < b.N; i++ { 81 err := t.DecodeXDR(rr) 82 if err != nil { 83 b.Fatal(err) 84 } 85 rr.Reset(e) 86 } 87 }