Class: ModernTimes::TimeTrack
- Inherits:
-
Object
- Object
- ModernTimes::TimeTrack
- Defined in:
- lib/modern_times/time_track.rb
Instance Attribute Summary collapse
-
#last_time ⇒ Object
readonly
Returns the value of attribute last_time.
-
#max_time ⇒ Object
readonly
Returns the value of attribute max_time.
-
#time_count ⇒ Object
readonly
Returns the value of attribute time_count.
-
#total_count ⇒ Object
readonly
Returns the value of attribute total_count.
Instance Method Summary collapse
- #avg_time ⇒ Object
-
#initialize ⇒ TimeTrack
constructor
A new instance of TimeTrack.
- #max_time_reset ⇒ Object
- #min_time ⇒ Object
- #min_time_reset ⇒ Object
- #perform ⇒ Object
- #reset ⇒ Object
- #to_s ⇒ Object
- #total_time ⇒ Object
-
#total_time_reset ⇒ Object
Return the total time and reset it.
Constructor Details
#initialize ⇒ TimeTrack
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_time ⇒ Object (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_time ⇒ Object (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_count ⇒ Object (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_count ⇒ Object (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_time ⇒ Object
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_reset ⇒ Object
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_time ⇒ Object
42 43 44 |
# File 'lib/modern_times/time_track.rb', line 42 def min_time @min_time || 0.0 end |
#min_time_reset ⇒ Object
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 |
#perform ⇒ Object
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 |
#reset ⇒ Object
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_s ⇒ Object
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_time ⇒ Object
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_reset ⇒ Object
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 |