Commit 69257d17 authored by Dmitriy Vyukov's avatar Dmitriy Vyukov

runtime: use RunParallel in more benchmarks

LGTM=bradfitz
R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/68020043
parent bb9531e1
...@@ -431,27 +431,15 @@ func TestMultiConsumer(t *testing.T) { ...@@ -431,27 +431,15 @@ func TestMultiConsumer(t *testing.T) {
} }
func BenchmarkChanNonblocking(b *testing.B) { func BenchmarkChanNonblocking(b *testing.B) {
const CallsPerSched = 1000
procs := runtime.GOMAXPROCS(-1)
N := int32(b.N / CallsPerSched)
c := make(chan bool, procs)
myc := make(chan int) myc := make(chan int)
for p := 0; p < procs; p++ { b.RunParallel(func(pb *testing.PB) {
go func() { for pb.Next() {
for atomic.AddInt32(&N, -1) >= 0 { select {
for g := 0; g < CallsPerSched; g++ { case <-myc:
select { default:
case <-myc:
default:
}
}
} }
c <- true }
}() })
}
for p := 0; p < procs; p++ {
<-c
}
} }
func BenchmarkSelectUncontended(b *testing.B) { func BenchmarkSelectUncontended(b *testing.B) {
...@@ -713,23 +701,11 @@ func BenchmarkChanCreation(b *testing.B) { ...@@ -713,23 +701,11 @@ func BenchmarkChanCreation(b *testing.B) {
func BenchmarkChanSem(b *testing.B) { func BenchmarkChanSem(b *testing.B) {
type Empty struct{} type Empty struct{}
const CallsPerSched = 1000 myc := make(chan Empty, runtime.GOMAXPROCS(0))
procs := runtime.GOMAXPROCS(0) b.RunParallel(func(pb *testing.PB) {
N := int32(b.N / CallsPerSched) for pb.Next() {
c := make(chan bool, procs) myc <- Empty{}
myc := make(chan Empty, procs) <-myc
for p := 0; p < procs; p++ { }
go func() { })
for atomic.AddInt32(&N, -1) >= 0 {
for g := 0; g < CallsPerSched; g++ {
myc <- Empty{}
<-myc
}
}
c <- true
}()
}
for p := 0; p < procs; p++ {
<-c
}
} }
...@@ -6,8 +6,6 @@ package runtime_test ...@@ -6,8 +6,6 @@ package runtime_test
import ( import (
"runtime" "runtime"
"sync"
"sync/atomic"
"testing" "testing"
"time" "time"
) )
...@@ -112,50 +110,28 @@ func TestFinalizerZeroSizedStruct(t *testing.T) { ...@@ -112,50 +110,28 @@ func TestFinalizerZeroSizedStruct(t *testing.T) {
} }
func BenchmarkFinalizer(b *testing.B) { func BenchmarkFinalizer(b *testing.B) {
const CallsPerSched = 1000 const Batch = 1000
procs := runtime.GOMAXPROCS(-1) b.RunParallel(func(pb *testing.PB) {
N := int32(b.N / CallsPerSched) var data [Batch]*int
var wg sync.WaitGroup for i := 0; i < Batch; i++ {
wg.Add(procs) data[i] = new(int)
for p := 0; p < procs; p++ { }
go func() { for pb.Next() {
var data [CallsPerSched]*int for i := 0; i < Batch; i++ {
for i := 0; i < CallsPerSched; i++ { runtime.SetFinalizer(data[i], fin)
data[i] = new(int)
} }
for atomic.AddInt32(&N, -1) >= 0 { for i := 0; i < Batch; i++ {
runtime.Gosched() runtime.SetFinalizer(data[i], nil)
for i := 0; i < CallsPerSched; i++ {
runtime.SetFinalizer(data[i], fin)
}
for i := 0; i < CallsPerSched; i++ {
runtime.SetFinalizer(data[i], nil)
}
} }
wg.Done() }
}() })
}
wg.Wait()
} }
func BenchmarkFinalizerRun(b *testing.B) { func BenchmarkFinalizerRun(b *testing.B) {
const CallsPerSched = 1000 b.RunParallel(func(pb *testing.PB) {
procs := runtime.GOMAXPROCS(-1) for pb.Next() {
N := int32(b.N / CallsPerSched) v := new(int)
var wg sync.WaitGroup runtime.SetFinalizer(v, fin)
wg.Add(procs) }
for p := 0; p < procs; p++ { })
go func() {
for atomic.AddInt32(&N, -1) >= 0 {
runtime.Gosched()
for i := 0; i < CallsPerSched; i++ {
v := new(int)
runtime.SetFinalizer(v, fin)
}
runtime.GC()
}
wg.Done()
}()
}
wg.Wait()
} }
...@@ -9,7 +9,6 @@ package runtime_test ...@@ -9,7 +9,6 @@ package runtime_test
import ( import (
"runtime" "runtime"
"sync/atomic"
"testing" "testing"
) )
...@@ -31,28 +30,17 @@ func BenchmarkSyscallExcessWork(b *testing.B) { ...@@ -31,28 +30,17 @@ func BenchmarkSyscallExcessWork(b *testing.B) {
} }
func benchmarkSyscall(b *testing.B, work, excess int) { func benchmarkSyscall(b *testing.B, work, excess int) {
const CallsPerSched = 1000 b.SetParallelism(excess)
procs := runtime.GOMAXPROCS(-1) * excess b.RunParallel(func(pb *testing.PB) {
N := int32(b.N / CallsPerSched) foo := 42
c := make(chan bool, procs) for pb.Next() {
for p := 0; p < procs; p++ { runtime.Entersyscall()
go func() { for i := 0; i < work; i++ {
foo := 42 foo *= 2
for atomic.AddInt32(&N, -1) >= 0 { foo /= 2
runtime.Gosched()
for g := 0; g < CallsPerSched; g++ {
runtime.Entersyscall()
for i := 0; i < work; i++ {
foo *= 2
foo /= 2
}
runtime.Exitsyscall()
}
} }
c <- foo == 42 runtime.Exitsyscall()
}() }
} _ = foo
for p := 0; p < procs; p++ { })
<-c
}
} }
...@@ -370,24 +370,11 @@ func TestSchedLocalQueueSteal(t *testing.T) { ...@@ -370,24 +370,11 @@ func TestSchedLocalQueueSteal(t *testing.T) {
} }
func benchmarkStackGrowth(b *testing.B, rec int) { func benchmarkStackGrowth(b *testing.B, rec int) {
const CallsPerSched = 1000 b.RunParallel(func(pb *testing.PB) {
procs := runtime.GOMAXPROCS(-1) for pb.Next() {
N := int32(b.N / CallsPerSched) stackGrowthRecursive(rec)
c := make(chan bool, procs) }
for p := 0; p < procs; p++ { })
go func() {
for atomic.AddInt32(&N, -1) >= 0 {
runtime.Gosched()
for g := 0; g < CallsPerSched; g++ {
stackGrowthRecursive(rec)
}
}
c <- true
}()
}
for p := 0; p < procs; p++ {
<-c
}
} }
func BenchmarkStackGrowth(b *testing.B) { func BenchmarkStackGrowth(b *testing.B) {
......
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