Skip to content

Instantly share code, notes, and snippets.

@AMekss
Created October 6, 2014 16:01
Show Gist options
  • Save AMekss/500de1e1ae7e9999a854 to your computer and use it in GitHub Desktop.
Save AMekss/500de1e1ae7e9999a854 to your computer and use it in GitHub Desktop.
Capistrano 3 task performance reporter
require 'logger'
require 'colored'
class Capistrano::BM
attr_writer :logger
def initialize
@logger = Logger.new(STDOUT)
@start_times = {}
@end_times = {}
@order = []
end
def log(text)
@logger.info text
end
def colorize_time(time_spent)
color = (time_spent > 60 ? :red : (time_spent > 30 ? :yellow : :green))
"#{time_spent}s".send(color)
end
def on_before(task)
@order << [:start, task]
@start_times[task] = Time.now
end
def on_after(task)
@order << [:end, task]
@end_times[task] = Time.now
end
def print_report
log "=========================================================="
log " Performance Report"
log "=========================================================="
indent = 0
(@order + [nil]).each_cons(2) do |payload1, payload2|
action, task = payload1
if action == :start
log "#{".." * indent}#{task}" unless task == payload2.last
indent += 1
else
indent -= 1
log "#{".." * indent}#{task} #{colorize_time((@end_times[task] - @start_times[task]).to_i)}"
end
end
log "=========================================================="
end
end
bm = Capistrano::BM.new
Rake.application.tasks.each do |current_task|
before(current_task, :"bm_#{current_task}_before_hook") do
bm.on_before(current_task)
end
after(current_task, :"bm_#{current_task}_after_hook") do
bm.on_after(current_task)
end
end
after(Rake.application.top_level_tasks.last, :"bm_print_report_hook") do
bm.print_report
end
@AMekss
Copy link
Author

AMekss commented Oct 15, 2014

Just add it to your custom capistrano recepies and it will append performace report after all capistrano executions

@AMekss
Copy link
Author

AMekss commented Oct 15, 2014

You might need to add colored gem as well, or remove this dependencie by overriding Capistrano::BM#colorize_time with the version which isn't using features of this gem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment