1
0
mirror of https://github.com/golang/go synced 2024-11-25 08:57:58 -07:00

encoding/binary: add more benchmarks

Also add a byte count to the varint benchmarks - this
isn't accurate, of course, but it allows a rough comparison to
the other benchmarks.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5496070
This commit is contained in:
Roger Peppe 2011-12-20 09:25:47 -08:00 committed by Rob Pike
parent 317ad14c6a
commit 16bf7d9e82
2 changed files with 41 additions and 17 deletions

View File

@ -171,11 +171,42 @@ func (br *byteSliceReader) Read(p []byte) (int, error) {
return n, nil return n, nil
} }
func BenchmarkRead(b *testing.B) { func BenchmarkReadSlice1000Int32s(b *testing.B) {
bsr := &byteSliceReader{}
slice := make([]int32, 1000)
buf := make([]byte, len(slice)*4)
b.SetBytes(int64(len(buf)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
bsr.remain = buf
Read(bsr, BigEndian, slice)
}
}
func BenchmarkReadStruct(b *testing.B) {
bsr := &byteSliceReader{}
var buf bytes.Buffer
Write(&buf, BigEndian, &s)
n := TotalSize(reflect.ValueOf(s))
b.SetBytes(int64(n))
t := s
b.ResetTimer()
for i := 0; i < b.N; i++ {
bsr.remain = buf.Bytes()
Read(bsr, BigEndian, &t)
}
b.StopTimer()
if !reflect.DeepEqual(s, t) {
panic("no match")
}
}
func BenchmarkReadInts(b *testing.B) {
var ls Struct var ls Struct
bsr := &byteSliceReader{} bsr := &byteSliceReader{}
var r io.Reader = bsr var r io.Reader = bsr
b.SetBytes(2 * (1 + 2 + 4 + 8))
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
bsr.remain = big bsr.remain = big
Read(r, BigEndian, &ls.Int8) Read(r, BigEndian, &ls.Int8)
@ -196,25 +227,19 @@ func BenchmarkRead(b *testing.B) {
for i := range want.Array { for i := range want.Array {
want.Array[i] = 0 want.Array[i] = 0
} }
b.StopTimer()
if !reflect.DeepEqual(ls, want) { if !reflect.DeepEqual(ls, want) {
panic("no match") panic("no match")
} }
} }
func BenchmarkWrite(b *testing.B) { func BenchmarkWriteInts(b *testing.B) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
var w io.Writer = buf var w io.Writer = buf
b.SetBytes(2 * (1 + 2 + 4 + 8))
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
buf.Reset() buf.Reset()
Write(w, BigEndian, &s.Int8)
Write(w, BigEndian, &s.Int16)
Write(w, BigEndian, &s.Int32)
Write(w, BigEndian, &s.Int64)
Write(w, BigEndian, &s.Uint8)
Write(w, BigEndian, &s.Uint16)
Write(w, BigEndian, &s.Uint32)
Write(w, BigEndian, &s.Uint64)
Write(w, BigEndian, s.Int8) Write(w, BigEndian, s.Int8)
Write(w, BigEndian, s.Int16) Write(w, BigEndian, s.Int16)
Write(w, BigEndian, s.Int32) Write(w, BigEndian, s.Int32)
@ -224,11 +249,8 @@ func BenchmarkWrite(b *testing.B) {
Write(w, BigEndian, s.Uint32) Write(w, BigEndian, s.Uint32)
Write(w, BigEndian, s.Uint64) Write(w, BigEndian, s.Uint64)
} }
b.StopTimer()
if !bytes.Equal(buf.Bytes()[:30], big[:30]) { if !bytes.Equal(buf.Bytes(), big[:30]) {
panic("first half doesn't match") panic("first half doesn't match")
} }
if !bytes.Equal(buf.Bytes()[30:], big[:30]) {
panic("second half doesn't match")
}
} }

View File

@ -165,6 +165,7 @@ func TestNonCanonicalZero(t *testing.T) {
func BenchmarkPutUvarint32(b *testing.B) { func BenchmarkPutUvarint32(b *testing.B) {
buf := make([]byte, MaxVarintLen32) buf := make([]byte, MaxVarintLen32)
b.SetBytes(4)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for j := uint(0); j < MaxVarintLen32; j++ { for j := uint(0); j < MaxVarintLen32; j++ {
PutUvarint(buf, 1<<(j*7)) PutUvarint(buf, 1<<(j*7))
@ -174,6 +175,7 @@ func BenchmarkPutUvarint32(b *testing.B) {
func BenchmarkPutUvarint64(b *testing.B) { func BenchmarkPutUvarint64(b *testing.B) {
buf := make([]byte, MaxVarintLen64) buf := make([]byte, MaxVarintLen64)
b.SetBytes(8)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for j := uint(0); j < MaxVarintLen64; j++ { for j := uint(0); j < MaxVarintLen64; j++ {
PutUvarint(buf, 1<<(j*7)) PutUvarint(buf, 1<<(j*7))