【问题标题】:How to profile Rake task?如何配置 Rake 任务?
【发布时间】:2011-05-11 03:30:17
【问题描述】:

我需要分析 rake 任务。因为我是菜鸟,我只知道如何分析 .rb 代码 像这样:ruby -Ilib -S ruby-prof -p graph_html profile.rb > profile.html

但是我如何分析特定的 Rake 任务?

【问题讨论】:

    标签: rake task profile ruby-prof


    【解决方案1】:

    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
    

    【讨论】:

    • `which rake` 可以解决问题……我不知道 ruby-prof 需要 ruby​​ 脚本的完整路径才能进行分析。
    • 对于那些使用 rbenv 的用户,请使用rbenv which rake
    • @collimarco 他们应该被反引号包围吗,比如`rbenv which rake`
    【解决方案2】:

    如果您想要“粗略”的分析并想找出哪个任务是瓶颈,我建议 Mike William 来自here 的出色代码。当我分析我的 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
    

    【讨论】:

      【解决方案3】:

      我认为值得一提的是,如果您碰巧使用 bundler,您可能希望使用 bundle 而不是直接使用 rake 对其进行分析。

      ruby-prof -p graph_html `which bundle` -- 'exec' 'rake' '-T' > profile.html

      【讨论】:

        猜你喜欢
        • 2010-10-09
        • 2015-10-11
        • 2013-05-12
        • 1970-01-01
        • 2011-05-11
        • 1970-01-01
        • 1970-01-01
        • 2012-05-30
        • 2013-06-15
        相关资源
        最近更新 更多