Skip to content

Commit

Permalink
Update benchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Mar 17, 2018
1 parent 73764b7 commit 15a6f73
Showing 1 changed file with 55 additions and 20 deletions.
75 changes: 55 additions & 20 deletions benchmark/async_vs_lightio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,74 @@

require 'benchmark/ips'

def run_async(count = 10000)
#
# It's hard to know exactly how to interpret these results.
#
# The main takeaway is that contention causes issues and if systems are not
# designed with that in mind, it will impact performance.
#
# $ ruby async_vs_lightio.rb
# Warming up --------------------------------------
# lightio (synchronous)
# 1.831k i/100ms
# async (synchronous) 1.061k i/100ms
# lightio (parallel) 172.000 i/100ms
# async (parallel) 156.000 i/100ms
# Calculating -------------------------------------
# lightio (synchronous)
# 30.588k (± 5.3%) i/s - 305.777k in 10.024506s
# async (synchronous) 65.660k (± 8.0%) i/s - 651.454k in 10.012904s
# lightio (parallel) 32.401k (±18.1%) i/s - 313.040k in 10.002671s
# async (parallel) 91.578k (±13.6%) i/s - 893.724k in 9.995852s
#
# Comparison:
# async (parallel): 91578.1 i/s
# async (synchronous): 65660.1 i/s - 1.39x slower
# lightio (parallel): 32401.4 i/s - 2.83x slower
# lightio (synchronous): 30587.5 i/s - 2.99x slower

DURATION = 0.000001

def run_async(count, repeats = 10000)
Async::Reactor.run do |task|
tasks = count.times.map do
# LightIO::Beam is a thread-like executor, use it instead Thread
count.times.map do
task.async do |subtask|
# do some io operations in beam
subtask.sleep(0.0001)
repeats.times do
subtask.sleep(DURATION)
end
end
end

tasks.each(&:wait)
end.each(&:wait)
end
end

def run_lightio(count = 10000)
beams = count.times.map do
# LightIO::Beam is a thread-like executor, use it instead Thread
def run_lightio(count, repeats = 10000)
count.times.map do
LightIO::Beam.new do
# do some io operations in beam
LightIO.sleep(0.0001)
repeats.times do
LightIO.sleep(DURATION)
end
end
end

beams.each(&:join)
end.each(&:join)
end

Benchmark.ips do |benchmark|
benchmark.report("lightio") do |count|
run_lightio(count)
benchmark.time = 10
benchmark.warmup = 2

benchmark.report("lightio (synchronous)") do |count|
run_lightio(1, count)
end

benchmark.report("async (synchronous)") do |count|
run_async(1, count)
end

benchmark.report("lightio (parallel)") do |count|
run_lightio(32, count/32)
end

benchmark.report("async") do |count|
run_async(count)
benchmark.report("async (parallel)") do |count|
run_async(32, count/32)
end

benchmark.compare!
Expand Down

0 comments on commit 15a6f73

Please sign in to comment.