Class: ModernTimes::TimeTrack

Inherits:
Object
  • Object
show all
Defined in:
lib/modern_times/time_track.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimeTrack

Returns a new instance of TimeTrack.



7
8
9
10
11
12
# File 'lib/modern_times/time_track.rb', line 7

def initialize
  @mutex = Mutex.new
  @total_count = 0
  @last_time   = 0.0
  reset
end

Instance Attribute Details

#last_timeObject (readonly)

Returns the value of attribute last_time.



5
6
7
# File 'lib/modern_times/time_track.rb', line 5

def last_time
  @last_time
end

#max_timeObject (readonly)

Returns the value of attribute max_time.



5
6
7
# File 'lib/modern_times/time_track.rb', line 5

def max_time
  @max_time
end

#time_countObject (readonly)

Returns the value of attribute time_count.



5
6
7
# File 'lib/modern_times/time_track.rb', line 5

def time_count
  @time_count
end

#total_countObject (readonly)

Returns the value of attribute total_count.



5
6
7
# File 'lib/modern_times/time_track.rb', line 5

def total_count
  @total_count
end

Instance Method Details

#avg_timeObject



46
47
48
49
50
51
# File 'lib/modern_times/time_track.rb', line 46

def avg_time
  @mutex.synchronize do
    return 0.0 if @time_count == 0
    @total_time / @time_count
  end
end

#max_time_resetObject



72
73
74
75
76
77
78
# File 'lib/modern_times/time_track.rb', line 72

def max_time_reset
  @mutex.synchronize do
    val = @max_time
    @max_time = 0.0
    return val
  end
end

#min_timeObject



42
43
44
# File 'lib/modern_times/time_track.rb', line 42

def min_time
  @min_time || 0.0
end

#min_time_resetObject



64
65
66
67
68
69
70
# File 'lib/modern_times/time_track.rb', line 64

def min_time_reset
  @mutex.synchronize do
    val = @min_time || 0.0
    @min_time = nil
    return val
  end
end

#performObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/modern_times/time_track.rb', line 23

def perform
  answer = nil
  @last_time = Benchmark.realtime { answer = yield }
  @mutex.synchronize do
    @total_count += 1
    @time_count  += 1
    @total_time  += @last_time
    @min_time = @last_time if !@min_time || @last_time < @min_time
    @max_time = @last_time if @last_time > @max_time
  end
  answer
end

#resetObject



14
15
16
17
18
19
20
21
# File 'lib/modern_times/time_track.rb', line 14

def reset
  @mutex.synchronize do
    @time_count  = 0
    @min_time    = nil
    @max_time    = 0.0
    @total_time  = 0.0
  end
end

#to_sObject



80
81
82
# File 'lib/modern_times/time_track.rb', line 80

def to_s
  "sample=#{@time_count} min=#{('%.1f' % (1000*min_time))}ms max=#{('%.1f' % (1000*max_time))}ms avg=#{('%.1f' % (1000*avg_time))}ms"
end

#total_timeObject



36
37
38
39
40
# File 'lib/modern_times/time_track.rb', line 36

def total_time
  @mutex.synchronize do
    [@time_count, @total_time]
  end
end

#total_time_resetObject

Return the total time and reset it. Kind of hackish but allows for tools like Hyperic to poll these values in an xml attribute setup.



55
56
57
58
59
60
61
62
# File 'lib/modern_times/time_track.rb', line 55

def total_time_reset
  @mutex.synchronize do
    retval = [@time_count, @total_time]
    @time_count = 0
    @total_time = 0.0
    return retval
  end
end