Sha256: c54db7b07975b1692e03a540ba0d66b58856adcd7498bc9de2751deb06b6f42b

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

module RorVsWild
  class Section
    attr_reader :started_at
    attr_accessor :kind, :file, :line, :calls, :command, :children_runtime, :total_runtime, :appendable_command

    def self.start(&block)
      section = Section.new
      block.call(section) if block_given?
      stack.push(section)
      section
    end

    def self.stop(&block)
      section = stack.pop
      block.call(section) if block_given?
      section.total_runtime = RorVsWild.clock_milliseconds - section.started_at
      current.children_runtime += section.total_runtime if current
      RorVsWild.agent.add_section(section)
    end

    def self.stack
      RorVsWild.agent.data[:section_stack] ||= []
    end

    def self.current
      stack.last
    end

    def initialize
      @calls = 1
      @total_runtime = 0
      @children_runtime = 0
      @started_at = RorVsWild.clock_milliseconds
      location = RorVsWild.agent.find_most_relevant_location(caller_locations)
      @file = RorVsWild.agent.relative_path(location.path)
      @line = location.lineno
      @appendable_command = false
    end

    def sibling?(section)
      kind == section.kind && line == section.line && file == section.file
    end

    def merge(section)
      self.calls += section.calls
      self.total_runtime += section.total_runtime
      self.children_runtime += section.children_runtime
      if section
        if appendable_command
          self.command = self.command.dup if self.command.frozen?
          self.command << "\n" + section.command
        end
      else
        self.command = section.command
      end
      self.appendable_command = appendable_command && section.appendable_command
    end

    def self_runtime
      total_runtime - children_runtime
    end

    COMMAND_MAX_SIZE = 1000

    def command=(value)
      @command = value && value.size > COMMAND_MAX_SIZE ? value[0, COMMAND_MAX_SIZE] + " [TRUNCATED]" : value
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rorvswild-1.4.4 lib/rorvswild/section.rb
rorvswild-1.4.3 lib/rorvswild/section.rb
rorvswild-1.4.2 lib/rorvswild/section.rb
rorvswild-1.4.1 lib/rorvswild/section.rb
rorvswild-1.4.0 lib/rorvswild/section.rb
rorvswild-1.3.5 lib/rorvswild/section.rb