Module: Computable

Included in:
SlowActions::Action, SlowActions::Controller, SlowActions::Session
Defined in:
lib/slow_actions/slow_actions_computation_module.rb

Overview

Computable module provides the attributes and methods to compute statistics on log entries

Cost is computed as

avg * Math.log(total + 0.1)

So that it can take into account the frequency for which an action is called

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#db_avgObject (readonly)

the average time for the database



11
12
13
# File 'lib/slow_actions/slow_actions_computation_module.rb', line 11

def db_avg
  @db_avg
end

#db_costObject (readonly)

cost for this action to query the db



23
24
25
# File 'lib/slow_actions/slow_actions_computation_module.rb', line 23

def db_cost
  @db_cost
end

#db_maxObject (readonly)

the maximum time an object ever took to query the database



17
18
19
# File 'lib/slow_actions/slow_actions_computation_module.rb', line 17

def db_max
  @db_max
end

#error_avgObject (readonly)

average error rate for this action



27
28
29
# File 'lib/slow_actions/slow_actions_computation_module.rb', line 27

def error_avg
  @error_avg
end

#render_avgObject (readonly)

the average time for rendering



9
10
11
# File 'lib/slow_actions/slow_actions_computation_module.rb', line 9

def render_avg
  @render_avg
end

#render_costObject (readonly)

cost for this action to render



21
22
23
# File 'lib/slow_actions/slow_actions_computation_module.rb', line 21

def render_cost
  @render_cost
end

#render_maxObject (readonly)

the maximum time an object ever took to render



15
16
17
# File 'lib/slow_actions/slow_actions_computation_module.rb', line 15

def render_max
  @render_max
end

#total_avgObject (readonly)

the average time for the entire object



13
14
15
# File 'lib/slow_actions/slow_actions_computation_module.rb', line 13

def total_avg
  @total_avg
end

#total_costObject (readonly)

cost for this action to complete



25
26
27
# File 'lib/slow_actions/slow_actions_computation_module.rb', line 25

def total_cost
  @total_cost
end

#total_maxObject (readonly)

the maximum time an object ever took to complete the entire action



19
20
21
# File 'lib/slow_actions/slow_actions_computation_module.rb', line 19

def total_max
  @total_max
end

Instance Method Details

#compute_timesObject

Perform all the computations in one loop



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/slow_actions/slow_actions_computation_module.rb', line 30

def compute_times
  @render_avg = 0.0
  @db_avg = 0.0
  @total_avg = 0.0
  @render_max = 0.0
  @db_max = 0.0
  @total_max = 0.0
  @error_avg = 0.0
    
  @log_entries.each do |la|
    if la.error?
      @error_avg += 1.0
      next
    end
    @render_avg += la.rendering
    @db_avg     += la.db
    @total_avg  += la.duration
    @render_max = la.rendering    if la.rendering    > @render_max
    @db_max     = la.db           if la.db           > @db_max
    @total_max  = la.duration     if la.duration     > @total_max
  end
  
  @render_avg /= @log_entries.size.to_f - @error_avg + 0.0001
  @db_avg     /= @log_entries.size.to_f - @error_avg + 0.0001
  @total_avg  /= @log_entries.size.to_f - @error_avg + 0.0001
  @error_avg  /= @log_entries.size.to_f

  # using math.log allows us to smooth out the score between
  # infrequent and very frequent actions. 0.1 is added for a
  # non-0 result
  @render_cost = @render_avg * Math.log(@log_entries.size+0.1)
  @db_cost     = @db_avg     * Math.log(@log_entries.size+0.1)
  @total_cost  = @total_avg  * Math.log(@log_entries.size+0.1)
end