Class: TUI::PerformanceLogger
- Inherits:
-
Object
- Object
- TUI::PerformanceLogger
- 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.
Constant Summary collapse
- LOG_PATH =
"log/tui_performance.log"
Instance Method Summary collapse
-
#enabled? ⇒ Boolean
True when logging is active.
-
#end_frame ⇒ Object
Logs the completed frame with all phase timings.
-
#info(message) ⇒ Object
Logs a one-off informational message (e.g. cache hit/miss).
-
#initialize(enabled: false) ⇒ PerformanceLogger
constructor
A new instance of PerformanceLogger.
-
#measure(name) { ... } ⇒ Object
Measures a named phase within the current frame.
-
#start_frame ⇒ Object
Marks the beginning of a render frame.
Constructor Details
#initialize(enabled: false) ⇒ PerformanceLogger
Returns a new instance of PerformanceLogger.
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.
39 40 41 |
# File 'lib/tui/performance_logger.rb', line 39 def enabled? @enabled end |
#end_frame ⇒ Object
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).
80 81 82 |
# File 'lib/tui/performance_logger.rb', line 80 def info() @logger&.info() end |
#measure(name) { ... } ⇒ Object
Measures a named phase within the current frame. Returns the block’s result so it can be used inline.
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_frame ⇒ Object
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 |