对 Rails 应用程序的第一次请求非常慢

发布于 2024-07-21 11:36:52 字数 129 浏览 9 评论 0原文

我的 Rails 应用程序的第一个(工作会话)请求总是滞后的。 切换到生产模式 没有帮助。

我使用 mongrel,其他请求都以可接受的速度处理。

我怎样才能让它更快?

问候

always the first request (of a working session) to my Rails app is lagging. Switching to production mode
doesn't help.

I use mongrel and the other requests are handled with acceptable speed.

How do I make it faster?

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

彼岸花似海 2024-07-28 11:36:52

如果您在处理第一个请求时发布日志内容,那么也许我们可以找出导致速度如此慢的原因。 例如,这是我的日志,当第一个用户访问该网站时,

Booting Mongrel (use 'script/server webrick' to force WEBrick)    
Rails 2.1.0 application starting on http://0.0.0.0:3000    
Debugger enabled    
Call with -d to detach    
Ctrl-C to shutdown server
** Starting Mongrel listening at 0.0.0.0:3000
** Starting Rails with development environment...
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/mime_type.rb:66: warning: already initialized constant CSV
** Rails loaded.
** Loading any Rails specific GemPlugins
** Signals ready.  TERM => stop.  USR2 => restart.  INT => stop (no restart).
** Rails signals registered.  HUP => reload (without restart).  It might not work well.
** Mongrel 1.1.5 available at 0.0.0.0:3000
** Use CTRL-C to stop.


Processing SessionsController#new (for 127.0.0.1 at 2009-05-26 12:26:00) [GET]
  Session ID: de2acf074759026e1ed6205724f547a9
  Parameters: {"action"=>"new", "controller"=>"sessions"}
Rendering sessions/new
Completed in 0.00587 (170 reqs/sec) | Rendering: 0.00298 (50%) | DB: 0.00092 (15%) | 200 OK [http://localhost/]

我认为 170 请求/秒对于我们的应用程序来说很好,但其他人可能会发现这很慢。 您可以从统计数据中看到,rails 提供的一半所需时间用于渲染响应 - 在本例中为登录屏幕生成 HTML。 如果此请求需要很长时间,我的第一个调用端口将是与登录屏幕关联的视图和帮助程序。

如果您确实有一个系统在第一个请求时需要很长时间来初始化自身,那么为什么不偷偷摸摸地编写自己的启动程序,首先运行rails,然后通过curl发送一个假请求。 这样您的用户就永远不会发现问题。

克里斯

If you post the contents of the log as that first request is processed then perhaps we can figure out what's making it so slow. For example, this is my log as the first user accesses the site

Booting Mongrel (use 'script/server webrick' to force WEBrick)    
Rails 2.1.0 application starting on http://0.0.0.0:3000    
Debugger enabled    
Call with -d to detach    
Ctrl-C to shutdown server
** Starting Mongrel listening at 0.0.0.0:3000
** Starting Rails with development environment...
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/mime_type.rb:66: warning: already initialized constant CSV
** Rails loaded.
** Loading any Rails specific GemPlugins
** Signals ready.  TERM => stop.  USR2 => restart.  INT => stop (no restart).
** Rails signals registered.  HUP => reload (without restart).  It might not work well.
** Mongrel 1.1.5 available at 0.0.0.0:3000
** Use CTRL-C to stop.


Processing SessionsController#new (for 127.0.0.1 at 2009-05-26 12:26:00) [GET]
  Session ID: de2acf074759026e1ed6205724f547a9
  Parameters: {"action"=>"new", "controller"=>"sessions"}
Rendering sessions/new
Completed in 0.00587 (170 reqs/sec) | Rendering: 0.00298 (50%) | DB: 0.00092 (15%) | 200 OK [http://localhost/]

I think 170 reqs/sec is fine for our app but others may find that slow. You can see from the stats that rails provides that half of the time required is spent rendering the response - in this case generating the HTML for the login screen. If this request was taking a long time, my first port of call would be the views and helpers associated with the login screen.

If you do have a system that takes a long time to initialize itself on the very first request then why not be sneaky and write your own startup program which first runs rails and then sends a fake request in via curl. That way your users never see the problem.

Chris

决绝 2024-07-28 11:36:52

这可能是因为您:

  • 需要并加载许多
    插件和 gems

  • 建立与外部的连接
    服务(然后缓存)

  • 缓存您自己的页面,并且仅
    发生在第一个请求之后,除非
    您“加热”缓存

其中任何一个都将不可避免地增加第一个请求的响应时间。

It can be because you are:

  • requiring and loading a number of
    plugins and gems

  • making a connection to an external
    service (then caching)

  • caching your own pages and that only
    occurs after the first request unless
    you 'warm' the cache

Any one of these will inevitably increase the response time of the first request.

唔猫 2024-07-28 11:36:52

这可能与我们的应用程序第一次在 Websphere 中启动时花费很长时间的原因相同。

当安装新版本的应用程序(或重新启动 WAS)时,WAS 必须执行大量初始工作来设置容器。

我们使用的解决方法是修改安装脚本和 WAS 启动脚本,以便它们在应用程序运行时自动浏览到应用程序(主页和选定的其他页面)。 这样,第一次真正访问它就是全速的。

我不知道如何用 Ruby 做到这一点,甚至不知道这是否可能。 你必须弄清楚这一点。

It may be the same reason as our applications taking a long time the first time we kick them off in Websphere.

WAS has to do a lot of initial work to set up the containers when a new version of the application is installed (or WAS is restarted).

The workaround we used was to modify the install scripts and WAS startup scripts so that they automatically browsed to the application (main page and selected other pages) as soon as it was running. That way, the first real access to it was at full speed.

I have no idea how to do this with Ruby or even whether it's possible. You'll have to figure that one out.

梦归所梦 2024-07-28 11:36:52

我猜你正在使用 Ferret 进行全文搜索? 雪貂连接可能需要一段时间才能初始化? 当我检查你的日志时,似乎数据库和视图都花费了正常的时间,但总计仍然是 10 秒。 所以我一定是别的什么,这就是为什么我猜测雪貂可能是问题所在。

I guess you are using Ferret for full-text search? Might it be that the ferret connection takes a while to initialize? When I check your log it seems that both the db and the view take a normal amount of time but the total is still 10 seconds. So I must be something else that's why i'm guessing Ferret might be the problem.

情栀口红 2024-07-28 11:36:52

也许您需要调整 apache conf 中的 PassengerPoolIdleTime var。 将其设置为 0 永远不会停止您的 Rails 进程。

Maybe you need to adjust the PassengerPoolIdleTime var in apache conf. Set it to 0 to never stop your rails process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文