Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add standard library atomic operations to benchmark #35

Merged
merged 1 commit into from
Nov 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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