Class: Benchin::Wrap::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/benchin/wrap/report.rb,
lib/benchin/wrap/report/node.rb,
lib/benchin/wrap/report/node_printer.rb

Overview

Represents tree-like time measurement data produced by Benchin::Wrap.

See #to_h method for more concrete example.

Defined Under Namespace

Classes: Node, NodePrinter

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Report


11
12
13
# File 'lib/benchin/wrap/report.rb', line 11

def initialize(name)
  @root = Node::Virtual.new(name)
end

Instance Method Details

#add_time(path, seconds) ⇒ Report

Adds seconds to a path in a tree and increases calls count by 1.


21
22
23
24
25
26
27
28
29
# File 'lib/benchin/wrap/report.rb', line 21

def add_time(path, seconds)
  target_node = path.reduce(@root) do |node, name|
    node.nested[name] ||= Node.new(name)
  end

  target_node.add_call(seconds)

  self
end

#to_hHash

Transforms report to a basic ruby types: arrays and hashes.

Examples:

report = Report.new('Report name')
report
  .add_time(%w[TOP NESTED], 10.0)
  .add_time(%w[TOP], 10.0)
  .add_time(%w[TOP], 5.0)
  .add_time(%w[NESTED], 7.0)
  .to_h
# will produce following structure
{
  name: 'Report name',
  total_seconds: 22.0,
  nested: [
    {
      name: 'TOP',
      total_seconds: 15.0,
      self_seconds: 5.0,
      child_seconds: 10.0,
      calls: 2,
      nested: [
        {
          name: 'NESTED',
          total_seconds: 10.0,
          self_seconds: 10.0,
          child_seconds: 0.0,
          calls: 1,
          nested: []
        }
      ]
    },
    {
      name: 'NESTED',
      total_seconds: 7.0,
      self_seconds: 7.0,
      child_seconds: 0.0,
      calls: 1,
      nested: []
    }
  ]
}

75
76
77
# File 'lib/benchin/wrap/report.rb', line 75

def to_h
  @root.to_h
end

#to_sString

Renders human readable report.


82
83
84
# File 'lib/benchin/wrap/report.rb', line 82

def to_s
  NodePrinter.new(@root).to_s
end