简体   繁体   English

应用程序加速Ruby on Rails

[英]Application Speed up for Ruby on Rails

I have a controller action which is taking 10 seconds to run. 我有一个控制器动作,需要10秒才能运行。 Most of the time is being spent in a view which is a large file.(more than 1500 lines). 大部分时间都花在一个大文件的视图中(超过1500行)。 How shall I go about debugging the speed issue? 我该如何调试速度问题?

Yuo can try NewRelic for example. Yu可以试试NewRelic I used the free version a few years ago and you can see the overall performance and especially the slow calls pretty good. 我几年前使用过免费版本,你可以看到整体性能,特别是慢速调用非常好。
As far as I remember you only have to install a gem, configure some things and it's good to go. 据我记得你只需安装一个宝石,配置一些东西,这是很好的去。

Ruby and Rails have basic profiling tools that can help you. Ruby和Rails具有可以帮助您的基本分析工具。 Take a look at this Rails guide: Performance Testing Rails Applications . 看看这个Rails指南: 性能测试Rails应用程序

That page describes how you can write test cases meant for benchmarking, to prevent performance regressions and how you can do profiling, which could be as simple as: 该页面描述了如何编写用于基准测试的测试用例,以防止性能回归以及如何进行性能分析,这可能很简单:

rails profiler 'Ruby.code.to.run'

If that doesn't do it for you, then consider using New Relic . 如果这不适合你,那么考虑使用New Relic But I would also take a look at Rack-Bug (btw, it has a Rails 3 branch for Rails 3). 但我还要看一下Rack-Bug (顺便说一句,它有一个Rails 3Rails 3分支 )。 Rack-Bug is a debugging toolbar that gives you lots of awesome metrics about where time has been spent in a request and it does so straight in the browser, alongside the normal response, so it's the least painful. Rack-Bug是一个调试工具栏,它为您提供了许多关于请求中花费时间的精彩指标,并且它在浏览器中直接执行,以及正常响应,因此它是最不痛苦的。

Now, what you're doing is wrong: 现在,你所做的是错的:

  • you should avoid fat views, as fat views are hard to test, hard to debug and hard to profile 你应该避免胖视图,因为胖视图很难测试,难以调试和难以分析
  • you should avoid slow code in your web process, because it will block other clients from accessing your web server, instead move all that logic out into an asynchronous job queue 您应该避免Web进程中的慢代码,因为它会阻止其他客户端访问您的Web服务器,而是将所有逻辑移出到异步作业队列中

So consider moving this processing into a jobs queue. 因此,请考虑将此处理移动到作业队列中。 Delayed_job is pretty reliable, properly maintained and works with Rails 3. Delayed_job非常可靠,维护得当,可以与Rails 3一起使用。

So your view will register a new job and return. 因此,您的观点将注册一份新工作并返回。 And when the job is done you'll set a flag somewhere that it is done. 当工作完成后,你会在某个地方设置一个标志。 Then with a special API call you can check for its completeness from client-side Javascript once every few seconds. 然后通过特殊的API调用,您可以每隔几秒从客户端Javascript检查其完整性。 And once completed you can show the results, redirect or start the download or whatever, again from Javascript. 一旦完成,您可以再次从Javascript显示结果,重定向或开始下载或其他任何内容。

And if the above is not an option (maybe this is a page that shows a report that should be instant), the first bottleneck you should consider are the SQL queries getting executed, so get familiar with MySQL's DESCRIBE command (or PostgreSQL's EXPLAIN ). 如果以上不是一个选项(也许这是一个显示应该是即时报告的页面),您应该考虑的第一个瓶颈是执行的SQL查询,因此熟悉MySQL的DESCRIBE命令(或PostgreSQL的EXPLAIN )。 It's easy that way to see where you need indexes. 通过这种方式可以轻松查看索引所在的位置。

NewRelic is the best way to analyze the application . NewRelic是分析应用程序的最佳方式。

With the below point , you can modify your code for speed-up of your application . 通过以下几点,您可以修改代码以加快应用程序的速度。

  • The N+1 query problem N + 1查询问题
  • Testing N+1 测试N + 1
  • Nested eager loading 嵌套的渴望加载
  • Indirect eager loading 间接急切加载
  • Rails grouping and aggregate calculations Rails分组和聚合计算

I would suggest to wrap some small pieces of your view into benchmark blocks and see in the log/development.log where are the slow parts. 我建议将您的视图的一小部分包装到benchmark块中,并在log / development.log中查看哪些部分是缓慢的。

<% benchmark "Showing users" do %>
  <!-- ... -->
<% end %>

<% benchmark "Performing some calculations" do %>
  <!-- ... -->
<% end %>

The best case will be that you find a few places where 90% time is spent. 最好的情况是你找到一些花费90%时间的地方。 Then you can work on them to significantly improve the performance. 然后,您可以使用它们来显着提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM