Class: Bulletproof::Runtime::RequestMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/bulletproof/runtime/request_monitor.rb

Overview

リクエスト中の AR ロード件数・GC 統計を計測し、閾値超過時に RuntimeWarning を生成する

Defined Under Namespace

Classes: Snapshot

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ RequestMonitor

Returns a new instance of RequestMonitor.



14
15
16
# File 'lib/bulletproof/runtime/request_monitor.rb', line 14

def initialize(config)
  @config = config
end

Instance Method Details

#monitorObject

ブロックを実行し、[戻り値, Array] を返す



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bulletproof/runtime/request_monitor.rb', line 19

def monitor
  gc_before = MemorySampler.gc_snapshot
  result    = nil
  ar_loads  = ModelLoadCollector.collect { result = yield }
  gc_after  = MemorySampler.gc_snapshot

  snapshot = Snapshot.new(
    ar_loads: ar_loads,
    total_records: ar_loads.sum(&:record_count),
    gc_count_delta: gc_after[:count] - gc_before[:count],
    allocated_objects_delta: gc_after[:total_allocated_objects] - gc_before[:total_allocated_objects]
  )

  [result, build_warnings(snapshot)]
end