Skip to content

Instantly share code, notes, and snippets.

Created May 24, 2013 07:50
Show Gist options
  • Save anonymous/5641961 to your computer and use it in GitHub Desktop.
Save anonymous/5641961 to your computer and use it in GitHub Desktop.
require 'goliath'
class Echo < Goliath::API
use Goliath::Rack::Params
use Goliath::Rack::Validation::RequiredParam, {:key => 'delay'}
def response(env)
EM::Synchrony.sleep params['delay']
[200, {}, params['delay']]
end
end
require 'em-http-request'
EM.run do
conn = EM::HttpRequest.new('http://localhost:9000/')
start = Time.now
r1 = conn.get :query => {delay: 1.5}, :keepalive => true
r2 = conn.get :query => {delay: 1.0}
r2.callback do
p Time.now - start # => 1.5 - keep-alive + pipelining
EM.stop
end
end
require "em-synchrony"
require "em-synchrony/em-http"
EventMachine.synchrony do
start = Time.now
multi = EventMachine::Synchrony::Multi.new
multi.add :a, EventMachine::HttpRequest.new("http://localhost:9000?delay=1").aget
multi.add :b, EventMachine::HttpRequest.new("http://localhost:9000?delay=1.5").aget
res = multi.perform
p "Look ma, no callbacks, and parallel HTTP requests!"
p Time.now - start # => 1.5 - keep-alive + pipelining
EventMachine.stop
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment