Skip to content

Instantly share code, notes, and snippets.

@benzittlau
Last active March 26, 2016 18:22
Show Gist options
  • Save benzittlau/4a933c9c66b567d9af8c to your computer and use it in GitHub Desktop.
Save benzittlau/4a933c9c66b567d9af8c to your computer and use it in GitHub Desktop.
Simple benchmarking util
# Usage:
# $bm = Benchmarking.new
# VisitGeneratorWorker.new.perform(work_order.id)
# $bm.finish
# $bm.results
#
# Inside what you're benchmarking:
# $bm.mark("This is a data point")
#
# Output:
#
#+------------+--------------+--------------+
#| Identifier | Differential | Overall Time |
#+------------+--------------+--------------+
#| Start | 0.0 | 0.0 |
#| End | 13.181004 | 13.181004 |
#+------------+--------------+--------------+
#
class Benchmarking
def initialize
@start_time = Time.now
@marks = [["Start", @start_time]]
end
def mark(identifier= "")
@marks << [identifier, Time.now]
end
def finish
mark("End")
end
def results
rows = []
last_time = @start_time
@marks.each do |mark|
identifier, time = mark
rows << [identifier, time - last_time, time - @start_time]
end
table = Terminal::Table.new :headings => ['Identifier', 'Differential', 'Overall Time'], :rows => rows
puts table
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment