module ResourceMonitor

Constants

VERSION

Public Class Methods

benchmark(input=nil) click to toggle source
# 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