Class: TUI::PerformanceLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/tui/performance_logger.rb

Overview

Frame-level performance logger for TUI render profiling.

When enabled via ‘–debug`, logs timing data for each render phase to `log/tui_performance.log`. Each frame produces one log line with phase durations in milliseconds, enabling bottleneck identification.

Uses monotonic clock to avoid wall-clock jitter.

Examples:

logger = PerformanceLogger.new(enabled: true)
logger.start_frame
logger.measure(:build_lines) { build_message_lines(tui) }
logger.measure(:line_count) { widget.line_count(width) }
logger.end_frame

Constant Summary collapse

LOG_PATH =
"log/tui_performance.log"

Instance Method Summary collapse

Constructor Details

#initialize(enabled: false) ⇒ PerformanceLogger

Returns a new instance of PerformanceLogger.

Parameters:

  • enabled (Boolean) (defaults to: false)

    whether to actually log (no-op when false)



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tui/performance_logger.rb', line 24

def initialize(enabled: false)
  @enabled = enabled
  @phases = {}
  @frame_start = nil
  @frame_count = 0
  @logger = nil

  return unless @enabled

  @logger = Logger.new(LOG_PATH, 1, 5 * 1024 * 1024) # 5MB rotation
  @logger.formatter = proc { |_sev, time, _prog, msg| "#{time.strftime("%H:%M:%S.%L")} #{msg}\n" }
  @logger.info("TUI Performance Logger started — pid=#{Process.pid}")
end

Instance Method Details

#enabled?Boolean

Returns true when logging is active.

Returns:

  • (Boolean)

    true when logging is active



39
40
41
# File 'lib/tui/performance_logger.rb', line 39

def enabled?
  @enabled
end

#end_frameObject

Logs the completed frame with all phase timings.



67
68
69
70
71
72
73
74
75
# File 'lib/tui/performance_logger.rb', line 67

def end_frame
  return unless @enabled

  total = ((monotonic_now - @frame_start) * 1000).round(2)
  @frame_count += 1

  parts = @phases.map { |name, ms| "#{name}=#{ms}ms" }
  @logger.info("frame=#{@frame_count} total=#{total}ms #{parts.join(" ")}")
end

#info(message) ⇒ Object

Logs a one-off informational message (e.g. cache hit/miss).

Parameters:

  • message (String)


80
81
82
# File 'lib/tui/performance_logger.rb', line 80

def info(message)
  @logger&.info(message)
end

#measure(name) { ... } ⇒ Object

Measures a named phase within the current frame. Returns the block’s result so it can be used inline.

Parameters:

  • name (Symbol)

    phase name (e.g. :build_lines, :line_count)

Yields:

  • the code to measure

Returns:

  • (Object)

    the block’s return value



57
58
59
60
61
62
63
64
# File 'lib/tui/performance_logger.rb', line 57

def measure(name)
  return yield unless @enabled

  start = monotonic_now
  result = yield
  @phases[name] = ((monotonic_now - start) * 1000).round(2)
  result
end

#start_frameObject

Marks the beginning of a render frame.



44
45
46
47
48
49
# File 'lib/tui/performance_logger.rb', line 44

def start_frame
  return unless @enabled

  @frame_start = monotonic_now
  @phases = {}
end