benchmark_test.go 2.4 KB

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