Module: ResourceMonitor

Defined in:
lib/resource_monitor.rb,
lib/resource_monitor/version.rb

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.benchmark(controller = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/resource_monitor.rb', line 5

def self.benchmark(controller=nil)
  # Recover the process id and memory usage in KB (http://stackoverflow.com/questions/7220896/get-current-ruby-process-memory-usage)
  pid, size, cpu_per, mem_per = `ps ax -o pid,rss,%cpu,%mem | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i)

  if controller
    last_executed_controller = controller.controller_name  || 'NoController'
    last_executed_action = controller.action_name || 'NoAction'
  else
    last_executed_controller = 'NoController'
    last_executed_action = 'NoAction'
  end

  data = {
    process_id: pid,
    cpu_percentage: cpu_per,
    ram_percentage: mem_per,
    ram_usage: (size / 1024).to_i,
    last_executed_controller: last_executed_controller,
    last_executed_action: last_executed_action
  }

  ActionCable.server.broadcast 'resources', data.to_json if defined?(ActionCable)
  return data
end