Module: Vernier::OutputHelpers

Instance Method Summary collapse

Instance Method Details

#collapse_stack_weights(samples, weights) ⇒ Object

Collapses parallel samples/weights arrays into a hash of total weight per stack index. Retained-mode profiles have one sample per object and many samples share the same stack, so collapsing first keeps later aggregation proportional to the number of unique stacks.



9
10
11
12
13
14
15
# File 'lib/vernier/output_helpers.rb', line 9

def collapse_stack_weights(samples, weights)
  stack_weights = Hash.new(0)
  samples.each_with_index do |stack_idx, idx|
    stack_weights[stack_idx] += weights[idx]
  end
  stack_weights
end

#sanitize_string(string) ⇒ Object

Returns a string safe to embed in JSON output: valid UTF-8 with any invalid bytes replaced. Method names and paths can carry arbitrary encodings (e.g. BINARY or Shift_JIS source), which would otherwise raise JSON::GeneratorError.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vernier/output_helpers.rb', line 21

def sanitize_string(string)
  if string.ascii_only?
    string
  elsif string.encoding == Encoding::UTF_8
    string.valid_encoding? ? string : string.scrub
  else
    # We don't know the real encoding; interpret the bytes as UTF-8
    # and replace anything invalid.
    string.dup.force_encoding(Encoding::UTF_8).scrub
  end
end