Class: Queris::Query::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/queris/query/timer.rb

Instance Method Summary collapse

Constructor Details

#initializeTimer

Returns a new instance of Timer.



4
5
6
7
8
# File 'lib/queris/query/timer.rb', line 4

def initialize
  @time_start={}
  @time={}
  @times_recorded={}
end

Instance Method Details

#finish(attr) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/queris/query/timer.rb', line 14

def finish(attr)
  attr = attr.to_sym
  start_time = @time_start[attr]
  raise "Query Profiling timing attribute #{attr} was never started." if start_time.nil?
  t = Time.now.to_f - start_time
  @time_start[attr]=nil
  record attr, (Time.now.to_f - start_time)
end

#record(attr, val) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/queris/query/timer.rb', line 22

def record(attr, val)
  attr = attr.to_sym
  @times_recorded[attr]||=0
  @times_recorded[attr]+=1
  @time[attr]=0 if @time[attr].nil? || @time[attr]=='?'
  @time[attr]+=val
end

#start(attr) ⇒ Object



9
10
11
12
13
# File 'lib/queris/query/timer.rb', line 9

def start(attr)
  attr = attr.to_sym
  @time_start[attr]=Time.now.to_f
  @time[attr] ||= '?'
end

#to_sObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/queris/query/timer.rb', line 29

def to_s
  mapped = @time.map do |k,v|
    v = v.round(4) if Numeric === v
    if (times = @times_recorded[k]) != 1
      "#{k}(#{times} times):#{v}"
    else
      "#{k}:#{v}"
    end
  end
  mapped.join ", "
end