Skip to content

Instantly share code, notes, and snippets.

@tmm1
Forked from mattetti/gist:111453
Created May 14, 2009 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tmm1/111491 to your computer and use it in GitHub Desktop.
Save tmm1/111491 to your computer and use it in GitHub Desktop.
net/http vs fibered em-http
require 'net/http'
require 'uri'
url = URI.parse 'http://localhost:5984/test/test'
req = Net::HTTP::Get.new(url.path)
start = Time.now
100.times do
http = Net::HTTP.start(url.host, 5984)
http.request(req)
end
p Time.now - start
# 2nd fastest
require 'net/http'
require 'uri'
url = URI.parse 'http://localhost:5984/test/test'
req = Net::HTTP::Get.new(url.path)
start = Time.now
http = Net::HTTP.start(url.host, 5984)
100.times do
http.request(req)
end
p Time.now - start
# fastest
require 'eventmachine'
require 'em-http'
require 'fiber'
def async_fetch(url)
f = Fiber.current
http = EventMachine::HttpRequest.new(url).get :timeout => 10
http.callback { f.resume(http) }
http.errback { f.resume(http) }
return Fiber.yield
end
start = Time.now
EventMachine.run do
n = 0
100.times do
Fiber.new{
data = async_fetch('http://localhost:5984/test/test')
EventMachine.stop if (n+=1) == 100
}.resume
end
end
p Time.now - start
# slowest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment