Class: LiquidProf::AsciiReporter

Inherits:
Reporter
  • Object
show all
Defined in:
lib/liquidprof/ascii_reporter.rb

Instance Attribute Summary

Attributes inherited from Reporter

#prof

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Reporter

#format_bytes, #initialize, #summarize_stats

Constructor Details

This class inherits a constructor from LiquidProf::Reporter

Class Method Details

.report(prof) ⇒ Object



11
12
13
# File 'lib/liquidprof/ascii_reporter.rb', line 11

def self.report(prof)
  AsciiReporter.new(prof).report()
end

Instance Method Details

#format_node_stats(stats, skip_times = false) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/liquidprof/ascii_reporter.rb', line 3

def format_node_stats(stats, skip_times=false)
  [
    skip_times ? nil : ("%dx" % stats[:calls][:avg]),
    "%.2fms" % (1000.0 * stats[:times][:avg]),
    format_bytes(stats[:lengths][:avg])
  ].compact.join(", ")
end

#format_table(lines) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/liquidprof/ascii_reporter.rb', line 48

def format_table(lines)
  max_width = []

  lines.each do |line|
    line.each_with_index do |column, i|
      next if i == line.length-1
      max_width[i] = [ max_width[i], column.length ].compact.max
    end
  end

  lines.each do |line|
    line.each_with_index do |column, i|
      next if i == line.length-1
      line[i] = column.rjust(max_width[i])
    end
  end

  table = ""
  table << [ " " * (max_width[0]+2), "-" * (max_width[1]+4), "" ].join("+") + "\n"
  table << lines[0..-2].map{ |line| line.join("  |  ") }.join("\n") + "\n"
  table << [ " " * (max_width[0]+2), "-" * (max_width[1]+4), "" ].join("+") + "\n"
  table << lines.last.join("     ").rjust(max_width[1])
  table
end

#reportObject



15
16
17
18
19
20
21
22
23
# File 'lib/liquidprof/ascii_reporter.rb', line 15

def report
  output = ""
  @prof.templates.each do |template|
    output << report_template(template)
    output << "\n"
    output << "\n" if @prof.templates.length > 1
  end
  output
end

#report_template(template) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/liquidprof/ascii_reporter.rb', line 25

def report_template(template)
  summarize_stats(template)
  sidenotes = Hash.new{ Array.new }
  res = render_source(template) do |node, line|
    sidenotes[line] += [ @prof.stats[node] ]
    node.raw_markup
  end
  sidenotes = sidenotes.inject(Array.new) do |a,(k,v)|
    a[k] = v.map{ |stats| format_node_stats(stats) }
    a
  end

  output = []
  res.each_with_index do |line, i|
    (sidenotes[i] || [""]).each_with_index do |note, j|
      output << ((j == 0) ? [ (i+1).to_s, note, line] : [ "", note, "" ])
    end
  end

  output << [ "", format_node_stats(@prof.stats[template.root], true), "" ]
  format_table(output)
end