Class: Benchin::Wrap
- Inherits:
-
Object
- Object
- Benchin::Wrap
- Defined in:
- lib/benchin/wrap.rb,
lib/benchin/wrap/report.rb,
lib/benchin/wrap/report/node.rb,
lib/benchin/wrap/report/node_printer.rb
Overview
Benchmark tool for high-level nested measurement. Check out Report#to_h for an example of the report structure.
It’s designed to use when:
-
you have some slow code (even one execution takes significant time)
-
you know which blocks of code you have to measure
-
you have to measure:
-
total time spend in each block
-
time spend in blocks executed inside a concrete block (child time)
-
time spend in a concrete block minus child block’s time (self time)
-
By using this information you can discover which block of slow code is the slowest one.
Measurement uses WALL time.
One of the most common possible examples of usage is to investigate slow requests in legacy and big codebases. In this case more classic instruments like stack profiling can be too focused on particular methods instead of logic chunks in your code.
It can be inconvenient to ‘drill’ Wrap instance to all the places where we have to wrap some code. To address this issue we have helper wrap which uses global Wrap instance.
Defined Under Namespace
Classes: Report
Instance Attribute Summary collapse
-
#report ⇒ Report
readonly
Collected measurement data.
Instance Method Summary collapse
-
#call(name) { ... } ⇒ Object
Wraps code block with WALL time measurement and returns the block result.
-
#initialize(name) ⇒ Wrap
constructor
A new instance of Wrap.
-
#reset ⇒ Wrap
Resets the #report to an empty state.
-
#to_h ⇒ Hash
Shortcut for ‘report.to_h`.
-
#to_s ⇒ String
Shortcut for ‘report.to_s`.
Constructor Details
Instance Attribute Details
#report ⇒ Report (readonly)
Returns collected measurement data.
73 74 75 |
# File 'lib/benchin/wrap.rb', line 73 def report @report end |
Instance Method Details
#call(name) { ... } ⇒ Object
Wraps code block with WALL time measurement and returns the block result.
Can be used in a nested way.
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/benchin/wrap.rb', line 119 def call(name) @current_path.push name starting = Process.clock_gettime(Process::CLOCK_MONOTONIC) result = yield ending = Process.clock_gettime(Process::CLOCK_MONOTONIC) elapsed = ending - starting report.add_time(@current_path, elapsed) @current_path.pop result end |
#reset ⇒ Wrap
Resets the #report to an empty state.
86 87 88 89 90 91 |
# File 'lib/benchin/wrap.rb', line 86 def reset @report = Report.new(@name) @current_path = [] self end |
#to_h ⇒ Hash
Shortcut for ‘report.to_h`.
144 145 146 |
# File 'lib/benchin/wrap.rb', line 144 def to_h report.to_s end |
#to_s ⇒ String
Shortcut for ‘report.to_s`.
136 137 138 |
# File 'lib/benchin/wrap.rb', line 136 def to_s report.to_s end |