]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/binary: add more benchmarks
authorRoger Peppe <rogpeppe@gmail.com>
Tue, 20 Dec 2011 17:25:47 +0000 (09:25 -0800)
committerRob Pike <r@golang.org>
Tue, 20 Dec 2011 17:25:47 +0000 (09:25 -0800)
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

src/pkg/encoding/binary/binary_test.go
src/pkg/encoding/binary/varint_test.go

index fd4fdb015747ef1cab8b61f9a3bbdc2af8804355..899505e0a55ad464c983f41f9a7625452c7a0d40 100644 (file)
@@ -171,11 +171,42 @@ func (br *byteSliceReader) Read(p []byte) (int, error) {
        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
        bsr := &byteSliceReader{}
        var r io.Reader = bsr
-
+       b.SetBytes(2 * (1 + 2 + 4 + 8))
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
                bsr.remain = big
                Read(r, BigEndian, &ls.Int8)
@@ -196,25 +227,19 @@ func BenchmarkRead(b *testing.B) {
        for i := range want.Array {
                want.Array[i] = 0
        }
+       b.StopTimer()
        if !reflect.DeepEqual(ls, want) {
                panic("no match")
        }
 }
 
-func BenchmarkWrite(b *testing.B) {
+func BenchmarkWriteInts(b *testing.B) {
        buf := new(bytes.Buffer)
        var w io.Writer = buf
-
+       b.SetBytes(2 * (1 + 2 + 4 + 8))
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
                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.Int16)
                Write(w, BigEndian, s.Int32)
@@ -224,11 +249,8 @@ func BenchmarkWrite(b *testing.B) {
                Write(w, BigEndian, s.Uint32)
                Write(w, BigEndian, s.Uint64)
        }
-
-       if !bytes.Equal(buf.Bytes()[:30], big[:30]) {
+       b.StopTimer()
+       if !bytes.Equal(buf.Bytes(), big[:30]) {
                panic("first half doesn't match")
        }
-       if !bytes.Equal(buf.Bytes()[30:], big[:30]) {
-               panic("second half doesn't match")
-       }
 }
index b553d6d4eb0589af408e56a5935d4dab4844d8e5..dc550f22f44e79c9eaee865956dd4ef0884ee29d 100644 (file)
@@ -165,6 +165,7 @@ func TestNonCanonicalZero(t *testing.T) {
 
 func BenchmarkPutUvarint32(b *testing.B) {
        buf := make([]byte, MaxVarintLen32)
+       b.SetBytes(4)
        for i := 0; i < b.N; i++ {
                for j := uint(0); j < MaxVarintLen32; j++ {
                        PutUvarint(buf, 1<<(j*7))
@@ -174,6 +175,7 @@ func BenchmarkPutUvarint32(b *testing.B) {
 
 func BenchmarkPutUvarint64(b *testing.B) {
        buf := make([]byte, MaxVarintLen64)
+       b.SetBytes(8)
        for i := 0; i < b.N; i++ {
                for j := uint(0); j < MaxVarintLen64; j++ {
                        PutUvarint(buf, 1<<(j*7))