Class: Omnizip::Profiler::MemoryProfiler
- Inherits:
-
Object
- Object
- Omnizip::Profiler::MemoryProfiler
- Defined in:
- lib/omnizip/profiler/memory_profiler.rb
Overview
Profiles memory allocation and retention
Instance Method Summary collapse
- #call_count(operation_name) ⇒ Object
-
#initialize ⇒ MemoryProfiler
constructor
A new instance of MemoryProfiler.
- #profile(operation_name) ⇒ Object
- #reset! ⇒ Object
- #total_calls ⇒ Object
Constructor Details
#initialize ⇒ MemoryProfiler
Returns a new instance of MemoryProfiler.
7 8 9 |
# File 'lib/omnizip/profiler/memory_profiler.rb', line 7 def initialize @call_counts = Hash.new(0) end |
Instance Method Details
#call_count(operation_name) ⇒ Object
57 58 59 |
# File 'lib/omnizip/profiler/memory_profiler.rb', line 57 def call_count(operation_name) @call_counts[operation_name] end |
#profile(operation_name) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/omnizip/profiler/memory_profiler.rb', line 11 def profile(operation_name) GC.start GC.disable gc_stat_before = GC.stat objspace_before = ObjectSpace.count_objects yield objspace_after = ObjectSpace.count_objects gc_stat_after = GC.stat GC.enable @call_counts[operation_name] += 1 # Calculate memory metrics total_allocated = gc_stat_after[:total_allocated_objects] - gc_stat_before[:total_allocated_objects] total_freed = gc_stat_after[:total_freed_objects] - gc_stat_before[:total_freed_objects] # Object allocation delta objspace_after[:TOTAL] objspace_before[:TOTAL] # Estimate memory based on object allocations # Average Ruby object is ~40 bytes estimated_memory = total_allocated * 40 Models::PerformanceResult.new( operation_name: operation_name, memory_allocated: estimated_memory, memory_retained: (total_allocated - total_freed) * 40, object_allocations: total_allocated, gc_runs: 0, call_count: @call_counts[operation_name], ) ensure GC.enable end |
#reset! ⇒ Object
53 54 55 |
# File 'lib/omnizip/profiler/memory_profiler.rb', line 53 def reset! @call_counts.clear end |
#total_calls ⇒ Object
61 62 63 |
# File 'lib/omnizip/profiler/memory_profiler.rb', line 61 def total_calls @call_counts.values.sum end |