benchmark_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // These tests verify the test running logic.
  2. package check_test
  3. import (
  4. "time"
  5. . "gopkg.in/check.v1"
  6. )
  7. var benchmarkS = Suite(&BenchmarkS{})
  8. type BenchmarkS struct{}
  9. func (s *BenchmarkS) TestCountSuite(c *C) {
  10. suitesRun += 1
  11. }
  12. func (s *BenchmarkS) TestBasicTestTiming(c *C) {
  13. helper := FixtureHelper{sleepOn: "Test1", sleep: 1000000 * time.Nanosecond}
  14. output := String{}
  15. runConf := RunConf{Output: &output, Verbose: true}
  16. Run(&helper, &runConf)
  17. expected := "PASS: check_test\\.go:[0-9]+: FixtureHelper\\.Test1\t0\\.0[0-9]+s\n" +
  18. "PASS: check_test\\.go:[0-9]+: FixtureHelper\\.Test2\t0\\.0[0-9]+s\n"
  19. c.Assert(output.value, Matches, expected)
  20. }
  21. func (s *BenchmarkS) TestStreamTestTiming(c *C) {
  22. helper := FixtureHelper{sleepOn: "SetUpSuite", sleep: 1000000 * time.Nanosecond}
  23. output := String{}
  24. runConf := RunConf{Output: &output, Stream: true}
  25. Run(&helper, &runConf)
  26. expected := "(?s).*\nPASS: check_test\\.go:[0-9]+: FixtureHelper\\.SetUpSuite\t[0-9]+\\.[0-9]+s\n.*"
  27. c.Assert(output.value, Matches, expected)
  28. }
  29. func (s *BenchmarkS) TestBenchmark(c *C) {
  30. helper := FixtureHelper{sleep: 100000}
  31. output := String{}
  32. runConf := RunConf{
  33. Output: &output,
  34. Benchmark: true,
  35. BenchmarkTime: 10000000,
  36. Filter: "Benchmark1",
  37. }
  38. Run(&helper, &runConf)
  39. c.Check(helper.calls[0], Equals, "SetUpSuite")
  40. c.Check(helper.calls[1], Equals, "SetUpTest")
  41. c.Check(helper.calls[2], Equals, "Benchmark1")
  42. c.Check(helper.calls[3], Equals, "TearDownTest")
  43. c.Check(helper.calls[4], Equals, "SetUpTest")
  44. c.Check(helper.calls[5], Equals, "Benchmark1")
  45. c.Check(helper.calls[6], Equals, "TearDownTest")
  46. // ... and more.
  47. expected := "PASS: check_test\\.go:[0-9]+: FixtureHelper\\.Benchmark1\t\\s+[0-9]+\t\\s+[0-9]+ ns/op\n"
  48. c.Assert(output.value, Matches, expected)
  49. }
  50. func (s *BenchmarkS) TestBenchmarkBytes(c *C) {
  51. helper := FixtureHelper{sleep: 100000}
  52. output := String{}
  53. runConf := RunConf{
  54. Output: &output,
  55. Benchmark: true,
  56. BenchmarkTime: 10000000,
  57. Filter: "Benchmark2",
  58. }
  59. Run(&helper, &runConf)
  60. expected := "PASS: check_test\\.go:[0-9]+: FixtureHelper\\.Benchmark2\t\\s+[0-9]+\t\\s+[0-9]+ ns/op\t\\s+ *[1-9]\\.[0-9]{2} MB/s\n"
  61. c.Assert(output.value, Matches, expected)
  62. }
  63. func (s *BenchmarkS) TestBenchmarkMem(c *C) {
  64. helper := FixtureHelper{sleep: 100000}
  65. output := String{}
  66. runConf := RunConf{
  67. Output: &output,
  68. Benchmark: true,
  69. BenchmarkMem: true,
  70. BenchmarkTime: 10000000,
  71. Filter: "Benchmark3",
  72. }
  73. Run(&helper, &runConf)
  74. expected := "PASS: check_test\\.go:[0-9]+: FixtureHelper\\.Benchmark3\t\\s+ [0-9]+\t\\s+ *[0-9]+ ns/op\t\\s+ [0-9]+ B/op\t\\s+ [1-9]+ allocs/op\n"
  75. c.Assert(output.value, Matches, expected)
  76. }