From 8c37de156b06eeb4f05d5852b5d73c00239e1ba9 Mon Sep 17 00:00:00 2001 From: normal Date: Tue, 2 May 2017 02:40:46 +0000 Subject: 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 --- benchmark/bm_vm_thread_condvar1.rb | 28 +++++++++++++++++++++++++++ benchmark/bm_vm_thread_condvar2.rb | 35 ++++++++++++++++++++++++++++++++++ benchmark/bm_vm_thread_sized_queue.rb | 1 + benchmark/bm_vm_thread_sized_queue2.rb | 23 ++++++++++++++++++++++ benchmark/bm_vm_thread_sized_queue3.rb | 22 +++++++++++++++++++++ benchmark/bm_vm_thread_sized_queue4.rb | 26 +++++++++++++++++++++++++ 6 files changed, 135 insertions(+) create mode 100644 benchmark/bm_vm_thread_condvar1.rb create mode 100644 benchmark/bm_vm_thread_condvar2.rb create mode 100644 benchmark/bm_vm_thread_sized_queue2.rb create mode 100644 benchmark/bm_vm_thread_sized_queue3.rb create mode 100644 benchmark/bm_vm_thread_sized_queue4.rb (limited to 'benchmark') diff --git a/benchmark/bm_vm_thread_condvar1.rb b/benchmark/bm_vm_thread_condvar1.rb new file mode 100644 index 0000000000..cf5706b23e --- /dev/null +++ b/benchmark/bm_vm_thread_condvar1.rb @@ -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 diff --git a/benchmark/bm_vm_thread_condvar2.rb b/benchmark/bm_vm_thread_condvar2.rb new file mode 100644 index 0000000000..7c8dc19481 --- /dev/null +++ b/benchmark/bm_vm_thread_condvar2.rb @@ -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) diff --git a/benchmark/bm_vm_thread_sized_queue.rb b/benchmark/bm_vm_thread_sized_queue.rb index ce15367aba..7b9af5482b 100644 --- a/benchmark/bm_vm_thread_sized_queue.rb +++ b/benchmark/bm_vm_thread_sized_queue.rb @@ -1,4 +1,5 @@ require 'thread' +# on producer, one consumer n = 1_000_000 q = Thread::SizedQueue.new(100) diff --git a/benchmark/bm_vm_thread_sized_queue2.rb b/benchmark/bm_vm_thread_sized_queue2.rb new file mode 100644 index 0000000000..de9f55e978 --- /dev/null +++ b/benchmark/bm_vm_thread_sized_queue2.rb @@ -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) diff --git a/benchmark/bm_vm_thread_sized_queue3.rb b/benchmark/bm_vm_thread_sized_queue3.rb new file mode 100644 index 0000000000..ce5f1796d8 --- /dev/null +++ b/benchmark/bm_vm_thread_sized_queue3.rb @@ -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 diff --git a/benchmark/bm_vm_thread_sized_queue4.rb b/benchmark/bm_vm_thread_sized_queue4.rb new file mode 100644 index 0000000000..a9b7d80ec0 --- /dev/null +++ b/benchmark/bm_vm_thread_sized_queue4.rb @@ -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) -- cgit v1.2.3