Commit e86286bd authored by Ian Lance Taylor's avatar Ian Lance Taylor Committed by Andrew Gerrand

runtime: more deflaking of TestCgoCheckBytes

Fixes #14519.

Change-Id: I8f78f67a463e6467e09df90446f7ebd28789d6c9
Reviewed-on: https://go-review.googlesource.com/19933Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-on: https://go-review.googlesource.com/22071
Run-TryBot: Andrew Gerrand <adg@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 16b00c92
......@@ -7,6 +7,7 @@
package runtime_test
import (
"fmt"
"internal/testenv"
"os/exec"
"runtime"
......@@ -161,22 +162,35 @@ func TestCgoCheckBytes(t *testing.T) {
t.Fatal(err)
}
cmd := testEnv(exec.Command(exe, "CgoCheckBytes"))
cmd.Env = append(cmd.Env, "GODEBUG=cgocheck=0")
// Try it 10 times to avoid flakiness.
const tries = 10
var tot1, tot2 time.Duration
for i := 0; i < tries; i++ {
cmd := testEnv(exec.Command(exe, "CgoCheckBytes"))
cmd.Env = append(cmd.Env, "GODEBUG=cgocheck=0", fmt.Sprintf("GO_CGOCHECKBYTES_TRY=%d", i))
start := time.Now()
cmd.Run()
d1 := time.Since(start)
start := time.Now()
cmd.Run()
d1 := time.Since(start)
cmd = testEnv(exec.Command(exe, "CgoCheckBytes"))
cmd = testEnv(exec.Command(exe, "CgoCheckBytes"))
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_CGOCHECKBYTES_TRY=%d", i))
start = time.Now()
cmd.Run()
d2 := time.Since(start)
start = time.Now()
cmd.Run()
d2 := time.Since(start)
if d1*20 < d2 {
t.Errorf("cgo check too slow: got %v, expected at most %v", d1, d2*10)
if d1*20 > d2 {
// The slow version (d2) was less than 20 times
// slower than the fast version (d1), so OK.
return
}
tot1 += d1
tot2 += d2
}
t.Errorf("cgo check too slow: got %v, expected at most %v", tot2/tries, (tot1/tries)*20)
}
func TestCgoCCodeSIGPROF(t *testing.T) {
......
......@@ -11,7 +11,9 @@ void foo2(void* p) {}
import "C"
import (
"fmt"
"os"
"runtime"
"strconv"
"time"
"unsafe"
)
......@@ -83,8 +85,16 @@ func CgoTraceback() {
}
func CgoCheckBytes() {
b := make([]byte, 1e6)
for i := 0; i < 1e3; i++ {
try, _ := strconv.Atoi(os.Getenv("GO_CGOCHECKBYTES_TRY"))
if try <= 0 {
try = 1
}
b := make([]byte, 1e6*try)
start := time.Now()
for i := 0; i < 1e3*try; i++ {
C.foo2(unsafe.Pointer(&b[0]))
if time.Since(start) > time.Second {
break
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment