Rails Server开发速度极慢

时间:2019-04-03 12:07:08

标签: ruby-on-rails ruby ruby-on-rails-5

启动服务器或更改.rb文件后,我有一个Rails 5应用程序,该应用程序非常慢。下面是我用rails server引导服务器并加载一个简单页面后的cli输出。另外,如果我更改模型,控制器或助手,则服务器将花费大约相同的时间来响应下一个请求。加载请求后,页面将正常加载,但是每次更改后的第一个请求可能要花费30秒,这加起来非常快。我已经尝试在开发中设置以下配置(类似的问题提示了这些更改)。另外,我有这个开发服务器,它在具有4GB内存的数字海洋小滴上运行。

config.assets.debug = false
config.assets.digest = false

我的输出:

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)`

1 个答案:

答案 0 :(得分:0)

就像每次您的rails服务器检测到页面更改一样,它会检查它们是否是挂起的迁移,这将需要一些时间。

#config/environments/development.rb

  config.active_record.migration_error = false

可以帮助您进行检查。

  

在开发环境中,您的应用程序代码会在每次请求时重新加载。这会减慢响应时间,但是非常适合开发,因为在更改代码时不必重启Web服务器。

与较大的项目一样,您将希望阻止Rails预加载不需要的所有代码,特别是如果您仅在项目的一部分中工作的话。也在环境中

#config/environments/development.rb

  # Do not eager load code on boot.
  config.eager_load = false

您可以阅读有关这些环境配置设置的更多信息here

尽管如果您不处理大型项目,那么4GB的内存似乎是一个危险信号……可能需要查看初始化信息以及所有项目在启动时加载的内容。