github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/encoding/gob/timing_test.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gob
     6  
     7  import (
     8  	"bytes"
     9  	"io"
    10  	"os"
    11  	"runtime"
    12  	"testing"
    13  )
    14  
    15  type Bench struct {
    16  	A int
    17  	B float64
    18  	C string
    19  	D []byte
    20  }
    21  
    22  func benchmarkEndToEnd(r io.Reader, w io.Writer, b *testing.B) {
    23  	b.StopTimer()
    24  	enc := NewEncoder(w)
    25  	dec := NewDecoder(r)
    26  	bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}
    27  	b.StartTimer()
    28  	for i := 0; i < b.N; i++ {
    29  		if enc.Encode(bench) != nil {
    30  			panic("encode error")
    31  		}
    32  		if dec.Decode(bench) != nil {
    33  			panic("decode error")
    34  		}
    35  	}
    36  }
    37  
    38  func BenchmarkEndToEndPipe(b *testing.B) {
    39  	r, w, err := os.Pipe()
    40  	if err != nil {
    41  		b.Fatal("can't get pipe:", err)
    42  	}
    43  	benchmarkEndToEnd(r, w, b)
    44  }
    45  
    46  func BenchmarkEndToEndByteBuffer(b *testing.B) {
    47  	var buf bytes.Buffer
    48  	benchmarkEndToEnd(&buf, &buf, b)
    49  }
    50  
    51  func TestCountEncodeMallocs(t *testing.T) {
    52  	if testing.Short() {
    53  		t.Skip("skipping malloc count in short mode")
    54  	}
    55  	if runtime.GOMAXPROCS(0) > 1 {
    56  		t.Skip("skipping; GOMAXPROCS>1")
    57  	}
    58  
    59  	const N = 1000
    60  
    61  	var buf bytes.Buffer
    62  	enc := NewEncoder(&buf)
    63  	bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}
    64  
    65  	allocs := testing.AllocsPerRun(N, func() {
    66  		err := enc.Encode(bench)
    67  		if err != nil {
    68  			t.Fatal("encode:", err)
    69  		}
    70  	})
    71  	if allocs != 0 {
    72  		t.Fatalf("mallocs per encode of type Bench: %v; wanted 0\n", allocs)
    73  	}
    74  }
    75  
    76  func TestCountDecodeMallocs(t *testing.T) {
    77  	if testing.Short() {
    78  		t.Skip("skipping malloc count in short mode")
    79  	}
    80  	if runtime.GOMAXPROCS(0) > 1 {
    81  		t.Skip("skipping; GOMAXPROCS>1")
    82  	}
    83  
    84  	const N = 1000
    85  
    86  	var buf bytes.Buffer
    87  	enc := NewEncoder(&buf)
    88  	bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}
    89  
    90  	// Fill the buffer with enough to decode
    91  	testing.AllocsPerRun(N, func() {
    92  		err := enc.Encode(bench)
    93  		if err != nil {
    94  			t.Fatal("encode:", err)
    95  		}
    96  	})
    97  
    98  	dec := NewDecoder(&buf)
    99  	allocs := testing.AllocsPerRun(N, func() {
   100  		*bench = Bench{}
   101  		err := dec.Decode(&bench)
   102  		if err != nil {
   103  			t.Fatal("decode:", err)
   104  		}
   105  	})
   106  	if allocs != 3 {
   107  		t.Fatalf("mallocs per decode of type Bench: %v; wanted 3\n", allocs)
   108  	}
   109  }