Class: RosettAi::Profiler

Inherits:
Object
  • Object
show all
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.

Examples:

profiler = RosettAi::Profiler.new('compile')
profiler.measure('load') { load_files }
profiler.measure('validate') { validate_files }
profiler.report

Author:

  • hugo

  • claude

Defined Under Namespace

Classes: NullProfiler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_name) ⇒ Profiler

Returns a new instance of Profiler.

Parameters:

  • command_name (String)


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_nameObject (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

#phasesObject (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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Profiler)

    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.

Parameters:

  • phase_name (String)

Returns:

  • (Object)

    block return value



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