Skip to content

Commit

Permalink
grunning: improve some tests
Browse files Browse the repository at this point in the history
Release note: None
Release justification: test-only changes
  • Loading branch information
irfansharif committed Aug 16, 2022
1 parent 3870f3a commit 0ee5479
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 67 deletions.
39 changes: 0 additions & 39 deletions pkg/util/grunning/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,41 @@ go_test(
"@io_bazel_rules_go//go/platform:aix_ppc64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:android_386": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:android_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:android_arm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:android_arm64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:darwin_386": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:darwin_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:darwin_arm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:darwin_arm64": [
Expand All @@ -74,7 +66,6 @@ go_test(
"@io_bazel_rules_go//go/platform:dragonfly_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:freebsd_386": [
Expand All @@ -96,13 +87,11 @@ go_test(
"@io_bazel_rules_go//go/platform:illumos_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:ios_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:ios_arm64": [
Expand All @@ -112,169 +101,141 @@ go_test(
"@io_bazel_rules_go//go/platform:js_wasm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_386": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_arm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_arm64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_mips": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_mips64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_mips64le": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_mipsle": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_ppc64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_ppc64le": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_riscv64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_s390x": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:netbsd_386": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:netbsd_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:netbsd_arm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:netbsd_arm64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:openbsd_386": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:openbsd_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:openbsd_arm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:openbsd_arm64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:plan9_386": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:plan9_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:plan9_arm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:solaris_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:windows_386": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:windows_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:windows_arm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"//conditions:default": [],
Expand Down
54 changes: 26 additions & 28 deletions pkg/util/grunning/enabled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ package grunning_test
import (
"runtime"
"sync"
"sync/atomic"
"testing"

"github.com/cockroachdb/cockroach/pkg/testutils/skip"
"github.com/cockroachdb/cockroach/pkg/util/grunning"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/stretchr/testify/require"
)

Expand All @@ -46,13 +44,7 @@ func TestEnabled(t *testing.T) {
func TestEquivalentGoroutines(t *testing.T) {
skip.UnderStress(t, "not applicable")

mu := struct {
syncutil.Mutex
nanos map[int]int64
}{}
mu.nanos = make(map[int]int64)

f := func(wg *sync.WaitGroup, id int) {
f := func(wg *sync.WaitGroup, result *int64) {
defer wg.Done()

var sum int
Expand All @@ -64,31 +56,27 @@ func TestEquivalentGoroutines(t *testing.T) {
}

nanos := grunning.Time().Nanoseconds()
mu.Lock()
mu.nanos[id] = nanos
mu.Unlock()
*result = nanos
}

const threads = 10
var wg sync.WaitGroup
results := make([]int64, threads)
for i := 0; i < threads; i++ {
i := i // copy loop variable
wg.Add(1)
go f(&wg, i)
go f(&wg, &results[i])
}
wg.Wait()

mu.Lock()
defer mu.Unlock()

total := int64(0)
for _, nanos := range mu.nanos {
total += nanos
for _, result := range results {
total += result
}

exp := 1.0 / threads
for i, nanos := range mu.nanos {
got := float64(nanos) / float64(total)
for i, result := range results {
got := float64(result) / float64(total)

t.Logf("thread=%02d expected≈%5.2f%% got=%5.2f%% of on-cpu time",
i+1, exp*100, got*100)
Expand All @@ -112,7 +100,7 @@ func TestProportionalGoroutines(t *testing.T) {
}

nanos := grunning.Time().Nanoseconds()
atomic.AddInt64(result, nanos)
*result = nanos
}

results := make([]int64, 10)
Expand Down Expand Up @@ -144,13 +132,17 @@ func TestProportionalGoroutines(t *testing.T) {
}

// TestPingPongHog is adapted from a benchmark in the Go runtime, forcing the
// scheduler to continually schedule goroutines.
// scheduler to continually schedule goroutines. It demonstrates that if two
// goroutines alternately cycle between running and waiting, they will get
// similar running times.
func TestPingPongHog(t *testing.T) {
skip.UnderStress(t, "not applicable")

defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))

// Create a CPU hog.
// Create a CPU hog. It makes the two goroutines that want to cycle between
// running and waiting also have to wait in runnable state, until the CPU
// hog is finished with its time slice.
stop, done := make(chan bool), make(chan bool)
go func() {
for {
Expand All @@ -172,7 +164,7 @@ func TestPingPongHog(t *testing.T) {
pong <- <-ping
}
pingern = grunning.Time().Nanoseconds()
close(stop)
close(stop) // stop the CPU hog
done <- true
}()
go func() {
Expand All @@ -183,9 +175,9 @@ func TestPingPongHog(t *testing.T) {
done <- true
}()
ping <- true // start ping-pong
<-stop
<-ping // let last ponger exit
<-done // make sure goroutines exit
<-stop // wait until the pinger tells the CPU hog to stop
<-ping // wait for the ponger to finish
<-done // make sure goroutines exit
<-done
<-done

Expand All @@ -194,7 +186,13 @@ func TestPingPongHog(t *testing.T) {
}

// BenchmarkGRunningTime measures how costly it is to read the current
// goroutine's running time.
// goroutine's running time. Results:
//
// goos: linux
// goarch: amd64
// cpu: Intel(R) Xeon(R) CPU @ 2.20GHz
// BenchmarkGRunningTime
// BenchmarkGRunningTime-24 38336452 31.59 ns/op
func BenchmarkGRunningTime(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = grunning.Time()
Expand Down

0 comments on commit 0ee5479

Please sign in to comment.