加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Ruby / Rails性能:OpenURI vs NET:HTTP vs C

发布时间:2020-12-16 19:10:26 所属栏目:百科 来源:网络整理
导读:我正在访问不同的数据服务器,我在不同的类中尝试了不同的方法,使用基本的http :: net,curb,rest-client和open-uri (1)如何衡量Ruby / Rails中的性能? (2)您认为哪种方法更快? 所有4种不同方法的示例代码: url = "..." begin io_output = open(url,:http_b
我正在访问不同的数据服务器,我在不同的类中尝试了不同的方法,使用基本的http :: net,curb,rest-client和open-uri

(1)如何衡量Ruby / Rails中的性能?
(2)您认为哪种方法更快?

所有4种不同方法的示例代码:

url = "..."
  begin 
    io_output = open(url,:http_basic_authentication => [@user_id,@user_password])
  rescue => e
    error = e.message #for debugging return this
    return '-'
  else 
    output = io_output.read

要么

require 'net/https'
uri = URI.parse("...")
http = Net::HTTP.new(uri.host,uri.port)
http.use_ssl = true 
http.verify_mode = OpenSSL::SSL::VERIFY_PEER 
data = http.get(uri.request_uri) #http request status
res = Net::HTTP.get_response(uri)
puts res.body if res.is_a?(Net::HTTPSuccess)

要么

require 'curb'
url = "..."
c = Curl::Easy.new(url) do |curl| 
curl.headers["Content-type"] = "application/json"
curl.headers["Authorization"] = "Token ..."
end
c.perform
puts c.body_str

要么

url = "..." 
resource = RestClient::Resource.new(url,:headers => { :Authorization => "Token ...",:content_type => "application/json"})  
 begin 
 output = resource.get
rescue => e
error = e.message #for debugging return this
return '-'
else ...
end

解决方法

通过从Google检索数据,我在下一个基准测试中获得了这种结果.
Warming up --------------------------------------
             OpenURI     3.000  i/100ms
           Net::HTTP     3.000  i/100ms
                curb     3.000  i/100ms
         rest_client     3.000  i/100ms
Calculating -------------------------------------
             OpenURI     34.848  (±11.5%) i/s -    687.000  in  20.013469s
           Net::HTTP     35.433  (±14.1%) i/s -    594.000  in  20.006947s
                curb     31.612  (±19.0%) i/s -    465.000  in  20.021108s
         rest_client     34.331  (±11.7%) i/s -    675.000  in  20.044486s

Comparison:
           Net::HTTP:       35.4 i/s
             OpenURI:       34.8 i/s - same-ish: difference falls within error
         rest_client:       34.3 i/s - same-ish: difference falls within error
                curb:       31.6 i/s - same-ish: difference falls within error

这是基准测试的源代码

require 'benchmark/ips'
require 'open-uri'
require 'net/http'
require 'curb'
require 'rest-client'

google_uri = URI('http://www.google.com/')
google_uri_string = google_uri.to_s

Benchmark.ips do |x|
  x.config(time: 20,warmup: 10)
  x.report('OpenURI') { open(google_uri_string) }
  x.report('Net::HTTP') { Net::HTTP.get(google_uri) }
  x.report('curb') { Curl.get(google_uri_string) }
  x.report('rest_client') { RestClient.get(google_uri_string) }
  x.compare!
end

环境:

> AWS EC2服务器
> Ruby版本 – 2.5.1p57
> Gems:curb-0.9.6,rest-client-2.0.2,benchmark-ips-2.7.2

笔记:

在运行此基准测试之前,不要忘记安装gem

gem install curb rest-client benchmark-ips

要在稳定的网络环境(如生产服务器)中运行,以获得更准确的结果

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读