Skip to content

Commit

Permalink
Merge pull request #16 from uber-go/refactor_stress
Browse files Browse the repository at this point in the history
Refactor stress tests and wait for the results
  • Loading branch information
billf committed Dec 13, 2016
2 parents 9e99152 + 9058d5b commit 14746df
Showing 1 changed file with 78 additions and 86 deletions.
164 changes: 78 additions & 86 deletions stress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,116 +22,108 @@ package atomic

import (
"runtime"
"sync"
"testing"
)

const _parallelism = 4
const _iterations = 1000
const (
_parallelism = 4
_iterations = 1000
)

func TestStressInt32(t *testing.T) {
func runStress(f func()) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(_parallelism))
atom := &Int32{0}

var wg sync.WaitGroup
wg.Add(_parallelism)
for i := 0; i < _parallelism; i++ {
go func() {
defer wg.Done()
for j := 0; j < _iterations; j++ {
atom.Load()
atom.Add(1)
atom.Sub(2)
atom.Inc()
atom.Dec()
atom.CAS(1, 0)
atom.Swap(5)
atom.Store(1)
f()
}
}()
}

wg.Wait()
}

func TestStressInt32(t *testing.T) {
var atom Int32
runStress(func() {
atom.Load()
atom.Add(1)
atom.Sub(2)
atom.Inc()
atom.Dec()
atom.CAS(1, 0)
atom.Swap(5)
atom.Store(1)
})
}

func TestStressInt64(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(_parallelism))
atom := &Int64{0}
for i := 0; i < _parallelism; i++ {
go func() {
for j := 0; j < _iterations; j++ {
atom.Load()
atom.Add(1)
atom.Sub(2)
atom.Inc()
atom.Dec()
atom.CAS(1, 0)
atom.Swap(5)
atom.Store(1)
}
}()
}
var atom Int64
runStress(func() {
atom.Load()
atom.Add(1)
atom.Sub(2)
atom.Inc()
atom.Dec()
atom.CAS(1, 0)
atom.Swap(5)
atom.Store(1)

})
}

func TestStressUint32(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(_parallelism))
atom := &Uint32{0}
for i := 0; i < _parallelism; i++ {
go func() {
for j := 0; j < _iterations; j++ {
atom.Load()
atom.Add(1)
atom.Sub(2)
atom.Inc()
atom.Dec()
atom.CAS(1, 0)
atom.Swap(5)
atom.Store(1)
}
}()
}
var atom Uint32
runStress(func() {
atom.Load()
atom.Add(1)
atom.Sub(2)
atom.Inc()
atom.Dec()
atom.CAS(1, 0)
atom.Swap(5)
atom.Store(1)
})
}

func TestStressUint64(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(_parallelism))
atom := &Uint64{0}
for i := 0; i < _parallelism; i++ {
go func() {
for j := 0; j < _iterations; j++ {
atom.Load()
atom.Add(1)
atom.Sub(2)
atom.Inc()
atom.Dec()
atom.CAS(1, 0)
atom.Swap(5)
atom.Store(1)
}
}()
}
var atom Uint64
runStress(func() {
atom.Load()
atom.Add(1)
atom.Sub(2)
atom.Inc()
atom.Dec()
atom.CAS(1, 0)
atom.Swap(5)
atom.Store(1)
})
}

func TestStressBool(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(_parallelism))
atom := NewBool(false)
for i := 0; i < _parallelism; i++ {
go func() {
for j := 0; j < _iterations; j++ {
atom.Load()
atom.Store(false)
atom.Swap(true)
atom.Load()
atom.Toggle()
atom.Toggle()
}
}()
}
var atom Bool
runStress(func() {
atom.Load()
atom.Store(false)
atom.Swap(true)
atom.Load()
atom.Toggle()
atom.Toggle()
})
}

func TestStressString(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(_parallelism))
atom := NewString("")
for i := 0; i < _parallelism; i++ {
go func() {
for j := 0; j < _iterations; j++ {
atom.Load()
atom.Store("abc")
atom.Load()
atom.Store("def")
}
}()
}
var atom String
runStress(func() {
atom.Load()
atom.Store("abc")
atom.Load()
atom.Store("def")

})
}

0 comments on commit 14746df

Please sign in to comment.