Created
February 3, 2009 00:12
-
-
Save tenderlove/57197 to your computer and use it in GitHub Desktop.
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
| class Net::BufferedIO #:nodoc: | |
| alias :old_rbuf_fill :rbuf_fill | |
| def rbuf_fill | |
| if @io.respond_to?(:read_nonblock) | |
| begin | |
| @rbuf << @io.read_nonblock(65536) | |
| rescue Errno::EWOULDBLOCK | |
| if IO.select([@io], nil, nil, @read_timeout) | |
| @rbuf << @io.read_nonblock(65536) | |
| else | |
| raise Timeout::TimeoutError | |
| end | |
| end | |
| else # SSL sockets do not have read_nonblock | |
| timeout(@read_timeout) { | |
| @rbuf << @io.sysread(65536) | |
| } | |
| end | |
| end | |
| end | |
| class KeepAliveClient | |
| @@mutex = Mutex.new | |
| @@connection_cache = Hash.new { |h,k| | |
| h[k] = { | |
| :connection => Net::HTTP.new(*k.split(':')).start, | |
| :options => {} | |
| } | |
| } | |
| class << self | |
| def get uri | |
| cache = @@connection_cache["#{uri.host}:#{uri.port}"] | |
| connection = cache[:connection] | |
| opts = cache[:options] | |
| @@mutex.synchronize { | |
| res = connection.get(uri.path, { | |
| 'Connection' => 'keep-alive', | |
| 'Keep-Alive' => '300', | |
| }) | |
| } | |
| end | |
| end | |
| end |
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
| user system total real | |
| net/http 0.600000 0.290000 0.890000 ( 17.072703) | |
| net/http2 0.570000 0.260000 0.830000 ( 5.463130) | |
| curb 0.110000 0.150000 0.260000 ( 18.505145) | |
| multicurb 0.360000 1.160000 1.520000 ( 4.958537) |
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' | |
| require 'benchmark' | |
| require 'rubygems' | |
| require 'curb' | |
| require 'keep_alive_client' | |
| N = 100 | |
| URL = 'http://www.pauldix.net/' | |
| p_url = URI.parse(URL) | |
| Benchmark.bm(10) do |x| | |
| x.report('net/http') { | |
| N.times { | |
| Net::HTTP.get(p_url) | |
| } | |
| } | |
| x.report('net/http2') { | |
| N.times { | |
| KeepAliveClient.get(p_url) | |
| } | |
| } | |
| x.report('curb') { | |
| N.times { | |
| c = Curl::Easy.perform(URL) | |
| } | |
| } | |
| x.report('multicurb') { | |
| multi = Curl::Multi.new | |
| N.times do | |
| easy = Curl::Easy.new(URL) do |curl| | |
| curl.on_success do |c| | |
| c.response_code | |
| end | |
| curl.on_failure do |c| | |
| puts "**** #{c.response_code}" | |
| end | |
| end | |
| multi.add(easy) | |
| end | |
| multi.perform | |
| } | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment