Class: Support::Rake::DurationTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/support/rake/duration_tracker.rb

Defined Under Namespace

Classes: DurationInfo

Constant Summary collapse

DIVIDER =
"-" * 80

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDurationTracker

Returns a new instance of DurationTracker.



13
14
15
16
# File 'lib/support/rake/duration_tracker.rb', line 13

def initialize
  @durations = []
  @mutex = Mutex.new
end

Class Method Details

.instanceObject



9
10
11
# File 'lib/support/rake/duration_tracker.rb', line 9

def self.instance
  @instance ||= DurationTracker.new
end

Instance Method Details



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/support/rake/duration_tracker.rb', line 24

def print_report!(verb: "updated")
  @mutex.synchronize do
    next if @durations.empty?

    puts
    puts DIVIDER
    puts "#{verb.capitalize} successfully!"
    puts

    @durations.each do |info|
      duration = info.seconds.round(3)
      duration = "<0.01" if duration < 0.01

      puts "#{info.name}: #{duration}s"
    end

    total_duration = @durations.sum(&:seconds).round(3)
    puts
    puts "Took #{total_duration} second#{'s' if total_duration != 1}"
    puts DIVIDER

    @durations = []
  end
end

#report_duration(name, seconds) ⇒ Object



18
19
20
21
22
# File 'lib/support/rake/duration_tracker.rb', line 18

def report_duration(name, seconds)
  @mutex.synchronize do
    @durations.push(DurationInfo.new(name: name, seconds: seconds))
  end
end