问题描述
当我跑步时
rails 服务器
或
耙-T
或其他一些 Rails 脚本,需要很多时间,大约 1 分钟.确定究竟是什么如此缓慢的最佳方法是什么?如何提高速度?
or some other rails script, it takes a lot of time, approx 1 minute.What is the best way to determine what exactly is so slow ?How can the speed be improved ?
Rails v 是 3.0.3,运行 ruby 1.9.2 (RVM) - Linux
Rails v is 3.0.3 run trough ruby 1.9.2 (RVM) - Linux
推荐答案
这也困扰着我,因为我已经切换到 Rails 3.
That is bothering me also, since I have switched to Rails 3.
关于你的第二个问题:我通过深入研究框架发现,在实际开始执行任务之前,初始化程序花费的时间大约是简单 rake 或 rails 调用的一半.
To your second question: I found by digging through the framework that the initializers take about half the time of a simple rake or rails call before it actually starts doing its task.
如果您将这些简单的时间线放入 $GEM_PATH/gems/railties-3.0.3/lib/rails/initializable.rb
中的初始化程序调用循环中(或者如果您喜欢):
If you put these simple timing lines into the loop of initializer calls in $GEM_PATH/gems/railties-3.0.3/lib/rails/initializable.rb
(or piggy-back it if you like):
def run_initializers(*args)
return if instance_variable_defined?(:@ran)
t0 = Time.now
initializers.tsort.each do |initializer|
t = Time.now
initializer.run(*args)
puts("%60s: %.3f sec" % [initializer.name, Time.now - t])
end
puts "%60s: %.3f sec" % ["for all", Time.now - t0]
@ran = true
end
或者,对于 railties 4.2.1:
Or, for railties 4.2.1:
def run_initializers(group=:default, *args)
return if instance_variable_defined?(:@ran)
t0 = Time.now
initializers.tsort.each do |initializer|
t = Time.now
initializer.run(*args) if initializer.belongs_to?(group)
puts("%60s: %.3f sec" % [initializer.name, Time.now - t])
end
puts "%60s: %.3f sec" % ["for all", Time.now - t0]
@ran = true
end
...你可以跟进发生的事情.在我的系统上,这是一台 2.4 Core 2 Duo MacBook,初始化程序大约需要 7 秒.
... you can follow up what happens. On my system, which is a 2.4 Core 2 Duo MacBook the initializers take about 7 seconds.
有一些在我的系统上特别慢.当我在一秒钟内过滤掉所有内容时,我在我的系统上得到了这个结果:
There are a few that are especially slow on my system. When I filter all out below a second, I get this result on my system:
load_active_support: 1.123 sec
active_support.initialize_time_zone: 1.579 sec
load_init_rb: 1.118 sec
set_routes_reloader: 1.291 sec
我相信有人(是我吗?)会花一些时间从那里开始并进行优化.
I am sure somebody (is it me?) will take some time to start there and optimize.
这篇关于慢轨堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!