Skip to content

Commit

Permalink
Add standard library atomic operations to benchmark (#35)
Browse files Browse the repository at this point in the history
Currently, we only benchmark our custom atomic types, but don't
benchmark the standard library types. Add benchmarks for ints and uints
that the standard library supports.
  • Loading branch information
prashantv authored Nov 14, 2017
1 parent 54e9e20 commit 8474b86
Showing 1 changed file with 71 additions and 7 deletions.
78 changes: 71 additions & 7 deletions stress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
package atomic

import (
"math"
"runtime"
"sync"
"sync/atomic"
"testing"
)

Expand All @@ -32,13 +34,17 @@ const (
)

var _stressTests = map[string]func() func(){
"i32": stressInt32,
"i64": stressInt64,
"u32": stressUint32,
"u64": stressUint64,
"f64": stressFloat64,
"bool": stressBool,
"string": stressString,
"i32/std": stressStdInt32,
"i32": stressInt32,
"i64/std": stressStdInt32,
"i64": stressInt64,
"u32/std": stressStdUint32,
"u32": stressUint32,
"u64/std": stressStdUint64,
"u64": stressUint64,
"f64": stressFloat64,
"bool": stressBool,
"string": stressString,
}

func TestStress(t *testing.T) {
Expand Down Expand Up @@ -87,6 +93,20 @@ func BenchmarkStress(b *testing.B) {
}
}

func stressStdInt32() func() {
var atom int32
return func() {
atomic.LoadInt32(&atom)
atomic.AddInt32(&atom, 1)
atomic.AddInt32(&atom, -2)
atomic.AddInt32(&atom, 1)
atomic.AddInt32(&atom, -1)
atomic.CompareAndSwapInt32(&atom, 1, 0)
atomic.SwapInt32(&atom, 5)
atomic.StoreInt32(&atom, 1)
}
}

func stressInt32() func() {
var atom Int32
return func() {
Expand All @@ -101,6 +121,20 @@ func stressInt32() func() {
}
}

func stressStdInt64() func() {
var atom int64
return func() {
atomic.LoadInt64(&atom)
atomic.AddInt64(&atom, 1)
atomic.AddInt64(&atom, -2)
atomic.AddInt64(&atom, 1)
atomic.AddInt64(&atom, -1)
atomic.CompareAndSwapInt64(&atom, 1, 0)
atomic.SwapInt64(&atom, 5)
atomic.StoreInt64(&atom, 1)
}
}

func stressInt64() func() {
var atom Int64
return func() {
Expand All @@ -115,6 +149,21 @@ func stressInt64() func() {
}
}

func stressStdUint32() func() {
var atom uint32
return func() {
atomic.LoadUint32(&atom)
atomic.AddUint32(&atom, 1)
// Adding `MaxUint32` is the same as subtracting 1
atomic.AddUint32(&atom, math.MaxUint32-1)
atomic.AddUint32(&atom, 1)
atomic.AddUint32(&atom, math.MaxUint32)
atomic.CompareAndSwapUint32(&atom, 1, 0)
atomic.SwapUint32(&atom, 5)
atomic.StoreUint32(&atom, 1)
}
}

func stressUint32() func() {
var atom Uint32
return func() {
Expand All @@ -129,6 +178,21 @@ func stressUint32() func() {
}
}

func stressStdUint64() func() {
var atom uint64
return func() {
atomic.LoadUint64(&atom)
atomic.AddUint64(&atom, 1)
// Adding `MaxUint64` is the same as subtracting 1
atomic.AddUint64(&atom, math.MaxUint64-1)
atomic.AddUint64(&atom, 1)
atomic.AddUint64(&atom, math.MaxUint64)
atomic.CompareAndSwapUint64(&atom, 1, 0)
atomic.SwapUint64(&atom, 5)
atomic.StoreUint64(&atom, 1)
}
}

func stressUint64() func() {
var atom Uint64
return func() {
Expand Down

0 comments on commit 8474b86

Please sign in to comment.