Add standard library atomic operations to benchmark (#35)
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.
This commit is contained in:
committed by
GitHub
parent
54e9e20165
commit
8474b86a5a
@@ -21,8 +21,10 @@
|
||||
package atomic
|
||||
|
||||
import (
|
||||
"math"
|
||||
"runtime"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -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) {
|
||||
@@ -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() {
|
||||
@@ -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() {
|
||||
@@ -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() {
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user