我有一个大型的Rails应用程序,我想要提高(糟糕的)性能。
使用ruby-prof运行并没有帮助我很多,我得到的输出类似于这个(在thin上以生产模式运行):
Thread ID: 9322800
Total: 1.607768
Sort by: self_time
%self total self wait child calls name
26.03 0.42 0.42 0.00 0.00 1657 Module#define_method
8.03 0.13 0.13 0.00 0.00 267 Set#initialize
4.41 0.07 0.07 0.00 0.00 44 PG::Result#values
4.28 0.07 0.07 0.00 0.00 1926 ActiveSupport::Callbacks::Callback#start
4.21 0.07 0.07 0.00 0.00 14835 Kernel#hash
4.13 0.08 0.07 0.00 0.01 469 Module#redefine_method
4.11 0.07 0.07 0.00 0.00 63 *<Class::ActiveRecord::Base>#with_scope
4.02 0.07 0.06 0.00 0.00 774 ActiveSupport::Callbacks::Callback#_compile_options
3.24 0.05 0.05 0.00 0.00 30 PG::Connection#async_exec
2.31 0.40 0.04 0.00 0.37 2130 *Module#class_eval
1.47 0.02 0.02 0.00 0.00 6 PG::Connection#unescape_bytea
1.03 0.05 0.02 0.00 0.03 390 *Array#select
* indicates recursively called methods
我猜可能是垃圾回收器花费了很多时间,因此由于我正在运行REE,我决定尝试使用GC.enable_stats来获取更多信息。 我在我的应用控制器中添加了以下内容:
around_filter :enable_gc_stats
private
def enable_gc_stats
GC.enable_stats
begin
yield
ensure
GC.disable_stats
GC.clear_stats
end
end
在我这里的生产模式下,使用REE和thin web服务器(由于它会使运行速度变慢,因此禁用了ruby-prof),在一个相对较大的页面上,我得到了以下结果:
Completed 200 OK in 1093ms (Views: 743.1ms | ActiveRecord: 139.2ms)
GC.collections: 11
GC.time: 666299 us 666.299 ms
GC.growth: 461 KB
GC.allocated_size: 152 MB
GC.num_allocations: 1,924,773
ObjectSpace.live_objects: 1,015,195
ObjectSpace.allocated_objects: 12,393,644
因此,对于一个花费了1093毫秒的页面来说,似乎将近700毫秒是在垃圾回收器中消耗的。 有人遇到过这种问题吗?我知道你不能特别帮助我的应用程序(它非常庞大,并且有很多宝石和其他东西)-但是否有技术或工具可以更好地了解为什么会创建那么多垃圾?
任何想法都会非常感激!
Set
的使用,请注意它在底层使用了Hash
- 对于小型集合(通常为10到100个条目),使用Array
更快捷。 - Nevir