Rails extremely slow on simple static render - ruby-on-rails

I'm relatively new to Rails, but decided to develop my new project on this platform. I'm using new Rails 4 framework based on Ruby 2.0 and Unicorn as application server served through Nginx.
After some initial development I deployed my application to Amazon EC2 small server, and was literally shocked by the slow performance, testing it with simple ab utility. Giving you more details:
unicorn config file:
# 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
and index.html.erb is just mostly static 3kb file
There are no DB queries, Unicorn running in production mode, partials cache enabled in Rails.
ab -n 1000 -c 100 http://myinstance.amazon.com/ returns like 30-40 requests/per second performance, and average 3-4 seconds response time with 1 unicorn's worker(remember I'm using small 1cpu instance), and even less when increased to four. Strange enough when I tried to test the application on amazon's medium instance, it didnt improve the performance, giving a hint that it's probably limited by disk io. Well, when I replicated the same simple application on PHP and node.js stacks, that I used before, significantly better performance(more requests per second served with much shorter response times) comparing to the above mentioned Rails.
Is it normal for a Rails application? Any hints how to improve performance? Maybe some configuration tweaks? I tried to search, but I see mostly general information about Rails code improvements that do not apply here due to oversimplified example.
UPDATE #1
I include pieces of production.log on request, they vary from:
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)
to:
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)
UPDATE #2
I tried to make 10000 consequent requests and server simply timed out in the middle
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

It seems that it is quite expected result. You compare 3 different things: full-featured framework (Rails), platform (Node.js) and language (PHP).
Let's see how we split the response time:
[total time] = [web server processing time] + [framework infrastructure time]
+ [language interpreter time] + [your app logic time]
Of course, in case if your application will do nothing, the last component can be neglected.
When you run PHP or Node.js application there remain only 2 components in the sum: [web server processing time] + [language interpreter time], so it can handle requests very fast. Rails adds here the 3d component [framework infrastructure time].
If your application will start to do something more advanced then time spent in framework and webserver can have less impact on the total time (relatively). Though JavaScript will outperform Ruby nevertheless.
To be more objective you'd better add something on top of Node.js (like Express.js) or PHP (like Yii or Zend Framework). Or use something more lightweight instead of Rails to process small request much faster (e.g. Sinatra)

Related

Rails Server Extremely Slow in Development

