github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/testing/benchmark_test.go (about)

     1  // Copyright 2013 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 testing_test
     6  
     7  import (
     8  	"bytes"
     9  	"runtime"
    10  	"sync/atomic"
    11  	"testing"
    12  	"text/template"
    13  )
    14  
    15  var roundDownTests = []struct {
    16  	v, expected int
    17  }{
    18  	{1, 1},
    19  	{9, 1},
    20  	{10, 10},
    21  	{11, 10},
    22  	{100, 100},
    23  	{101, 100},
    24  	{999, 100},
    25  	{1000, 1000},
    26  	{1001, 1000},
    27  }
    28  
    29  func TestRoundDown10(t *testing.T) {
    30  	for _, tt := range roundDownTests {
    31  		actual := testing.RoundDown10(tt.v)
    32  		if tt.expected != actual {
    33  			t.Errorf("roundDown10(%d): expected %d, actual %d", tt.v, tt.expected, actual)
    34  		}
    35  	}
    36  }
    37  
    38  var roundUpTests = []struct {
    39  	v, expected int
    40  }{
    41  	{0, 1},
    42  	{1, 1},
    43  	{2, 2},
    44  	{3, 3},
    45  	{5, 5},
    46  	{9, 10},
    47  	{999, 1000},
    48  	{1000, 1000},
    49  	{1400, 2000},
    50  	{1700, 2000},
    51  	{2700, 3000},
    52  	{4999, 5000},
    53  	{5000, 5000},
    54  	{5001, 10000},
    55  }
    56  
    57  func TestRoundUp(t *testing.T) {
    58  	for _, tt := range roundUpTests {
    59  		actual := testing.RoundUp(tt.v)
    60  		if tt.expected != actual {
    61  			t.Errorf("roundUp(%d): expected %d, actual %d", tt.v, tt.expected, actual)
    62  		}
    63  	}
    64  }
    65  
    66  func TestRunParallel(t *testing.T) {
    67  	testing.Benchmark(func(b *testing.B) {
    68  		procs := uint32(0)
    69  		iters := uint64(0)
    70  		b.SetParallelism(3)
    71  		b.RunParallel(func(pb *testing.PB) {
    72  			atomic.AddUint32(&procs, 1)
    73  			for pb.Next() {
    74  				atomic.AddUint64(&iters, 1)
    75  			}
    76  		})
    77  		if want := uint32(3 * runtime.GOMAXPROCS(0)); procs != want {
    78  			t.Errorf("got %v procs, want %v", procs, want)
    79  		}
    80  		if iters != uint64(b.N) {
    81  			t.Errorf("got %v iters, want %v", iters, b.N)
    82  		}
    83  	})
    84  }
    85  
    86  func TestRunParallelFail(t *testing.T) {
    87  	testing.Benchmark(func(b *testing.B) {
    88  		b.RunParallel(func(pb *testing.PB) {
    89  			// The function must be able to log/abort
    90  			// w/o crashing/deadlocking the whole benchmark.
    91  			b.Log("log")
    92  			b.Error("error")
    93  		})
    94  	})
    95  }
    96  
    97  func ExampleB_RunParallel() {
    98  	// Parallel benchmark for text/template.Template.Execute on a single object.
    99  	testing.Benchmark(func(b *testing.B) {
   100  		templ := template.Must(template.New("test").Parse("Hello, {{.}}!"))
   101  		// RunParallel will create GOMAXPROCS goroutines
   102  		// and distribute work among them.
   103  		b.RunParallel(func(pb *testing.PB) {
   104  			// Each goroutine has its own bytes.Buffer.
   105  			var buf bytes.Buffer
   106  			for pb.Next() {
   107  				// The loop body is executed b.N times total across all goroutines.
   108  				buf.Reset()
   109  				templ.Execute(&buf, "World")
   110  			}
   111  		})
   112  	})
   113  }