Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Last active August 17, 2018 20:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hopsoft/6213698 to your computer and use it in GitHub Desktop.
Save hopsoft/6213698 to your computer and use it in GitHub Desktop.
Add profiling to your rake tasks.
group :development do
gem "ruby-prof"
end
if ENV["TIMESTAMPS"]
module Rake
class Task
def execute_with_timestamps(*args)
start = Time.now
execute_without_timestamps(*args)
execution_time_in_seconds = Time.now - start
puts
printf("%s took %.1f seconds\n", name, execution_time_in_seconds)
end
alias_method :execute_without_timestamps, :execute
alias_method :execute, :execute_with_timestamps
end
end
end
if ENV["PROFILE"]
require "ruby-prof"
module Rake
class Task
def execute_with_profile(*args)
RubyProf.start
execute_without_profile(*args)
result = RubyProf.stop
printer = RubyProf::CallStackPrinter.new(result)
printer.print STDOUT, {}
end
alias_method :execute_without_profile, :execute
alias_method :execute, :execute_with_profile
end
end
end
TIMESTAMPS=true rake mytask
PROFILE=true rake mytask > /path/to/profile.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment