Class: JekyllImgFlow::ProcessingStats

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-imgflow/processing_stats.rb

Overview

ProcessingStats — collects cache hit/miss, per-operation timing, and compression ratio metrics during a Jekyll build. Accessed by the performance benchmark to report beyond wall-clock time.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessingStats

Returns a new instance of ProcessingStats.



10
11
12
13
14
15
16
# File 'lib/jekyll-imgflow/processing_stats.rb', line 10

def initialize
  @cache_hits = 0
  @cache_misses = 0
  @operation_timings = Hash.new { |h, k| h[k] = 0.0 }
  @compression_ratios = {}
  @mutex = Mutex.new
end

Instance Attribute Details

#cache_hitsObject (readonly)

Returns the value of attribute cache_hits.



8
9
10
# File 'lib/jekyll-imgflow/processing_stats.rb', line 8

def cache_hits
  @cache_hits
end

#cache_missesObject (readonly)

Returns the value of attribute cache_misses.



8
9
10
# File 'lib/jekyll-imgflow/processing_stats.rb', line 8

def cache_misses
  @cache_misses
end

#compression_ratiosObject (readonly)

Returns the value of attribute compression_ratios.



8
9
10
# File 'lib/jekyll-imgflow/processing_stats.rb', line 8

def compression_ratios
  @compression_ratios
end

#operation_timingsObject (readonly)

Returns the value of attribute operation_timings.



8
9
10
# File 'lib/jekyll-imgflow/processing_stats.rb', line 8

def operation_timings
  @operation_timings
end

Instance Method Details

#average_compression_ratiosObject

Average compression ratio per format



44
45
46
47
48
# File 'lib/jekyll-imgflow/processing_stats.rb', line 44

def average_compression_ratios
  @compression_ratios.transform_values do |data|
    (data[:ratios].sum / data[:count]).round(1) if data[:count].positive?
  end
end

#cache_hit_rateObject



54
55
56
57
58
# File 'lib/jekyll-imgflow/processing_stats.rb', line 54

def cache_hit_rate
  return 0.0 if total_processed.zero?

  (@cache_hits.to_f / total_processed * 100).round(1)
end

#record_cache_hitObject



18
19
20
# File 'lib/jekyll-imgflow/processing_stats.rb', line 18

def record_cache_hit
  @mutex.synchronize { @cache_hits += 1 }
end

#record_cache_missObject



22
23
24
# File 'lib/jekyll-imgflow/processing_stats.rb', line 22

def record_cache_miss
  @mutex.synchronize { @cache_misses += 1 }
end

#record_compression_ratio(format, original_size, output_size) ⇒ Object

Record compression ratio for a format (e.g. "webp" => 78.5 percent saved)



32
33
34
35
36
37
38
39
40
41
# File 'lib/jekyll-imgflow/processing_stats.rb', line 32

def record_compression_ratio(format, original_size, output_size)
  return if original_size.zero?

  ratio = ((original_size - output_size).to_f / original_size * 100).round(1)
  @mutex.synchronize do
    @compression_ratios[format] ||= { ratios: [], count: 0 }
    @compression_ratios[format][:ratios] << ratio
    @compression_ratios[format][:count] += 1
  end
end

#record_operation_time(type, seconds) ⇒ Object

Record wall-clock time for a specific operation type (:resize, :format, etc.)



27
28
29
# File 'lib/jekyll-imgflow/processing_stats.rb', line 27

def record_operation_time(type, seconds)
  @mutex.synchronize { @operation_timings[type] += seconds }
end

#resetObject



71
72
73
74
75
76
77
78
# File 'lib/jekyll-imgflow/processing_stats.rb', line 71

def reset
  @mutex.synchronize do
    @cache_hits = 0
    @cache_misses = 0
    @operation_timings.clear
    @compression_ratios.clear
  end
end

#to_hObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/jekyll-imgflow/processing_stats.rb', line 60

def to_h
  {
    cache_hits: @cache_hits,
    cache_misses: @cache_misses,
    cache_hit_rate: cache_hit_rate,
    total_processed: total_processed,
    operation_timings: @operation_timings.transform_values { |v| v.round(3) },
    compression_ratios: average_compression_ratios
  }
end

#total_processedObject



50
51
52
# File 'lib/jekyll-imgflow/processing_stats.rb', line 50

def total_processed
  @cache_hits + @cache_misses
end