How to profile Rake task?
Asked Answered
M

3

20

I need to profile the rake task. Cause I'm noob I know only how to profile .rb code like this: ruby -Ilib -S ruby-prof -p graph_html profile.rb > profile.html

But how do I profile a specific Rake task?

Mall answered 16/11, 2010 at 23:18 Comment(0)
S
19

Rake is just a Ruby script, so you should be able to just call ruby-prof against rake, in the same way you'd profile any other script.

Given your invocation of ruby-prof, try:

ruby -Ilib -S ruby-prof -p graph_html `which rake` TASK > profile.html

I've just used the following command line:

ruby-prof -p graph_html /usr/local/bin/rake19 import_from_aws file=~/sourcedata batch=test1 > /tmp/profile.html

To profile an invocation of:

rake19 import_from_aws file=~/sourcedata batch=test1
Speight answered 1/12, 2010 at 15:0 Comment(3)
The `which rake` does the trick… I didn't knew ruby-prof required the complete path of the ruby script to profile.Trichloroethylene
For those using rbenv use rbenv which rakeSoares
@Soares Should they be surrounded by backticks, like `rbenv which rake`?Annulment
D
1

If you want 'coarse' profiling and want to find out which task is the bottleneck, I suggest Mike William's excellent piece of code from here. It worked beautifully when I was profiling my Rake tasks.

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
Dislodge answered 20/8, 2012 at 10:41 Comment(0)
G
0

I think it's worth mentioning that you might want to profile it using bundle instead of rake directly if you happen to be using bundler.

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

Gluten answered 27/6, 2018 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.