Class: RosettAi::Profiler
- Inherits:
-
Object
- Object
- RosettAi::Profiler
- Defined in:
- lib/rosett_ai/profiler.rb
Overview
Lightweight profiler for CLI command performance measurement.
Enabled via RAI_PROFILE=1 environment variable.
Output goes to stderr to preserve piped command compatibility.
Uses monotonic clock for accurate elapsed time measurement.
Defined Under Namespace
Classes: NullProfiler
Instance Attribute Summary collapse
-
#command_name ⇒ Object
readonly
Returns the value of attribute command_name.
-
#phases ⇒ Object
readonly
Returns the value of attribute phases.
Class Method Summary collapse
-
.enabled? ⇒ Boolean
True if profiling is enabled.
-
.for(command_name) ⇒ Profiler
Real profiler if enabled, no-op otherwise.
Instance Method Summary collapse
-
#initialize(command_name) ⇒ Profiler
constructor
A new instance of Profiler.
-
#measure(phase_name) ⇒ Object
Measure a named phase.
-
#report
Print profiling summary to stderr.
Constructor Details
#initialize(command_name) ⇒ Profiler
Returns a new instance of Profiler.
35 36 37 38 39 |
# File 'lib/rosett_ai/profiler.rb', line 35 def initialize(command_name) @command_name = command_name @phases = [] @start_time = monotonic_now end |
Instance Attribute Details
#command_name ⇒ Object (readonly)
Returns the value of attribute command_name.
32 33 34 |
# File 'lib/rosett_ai/profiler.rb', line 32 def command_name @command_name end |
#phases ⇒ Object (readonly)
Returns the value of attribute phases.
32 33 34 |
# File 'lib/rosett_ai/profiler.rb', line 32 def phases @phases end |
Class Method Details
.enabled? ⇒ Boolean
Returns true if profiling is enabled.
23 24 25 |
# File 'lib/rosett_ai/profiler.rb', line 23 def self.enabled? ENV['RAI_PROFILE'] == '1' end |
.for(command_name) ⇒ Profiler
Returns real profiler if enabled, no-op otherwise.
28 29 30 |
# File 'lib/rosett_ai/profiler.rb', line 28 def self.for(command_name) enabled? ? new(command_name) : NullProfiler.new end |
Instance Method Details
#measure(phase_name) ⇒ Object
Measure a named phase.
45 46 47 48 49 50 51 |
# File 'lib/rosett_ai/profiler.rb', line 45 def measure(phase_name) start = monotonic_now result = yield elapsed = monotonic_now - start @phases << { name: phase_name, elapsed_ms: (elapsed * 1000).round(1) } result end |
#report
This method returns an undefined value.
Print profiling summary to stderr.
56 57 58 59 60 61 |
# File 'lib/rosett_ai/profiler.rb', line 56 def report total = (monotonic_now - @start_time) * 1000 parts = @phases.map { |phase| "#{phase[:name]}: #{phase[:elapsed_ms]}ms" } parts << "total: #{total.round(1)}ms" warn "[profile] #{parts.join(' | ')}" end |