From 70bd1261d36be490ebd22a62b385a3c5d23b6240 Mon Sep 17 00:00:00 2001 From: Bill Fumerola Date: Tue, 9 May 2017 15:23:09 -0700 Subject: [PATCH] Use stress tests for benchmarks --- stress_test.go | 178 ++++++++++++++++++++++++++----------------------- 1 file changed, 95 insertions(+), 83 deletions(-) diff --git a/stress_test.go b/stress_test.go index 3628c3d..72a65bd 100644 --- a/stress_test.go +++ b/stress_test.go @@ -31,112 +31,124 @@ const ( _iterations = 1000 ) -func runStress(f func()) { - defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(_parallelism)) +var _stressTests = map[string]func(){ + "i32": stressInt32, + "i64": stressInt64, + "u32": stressUint32, + "u64": stressUint64, + "f64": stressFloat64, + "bool": stressBool, + "string": stressString, +} - var wg sync.WaitGroup - wg.Add(_parallelism) - for i := 0; i < _parallelism; i++ { - go func() { - defer wg.Done() - for j := 0; j < _iterations; j++ { +func TestStress(t *testing.T) { + for name, f := 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) + for i := 0; i < _parallelism; i++ { + go func() { + defer wg.Done() + <-start + for j := 0; j < _iterations; j++ { + f() + } + }() + } + close(start) + wg.Wait() + }) + } +} + +func BenchmarkStress(b *testing.B) { + for name, f := range _stressTests { + b.Run(name, func(b *testing.B) { + for i := 0; i < b.N; i++ { f() } - }() + }) } - - wg.Wait() } -func TestStressInt32(t *testing.T) { +func stressInt32() { 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) - }) + 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) { +func stressInt64() { 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) - - }) + 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) { +func stressUint32() { 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) - }) + 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) { +func stressUint64() { 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) - }) + atom.Load() + atom.Add(1) + atom.Sub(2) + atom.Inc() + atom.Dec() + atom.CAS(1, 0) + atom.Swap(5) + atom.Store(1) } -func TestStressFloat64(t *testing.T) { +func stressFloat64() { var atom Float64 - runStress(func() { - atom.Load() - atom.CAS(1.0, 0.1) - atom.Add(1.1) - atom.Sub(0.2) - atom.Store(1.0) - }) + atom.Load() + atom.CAS(1.0, 0.1) + atom.Add(1.1) + atom.Sub(0.2) + atom.Store(1.0) } -func TestStressBool(t *testing.T) { +func stressBool() { var atom Bool - runStress(func() { - atom.Load() - atom.Store(false) - atom.Swap(true) - atom.CAS(true, false) - atom.CAS(true, false) - atom.Load() - atom.Toggle() - atom.Toggle() - }) + atom.Load() + atom.Store(false) + atom.Swap(true) + atom.CAS(true, false) + atom.CAS(true, false) + atom.Load() + atom.Toggle() + atom.Toggle() } -func TestStressString(t *testing.T) { +func stressString() { var atom String - runStress(func() { - atom.Load() - atom.Store("abc") - atom.Load() - atom.Store("def") - - }) + atom.Load() + atom.Store("abc") + atom.Load() + atom.Store("def") + atom.Load() + atom.Store("") }