私は Rails に比較的慣れていませんが、このプラットフォームで新しいプロジェクトを開発することにしました。Nginx を介して提供されるアプリケーション サーバーとして、Ruby 2.0 と Unicorn に基づく新しい Rails 4 フレームワークを使用しています。いくつかの初期開発の後、アプリケーションを Amazon EC2 小規模サーバーにデプロイしました。パフォーマンスの遅さに文字通りショックを受け、単純なab
ユーティリティでテストしました。詳細を提供します:
ユニコーン設定ファイル:
# 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
ルート.rb:
root to: "home#index"
home_controller.rb:
def index
render "index", layout: false
end
index.html.erb
ほとんどが静的な 3kb ファイルです
DB クエリはなく、
Unicorn はプロダクション モードで実行され、パーシャル キャッシュは Rails で有効になっています。
ab -n 1000 -c 100 http://myinstance.amazon.com/
毎秒 30 ~ 40 のリクエストのパフォーマンスが返され、1 つのユニコーンのワーカー (小さな 1cpu インスタンスを使用していることを思い出してください) で平均 3 ~ 4 秒の応答時間が返され、4 つに増やすとさらに短くなります。Amazonの中規模インスタンスでアプリケーションをテストしようとしたとき、奇妙なことに、パフォーマンスは改善されず、おそらくディスクIOによって制限されていることが示唆されました. 以前に使用した同じ単純なアプリケーションを PHP と node.js スタックで複製したところ、上記の Rails と比較して、パフォーマンスが大幅に向上しました (1 秒あたりのリクエスト数が多く、応答時間は大幅に短縮されました)。
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