aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bench_test.go21
-rw-r--r--stm_test.go7
2 files changed, 27 insertions, 1 deletions
diff --git a/bench_test.go b/bench_test.go
index 3dbdf73..0d176d8 100644
--- a/bench_test.go
+++ b/bench_test.go
@@ -3,6 +3,8 @@ package stm
import (
"sync"
"testing"
+
+ "github.com/anacrolix/missinggo/iter"
)
func BenchmarkAtomicGet(b *testing.B) {
@@ -126,7 +128,24 @@ func BenchmarkReadVarChannel(b *testing.B) {
}
}
+func parallelPingPongs(b *testing.B, n int) {
+ var wg sync.WaitGroup
+ wg.Add(n)
+ for range iter.N(n) {
+ go func() {
+ defer wg.Done()
+ testPingPong(b, b.N, func(string) {})
+ }()
+ }
+ wg.Wait()
+}
+
+func BenchmarkPingPong4(b *testing.B) {
+ b.ReportAllocs()
+ parallelPingPongs(b, 4)
+}
+
func BenchmarkPingPong(b *testing.B) {
b.ReportAllocs()
- testPingPong(b, b.N, func(string) {})
+ parallelPingPongs(b, 1)
}
diff --git a/stm_test.go b/stm_test.go
index 28f56ec..aeb0907 100644
--- a/stm_test.go
+++ b/stm_test.go
@@ -1,8 +1,11 @@
package stm
import (
+ "sync"
"testing"
"time"
+
+ _ "github.com/anacrolix/envpprof"
)
func TestDecrement(t *testing.T) {
@@ -220,7 +223,9 @@ func testPingPong(t testing.TB, n int, afterHit func(string)) {
doneVar := NewVar(false)
hits := NewVar(0)
ready := NewVar(true) // The ball is ready for hitting.
+ var wg sync.WaitGroup
bat := func(from, to interface{}, noise string) {
+ defer wg.Done()
for !Atomically(func(tx *Tx) {
if tx.Get(doneVar).(bool) {
tx.Return(true)
@@ -238,12 +243,14 @@ func testPingPong(t testing.TB, n int, afterHit func(string)) {
AtomicSet(ready, true)
}
}
+ wg.Add(2)
go bat(false, true, "ping!")
go bat(true, false, "pong!")
Atomically(func(tx *Tx) {
tx.Assert(tx.Get(hits).(int) >= n)
tx.Set(doneVar, true)
})
+ wg.Wait()
}
func TestPingPong(t *testing.T) {