Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created December 7, 2009 23:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenderlove/251244 to your computer and use it in GitHub Desktop.
Save tenderlove/251244 to your computer and use it in GitHub Desktop.
class Net::HTTP::BufferedIO < Net::BufferedIO
@@patch = false
def self.patch= v; @@patch = v; end
def rbuf_fill
return super unless @@patch
begin
@rbuf << @io.read_nonblock(BUFSIZE)
rescue Errno::EWOULDBLOCK, Errno::EAGAIN
retry unless @read_timeout
if IO.select([@io], nil, nil, @read_timeout)
retry
else
raise Timeout::Error, 'IO timeout'
end
end
end
end
require 'net/http'
require 'benchmark'
require 'io_select_http'
S = '/~apatterson/small.html' # => 150B
M = '/~apatterson/medium.html' # => 71K
L = '/~apatterson/large.html' # => 82M
GC.disable
n = 5000
m = 500
Benchmark.bm(14) do |x|
small_uri = URI.parse("http://localhost/#{S}")
medium_uri = URI.parse("http://localhost/#{M}")
large_uri = URI.parse("http://localhost/#{L}")
#x.report('small') do
# n.times do
# Net::HTTP.get(small_uri)
# end
#end
#x.report('small patched') do
# Net::HTTP::BufferedIO.patch = true
# n.times do
# Net::HTTP.get(small_uri)
# end
# Net::HTTP::BufferedIO.patch = false
#end
x.report('medium') do
m.times do
Net::HTTP.get(medium_uri)
end
end
x.report('medium patched') do
Net::HTTP::BufferedIO.patch = true
m.times do
Net::HTTP.get(medium_uri)
end
Net::HTTP::BufferedIO.patch = false
end
#x.report('large') do
# Net::HTTP.get(large_uri)
#end
#x.report('large patched') do
# Net::HTTP::BufferedIO.patch = true
# Net::HTTP.get(large_uri)
# Net::HTTP::BufferedIO.patch = false
#end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment