mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
more benchmarks for SizedQueue and ConditionVariable
I'm working on patches to improve everything in thread_sync.c, add these to track progress. * benchmark/bm_vm_thread_sized_queue.rb: add description * benchmark/bm_vm_thread_condvar1.rb: new benchmark * benchmark/bm_vm_thread_condvar2.rb: ditto * benchmark/bm_vm_thread_sized_queue2.rb: ditto * benchmark/bm_vm_thread_sized_queue3.rb: ditto * benchmark/bm_vm_thread_sized_queue4.rb: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58535 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ca8f454f09
commit
7ef49716d5
6 changed files with 135 additions and 0 deletions
28
benchmark/bm_vm_thread_condvar1.rb
Normal file
28
benchmark/bm_vm_thread_condvar1.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
# two threads, two mutex, two condvar ping-pong
|
||||
require 'thread'
|
||||
m1 = Mutex.new
|
||||
m2 = Mutex.new
|
||||
cv1 = ConditionVariable.new
|
||||
cv2 = ConditionVariable.new
|
||||
max = 100000
|
||||
i = 0
|
||||
wait = nil
|
||||
m2.synchronize do
|
||||
wait = Thread.new do
|
||||
m1.synchronize do
|
||||
m2.synchronize { cv2.signal }
|
||||
while (i += 1) < max
|
||||
cv1.wait(m1)
|
||||
cv2.signal
|
||||
end
|
||||
end
|
||||
end
|
||||
cv2.wait(m2)
|
||||
end
|
||||
m1.synchronize do
|
||||
while i < max
|
||||
cv1.signal
|
||||
cv2.wait(m1)
|
||||
end
|
||||
end
|
||||
wait.join
|
35
benchmark/bm_vm_thread_condvar2.rb
Normal file
35
benchmark/bm_vm_thread_condvar2.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
# many threads, one mutex, many condvars
|
||||
require 'thread'
|
||||
m = Mutex.new
|
||||
cv1 = ConditionVariable.new
|
||||
cv2 = ConditionVariable.new
|
||||
max = 1000
|
||||
n = 100
|
||||
waiting = 0
|
||||
scvs = []
|
||||
waiters = n.times.map do |i|
|
||||
start_cv = ConditionVariable.new
|
||||
scvs << start_cv
|
||||
start_mtx = Mutex.new
|
||||
start_mtx.synchronize do
|
||||
th = Thread.new(start_mtx, start_cv) do |sm, scv|
|
||||
m.synchronize do
|
||||
sm.synchronize { scv.signal }
|
||||
max.times do
|
||||
cv2.signal if (waiting += 1) == n
|
||||
cv1.wait(m)
|
||||
end
|
||||
end
|
||||
end
|
||||
start_cv.wait(start_mtx)
|
||||
th
|
||||
end
|
||||
end
|
||||
m.synchronize do
|
||||
max.times do
|
||||
cv2.wait(m) until waiting == n
|
||||
waiting = 0
|
||||
cv1.broadcast
|
||||
end
|
||||
end
|
||||
waiters.each(&:join)
|
|
@ -1,4 +1,5 @@
|
|||
require 'thread'
|
||||
# on producer, one consumer
|
||||
|
||||
n = 1_000_000
|
||||
q = Thread::SizedQueue.new(100)
|
||||
|
|
23
benchmark/bm_vm_thread_sized_queue2.rb
Normal file
23
benchmark/bm_vm_thread_sized_queue2.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
require 'thread'
|
||||
# one producer, many consumers
|
||||
n = 1_000_000
|
||||
m = 10
|
||||
q = Thread::SizedQueue.new(100)
|
||||
consumers = m.times.map do
|
||||
Thread.new do
|
||||
while q.pop
|
||||
# consuming
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
producer = Thread.new do
|
||||
while n > 0
|
||||
q.push true
|
||||
n -= 1
|
||||
end
|
||||
m.times { q.push nil }
|
||||
end
|
||||
|
||||
producer.join
|
||||
consumers.each(&:join)
|
22
benchmark/bm_vm_thread_sized_queue3.rb
Normal file
22
benchmark/bm_vm_thread_sized_queue3.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
require 'thread'
|
||||
# many producers, one consumer
|
||||
n = 1_000_000
|
||||
m = 10
|
||||
q = Thread::SizedQueue.new(100)
|
||||
consumer = Thread.new do
|
||||
while q.pop
|
||||
# consuming
|
||||
end
|
||||
end
|
||||
|
||||
producers = m.times.map do
|
||||
Thread.new do
|
||||
while n > 0
|
||||
q.push true
|
||||
n -= 1
|
||||
end
|
||||
end
|
||||
end
|
||||
producers.each(&:join)
|
||||
q.push nil
|
||||
consumer.join
|
26
benchmark/bm_vm_thread_sized_queue4.rb
Normal file
26
benchmark/bm_vm_thread_sized_queue4.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
require 'thread'
|
||||
# many producers, many consumers
|
||||
nr = 1_000_000
|
||||
n = 10
|
||||
m = 10
|
||||
q = Thread::SizedQueue.new(100)
|
||||
consumers = n.times.map do
|
||||
Thread.new do
|
||||
while q.pop
|
||||
# consuming
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
producers = m.times.map do
|
||||
Thread.new do
|
||||
while nr > 0
|
||||
q.push true
|
||||
nr -= 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
producers.each(&:join)
|
||||
n.times { q.push nil }
|
||||
consumers.each(&:join)
|
Loading…
Reference in a new issue