Module: Timer
- Defined in:
- lib/imsg-grep/dev/timer.rb
Overview
Simple timer for profiling with lap times and progress bars
Class Method Summary collapse
-
.finish ⇒ Object
Display final timing report with bars and percentages.
-
.lap(msg) ⇒ Object
Record lap time with message.
-
.start ⇒ Object
Initialize timer state.
Class Method Details
.finish ⇒ Object
Display final timing report with bars and percentages
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/imsg-grep/dev/timer.rb', line 25 def self.finish max_lap_time = @laps.map { |lap| lap[:time] }.max longest_line = @laps.map { |lap| lap[:line].length }.max start_col = longest_line + 3 @laps.reverse.each do |lap| pct = "%4.1f" % (lap[:time] / @total_time * 100) = (lap[:time] / max_lap_time * 20).round = "█" * + "░" * (20 - ) padding = " " * [0, start_col - lap[:line].length].max $stderr.print "\e[#{@laps.size - lap[:line_num]}A\r#{lap[:line]}#{padding}#{bar} #{pct}%\e[#{@laps.size - lap[:line_num]}B\r" end $stderr.puts end |
.lap(msg) ⇒ Object
Record lap time with message
14 15 16 17 18 19 20 21 22 |
# File 'lib/imsg-grep/dev/timer.rb', line 14 def self.lap(msg) now = Process.clock_gettime(Process::CLOCK_MONOTONIC) lap_time = (now - @last_lap) * 1000 @total_time += lap_time line = "%5.0fms / %5.0fms: #{msg}" % [lap_time, @total_time] $stderr.puts line @laps << { msg: msg, time: lap_time, line: line, line_num: @laps.size } @last_lap = now end |
.start ⇒ Object
Initialize timer state
6 7 8 9 10 11 |
# File 'lib/imsg-grep/dev/timer.rb', line 6 def self.start @t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC) @last_lap = @t0 @total_time = 0.0 @laps = [] end |