ruby-changes:46415
From: normal <ko1@a...>
Date: Tue, 2 May 2017 11:40:51 +0900 (JST)
Subject: [ruby-changes:46415] normal:r58535 (trunk): more benchmarks for SizedQueue and ConditionVariable
normal 2017-05-02 11:40:46 +0900 (Tue, 02 May 2017) New Revision: 58535 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58535 Log: 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 Added files: trunk/benchmark/bm_vm_thread_condvar1.rb trunk/benchmark/bm_vm_thread_condvar2.rb trunk/benchmark/bm_vm_thread_sized_queue2.rb trunk/benchmark/bm_vm_thread_sized_queue3.rb trunk/benchmark/bm_vm_thread_sized_queue4.rb Modified files: trunk/benchmark/bm_vm_thread_sized_queue.rb Index: benchmark/bm_vm_thread_sized_queue.rb =================================================================== --- benchmark/bm_vm_thread_sized_queue.rb (revision 58534) +++ benchmark/bm_vm_thread_sized_queue.rb (revision 58535) @@ -1,4 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/bm_vm_thread_sized_queue.rb#L1 require 'thread' +# on producer, one consumer n = 1_000_000 q = Thread::SizedQueue.new(100) Index: benchmark/bm_vm_thread_condvar2.rb =================================================================== --- benchmark/bm_vm_thread_condvar2.rb (nonexistent) +++ benchmark/bm_vm_thread_condvar2.rb (revision 58535) @@ -0,0 +1,35 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/bm_vm_thread_condvar2.rb#L1 +# 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) Index: benchmark/bm_vm_thread_sized_queue4.rb =================================================================== --- benchmark/bm_vm_thread_sized_queue4.rb (nonexistent) +++ benchmark/bm_vm_thread_sized_queue4.rb (revision 58535) @@ -0,0 +1,26 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/bm_vm_thread_sized_queue4.rb#L1 +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) Index: benchmark/bm_vm_thread_sized_queue2.rb =================================================================== --- benchmark/bm_vm_thread_sized_queue2.rb (nonexistent) +++ benchmark/bm_vm_thread_sized_queue2.rb (revision 58535) @@ -0,0 +1,23 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/bm_vm_thread_sized_queue2.rb#L1 +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) Index: benchmark/bm_vm_thread_condvar1.rb =================================================================== --- benchmark/bm_vm_thread_condvar1.rb (nonexistent) +++ benchmark/bm_vm_thread_condvar1.rb (revision 58535) @@ -0,0 +1,28 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/bm_vm_thread_condvar1.rb#L1 +# 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 Index: benchmark/bm_vm_thread_sized_queue3.rb =================================================================== --- benchmark/bm_vm_thread_sized_queue3.rb (nonexistent) +++ benchmark/bm_vm_thread_sized_queue3.rb (revision 58535) @@ -0,0 +1,22 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/bm_vm_thread_sized_queue3.rb#L1 +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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/