Class: Rack::MiniProfiler::TimerStruct::Request

Inherits:
Base
  • Object
show all
Defined in:
lib/mini_profiler/timer_struct/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], #[]=, #attributes, #to_json

Constructor Details

#initialize(name, page, parent) ⇒ Request

Returns a new instance of Request.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mini_profiler/timer_struct/request.rb', line 14

def initialize(name, page, parent)
  start_millis = (Time.now.to_f * 1000).to_i - page[:started]
  depth        = parent ? parent.depth + 1 : 0
  super(
    :id                                      => MiniProfiler.generate_id,
    :name                                    => name,
    :duration_milliseconds                   => 0,
    :duration_without_children_milliseconds  => 0,
    :start_milliseconds                      => start_millis,
    :parent_timing_id                        => nil,
    :children                                => [],
    :has_children                            => false,
    :key_values                              => nil,
    :has_sql_timings                         => false,
    :has_duplicate_sql_timings               => false,
    :trivial_duration_threshold_milliseconds => 2,
    :sql_timings                             => [],
    :sql_timings_duration_milliseconds       => 0,
    :is_trivial                              => false,
    :is_root                                 => false,
    :depth                                   => depth,
    :executed_readers                        => 0,
    :executed_scalars                        => 0,
    :executed_non_queries                    => 0,
    :custom_timing_stats                     => {},
    :custom_timings                          => {}
  )
  @children_duration = 0
  @start             = Time.now
  @parent            = parent
  @page              = page
end

Instance Attribute Details

#children_durationObject

Returns the value of attribute children_duration.



12
13
14
# File 'lib/mini_profiler/timer_struct/request.rb', line 12

def children_duration
  @children_duration
end

Class Method Details

.createRoot(name, page) ⇒ Object



6
7
8
9
10
# File 'lib/mini_profiler/timer_struct/request.rb', line 6

def self.createRoot(name, page)
  TimerStruct::Request.new(name, page, nil).tap do |timer|
    timer[:is_root] = true
  end
end

Instance Method Details

#add_child(name) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/mini_profiler/timer_struct/request.rb', line 75

def add_child(name)
  TimerStruct::Request.new(name, @page, self).tap do |timer|
    self[:children].push(timer)
    self[:has_children]      = true
    timer[:parent_timing_id] = self[:id]
    timer[:depth]            = self[:depth] + 1
  end
end

#add_custom(type, elapsed_ms, page) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/mini_profiler/timer_struct/request.rb', line 94

def add_custom(type, elapsed_ms, page)
  TimerStruct::Custom.new(type, elapsed_ms, page, self).tap do |timer|
    timer[:parent_timing_id] = self[:id]

    self[:custom_timings][type] ||= []
    self[:custom_timings][type].push(timer)

    self[:custom_timing_stats][type] ||= {:count => 0, :duration => 0.0}
    self[:custom_timing_stats][type][:count]    += 1
    self[:custom_timing_stats][type][:duration] += elapsed_ms

    page[:custom_timing_stats][type] ||= {:count => 0, :duration => 0.0}
    page[:custom_timing_stats][type][:count]    += 1
    page[:custom_timing_stats][type][:duration] += elapsed_ms
  end
end

#add_sql(query, elapsed_ms, page, skip_backtrace = false, full_backtrace = false) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/mini_profiler/timer_struct/request.rb', line 84

def add_sql(query, elapsed_ms, page, skip_backtrace = false, full_backtrace = false)
  TimerStruct::Sql.new(query, elapsed_ms, page, self , skip_backtrace, full_backtrace).tap do |timer|
    self[:sql_timings].push(timer)
    timer[:parent_timing_id] = self[:id]
    self[:has_sql_timings]   = true
    self[:sql_timings_duration_milliseconds] += elapsed_ms
    page[:duration_milliseconds_in_sql]      += elapsed_ms
  end
end

#childrenObject



63
64
65
# File 'lib/mini_profiler/timer_struct/request.rb', line 63

def children
  self[:children]
end

#custom_timingsObject



67
68
69
# File 'lib/mini_profiler/timer_struct/request.rb', line 67

def custom_timings
  self[:custom_timings]
end

#depthObject



59
60
61
# File 'lib/mini_profiler/timer_struct/request.rb', line 59

def depth
  self[:depth]
end

#duration_msObject



47
48
49
# File 'lib/mini_profiler/timer_struct/request.rb', line 47

def duration_ms
  self[:duration_milliseconds]
end

#record_time(milliseconds = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/mini_profiler/timer_struct/request.rb', line 111

def record_time(milliseconds = nil)
  milliseconds ||= (Time.now - @start) * 1000
  self[:duration_milliseconds]                  = milliseconds
  self[:is_trivial]                             = true if milliseconds < self[:trivial_duration_threshold_milliseconds]
  self[:duration_without_children_milliseconds] = milliseconds - @children_duration

  if @parent
    @parent.children_duration += milliseconds
  end

end

#sql_timingsObject



71
72
73
# File 'lib/mini_profiler/timer_struct/request.rb', line 71

def sql_timings
  self[:sql_timings]
end

#startObject



55
56
57
# File 'lib/mini_profiler/timer_struct/request.rb', line 55

def start
  @start
end

#start_msObject



51
52
53
# File 'lib/mini_profiler/timer_struct/request.rb', line 51

def start_ms
  self[:start_milliseconds]
end