class SpeedGun::Source

Constants

KEEP_RANGE

Attributes

file[R]
id[R]
lines[R]

Public Class Methods

from_hash(hash, id = nil) click to toggle source
# File lib/speed_gun/source.rb, line 42
def self.from_hash(hash, id = nil)
  source = new(hash['file'], nil)

  lines = (hash['lines'] || []).map do |line_id, hash|
    Line.from_hash(hash, line_id)
  end

  source.instance_variable_set(:@lines, lines)
  source.instance_variable_set(:@id, id) if id

  source
end
new(file, linesamples) click to toggle source
# File lib/speed_gun/source.rb, line 55
def initialize(file, linesamples)
  @id = SecureRandom.uuid
  @file = file
  @lines = []
  analyze(file, linesamples) if linesamples
end

Public Instance Methods

analyze(file, linesamples) click to toggle source
# File lib/speed_gun/source.rb, line 62
def analyze(file, linesamples)
  code_lines = File.exist?(file) ? File.readlines(file) : []

  keep_lines = []
  code_lines.each_with_index do |line, idx|
    sample = linesamples[idx + 1] || [0, 0, 0, 0]
    wall, cpu, calls, allocations = *sample

    keep_lines.push(idx) if calls > 0
    @lines.push(Line.new(idx + 1, line, wall, cpu, calls, allocations))
  end

  @lines.select! do |line|
    idx = line.line - 1
    ((KEEP_RANGE * -1)..KEEP_RANGE).any? { |n| keep_lines.include?(idx + n) }
  end
end
to_hash() click to toggle source
# File lib/speed_gun/source.rb, line 84
def to_hash
  {
    file: file,
    lines: lines.map { |line| [ line.id, line.to_hash ] }
  }
end
to_s() click to toggle source
# File lib/speed_gun/source.rb, line 80
def to_s
  "#{file}====\n#{lines.map(&:to_s).join('')}"
end