Class: Timify
- Inherits:
-
Object
- Object
- Timify
- Defined in:
- lib/timify.rb
Overview
Calculates the time running from one location to another inside your code. More info: https://github.com/MarioRuiz/timify/
attr_accessor: min_time_to_show: minimum time to show the elapsed time when calling 'add' method show: print out results on screen status: (default :on) You can set :on or :off the status so it will counting the time or not
attr_reader: name: name given for the Timify instance initial_time: when the instance was created total: total time elapsed max_time_spent: maximum time measured for this instance
Instance Attribute Summary collapse
-
#initial_time ⇒ Object
readonly
Returns the value of attribute initial_time.
-
#max_time_spent ⇒ Object
readonly
Returns the value of attribute max_time_spent.
-
#min_time_to_show ⇒ Object
Returns the value of attribute min_time_to_show.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#show ⇒ Object
Returns the value of attribute show.
-
#status ⇒ Object
Returns the value of attribute status.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Instance Method Summary collapse
-
#add(*label) ⇒ Object
Adds a new point to count the time elapsed.
-
#initialize(name, min_time_to_show: 0, show: true) ⇒ Timify
constructor
input: name: name for the instance min_time_to_show: minimum time to show the elapsed time when calling 'add' method show: print out results on screen.
-
#totals(json: false) ⇒ Object
returns all data for this instance.
Constructor Details
#initialize(name, min_time_to_show: 0, show: true) ⇒ Timify
input: name: name for the instance min_time_to_show: minimum time to show the elapsed time when calling 'add' method show: print out results on screen
examples: t = Timify.new :create_user t.show = false $tim = Timify.new :dbprocess, show:false, min_time_to_show: 0.5
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/timify.rb', line 30 def initialize(name, min_time_to_show: 0, show: true) @name=name @min_time_to_show=min_time_to_show @show=show @initial_time=Time.new @max_time_spent=0 @timify_prev=@initial_time @location_prev=nil @timify={} @timify_by_label={} @timify_by_range={} @count={} @total=0 puts "<#{@name}> Timify init:<#{@initial_time}>. Location: #{caller[0].scan(/(.+):in\s/).join}" end |
Instance Attribute Details
#initial_time ⇒ Object (readonly)
Returns the value of attribute initial_time.
17 18 19 |
# File 'lib/timify.rb', line 17 def initial_time @initial_time end |
#max_time_spent ⇒ Object (readonly)
Returns the value of attribute max_time_spent.
17 18 19 |
# File 'lib/timify.rb', line 17 def max_time_spent @max_time_spent end |
#min_time_to_show ⇒ Object
Returns the value of attribute min_time_to_show.
16 17 18 |
# File 'lib/timify.rb', line 16 def min_time_to_show @min_time_to_show end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/timify.rb', line 17 def name @name end |
#show ⇒ Object
Returns the value of attribute show.
16 17 18 |
# File 'lib/timify.rb', line 16 def show @show end |
#status ⇒ Object
Returns the value of attribute status.
16 17 18 |
# File 'lib/timify.rb', line 16 def status @status end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
17 18 19 |
# File 'lib/timify.rb', line 17 def total @total end |
Instance Method Details
#add(*label) ⇒ Object
Adds a new point to count the time elapsed. It will count from the last 'add' call or Timify creation in case of the first 'add'.
input: label: (optional) In case supplied it will summarize all the ones with the same label
output: (float) time elapsed in seconds
examples: t=Timify.new :example t.add; run_sqls; t.add :database t.add #some processes t.add #some processes send_email_alert if t.add > 0.2 #some processes do_log(t.totals[:message]) if t.add > 0.5
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/timify.rb', line 67 def add(*label) return 0 if @status==:off if !label.empty? label=label[0] else label="" end time_now=Time.new time_spent=(time_now-@timify_prev).to_f @total=(time_now-@initial_time).to_f new_max=false location=caller[0].scan(/(.+):in\s/).join if time_spent > @max_time_spent then new_max = true @max_time_spent = time_spent end @timify[location]=@timify[location].to_f+time_spent @count[location]=@count[location].to_i+1 if !label.empty? @timify_by_label[label]=@timify_by_label[label].to_f+time_spent @count[label]=@count[label].to_i+1 end if !@location_prev.nil? @timify_by_range["#{@location_prev} - #{location}"]=@timify_by_range["#{@location_prev} - #{location}"].to_f + time_spent @count["#{@location_prev} - #{location}"]=@count["#{@location_prev} - #{location}"].to_i+1 end if @total > 0 percent=((@timify[location]/@total)*100).round(0) else percent=0 end if time_spent>=@min_time_to_show if @show puts "<#{@name}>#{"<#{label}>" if !label.empty?}#{"(New Max)" if new_max}: #{location} (#{percent}%): #{@total.round(2)}; #{time_spent.round(2)}" end end @timify_prev=time_now @location_prev=location return time_spent end |
#totals(json: false) ⇒ Object
returns all data for this instance
input: json: (boolean) in case of true the output will be in json format instead of a hash
output: (Hash or json string) name: (String) name given for this instance total_time: (float) total elapsed time from initialization to last 'add' call started: (Time) finished: (Time) message: (String) a printable friendly message giving all information locations, labels, ranges: (Hash) the resultant hash contains: secs: (float) number of seconds percent: (integer) percentage in reference to the total time count: (integer) number of times locations: (Hash) All summary data by location where was called labels: (Hash) All summary data by label given on 'add' method ranges: (Hash) All summary data by ranges where was called, from last 'add' call to current 'add' call
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/timify.rb', line 132 def totals(json: false) return {} if @status==:off require 'json' if json output={ name: @name, total_time: @total.to_f, started: @initial_time, finished: @timify_prev, locations: {}, labels: {}, ranges: {} } ="\n\nTotal time <#{@name}>:#{@total.to_f.round(2)}" +="\nTotal time by location:\n" @timify.each {|location, secs| if @total==0 then percent=0 else percent=(secs*100/(@total).to_f).round(0) end += "\t#{location}: #{secs.round(2)} (#{percent}%) ##{@count[location]}" output[:locations][location]={ secs: secs, percent: percent, count: @count[location] } } if !@timify_by_label.empty? += "\nTotal time by label:\n" @timify_by_label.each {|label, secs| if @total==0 then percent=0 else percent=(secs*100/(@total).to_f).round(0) end += "\t#{label}: #{secs.round(2)} (#{percent}%) ##{@count[label]}" output[:labels][label]={ secs: secs, percent: percent, count: @count[label] } } end if !@timify_by_range.empty? += "\nTotal time by range:\n" @timify_by_range.each {|range, secs| if @total==0 then percent=0 else percent=(secs*100/(@total).to_f).round(0) end += "\t#{range}: #{secs.round(2)} (#{percent}%) ##{@count[range]}" output[:ranges][range]={ secs: secs, percent: percent, count: @count[range] } } end += "\n\n" output[:message]= puts if @show output=output.to_json if json return output end |