我需要分析 rake 任务。因为我是菜鸟,我只知道如何分析 .rb 代码,如下所示:ruby -Ilib -S ruby-prof -p graph_html profile.rb > profile.html
但是我如何分析一个特定的 Rake 任务呢?
Rake 只是一个 Ruby 脚本,因此您应该能够针对 rake 调用 ruby-prof,就像您分析任何其他脚本一样。
鉴于您对 ruby-prof 的调用,请尝试:
ruby -Ilib -S ruby-prof -p graph_html `which rake` TASK > profile.html
我刚刚使用了以下命令行:
ruby-prof -p graph_html /usr/local/bin/rake19 import_from_aws file=~/sourcedata batch=test1 > /tmp/profile.html
分析调用:
rake19 import_from_aws file=~/sourcedata batch=test1
如果您想要“粗略”的分析并想找出哪个任务是瓶颈,我建议 Mike William 的优秀代码来自这里。当我分析我的 Rake 任务时,它工作得很好。
module Rake
class Task
def execute_with_timestamps(*args)
start = Time.now
execute_without_timestamps(*args)
execution_time_in_seconds = Time.now - start
printf("** %s took %.1f seconds\n", name, execution_time_in_seconds)
end
alias :execute_without_timestamps :execute
alias :execute :execute_with_timestamps
end
end
我认为值得一提的是,如果您碰巧使用 bundler,您可能希望使用 bundle 而不是直接使用 rake 对其进行分析。
ruby-prof -p graph_html `which bundle` -- 'exec' 'rake' '-T' > profile.html