Module: ResourceMonitor

Defined in:
lib/resource_monitor.rb,
lib/resource_monitor/engine.rb,
lib/resource_monitor/version.rb,
app/channels/resource_monitor/resource_channel.rb,
app/controllers/resource_monitor/resource_controller.rb,
app/controllers/resource_monitor/application_controller.rb

Defined Under Namespace

Classes: ApplicationController, Engine, ResourceChannel, ResourceController

Constant Summary collapse

VERSION =
'1.0'

Class Method Summary collapse

Class Method Details

.benchmark(input = 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
29
# File 'lib/resource_monitor.rb', line 5

def self.benchmark(input=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 input
    if input.kind_of?(ActionController::Base)
      details = "Controller: #{input.controller_name}\nAction: #{input.action_name}"
    elsif input.kind_of?(ActiveRecord::Base)
      details = "Model: #{input.model_name.human}"
    end
  else
    details = "No details available"
  end

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

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