Concurrently modify same atomic in stress tests (#33)

Currently, the stress tests start multiple goroutines, but they all have
their own local atomic, so they are not concurrently modifying the same
values. Return a function which can be run concurrently to modify the
same atomic.
This commit is contained in:
Prashant Varanasi
2017-11-14 10:36:59 -08:00
committed by GitHub
parent e81582a97d
commit 0ee2c2d995

View File

@@ -31,7 +31,7 @@ const (
_iterations = 1000
)
var _stressTests = map[string]func(){
var _stressTests = map[string]func() func(){
"i32": stressInt32,
"i64": stressInt64,
"u32": stressUint32,
@@ -42,13 +42,14 @@ var _stressTests = map[string]func(){
}
func TestStress(t *testing.T) {
for name, f := range _stressTests {
for name, ff := range _stressTests {
t.Run(name, func(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(_parallelism))
start := make(chan struct{})
var wg sync.WaitGroup
wg.Add(_parallelism)
f := ff()
for i := 0; i < _parallelism; i++ {
go func() {
defer wg.Done()
@@ -65,8 +66,9 @@ func TestStress(t *testing.T) {
}
func BenchmarkStress(b *testing.B) {
for name, f := range _stressTests {
for name, ff := range _stressTests {
b.Run(name, func(b *testing.B) {
f := ff()
for i := 0; i < b.N; i++ {
f()
}
@@ -74,8 +76,9 @@ func BenchmarkStress(b *testing.B) {
}
}
func stressInt32() {
func stressInt32() func() {
var atom Int32
return func() {
atom.Load()
atom.Add(1)
atom.Sub(2)
@@ -85,9 +88,11 @@ func stressInt32() {
atom.Swap(5)
atom.Store(1)
}
}
func stressInt64() {
func stressInt64() func() {
var atom Int64
return func() {
atom.Load()
atom.Add(1)
atom.Sub(2)
@@ -97,9 +102,11 @@ func stressInt64() {
atom.Swap(5)
atom.Store(1)
}
}
func stressUint32() {
func stressUint32() func() {
var atom Uint32
return func() {
atom.Load()
atom.Add(1)
atom.Sub(2)
@@ -109,9 +116,11 @@ func stressUint32() {
atom.Swap(5)
atom.Store(1)
}
}
func stressUint64() {
func stressUint64() func() {
var atom Uint64
return func() {
atom.Load()
atom.Add(1)
atom.Sub(2)
@@ -121,18 +130,22 @@ func stressUint64() {
atom.Swap(5)
atom.Store(1)
}
}
func stressFloat64() {
func stressFloat64() func() {
var atom Float64
return func() {
atom.Load()
atom.CAS(1.0, 0.1)
atom.Add(1.1)
atom.Sub(0.2)
atom.Store(1.0)
}
}
func stressBool() {
func stressBool() func() {
var atom Bool
return func() {
atom.Load()
atom.Store(false)
atom.Swap(true)
@@ -142,9 +155,11 @@ func stressBool() {
atom.Toggle()
atom.Toggle()
}
}
func stressString() {
func stressString() func() {
var atom String
return func() {
atom.Load()
atom.Store("abc")
atom.Load()
@@ -152,3 +167,4 @@ func stressString() {
atom.Load()
atom.Store("")
}
}