Skip to content

Instantly share code, notes, and snippets.

@hopsoft

hopsoft/Gemfile

Last active Aug 17, 2018
Embed
What would you like to do?
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