| 1 |
# Test that -benchtime 1x only runs a total of 1 loop iteration. |
| 2 |
# See golang.org/issue/32051. |
| 3 |
|
| 4 |
go test -run ^$ -bench . -benchtime 1x |
| 5 |
|
| 6 |
-- go.mod -- |
| 7 |
module bench |
| 8 |
|
| 9 |
go 1.16 |
| 10 |
-- x_test.go -- |
| 11 |
package bench |
| 12 |
|
| 13 |
import ( |
| 14 |
"fmt" |
| 15 |
"os" |
| 16 |
"testing" |
| 17 |
) |
| 18 |
|
| 19 |
var called = false |
| 20 |
|
| 21 |
func TestMain(m *testing.M) { |
| 22 |
m.Run() |
| 23 |
if !called { |
| 24 |
fmt.Println("benchmark never called") |
| 25 |
os.Exit(1) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
func Benchmark(b *testing.B) { |
| 30 |
if b.N > 1 { |
| 31 |
b.Fatalf("called with b.N=%d; want b.N=1 only", b.N) |
| 32 |
} |
| 33 |
if called { |
| 34 |
b.Fatal("called twice") |
| 35 |
} |
| 36 |
called = true |
| 37 |
} |