Module: RailsMemoryProfiler::ReportStore

Defined in:
lib/rails_memory_profiler/report_store.rb

Overview

Thread-safe circular buffer that holds per-request profiling reports. Capacity is controlled by Configuration#store_size. When full, the oldest report is evicted to make room for each new one.

Each report is a Hash with the keys:

  • :id — unique hex string assigned on push

  • :path, :method, :controller, :action

  • :allocated_objects, :retained_objects

  • :duration_ms

  • :recorded_atTime of capture

  • :detail — optional breakdown Hash (present when Configuration#detailed_reports is enabled)

Class Method Summary collapse

Class Method Details

.allArray<Hash>

Returns all stored reports in insertion order (oldest first).

Returns:

  • (Array<Hash>)


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rails_memory_profiler/report_store.rb', line 43

def all
  mutex.synchronize do
    stored = @stored || 0
    return [] if stored.zero?

    if stored < capacity
      buffer.first(stored)
    else
      buffer[@write_pos..] + buffer[0...@write_pos]
    end
  end
end

.clearvoid

This method returns an undefined value.

Removes all stored reports.



59
60
61
# File 'lib/rails_memory_profiler/report_store.rb', line 59

def clear
  mutex.synchronize { reset! }
end

.find(id) ⇒ Hash?

Returns the report with the given id, or nil if not found or evicted.

Parameters:

  • id (String)

Returns:

  • (Hash, nil)


36
37
38
# File 'lib/rails_memory_profiler/report_store.rb', line 36

def find(id)
  all.find { |r| r[:id] == id }
end

.push(report) ⇒ void

This method returns an undefined value.

Adds a report to the buffer, assigning a unique :id key. Evicts the oldest entry when the buffer is at capacity.

Parameters:

  • report (Hash)

    profiling data without an :id key



23
24
25
26
27
28
29
30
# File 'lib/rails_memory_profiler/report_store.rb', line 23

def push(report)
  mutex.synchronize do
    ensure_buffer_size
    buffer[@write_pos] = report.merge(id: SecureRandom.hex(6))
    @write_pos = (@write_pos + 1) % capacity
    @stored    = [@stored + 1, capacity].min
  end
end

.sizeInteger

Returns the number of reports currently stored.

Returns:

  • (Integer)


66
67
68
# File 'lib/rails_memory_profiler/report_store.rb', line 66

def size
  mutex.synchronize { @stored || 0 }
end