-
-
Save tmm1/111491 to your computer and use it in GitHub Desktop.
net/http vs fibered em-http
This file contains hidden or 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
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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
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