Dave Cheney | 787976c | 2013-05-31 23:03:22 +1000 | [diff] [blame] | 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 ( |
Dmitriy Vyukov | c3922f0 | 2014-02-17 06:29:56 +0400 | [diff] [blame] | 8 | "bytes" |
| 9 | "runtime" |
| 10 | "sync/atomic" |
Dave Cheney | 787976c | 2013-05-31 23:03:22 +1000 | [diff] [blame] | 11 | "testing" |
Dmitriy Vyukov | c3922f0 | 2014-02-17 06:29:56 +0400 | [diff] [blame] | 12 | "text/template" |
Dave Cheney | 787976c | 2013-05-31 23:03:22 +1000 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | var roundDownTests = []struct { |
| 16 | v, expected int |
| 17 | }{ |
| 18 | {1, 1}, |
| 19 | {9, 1}, |
Dave Cheney | a28609d | 2013-06-02 09:13:12 +1000 | [diff] [blame] | 20 | {10, 10}, |
Dave Cheney | 787976c | 2013-05-31 23:03:22 +1000 | [diff] [blame] | 21 | {11, 10}, |
Dave Cheney | a28609d | 2013-06-02 09:13:12 +1000 | [diff] [blame] | 22 | {100, 100}, |
| 23 | {101, 100}, |
| 24 | {999, 100}, |
| 25 | {1000, 1000}, |
| 26 | {1001, 1000}, |
Dave Cheney | 787976c | 2013-05-31 23:03:22 +1000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | func TestRoundDown10(t *testing.T) { |
| 30 | for _, tt := range roundDownTests { |
| 31 | actual := testing.RoundDown10(tt.v) |
| 32 | if tt.expected != actual { |
Dave Cheney | a28609d | 2013-06-02 09:13:12 +1000 | [diff] [blame] | 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}, |
Josh Bleecher Snyder | a12cc71 | 2014-06-24 08:39:30 -0700 | [diff] [blame] | 44 | {3, 3}, |
Dave Cheney | a28609d | 2013-06-02 09:13:12 +1000 | [diff] [blame] | 45 | {5, 5}, |
| 46 | {9, 10}, |
| 47 | {999, 1000}, |
| 48 | {1000, 1000}, |
| 49 | {1400, 2000}, |
| 50 | {1700, 2000}, |
Josh Bleecher Snyder | a12cc71 | 2014-06-24 08:39:30 -0700 | [diff] [blame] | 51 | {2700, 3000}, |
Dave Cheney | a28609d | 2013-06-02 09:13:12 +1000 | [diff] [blame] | 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) |
Dave Cheney | 787976c | 2013-05-31 23:03:22 +1000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | } |
Dmitriy Vyukov | c3922f0 | 2014-02-17 06:29:56 +0400 | [diff] [blame] | 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") |
Dmitriy Vyukov | c3922f0 | 2014-02-17 06:29:56 +0400 | [diff] [blame] | 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 | } |