benchmarks/pool.rb in oxblood-0.1.0.dev12 vs benchmarks/pool.rb in oxblood-0.1.0

- old
+ new

@@ -1,45 +1,64 @@ require 'concurrent' require 'redis' require 'oxblood' require 'benchmark' -N = 10_000 +N = 5_000 TASKS = 1_000 -POOL_SIZE = 32 +POOL_SIZE = Concurrent.processor_count + 1 +Concurrent.use_stdlib_logger(Logger::DEBUG) + def worker_pool - Concurrent::FixedThreadPool.new(POOL_SIZE * 2) + Concurrent::FixedThreadPool.new(POOL_SIZE) end -RedisPool = ConnectionPool.new(size: POOL_SIZE) { Redis.new } +RedisPool = ConnectionPool.new(size: POOL_SIZE) { Redis.new(driver: :ruby) } OxbloodPool = Oxblood::Pool.new(size: POOL_SIZE) def benchmark(label, &blk) sec = Benchmark.realtime(&blk) puts [label, sec.round(3)].join(': ') end -def run(&blk) +def run(name, &blk) + puts "Running #{name}" + pool = worker_pool TASKS.times { pool.post(&blk) } - sleep 0.1 while pool.completed_task_count != TASKS + sleep 0.1 while pool.scheduled_task_count != TASKS + pool.shutdown + pool.wait_for_termination + puts "#{pool.completed_task_count} tasks finished successfully" end def redis - RedisPool.with { |r| r.pipelined { N.times { r.ping } } } + RedisPool.with do |r| + r.pipelined do + N.times { r.ping } + end + end end def oxblood - OxbloodPool.pipelined { |p| N.times { p.ping } } + OxbloodPool.with do |s| + s.pipelined do |p| + N.times { p.ping } + end + end end +# Check that everything is working +redis +oxblood + # Warmup JVM if RUBY_ENGINE == 'jruby' 10.times do redis oxblood end end -benchmark('redis-rb') { run { redis } } -benchmark('oxblood') { run { oxblood } } +benchmark('redis-rb') { run('redis-rb') { redis } } +benchmark('oxblood') { run('oxblood') { oxblood } }