Skip to content

Instantly share code, notes, and snippets.

@lcowell
Created March 14, 2012 03:33
Show Gist options
  • Save lcowell/2033824 to your computer and use it in GitHub Desktop.
Save lcowell/2033824 to your computer and use it in GitHub Desktop.
em_inline_benchmark
require 'eventmachine'
require 'em-synchrony'
require "em-synchrony/em-http"
require 'net/http'
require 'benchmark'
def em_http
EM.synchrony do
multi = EventMachine::Synchrony::Multi.new
multi.add :page1, EventMachine::HttpRequest.new("http://localhost:3000/page1").aget
multi.add :page2, EventMachine::HttpRequest.new("http://localhost:3000/page1").aget
multi.add :page3, EventMachine::HttpRequest.new("http://localhost:3000/page1").aget
multi.add :page4, EventMachine::HttpRequest.new("http://localhost:3000/page1").aget
data = multi.perform.responses[:callback].values
EventMachine.stop
end
end
def inline_http
(1..4).each do |n|
uri = URI.parse("http://localhost:3000/page#{n}")
Net::HTTP.get(uri)
end
end
Benchmark.bmbm do |x|
x.report("em") { em_http }
x.report("inline") { inline_http }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment