Class: Vernier::Output::FileListing

Inherits:
Object
  • Object
show all
Includes:
Vernier::OutputHelpers
Defined in:
lib/vernier/output/file_listing.rb

Defined Under Namespace

Classes: SamplesByLocation

Instance Method Summary collapse

Methods included from Vernier::OutputHelpers

#collapse_stack_weights, #sanitize_string

Constructor Details

#initialize(profile) ⇒ FileListing

Returns a new instance of FileListing.



19
20
21
# File 'lib/vernier/output/file_listing.rb', line 19

def initialize(profile)
  @profile = profile
end

Instance Method Details

#format_file(output, filename, all_samples, total:) ⇒ Object



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

def format_file(output, filename, all_samples, total:)
  samples = all_samples[filename]

  # file_name, lines, file_wall, file_cpu, file_idle, file_sort
  output << sprintf(" TOTAL |  SELF  | LINE SOURCE\n")
  File.readlines(filename).each_with_index do |line, i|
    lineno = i + 1
    calls = samples[lineno]

    if calls && calls.total > 0
      output << sprintf("%5.1f%% | %5.1f%% | % 4i  %s", 100 * calls.total / total.to_f, 100 * calls.self / total.to_f, lineno, line)
    else
      output << sprintf("       |        | % 4i  %s", lineno, line)
    end
  end
end

#format_file_html(output, filename, relevant_files) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/vernier/output/file_listing.rb', line 163

def format_file_html(output, filename, relevant_files)
  samples = relevant_files[filename]

  # file_name, lines, file_wall, file_cpu, file_idle, file_sort
  output << sprintf(" TOTAL |  SELF  | LINE SOURCE\n")
  File.readlines(filename).each_with_index do |line, i|
    lineno = i + 1
    calls = samples[lineno]

    if calls && calls.total > 0
      output << sprintf("%5.1f%% | %5.1f%% | % 4i  %s", 100 * calls.total / total.to_f, 100 * calls.self / total.to_f, lineno, CGI::escapeHTML(line))
    else
      output << sprintf("       |        | % 4i  %s", lineno, CGI::escapeHTML(line))
    end
  end
end

#html_output(output, relevant_files) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/vernier/output/file_listing.rb', line 151

def html_output(output, relevant_files)
  output << "<pre>"
  output << "  SELF     FILE\n"
  relevant_files.sort_by {|k, v| -v.values.map(&:self).sum }.each do |filename, file_contents|
    tmpl = "<details style=\"display:inline-block;vertical-align:top;\"><summary>%s</summary>"
    output << sprintf("% 5.1f%%   #{tmpl}\n", file_contents.values.map(&:self).sum * 100 / total.to_f, filename)
    format_file_html(output, filename, relevant_files)
    output << "</details>\n"
  end
  output << "</pre>"
end

#output(template: nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/vernier/output/file_listing.rb', line 107

def output(template: nil)
  output = +""

  relevant_files = samples_by_file.select do |k, v|
    next if k.start_with?("gem:")
    next if k.start_with?("rubylib:")
    next if k.start_with?("<")
    v.values.map(&:total).sum > total * 0.01
  end

  if template == "html"
    html_output(output, relevant_files)
  else
    relevant_files.keys.sort.each do |filename|
      output << "="*80 << "\n"
      output << filename << "\n"
      output << "-"*80 << "\n"
      format_file(output, filename, samples_by_file, total: total)
    end
    output << "="*80 << "\n"
  end
end

#samples_by_fileObject



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vernier/output/file_listing.rb', line 23

def samples_by_file
  return @samples_by_file if defined?(@samples_by_file)

  thread = @profile.main_thread
  if Hash === thread
    # live profile
    stack_table = @profile._stack_table
    filename_filter = FilenameFilter.new
  else
    stack_table = thread.stack_table
    filename_filter = ->(x) { x }
  end

  weights = thread[:weights]
  samples = thread[:samples]

  stack_weights = collapse_stack_weights(samples, weights)

  self_by_frame, total_by_frame = frame_weights(stack_table, stack_weights)

  samples_by_file = Hash.new do |h, k|
    h[k] = Hash.new do |h2, k2|
      h2[k2] = SamplesByLocation.new
    end
  end

  self_by_frame.each_with_index do |self_weight, frame|
    total_weight = total_by_frame[frame]
    next if self_weight == 0 && total_weight == 0

    line = stack_table.frame_line_no(frame)
    func_index = stack_table.frame_func_idx(frame)
    filename = stack_table.func_filename(func_index)

    location = samples_by_file[filename][line]
    location.self += self_weight
    location.total += total_weight
  end

  @samples_by_file = samples_by_file.transform_keys! do |filename|
    filename_filter.call(filename)
  end
end

#totalObject



130
131
132
# File 'lib/vernier/output/file_listing.rb', line 130

def total
  @total ||= @profile.main_thread[:weights].sum
end