Skip to content

Commit e184646

Browse files
committed
binary: benchmark decoding of structs of int
1 parent 50c5290 commit e184646

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

binary_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package binary
33
import (
44
"bytes"
55
"errors"
6+
"fmt"
7+
"io"
68
"reflect"
79
"testing"
810
"time"
@@ -303,3 +305,77 @@ func BenchmarkEncodeStructI3(b *testing.B) {
303305
}
304306

305307
}
308+
309+
type bufferT struct {
310+
buf []byte
311+
}
312+
313+
func (buf *bufferT) Read(data []byte) (int, error) {
314+
n := copy(data, buf.buf)
315+
if n != len(data) {
316+
panic(fmt.Errorf("read too few bytes. got=%d want=%d", n, len(data)))
317+
}
318+
return n, nil
319+
}
320+
321+
func getTestBuffer(b *testing.B) io.Reader {
322+
return &bufferT{
323+
buf: []byte{0, 4, 0, 0, 0, 0, 0, 0},
324+
}
325+
}
326+
327+
func BenchmarkDecodeStructI1(b *testing.B) {
328+
329+
type Struct struct {
330+
I int64
331+
}
332+
333+
var s Struct
334+
335+
buf := getTestBuffer(b)
336+
b.ResetTimer()
337+
for i := 0; i < b.N; i++ {
338+
dec := NewDecoder(buf)
339+
_ = dec.Decode(&s)
340+
}
341+
342+
}
343+
344+
func BenchmarkDecodeStructI2(b *testing.B) {
345+
346+
type Struct struct {
347+
I int64
348+
}
349+
350+
var s Struct
351+
352+
buf := getTestBuffer(b)
353+
dec := NewDecoder(buf)
354+
355+
b.ResetTimer()
356+
for i := 0; i < b.N; i++ {
357+
_ = dec.Decode(&s)
358+
}
359+
360+
}
361+
362+
func BenchmarkDecodeStructI3(b *testing.B) {
363+
364+
type Struct struct {
365+
I int64
366+
}
367+
368+
var s Struct
369+
370+
buf := getTestBuffer(b)
371+
dec := NewDecoder(buf)
372+
373+
b.ResetTimer()
374+
for i := 0; i < b.N; i++ {
375+
err := dec.Decode(&s)
376+
if err != nil {
377+
b.Fatalf("error: %v\n", err)
378+
}
379+
}
380+
381+
}

0 commit comments

Comments
 (0)