Module: NA::Benchmark
- Defined in:
- lib/na/benchmark.rb,
lib/na.rb
Overview
Provides benchmarking utilities for measuring code execution time.
Class Attribute Summary collapse
-
.enabled ⇒ Object
Returns the value of attribute enabled.
-
.timings ⇒ Object
Returns the value of attribute timings.
Class Method Summary collapse
-
.init ⇒ void
Initialize benchmarking state.
-
.measure(label) ⇒ Object
Measure the execution time of a block.
-
.report ⇒ void
Output a performance report to STDERR.
Class Attribute Details
.enabled ⇒ Object
Returns the value of attribute enabled.
10 11 12 |
# File 'lib/na/benchmark.rb', line 10 def enabled @enabled end |
.timings ⇒ Object
Returns the value of attribute timings.
10 11 12 |
# File 'lib/na/benchmark.rb', line 10 def timings @timings end |
Class Method Details
.init ⇒ void
This method returns an undefined value.
Initialize benchmarking state
15 16 17 18 19 |
# File 'lib/na/benchmark.rb', line 15 def init @enabled = %w[1 true].include?(ENV.fetch('NA_BENCHMARK', nil)) @timings = [] @start_time = Time.now end |
.measure(label) ⇒ Object
Measure the execution time of a block
27 28 29 |
# File 'lib/na/benchmark.rb', line 27 def self.measure(_label) yield end |
.report ⇒ void
This method returns an undefined value.
Output a performance report to STDERR
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/na/benchmark.rb', line 42 def report return unless @enabled total = @timings.sum { |t| t[:duration] } warn "\n#{NA::Color.template('{y}=== NA Performance Report ===')}" warn NA::Color.template("{dw}Total: {bw}#{total.round(2)}ms{x}") warn NA::Color.template("{dw}GC Count: {bw}#{GC.count}{x}") if defined?(GC) if defined?(GC) warn NA::Color.template("{dw}Memory: {bw}#{(GC.stat[:heap_live_slots] * 40 / 1024.0).round(1)}KB{x}") end warn '' @timings.each do |timing| pct = total.positive? ? ((timing[:duration] / total) * 100).round(1) : 0 = '█' * [(pct / 2).round, 50].min warn NA::Color.template( "{dw}[{y}#{bar.ljust(25)}{dw}] {bw}#{timing[:duration].to_s.rjust(7)}ms {dw}(#{pct.to_s.rjust(5)}%) {x}#{timing[:label]}" ) end warn NA::Color.template("{y}#{'=' * 50}{x}\n") end |