net-http-persistent icon indicating copy to clipboard operation
net-http-persistent copied to clipboard

Persisting the ConnectionPool?

Open AnilRh opened this issue 1 year ago • 0 comments

Perhaps I'm missing something obvious, but when I try to use this gem to optimize multiple calls to the same endpoint, but from different code blocks, it doesn't work.

Version 2.9.4 of the gem worked fine. The ConnectionPool in V2 appears to have been stored as class variable. When upgrading to version 3.0.1 I only see optimizations if I keep the instance of Net::HTTP::Persistent.new around and use it to make all of my calls.

Am I doing something stupid? (btw, the reason I can't keep the instance around is that I'm actually making a call through Savon, which uses HTTI, and it creates the the Net::HTTP::Persistent instance.

Example Code:

The setup (not interesting, just gets us ready):

require 'httpi'
require 'benchmark'

def bench(&block)
  time = Benchmark.measure do
    5.times do |i|
      t = Time.now
      yield
      puts "Request took #{Time.now - t} seconds"
    end
  end
  puts "Total time #{time.real}"
end

# here are a couple of sites to try with. They are geographically far from me, so good for testing.
url = "https://eryang.world.tmall.com"
url = "https://kenh14.vn"

Testing with HTTPI - every request is just as slow as the last:

puts "**** Testing with HTTPI\n\n"
HTTPI.adapter = :net_http_persistent
request = HTTPI::Request.new(url)
bench do
  HTTPI.get(request)
end

Testing using the same HTTP:: Persistent instance - we see a speed improvement here:

puts "**** Testing with Net::HTTP::Persistent with existing instance"
http = Net::HTTP::Persistent.new name: 'my_app_name'
bench do
    response = http.request url
end

Testing as if each block of code creates its own HTTP:: Persistent instance. This is slow.

puts "**** Testing with Net::HTTP::Persistent with new instance"
bench do
    http = Net::HTTP::Persistent.new name: 'my_app_name'
    response = http.request url
end

If you try the first example above using version 2.94 of the gem it is fast.

AnilRh avatar Feb 15 '24 20:02 AnilRh