I've got a rails 5 application that is very slow after I boot the server or change a .rb file. Below is my cli output after I booted the server with rails server and loaded a simple page. Also, if I change a model, controller or helper, the server takes about the same time to respond to the next request. After the request is loaded, the pages load normally but that first request after each change can take 30 seconds which adds up quite fast. I have tried setting the following configs in `development (similar questions suggested these changes). Also, I have this development server running on a digital ocean droplet with 4GB of memory.
config.assets.debug = false
config.assets.digest = false
My output:
rails server
=> Booting Puma
=> Rails 5.2.2 application starting in development
=> Run rails server -h for more startup options
Puma starting in single mode...
* Version 3.12.0 (ruby 2.5.3-p105), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Started GET "/treadmill-parts-model-select" for 98.251.169.224 at 2019-04-03
11:51:29 +0000
Cannot render console from 98.251.169.224! Allowed networks: 127.0.0.1, ::1,
127.0.0.0/127.255.255.255
(2.1ms) SET NAMES utf8, ##SESSION.sql_mode = CONCAT(CONCAT(##sql_mode,
',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),
##SESSION.sql_auto_is_null = 0, ##SESSION.wait_timeout = 2147483
↳ /usr/local/rvm/gems/ruby-2.5.3/gems/activerecord-
5.2.2/lib/active_record/log_subscriber.rb:98
(2.4ms) SELECT 'schema_migrations'.'version' FROM 'schema_migrations' ORDER
BY 'schema_migrations'.'version' ASC
↳ /usr/local/rvm/gems/ruby-2.5.3/gems/activerecord-
5.2.2/lib/active_record/log_subscriber.rb:98
Processing by SearchController#treadmill_model_search as HTML
Rendering search/treadmill_model_search.html.erb within layouts/application
Product Load (113.8ms) SELECT DISTINCT 'products'.'brand' FROM 'products' WHERE 'products'.'product_type' = 'Treadmill' ORDER BY 'products'.'brand' ASC
↳ app/views/search/treadmill_model_search.html.erb:22
Rendered search/treadmill_model_search.html.erb within layouts/application (167.6ms)
Rendered layouts/_header.html.erb (5.8ms)
Rendered layouts/_flash_messages.html.erb (1.1ms)
Rendered layouts/_footer.html.erb (0.5ms)
Completed 200 OK in 18746ms (Views: 18565.0ms | ActiveRecord: 124.7ms)`
Looks like every time your rails server detects a page change it checks to see if their is a migration pending, which will take some time.
#config/environments/development.rb
config.active_record.migration_error = false
could help with that check.
In the development environment your application's code is reloaded on every request. This slows down response time but is perfect for development since you don't have to restart the web server when you make code changes.
As with larger projects you will want to stop rails from pre-loading all the code which isn't needed especially if you are only working on a portion of your project anyways. Also in the environment
#config/environments/development.rb
# Do not eager load code on boot.
config.eager_load = false
You can read more on these environment config settings here
Though the 4GB of memory seems like a red flag if you aren't working on a massive project... might need to look into your initializes and what all your project is loading up on boot.

Recaptcha rails 5 error

I am having an error with recaptcha recognizing my keys in secrets.yml in production. In development, everything works fine!
I was able to upload successfully my site using capistrano and nginx, I placed the recaptcha keys in secrets.yml but I get the following error in rails logs.
[70624ace-d7c2-41d4-a312-81e558237559] app/views/contacts/_new.html.erb:1:in `_app_views_contacts__new_html_erb___113748072819217608_34958780'
[70624ace-d7c2-41d4-a312-81e558237559] app/views/welcome/index.html.erb:97:in `_app_views_welcome_index_html_erb__2483501284442413835_31069860'
I, [2018-02-13T15:06:06.655443 #23369] INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Started GET "/" for 66.249.70.22 at 2018-02-13 15:06:06 +0000
I, [2018-02-13T15:06:06.656262 #23369] INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Processing by WelcomeController#index as HTML
I, [2018-02-13T15:06:06.657082 #23369] INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Rendering welcome/index.html.erb within layouts/application
I, [2018-02-13T15:06:06.666156 #23369] INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Rendered contacts/_new.html.erb (8.3ms)
I, [2018-02-13T15:06:06.666316 #23369] INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Rendered welcome/index.html.erb within layouts/application (9.1ms)
I, [2018-02-13T15:06:06.666485 #23369] INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Completed 401 Unauthorized in 10ms
F, [2018-02-13T15:06:06.667938 #23369] FATAL -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f]
F, [2018-02-13T15:06:06.668014 #23369] FATAL -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] ActionView::Template::Error (No site key specified.):
F, [2018-02-13T15:06:06.668157 #23369] FATAL -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] 9:
I am trying to follow this tutorial https://github.com/ambethia/recaptcha, but it does not provide much detail about placing the keys in production.
I have tried many things, like adding export in front of the keys, placing them in different files, and checking secrets.yml for any syntax error. I have also made sure that I am using RECAPTHCA_SITE_KEY instead of RECAPTCHA_PUBLIC_KEY.
Also, I have placed my domain in recaptcha's site.
I would appreciate if you could help me solve the problem as well as provide some explanation of why I was having the error.
This is my secrets.yml in production:
production:
secret_key_base: ...SECRET_KEY...
RECAPTCHA_SITE_KEY: "...SITE_KEY..."
RECAPTCHA_PRIVATE_KEY: "...PRIVATE_KEY..."
Thanks!
You need to configure recaptcha to pull the keys from your secrets.yml by adding an initializer file, config/initializers/recaptcha.rb:
# config/initializers/recaptcha.rb
Recaptcha.configure do |config|
config.site_key = Rails.application.secrets[:RECAPTCHA_SITE_KEY]
config.secret_key = Rails.application.secrets[:RECAPTCHA_PRIVATE_KEY]
# Uncomment the following line if you are using a proxy server:
# config.proxy = 'http://myproxy.com.au:8080'
end

Authenticated requests for an Rails API backend and React frontend fail when app deployed to Heroku

I have an app which uses Rails API backend and React frontend. It works locally but after I deployed it to Heroku all the requests that require authentication fail and give
Error: "Token is invalid"
I have checked the headers and the token is there. Can't figure out by looking at Heroku logs what the problem is.
Did someone have a similar issue?
Thanks!
A snippet from Heroku logs:
2017-10-26T17:56:36.419481+00:00 app[web.1]: I, [2017-10-
26T17:56:36.419403 #4] INFO -- : [bc68efaf-22e2-4274-92d8-
d1202d2b1228] Parameters: {"auth"=>{}}
2017-10-26T17:56:36.420592+00:00 app[web.1]: I, [2017-10-
26T17:56:36.420511 #4] INFO -- : [bc68efaf-22e2-4274-92d8-
d1202d2b1228] [active_model_serializers] Rendered
ActiveModel::Serializer::Null with Hash (0.18ms)
2017-10-26T17:56:36.421050+00:00 app[web.1]: I, [2017-10-
26T17:56:36.420931 #4] INFO -- : [bc68efaf-22e2-4274-92d8-
d1202d2b1228] Filter chain halted as :authenticate_token! rendered or
redirected
2017-10-26T17:56:36.421378+00:00 app[web.1]: I, [2017-10-
26T17:56:36.421175 #4] INFO -- : [bc68efaf-22e2-4274-92d8-
d1202d2b1228] Completed 403 Forbidden in 2ms (Views: 1.1ms |
ActiveRecord: 0.0ms)
Heroku has settings where you can define variables. It's possible you have a .env variable or something similar that needs to be defined there, because Heroku isn't recognizing it.

Ruby on Rails, image compilation and precompilation

I just deployed a rails app and I am having an error when I load a page saying:
We're sorry, but something went wrong.
In the production.log I see that message:
Started GET "/" for 128.179.252.130 at 2014-11-30 08:32:28 +0000
Processing by StaticPagesController#home as HTML
Rendered static_pages/home.html.erb within layouts/application (0.4ms)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_header.html.erb (2.4ms)
Completed 500 Internal Server Error in 4ms
ActionView::Template::Error (./icons/icon_nameplate.png isn't precompiled):
37:<ul class="dropdown-menu">
38:
39:<li>
40: <%= menu_dropdown('Profile', "./icons/icon_nameplate.png", "Nameplate Icon", current_user) %>
41:</li>
So I believe the error comes from this part of the message:
./icons/icon_nameplate.png isn't precompiled
I searched the internet, found three possible solution, none of which did work:
1. config.assets.compile = true (in production.rb file)
2. config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif] (still in production.rb file)
3. bundle exec rake assets:precompile RAILS_ENV=production
All of this changed absolutely nothing, and the website still shows the same error. So I am turning for you to help.
Thank you very much in advance.
What finally did the trick for me is completely restart the rails app. This is described in the link below.
The recommended version did the trick for me which is:
Create (or update) a file called restart.txt in the tmp folder of your Rails application on the server.
After that no problem at all.
Cheers

Heroku: see params and sql activity in logs?

When i view my heroku logs on the server (with heroku logs --tail --app myapp) i see something like this:
2011-06-21T14:09:25+00:00 app[web.1]: Started PUT "/reports/19.xml" for 77.89.149.137 at 2011-06-21 07:09:25 -0700
2011-06-21T14:09:25+00:00 heroku[router]: PUT myapp.heroku.com/reports/19.xml dyno=web.1 queue=0 wait=0ms service=7ms status=401 bytes=28
2011-06-21T14:09:26+00:00 heroku[nginx]: PUT /reports/19.xml HTTP/1.1 | 77.89.149.137 | 656 | http | 401
While in my local log i'd see something like this:
Started PUT "/reports/19" for 127.0.0.1 at 2011-06-21 15:27:01 +0100
Processing by ReportsController#update as XML
Parameters: {"report"=>{"workflow_status"=>"3"}, "id"=>"19"}
Person Load (0.9ms) SELECT "people".* FROM "people" WHERE "people"."email" = 'madeupemai#lkklj.com' LIMIT 1
Report Load (0.4ms) SELECT "reports".* FROM "reports" WHERE "reports"."id" = 19 LIMIT 1
DEPRECATION WARNING: Object#returning has been deprecated in favor of Object#tap. (called from update_report at /home/max/work/rails_apps/flamingo_container/flamingo/vendor/plugins/resource_this/lib/resource_this.rb:135)
Creating scope :open. Overwriting existing method Task.open.
Task Load (2.0ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = 14 LIMIT 1
Completed 200 OK in 1648ms (Views: 568.2ms | ActiveRecord: 3.2ms)
Ie with a lot more information, particularly the params, info from the router, generated sql, any templates rendered etc etc.
Does anyone know how i can get my heroku log to be as verbose as my development one? I've done the following already:
1) Set the log level in the relevant (rails 3) environment file:
config.log_level = :debug
2) Told heroku to use a different logger level, as described on http://devcenter.heroku.com/articles/logging
heroku config:add LOG_LEVEL=DEBUG --app myapp #from CLI
Neither has made any difference...any ideas anyone?
thanks, max
You're essentially wanting to show the SQL / params output in the Heroku logs. You can do this by adding the line shown below to the config block within your production.rb file:
MyAppNameHere::Application.configure do
# add this line
config.logger = Logger.new(STDOUT)
end
By the way, setting the log level to debug just means that Rails.logger.debug will output to the logs when you're on Heroku
In your production.rb add config.log_level = :debug and redeploy. That will give you the same logs as development mode :)
The detailed log you want is generated by the function start_processing in log_subscriber.rb.
action_controller/log_subscriber.rb:
def start_processing(event)
payload = event.payload
params = payload[:params].except(*INTERNAL_PARAMS)
info " Processing by #{payload[:controller]}##{payload[:action]} as #{payload[:formats].first.to_s.upcase}"
info " Parameters: #{params.inspect}" unless params.empty?
end
I checked with rails 3.0.4 in development and production environment. In both environments, we have the detailed logs.
This is an info level log. That's why the debug log level is not changing the output.
I installed the plugin used by heroku rails_log_stdout (Heroku logging) but I still have the desired output.
Right now, I can't test with heroku to find out why you don't have all the logs.
In the heroku example (see Heroku logging, section log retrieval), we don't see the "Processing" and "Parameters" lines. I think that either this method is not called when the app is running on heroku (it is somehow disabled) or heroku skips logs starting with whitespaces. Could you try to log messages starting with whitespaces and see if heroku is showing them?
Rails doesn't generate those logs when in production mode. http://groups.google.com/group/heroku/browse_thread/thread/d778fafedc9a378a
The real problem is actually due to the way Heroku works in conjunction with rails. The right way to solve this is to add: gem 'rails_12factor', group: :production
See https://devcenter.heroku.com/articles/rails-integration-gems for more info on the matter
Looks like the puma server doesn't play well with Heroku. In my project, I tried everything, but it still wouldn't log. I then replaced Puma with Unicorn, and bam, full logs are being shown.

Resources