class RuntimeProfiler::TextReport

Constants

COUNT_WIDTH
DURATION_WIDTH
FULL_DETAILS_TEMPLATE
METHODS_DETAILS_TEMPLATE
SQLS_DETAILS_TEMPLATE
TOTAL_RUNTIME_WIDTH

Attributes

data[RW]
options[RW]

Public Class Methods

new(json_file, options) click to toggle source
# File lib/runtime_profiler/text_report.rb, line 88
def initialize(json_file, options)
  self.data = JSON.parse(File.read(json_file))
  self.options = options
end

Public Instance Methods

print() click to toggle source

Private Instance Methods

calls_above(data) click to toggle source
# File lib/runtime_profiler/text_report.rb, line 254
def calls_above(data)
  data.select { |d| d['total_calls'] > options.calls_above }
end
details_template_data() click to toggle source
# File lib/runtime_profiler/text_report.rb, line 258
def details_template_data
  summary = data['profiling']['summary']

  template_data = [
    summary['total_runtime'] ? summary['total_runtime'].round(rounding) : 'n/a',
    summary['db_runtime'] ? summary['db_runtime'].round(rounding) : 'n/a',
    summary['view_runtime'] ? summary['view_runtime'].round(rounding) : 'n/a'
  ]

  methods_data = [
    summary['slowest_method']['method'],
    summary['slowest_method']['total_runtime'].round(rounding),

    summary['mostly_called_method']['method'],
    summary['mostly_called_method']['total_calls'],
    summary['mostly_called_method']['total_runtime'].round(rounding)
  ]

  sqls_data = [
    summary['total_sql_calls'],
    summary['total_unique_sql_calls'],

    summary['slowest_sql']['total_runtime'].round(rounding),
    summary['slowest_sql']['sql'],
    summary['slowest_sql']['source'],

    summary['mostly_called_sql']['total_calls'],
    summary['mostly_called_sql']['total_runtime'].round(rounding),
    summary['mostly_called_sql']['sql'],
    summary['mostly_called_sql']['runtimes'].map { |runtime| runtime[1] }.uniq
  ]

  if only_methods?
    template_data.concat(methods_data)
  elsif only_sqls?
    template_data.concat(sqls_data)
  else
    template_data
      .concat(methods_data)
      .concat(sqls_data)
  end
end
only_methods?() click to toggle source
# File lib/runtime_profiler/text_report.rb, line 110
def only_methods?
  options.only_methods.present? && options.only_sqls.blank?
end
only_sqls?() click to toggle source
# File lib/runtime_profiler/text_report.rb, line 114
def only_sqls?
  options.only_sqls.present? && options.only_methods.blank?
end
print_profiled_methods() click to toggle source
print_profiled_sql_calls() click to toggle source
print_summary() click to toggle source
rounding() click to toggle source
# File lib/runtime_profiler/text_report.rb, line 118
def rounding
  options.rounding
end
runtime_above(data) click to toggle source
# File lib/runtime_profiler/text_report.rb, line 250
def runtime_above(data)
  data.select { |d| d['total_runtime'] > options.runtime_above }
end
sort(data, methods = true) click to toggle source
# File lib/runtime_profiler/text_report.rb, line 233
def sort(data, methods = true)
  if methods
    data.sort_by do |d|
      if options.sort_by == 'max_runtime'
        -d['max']
      elsif options.sort_by == 'total_runtime'
        -d['total_runtime']
      elsif options.sort_by == 'total_calls'
        -d['total_calls']
      end
    end
  else
    options.sort_by = 'total_runtime' if options.sort_by == 'max_runtime'
    data.sort_by { |d| options.sort_by == 'total_runtime' ? -d['total_runtime'] : -d['total_calls'] }
  end
end
sql_width() click to toggle source
# File lib/runtime_profiler/text_report.rb, line 226
def sql_width
  @sql_width ||= begin
    terminal_width = Hirb::Util.detect_terminal_size.first
    (terminal_width - COUNT_WIDTH - DURATION_WIDTH - TOTAL_RUNTIME_WIDTH) / 2
  end
end
wrap_list(list, width) click to toggle source
# File lib/runtime_profiler/text_report.rb, line 220
def wrap_list(list, width)
  list.map do |text|
    wrap_text(text, width)
  end.flatten
end
wrap_text(text, width) click to toggle source
# File lib/runtime_profiler/text_report.rb, line 214
def wrap_text(text, width)
  return [text] if text.length <= width

  text.scan(/.{1,#{width}}/)
end