Class: Analyzer
- Inherits:
-
Object
- Object
- Analyzer
- Defined in:
- lib/production_log/analyzer.rb
Overview
Calculates statistics for production logs.
Constant Summary collapse
- VERSION =
The version of the production log analyzer you are using.
'1.5.0'
Instance Attribute Summary collapse
-
#db_times ⇒ Object
readonly
An Array of all the request database times for the log file.
-
#logfile_name ⇒ Object
readonly
The logfile being read by the Analyzer.
-
#render_times ⇒ Object
readonly
An Array of all the request render times for the log file.
-
#request_times ⇒ Object
readonly
An Array of all the request total times for the log file.
Class Method Summary collapse
-
.email(file_name, recipient, subject, count = 10) ⇒ Object
Generates and sends an email report with lots of fun stuff in it.
-
.envelope(recipient, subject = nil) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#average_db_time ⇒ Object
The average total database time for all requests.
-
#average_render_time ⇒ Object
The average total render time for all requests.
-
#average_request_time ⇒ Object
The average total request time for all requests.
-
#db_time_std_dev ⇒ Object
The standard deviation of the total database time for all requests.
-
#db_times_summary ⇒ Object
A list of count/min/max/avg/std dev for database times.
-
#initialize(logfile_name) ⇒ Analyzer
constructor
Creates a new Analyzer that will read data from
logfile_name
. -
#longest_request_name ⇒ Object
:nodoc:.
-
#pad_request_name(name) ⇒ Object
:nodoc:.
-
#process ⇒ Object
Processes the log file collecting statistics from each found LogEntry.
-
#render_time_std_dev ⇒ Object
The standard deviation of the total render time for all requests.
-
#render_times_summary ⇒ Object
A list of count/min/max/avg/std dev for request times.
-
#report(count) ⇒ Object
Builds a report containing
count
slow items. -
#request_time_std_dev ⇒ Object
The standard deviation of the total request time for all requests.
-
#request_times_summary ⇒ Object
A list of count/min/max/avg/std dev for request times.
-
#slowest_db_times(limit = 10) ⇒ Object
The
limit
slowest total database times. -
#slowest_render_times(limit = 10) ⇒ Object
The
limit
slowest total render times for all requests. -
#slowest_request_times(limit = 10) ⇒ Object
The
limit
slowest total request times. -
#slowest_times(records, limit) ⇒ Object
:nodoc:.
-
#summarize(title, records) ⇒ Object
:nodoc:.
-
#time_average(records) ⇒ Object
:nodoc:.
-
#time_std_dev(records) ⇒ Object
:nodoc:.
Constructor Details
#initialize(logfile_name) ⇒ Analyzer
Creates a new Analyzer that will read data from logfile_name
.
174 175 176 177 178 179 |
# File 'lib/production_log/analyzer.rb', line 174 def initialize(logfile_name) @logfile_name = logfile_name @request_times = Hash.new { |h,k| h[k] = [] } @db_times = Hash.new { |h,k| h[k] = [] } @render_times = Hash.new { |h,k| h[k] = [] } end |
Instance Attribute Details
#db_times ⇒ Object (readonly)
An Array of all the request database times for the log file.
133 134 135 |
# File 'lib/production_log/analyzer.rb', line 133 def db_times @db_times end |
#logfile_name ⇒ Object (readonly)
The logfile being read by the Analyzer.
123 124 125 |
# File 'lib/production_log/analyzer.rb', line 123 def logfile_name @logfile_name end |
#render_times ⇒ Object (readonly)
An Array of all the request render times for the log file.
138 139 140 |
# File 'lib/production_log/analyzer.rb', line 138 def render_times @render_times end |
#request_times ⇒ Object (readonly)
An Array of all the request total times for the log file.
128 129 130 |
# File 'lib/production_log/analyzer.rb', line 128 def request_times @request_times end |
Class Method Details
.email(file_name, recipient, subject, count = 10) ⇒ Object
Generates and sends an email report with lots of fun stuff in it. This way, Mail.app will behave when given tabs.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/production_log/analyzer.rb', line 144 def self.email(file_name, recipient, subject, count = 10) analyzer = self.new file_name analyzer.process body = analyzer.report count email = self.envelope(recipient, subject) email << nil email << "<pre>#{body}</pre>" email = email.join($/) << $/ return email if $TESTING IO.popen("/usr/sbin/sendmail -i -t", "w+") do |sm| sm.print email sm.flush end end |
.envelope(recipient, subject = nil) ⇒ Object
:nodoc:
162 163 164 165 166 167 168 169 |
# File 'lib/production_log/analyzer.rb', line 162 def self.envelope(recipient, subject = nil) # :nodoc: envelope = {} envelope['To'] = recipient envelope['Subject'] = subject || "pl_analyze" envelope['Content-Type'] = "text/html" return envelope.map { |(k,v)| "#{k}: #{v}" } end |
Instance Method Details
#average_db_time ⇒ Object
The average total database time for all requests.
238 239 240 |
# File 'lib/production_log/analyzer.rb', line 238 def average_db_time return time_average(@db_times) end |
#average_render_time ⇒ Object
The average total render time for all requests.
259 260 261 |
# File 'lib/production_log/analyzer.rb', line 259 def average_render_time return time_average(@render_times) end |
#average_request_time ⇒ Object
The average total request time for all requests.
217 218 219 |
# File 'lib/production_log/analyzer.rb', line 217 def average_request_time return time_average(@request_times) end |
#db_time_std_dev ⇒ Object
The standard deviation of the total database time for all requests.
245 246 247 |
# File 'lib/production_log/analyzer.rb', line 245 def db_time_std_dev return time_std_dev(@db_times) end |
#db_times_summary ⇒ Object
A list of count/min/max/avg/std dev for database times.
287 288 289 |
# File 'lib/production_log/analyzer.rb', line 287 def db_times_summary return summarize("DB Times", @db_times) end |
#longest_request_name ⇒ Object
:nodoc:
394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/production_log/analyzer.rb', line 394 def longest_request_name # :nodoc: return @longest_req if defined? @longest_req names = @request_times.keys.map do |name| (name||'Unknown').length + 1 # + : - HACK where does nil come from? end @longest_req = names.max @longest_req = 'Unknown'.length + 1 if @longest_req.nil? return @longest_req end |
#pad_request_name(name) ⇒ Object
:nodoc:
408 409 410 411 412 413 |
# File 'lib/production_log/analyzer.rb', line 408 def pad_request_name(name) # :nodoc: name = (name||'Unknown') + ':' # HACK where does nil come from? padding_width = longest_request_name - name.length padding_width = 0 if padding_width < 0 name += (' ' * padding_width) end |
#process ⇒ Object
Processes the log file collecting statistics from each found LogEntry.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/production_log/analyzer.rb', line 184 def process if File.directory?(@logfile_name) dir_path = @logfile_name Dir.new(dir_path).each do |filename| unless filename[0,1] == "." file_path = File.join(dir_path, filename) File.open file_path do |fp| LogParser.parse fp do |entry| entry_page = entry.page next if entry_page.nil? @request_times[entry_page] << entry.request_time @db_times[entry_page] << entry.db_time @render_times[entry_page] << entry.render_time end end end end else File.open @logfile_name do |fp| LogParser.parse fp do |entry| entry_page = entry.page next if entry_page.nil? @request_times[entry_page] << entry.request_time @db_times[entry_page] << entry.db_time @render_times[entry_page] << entry.render_time end end end end |
#render_time_std_dev ⇒ Object
The standard deviation of the total render time for all requests.
266 267 268 |
# File 'lib/production_log/analyzer.rb', line 266 def render_time_std_dev return time_std_dev(@render_times) end |
#render_times_summary ⇒ Object
A list of count/min/max/avg/std dev for request times.
294 295 296 |
# File 'lib/production_log/analyzer.rb', line 294 def render_times_summary return summarize("Render Times", @render_times) end |
#report(count) ⇒ Object
Builds a report containing count
slow items.
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/production_log/analyzer.rb', line 301 def report(count) return "No requests to analyze" if request_times.empty? text = [] text << request_times_summary text << nil text << "Slowest Request Times:" slowest_request_times(count).each do |time, name| text << "\t#{name} took #{'%0.3f' % time}s" end text << nil text << "-" * 72 text << nil text << db_times_summary text << nil text << "Slowest Total DB Times:" slowest_db_times(count).each do |time, name| text << "\t#{name} took #{'%0.3f' % time}s" end text << nil text << "-" * 72 text << nil text << render_times_summary text << nil text << "Slowest Total Render Times:" slowest_render_times(count).each do |time, name| unless ('%0.3f' % time) == "0.000" text << "\t#{name} took #{'%0.3f' % time}s" end end text << nil return text.join($/) end |
#request_time_std_dev ⇒ Object
The standard deviation of the total request time for all requests.
224 225 226 |
# File 'lib/production_log/analyzer.rb', line 224 def request_time_std_dev return time_std_dev(@request_times) end |
#request_times_summary ⇒ Object
A list of count/min/max/avg/std dev for request times.
280 281 282 |
# File 'lib/production_log/analyzer.rb', line 280 def request_times_summary return summarize("Request Times", @request_times) end |
#slowest_db_times(limit = 10) ⇒ Object
The limit
slowest total database times.
252 253 254 |
# File 'lib/production_log/analyzer.rb', line 252 def slowest_db_times(limit = 10) return slowest_times(@db_times, limit) end |
#slowest_render_times(limit = 10) ⇒ Object
The limit
slowest total render times for all requests.
273 274 275 |
# File 'lib/production_log/analyzer.rb', line 273 def slowest_render_times(limit = 10) return slowest_times(@render_times, limit) end |
#slowest_request_times(limit = 10) ⇒ Object
The limit
slowest total request times.
231 232 233 |
# File 'lib/production_log/analyzer.rb', line 231 def slowest_request_times(limit = 10) return slowest_times(@request_times, limit) end |
#slowest_times(records, limit) ⇒ Object
:nodoc:
370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/production_log/analyzer.rb', line 370 def slowest_times(records, limit) # :nodoc: slowest_times = SlowestTimes.new limit records.each do |name, times| times.each do |time| slowest_times << [time, name] end end return slowest_times.sort_by { |time, name| time }.reverse end |
#summarize(title, records) ⇒ Object
:nodoc:
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/production_log/analyzer.rb', line 341 def summarize(title, records) # :nodoc: record = nil list = [] # header record = [pad_request_name("#{title} Summary"), 'Count', 'Avg', 'Std Dev', 'Min', 'Max'] list << record.join("\t") # all requests times = records.values.flatten record = [times.average, times.standard_deviation, times.min, times.max] record.map! { |v| "%0.3f" % v } record.unshift [pad_request_name('ALL REQUESTS'), times.size] list << record.join("\t") # spacer list << nil records.sort_by { |k,v| v.size}.reverse_each do |req, times| record = [times.average, times.standard_deviation, times.min, times.max] record.map! { |v| "%0.3f" % v } record.unshift ["#{pad_request_name req}", times.size] list << record.join("\t") end return list.join("\n") end |
#time_average(records) ⇒ Object
:nodoc:
382 383 384 385 386 |
# File 'lib/production_log/analyzer.rb', line 382 def time_average(records) # :nodoc: times = records.values.flatten times.delete 0 return times.average end |
#time_std_dev(records) ⇒ Object
:nodoc:
388 389 390 391 392 |
# File 'lib/production_log/analyzer.rb', line 388 def time_std_dev(records) # :nodoc: times = records.values.flatten times.delete 0 return times.standard_deviation end |