Add thread benchmark.
@@ -53,4 +53,61 @@ func BenchmarkBasicMath(b *testing.B) {
c = a % b
`)
})
+ b.Run("thread_and_channel", func(b *testing.B) {
+ script := `
+THREAD_COUNT = 503
+RES = Channel.new
+
+class Receiver
+ def initialize(name)
+ @name = name
+ @mailbox = Channel.new
+ end
+
+ def next=(n)
+ @next = n
+ end
+
+ def put(msg)
+ @mailbox.deliver(msg)
+ end
+
+ def message_loop
+ while true do
+ msg = @mailbox.receive
+ if msg == 0
+ RES.deliver(@name)
+ else
+ @next.put(msg - 1)
+ end
+ end
+ end
+
+ def run
+ thread do
+ message_loop
+ end
+ end
+end
+
+receivers = []
+THREAD_COUNT.times do |i|
+ r = Receiver.new(i + 1)
+ receivers[i] = r
+ if i > 0
+ receivers[i-1].next = r
+ end
+end
+
+receivers[THREAD_COUNT - 1].next = receivers[0]
+
+THREAD_COUNT.times do |i|
+ receivers[i].run
+end
+
+receivers[0].put(1000)
+RES.receive
+`
+ runBench(b, script)
+ })
}
\ No newline at end of file
