Skip to content

Instantly share code, notes, and snippets.

@careo
Created December 31, 2009 21:05
Show Gist options
  • Select an option

  • Save careo/266911 to your computer and use it in GitHub Desktop.

Select an option

Save careo/266911 to your computer and use it in GitHub Desktop.
require 'uri'
require 'rubygems'
require 'fiber'
require 'eventmachine'
require 'em-http'
#require 'net/http'
#require "rest_client"
module Net
module HTTP
def self.start host, port
http = Request.new
uri = URI.parse ""
uri.host = host
uri.port = port
http.uri = uri
return http
end
class Request
attr_accessor :request, :uri
def self.start host, port
end
def request req
f = Fiber.current
uri.path = req.path
case req
when Get
http = EventMachine::HttpRequest.new(uri).get :timeout => 60
end
http.callback { f.resume(Response.new(http)) }
http.errback { f.resume(Response.new(http)) }
return Fiber.yield
end
end
class HTTPRequest
attr_accessor :path
def initialize path
@path = path
end
end
class Get < HTTPRequest
end
class Response
attr_accessor :http
def initialize http
@http = http
end
def body
@http.response
end
end
end
end
def request
url = URI.parse "http://localhost:3000/delay/5"
req = Net::HTTP::Get.new(url.path)
http = Net::HTTP.start(url.host, url.port)
res = http.request(req)
res.body
end
def rest_client
RestClient.get("http://localhost:3000/delay/5")
end
EM.run do
Fiber.new do
start = Time.now
10.times do
before = Time.now
response = request
#response = rest_client
after = Time.now
p [after-before, response]
end
finish = Time.now
puts "Total time: #{finish - start}"
end.resume
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment