Rails在简单的静态渲染上非常慢

时间:2013-07-20 12:41:15

标签: ruby-on-rails performance

我对Rails比较陌生,但决定在这个平台上开发我的新项目。我正在使用基于Ruby 2.0和Unicorn的新Rails 4框架作为通过Nginx提供的应用程序服务器。 经过一些初步开发后,我将我的应用程序部署到Amazon EC2小型服务器,并且由于性能缓慢而感到震惊,使用简单的ab实用程序对其进行测试。给你更多细节:

unicorn配置文件:

# config/unicorn.rb
env = ENV["RAILS_ENV"] || "development"

worker_processes 1

listen 8080, :backlog => 64

preload_app true

timeout 30

pid "/tmp/unicorn.mysite.pid"


if env == "production"

  working_directory "/mypath/current"

  user 'ubuntu', 'ubuntu'
  shared_path = "/mypath/shared"

  stderr_path "#{shared_path}/log/unicorn.stderr.log"
  stdout_path "#{shared_path}/log/unicorn.stdout.log"
end

before_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
  end

  old_pid = "/tmp/unicorn.mysite.pid.oldbin"
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
    end
  end
end

after_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
  end

end

routes.rb中:

root to: "home#index"

home_controller.rb:

def index
    render "index", layout: false
end

index.html.erb只是静态的3kb文件
没有数据库查询,Unicorn在生产模式下运行,在Rails中启用了部分缓存。

ab -n 1000 -c 100 http://myinstance.amazon.com/返回类似30-40个请求/秒的性能,与1个独角兽的工作者平均3-4秒的响应时间(记住我使用的是小型1cpu实例),当增加到4时,甚至更少。当我试图在亚马逊的中型实例上测试应用程序时,它有点奇怪,它并没有提高性能,暗示它可能受到磁盘io的限制。好吧,当我在PHP和node.js堆栈上复制相同的简单应用程序(我之前使用过)时,与上面提到的Rails相比,性能明显更好(每秒请求的响应时间更短,响应时间更短)。 /> Rails应用程序是正常的吗?任何提示如何提高性能?也许一些配置调整?我试图搜索,但我看到大部分关于Rails代码改进的一般信息,由于过于简单的例子,这里不适用。

更新#1 我根据要求提供了production.log件,其中包括:

I, [2013-07-20T13:21:44.830189 #1852]  INFO -- : Started GET "/" for xx.xx.xx.xx at 2013-07-20 13:21:44 +0000
I, [2013-07-20T13:21:44.831420 #1852]  INFO -- : Processing by HomeController#index as */*
I, [2013-07-20T13:21:44.832519 #1852]  INFO -- :   Rendered layouts/_head.html.erb (0.5ms)
I, [2013-07-20T13:21:44.834213 #1852]  INFO -- :   Rendered layouts/_header.html.erb (1.5ms)
I, [2013-07-20T13:21:44.834966 #1852]  INFO -- :   Rendered layouts/_footer.html.erb (0.0ms)
I, [2013-07-20T13:21:44.835143 #1852]  INFO -- :   Rendered home/index.html.erb (3.2ms)
I, [2013-07-20T13:21:44.835356 #1852]  INFO -- : Completed 200 OK in 4ms (Views: 3.5ms | ActiveRecord: 0.0ms)

为:

I, [2013-07-20T13:21:44.689225 #1852]  INFO -- : Started GET "/" for xx.xx.xx.xx at 2013-07-20 13:21:44 +0000
I, [2013-07-20T13:21:44.690629 #1852]  INFO -- : Processing by HomeController#index as */*
I, [2013-07-20T13:21:44.716144 #1852]  INFO -- :   Rendered layouts/_head.html.erb (24.7ms)
I, [2013-07-20T13:21:44.718191 #1852]  INFO -- :   Rendered layouts/_header.html.erb (1.7ms)
I, [2013-07-20T13:21:44.718919 #1852]  INFO -- :   Rendered layouts/_footer.html.erb (0.0ms)
I, [2013-07-20T13:21:44.719042 #1852]  INFO -- :   Rendered home/index.html.erb (27.7ms)
I, [2013-07-20T13:21:44.719280 #1852]  INFO -- : Completed 200 OK in 28ms (Views: 28.2ms | ActiveRecord: 0.0ms)


更新#2 我试图制作10000个后续请求,服务器只是在中间超时

ab -n 10000 -c 100 http://myinstance.amazon.com/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking myinstance.amazon.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
apr_socket_recv: Connection timed out (110)
Total of 3457 requests completed

1 个答案:

答案 0 :(得分:1)

似乎是非常期待的结果。您可以比较3种不同的东西:全功能框架( Rails ),平台( Node.js )和语言( PHP )。

让我们看看我们如何分割响应时间:

[total time] = [web server processing  time] + [framework infrastructure time] 
             + [language interpreter time] + [your app logic time]

当然,如果您的应用程序什么也不做,最后一个组件可以忽略不计。

当您运行 PHP Node.js 应用程序时,总和中仅保留 2个组件[web server processing time] + [language interpreter time],因此它可以非常快速地处理请求。 Rails 在此添加 3d 组件[framework infrastructure time]

如果您的应用程序将开始执行更高级的操作,那么在框架和Web服务器上花费的时间对总时间(相对)的影响会更小。虽然 JavaScript 仍然会胜过 Ruby

为了更客观,您最好在 Node.js (如Express.js)或 PHP (如Yii或Zend Framework)之上添加内容。或者使用更轻量级的东西而不是 Rails 来更快地处理小请求(例如Sinatra)