Skip to content

Instantly share code, notes, and snippets.

@ryangreenberg
Last active December 12, 2015 03:58
Show Gist options
  • Save ryangreenberg/4711186 to your computer and use it in GitHub Desktop.
Save ryangreenberg/4711186 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -KU -rubygems
require 'sinatra'
get '/:time' do
sleep params[:time].to_i
"Just slept for #{sleep_time} seconds"
end
#!/usr/bin/env ruby -KU -rubygems
require 'rest_client'
require 'open-uri'
require 'net/http'
require 'socket'
SLEEP_URL = "http://localhost:4567/5" # server will sleep for 2 seconds
SLEEP_URI = URI.parse(SLEEP_URL)
request_methods = [
lambda { RestClient::Request.execute(:method => "GET", :url => SLEEP_URL) },
lambda { open(SLEEP_URL).read },
lambda { Net::HTTP.get(URI.parse(SLEEP_URL)) },
lambda do
host = SLEEP_URI.host
port = SLEEP_URI.port
path = SLEEP_URI.path
request = "GET #{path} HTTP/1.0\r\n\r\n"
socket = TCPSocket.open(host, port)
socket.print(request)
response = socket.read
end
]
request_methods.each do |ea|
start_time = Time.now
begin
Timeout.timeout(1) do
ea.call
end
rescue Timeout::Error => e
print "Timeout..."
end
puts "Request lasted for #{Time.now - start_time}"
end
Timeout...Request lasted for 1.001413
Timeout...Request lasted for 1.001712
Timeout...Request lasted for 1.001111
Timeout...Request lasted for 1.001298
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment