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

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

Constructor Details

#initialize(name, page, parent) ⇒ Request

Returns a new instance of Request.



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
46
47
# File 'lib/mini_profiler/timer_struct/request.rb', line 16

def initialize(name, page, parent)
  start_millis = (Process.clock_gettime(Process::CLOCK_MONOTONIC) * 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             = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @parent            = parent
  @page              = page
end

Instance Attribute Details

#children_durationObject

Returns the value of attribute children_duration.



14
15
16
# File 'lib/mini_profiler/timer_struct/request.rb', line 14

def children_duration
  @children_duration
end

#parentObject

Returns the value of attribute parent.



14
15
16
# File 'lib/mini_profiler/timer_struct/request.rb', line 14

def parent
  @parent
end

#startObject

Returns the value of attribute start.



14
15
16
# File 'lib/mini_profiler/timer_struct/request.rb', line 14

def start
  @start
end

Class Method Details

.createRoot(name, page) ⇒ Object



8
9
10
11
12
# File 'lib/mini_profiler/timer_struct/request.rb', line 8

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



81
82
83
84
85
86
87
88
# File 'lib/mini_profiler/timer_struct/request.rb', line 81

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/mini_profiler/timer_struct/request.rb', line 134

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, params = nil, skip_backtrace = false, full_backtrace = false) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/mini_profiler/timer_struct/request.rb', line 104

def add_sql(query, elapsed_ms, page, params = nil, skip_backtrace = false, full_backtrace = false)
  TimerStruct::Sql.new(query, elapsed_ms, page, self, params, 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
    page[:sql_count] += 1
  end
end

#adjust_depthObject



178
179
180
181
# File 'lib/mini_profiler/timer_struct/request.rb', line 178

def adjust_depth
  self[:depth] = self.parent ? self.parent[:depth] + 1 : 0
  self[:children].each(&:adjust_depth)
end

#childrenObject



69
70
71
# File 'lib/mini_profiler/timer_struct/request.rb', line 69

def children
  self[:children]
end

#custom_timingsObject



73
74
75
# File 'lib/mini_profiler/timer_struct/request.rb', line 73

def custom_timings
  self[:custom_timings]
end

#depthObject



65
66
67
# File 'lib/mini_profiler/timer_struct/request.rb', line 65

def depth
  self[:depth]
end

#duration_msObject



53
54
55
# File 'lib/mini_profiler/timer_struct/request.rb', line 53

def duration_ms
  self[:duration_milliseconds]
end

#duration_ms_in_sqlObject



57
58
59
# File 'lib/mini_profiler/timer_struct/request.rb', line 57

def duration_ms_in_sql
  @attributes[:duration_milliseconds_in_sql]
end

#move_child(child, destination) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mini_profiler/timer_struct/request.rb', line 90

def move_child(child, destination)
  if index = self[:children].index(child)
    self[:children].slice!(index)
    self[:has_children] = self[:children].size > 0

    destination[:children].push(child)
    destination[:has_children] = true

    child[:parent_timing_id] = destination[:id]
    child.parent = destination
    child.adjust_depth
  end
end

#move_custom(type, custom, destination) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/mini_profiler/timer_struct/request.rb', line 151

def move_custom(type, custom, destination)
  if index = self[:custom_timings][type]&.index(custom)
    custom[:parent_timing_id] = destination[:id]
    custom.parent = destination
    self[:custom_timings][type].slice!(index)
    if self[:custom_timings][type].size == 0
      self[:custom_timings].delete(type)
      self[:custom_timing_stats].delete(type)
    else
      self[:custom_timing_stats][type][:count] -= 1
      self[:custom_timing_stats][type][:duration] -= custom[:duration_milliseconds]
    end
    destination[:custom_timings][type] ||= []
    destination[:custom_timings][type].push(custom)
    destination[:custom_timing_stats][type] ||= { count: 0, duration: 0.0 }
    destination[:custom_timing_stats][type][:count] += 1
    destination[:custom_timing_stats][type][:duration] += custom[:duration_milliseconds]
  end
end

#move_sql(sql, destination) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/mini_profiler/timer_struct/request.rb', line 115

def move_sql(sql, destination)
  if index = self[:sql_timings].index(sql)
    self[:sql_timings].slice!(index)
    self[:has_sql_timings] = self[:sql_timings].size > 0
    self[:sql_timings_duration_milliseconds] -= sql[:duration_milliseconds]
    destination[:sql_timings].push(sql)
    destination[:has_sql_timings] = true
    destination[:sql_timings_duration_milliseconds] += sql[:duration_milliseconds]
    sql[:parent_timing_id] = destination[:id]
    sql.parent = destination
  end
end

#nameObject



49
50
51
# File 'lib/mini_profiler/timer_struct/request.rb', line 49

def name
  @attributes[:name]
end

#record_time(milliseconds = nil) ⇒ Object



171
172
173
174
175
176
# File 'lib/mini_profiler/timer_struct/request.rb', line 171

def record_time(milliseconds = nil)
  milliseconds ||= (Process.clock_gettime(Process::CLOCK_MONOTONIC) - @start) * 1000
  self[:duration_milliseconds]                  = milliseconds
  self[:is_trivial]                             = true if milliseconds < self[:trivial_duration_threshold_milliseconds]
  self[:duration_without_children_milliseconds] = milliseconds - self[:children].sum(&:duration_ms)
end

#report_reader_duration(elapsed_ms, row_count = nil, class_name = nil) ⇒ Object

please call SqlTiming#report_reader_duration instead



129
130
131
132
# File 'lib/mini_profiler/timer_struct/request.rb', line 129

def report_reader_duration(elapsed_ms, row_count = nil, class_name = nil)
  last_time = self[:sql_timings]&.last
  last_time&.report_reader_duration(elapsed_ms, row_count, class_name)
end

#sql_timingsObject



77
78
79
# File 'lib/mini_profiler/timer_struct/request.rb', line 77

def sql_timings
  self[:sql_timings]
end

#start_msObject



61
62
63
# File 'lib/mini_profiler/timer_struct/request.rb', line 61

def start_ms
  self[:start_milliseconds]
end