aboutsummaryrefslogtreecommitdiff
path: root/tests/benchmarks/ping-pong
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2025-01-23 09:54:11 -0300
committerEuAndreh <eu@euandre.org>2025-01-23 11:36:44 -0300
commit5f6607152a96b85f1518028b8f1ee1ef1aefda75 (patch)
treebe54946ddb2510a317322a2660563c83cab2117c /tests/benchmarks/ping-pong
parenttests/stm.go: Turn example into functional test (diff)
downloadstm-5f6607152a96b85f1518028b8f1ee1ef1aefda75.tar.gz
stm-5f6607152a96b85f1518028b8f1ee1ef1aefda75.tar.xz
Move benchmarks from tests/stm.go to tests/benchmarks/*
Diffstat (limited to 'tests/benchmarks/ping-pong')
l---------tests/benchmarks/ping-pong/main.go1
-rw-r--r--tests/benchmarks/ping-pong/stm.go59
2 files changed, 60 insertions, 0 deletions
diff --git a/tests/benchmarks/ping-pong/main.go b/tests/benchmarks/ping-pong/main.go
new file mode 120000
index 0000000..f67563d
--- /dev/null
+++ b/tests/benchmarks/ping-pong/main.go
@@ -0,0 +1 @@
+../../main.go \ No newline at end of file
diff --git a/tests/benchmarks/ping-pong/stm.go b/tests/benchmarks/ping-pong/stm.go
new file mode 100644
index 0000000..6631ea8
--- /dev/null
+++ b/tests/benchmarks/ping-pong/stm.go
@@ -0,0 +1,59 @@
+package stm
+
+import (
+ "flag"
+ "sync"
+)
+
+
+
+func testPingPong(n int, afterHit func(string)) {
+ ball := NewBuiltinEqVar(false)
+ doneVar := NewVar(false)
+ hits := NewVar(0)
+ ready := NewVar(true) // The ball is ready for hitting.
+ var wg sync.WaitGroup
+ bat := func(from, to bool, noise string) {
+ defer wg.Done()
+ for !Atomically(func(tx *Tx) any {
+ if doneVar.Get(tx) {
+ return true
+ }
+ tx.Assert(ready.Get(tx))
+ if ball.Get(tx) == from {
+ ball.Set(tx, to)
+ hits.Set(tx, hits.Get(tx)+1)
+ ready.Set(tx, false)
+ return false
+ }
+ return tx.Retry()
+ }).(bool) {
+ afterHit(noise)
+ AtomicSet(ready, true)
+ }
+ }
+ wg.Add(2)
+ go bat(false, true, "ping!")
+ go bat(true, false, "pong!")
+ Atomically(VoidOperation(func(tx *Tx) {
+ tx.Assert(hits.Get(tx) >= n)
+ doneVar.Set(tx, true)
+ }))
+ wg.Wait()
+}
+
+
+var nFlag = flag.Int(
+ "n",
+ 1_000,
+ "The number of iterations to execute",
+)
+
+func MainTest() {
+ flag.Parse()
+ n := *nFlag
+
+ for i := 0; i < n; i++ {
+ testPingPong(n, func(string) {})
+ }
+}