Module: Flows::Plugin::Profiler

Defined in:
lib/flows/plugin/profiler.rb,
lib/flows/plugin/profiler/report.rb,
lib/flows/plugin/profiler/wrapper.rb,
lib/flows/plugin/profiler/injector.rb,
lib/flows/plugin/profiler/report/raw.rb,
lib/flows/plugin/profiler/report/flat.rb,
lib/flows/plugin/profiler/report/tree.rb,
lib/flows/plugin/profiler/report/events.rb,
lib/flows/plugin/profiler/report/tree/node.rb,
lib/flows/plugin/profiler/report/flat/method_report.rb,
lib/flows/plugin/profiler/report/tree/calculated_node.rb

Overview

Note:

even without calling Profiler.profile using this module has some performance impact. Don't left this module used in production environments.

Allows to record execution count and time of particular method on class or singleton class.

Recorded data can be displayed in a different ways. See Report implementations for possible options.

To do a measurement you have call your classes inside Profiler.profile block.

Examples:

class MyClass
  CallProfiler = Flows::Plugin::Profiler.for_method(:call)
  include CallProfiler

  def call(a, b)
    # some work here
  end
end

class AnotherClass
  CallProfiler = Flows::Plugin::Profiler.for_method(:perform)
  extend CallProfiler

  def self.perform(x)
    MyClass.new.call(x, x)
  end
end

last_result = Flows::Plugin::Profiler.profile do
  AnotherClass.perform(2)
  AnotherClass.perform(6)
end

Profiler.last_report.to_a
# => [
#   [:started,  AnotherClass, :singleton, :perform, nil],
#   [:started,  MyClass,      :instance,  :call,    nil],
#   [:finished, MyClass,      :instance,  :call,    7.3],
#   [:finished, AnotherClass, :singleton, :perform, 10.5],
#   [:started,  AnotherClass, :singleton, :perform, nil],
#   [:started,  MyClass,      :instance,  :call,    nil],
#   [:finished, MyClass,      :instance,  :call,    8.8],
#   [:finished, AnotherClass, :singleton, :perform, 14.2]
# ]

Since:

  • 0.4.0

Defined Under Namespace

Modules: Injector, Wrapper Classes: Report

Constant Summary collapse

THREAD_VAR_FLAG =

Since:

  • 0.4.0

:flows_profiler_flag
THREAD_VAR_REPORT =

Since:

  • 0.4.0

:flows_profiler_report

Class Method Summary collapse

Class Method Details

.for_method(method_name) ⇒ Module

Generates profiler module for a particular method.

Use include for instance methods and extend for singleton ones.

Parameters:

  • method_name (Symbol)

    method to wrap with profiling.

Returns:

  • (Module)

    module to include or extend.

Since:

  • 0.4.0



63
64
65
66
67
68
69
# File 'lib/flows/plugin/profiler.rb', line 63

def for_method(method_name)
  Module.new.tap do |mod|
    injector_mod = Injector.make_module(method_name)
    mod.const_set(:Injector, injector_mod)
    mod.extend injector_mod
  end
end

.last_reportReport?

Returns last generated report if some.

Returns:

  • (Report, nil)

    last generated report if some.

Since:

  • 0.4.0



97
98
99
# File 'lib/flows/plugin/profiler.rb', line 97

def last_report
  Thread.current[THREAD_VAR_REPORT]
end

.profile(report = :raw) { ... } ⇒ Object

Profiles a block execution.

Parameters:

  • report (Report, Symbol) (defaults to: :raw)

    desired Report to be used. In case of symbol :some_name the Flows::Plugin::Profiler::Report::SomeName.new will be used.

Yields:

  • code to profile

Returns:

  • block result

Since:

  • 0.4.0



78
79
80
81
82
83
84
85
86
87
# File 'lib/flows/plugin/profiler.rb', line 78

def profile(report = :raw)
  thread = Thread.current

  thread[THREAD_VAR_FLAG] = true
  thread[THREAD_VAR_REPORT] = make_report(report)

  yield
ensure
  thread[THREAD_VAR_FLAG] = false
end

.resetObject

Resets thread-local variables used for reporting.

Since:

  • 0.4.0



90
91
92
93
94
# File 'lib/flows/plugin/profiler.rb', line 90

def reset
  thread = Thread.current
  thread[THREAD_VAR_FLAG] = false
  thread[THREAD_VAR_REPORT] = nil
end