对 Rails 应用程序的第一次请求非常慢
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您在处理第一个请求时发布日志内容,那么也许我们可以找出导致速度如此慢的原因。 例如,这是我的日志,当第一个用户访问该网站时,
我认为 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
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
这可能是因为您:
需要并加载许多
插件和 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.
这可能与我们的应用程序第一次在 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.
我猜你正在使用 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.
也许您需要调整 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.