Typhoeus gem 不会减少响应时间
基本上我有 857 个图像链接需要检查。我用 3 种不同的方法实现它,每种方法运行 3 次。
方法 1:使用 Typhoeus 和 Hydra(并行请求)
hydra = Typhoeus::Hydra.new(:max_concurrency => 50)
st = Time.now
@image_urls.each do |image_url|
request = Typhoeus::Request.new(image_url)
hydra.queue(request)
end
hydra.run
et = Time.now
puts "\n" + (et - st).to_s() + " seconds"
耗时:117.65、99.45、102.01 秒
方法 2:使用 Typhoeus(单个请求)
st = Time.now
@image_urls.each do |image_url|
response = Typhoeus::Request.head(image_url)
end
et = Time.now
puts "\n" + (et - st).to_s() + " seconds"
耗时:33.85、31.89、 30.18 秒
方法 3:使用 Net::HTTP Ruby 库
st = Time.now
@image_urls.each do |image_url|
url = URI.parse(image_url)
req = Net::HTTP.new(url.host, url.port)
res = req.request_head(url.path).code
end
et = Time.now
puts "\n" + (et - st).to_s() + " seconds"
耗时: 83.30, 67.62, 75.26 秒
最初我认为方法 1:Typhhoeus 和 Hydra 应该通过发送并行请求而不是一次发送 1 个请求来加快 Http 响应时间。然而,上面的结果表明我的响应时间实际上变慢了。
原因之一可能是针对标头的 http 请求的开销比普通的 http GET 请求要少。除此之外,我在这里做错了什么吗?需要建议来优化这个过程,我只需要检索 http 状态代码。
Basically I have 857 image links to check. I implemented it in 3 different methods and run them 3 times each.
Method 1: Using Typhoeus and Hydra (Parallel Requests)
hydra = Typhoeus::Hydra.new(:max_concurrency => 50)
st = Time.now
@image_urls.each do |image_url|
request = Typhoeus::Request.new(image_url)
hydra.queue(request)
end
hydra.run
et = Time.now
puts "\n" + (et - st).to_s() + " seconds"
Time taken: 117.65, 99.45, 102.01 seconds
Method 2: Using Typhoeus (Singular Request)
st = Time.now
@image_urls.each do |image_url|
response = Typhoeus::Request.head(image_url)
end
et = Time.now
puts "\n" + (et - st).to_s() + " seconds"
Time taken: 33.85, 31.89, 30.18 seconds
Method 3: Using Net::HTTP Ruby library
st = Time.now
@image_urls.each do |image_url|
url = URI.parse(image_url)
req = Net::HTTP.new(url.host, url.port)
res = req.request_head(url.path).code
end
et = Time.now
puts "\n" + (et - st).to_s() + " seconds"
Time taken: 83.30, 67.62, 75.26 seconds
Initially I thought Method 1: Typhoeus and Hydra is suppose to speed up Http response time by sending parallel requests instead of sending 1 at a time. However, the above result show me that I am in fact getting a slower response time.
One reason could be a http request for the header has lesser overhead than a normal http GET request. Other than that, am I doing something wrong here? Need advice to optimize this process, I just need to retrieve the http status code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所怀疑的,您肯定会希望使用相同的 HTTP 方法进行基准测试。 HEAD 请求的返回速度往往比 GET 请求快得多。
如果出现一些误报并不影响交易,您可能需要尝试降低连接/响应超时。如果准确性至关重要,请降低最大并发数(尝试 10-15)并利用
Typhoeus::Request#on_complete
进行 HTTP 状态代码处理。As you suspected, you'll definitely want to benchmark using the same HTTP method. HEAD requests will tend to return much more quickly than GET requests.
If having a few false positives isn't a deal-breaker, you might want to try lowering connect/response timeouts. If accuracy is paramount, lower the max concurrency (try 10-15) and take advantage of
Typhoeus::Request#on_complete
to do HTTP status code processing.