Created
July 4, 2016 14:49
-
-
Save prathmeshranaut/ea95bd791df1e613a0d6b185d404c267 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "rubygems" | |
require "bundler/setup" | |
require "celluloid/autostart" | |
require "benchmark/ips" | |
class BenchmarkingActor | |
include Celluloid | |
def initialize | |
@condition = Condition.new | |
end | |
def example_method; end | |
def finished | |
@condition.signal | |
end | |
def wait_until_finished | |
@condition.wait | |
end | |
end | |
example_actor = BenchmarkingActor.new | |
mailbox = Celluloid::Mailbox.new | |
latch_in = Queue.new | |
latch_out = Queue.new | |
latch = Thread.new do | |
loop do | |
n = latch_in.pop | |
for i in 0..n; mailbox.receive; end | |
latch_out << :done | |
end | |
end | |
Benchmark.ips do |ips| | |
ips.report("spawn") { BenchmarkingActor.new.terminate } | |
ips.report("calls") { example_actor.example_method } | |
ips.report("async calls") do |n| | |
waiter = example_actor.future.wait_until_finished | |
(n - 1).times { example_actor.async.example_method } | |
example_actor.async.finished | |
waiter.value | |
end | |
ips.report("messages") do |n| | |
latch_in << n | |
for i in 0..n; mailbox << :message; end | |
latch_out.pop | |
end | |
end | |
------------------------- | |
Benchmarking Results | |
------------------------- | |
spawn 419 i/100ms | |
calls 1319 i/100ms | |
async calls 682 i/100ms | |
messages 6853 i/100ms | |
------------------------------------------------- | |
spawn 4413.2 (±7.9%) i/s - 22207 in 5.063493s | |
calls 14054.2 (±12.5%) i/s - 69907 in 5.061426s | |
async calls 26224.3 (±21.9%) i/s - 122760 in 5.015581s | |
messages 470327.4 (±13.3%) i/s - 2302608 in 5.010365s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks good.