From 2dcfb3a0d00aecf46d2fcb77bda57a86397054eb Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 9 Apr 2014 14:57:25 -0300 Subject: [PATCH] Log periodically memory consumption --- config/initializers/log_memory_consumption.rb | 5 +++++ lib/log_memory_consumption_job.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 0 deletions(-) create mode 100644 config/initializers/log_memory_consumption.rb create mode 100644 lib/log_memory_consumption_job.rb diff --git a/config/initializers/log_memory_consumption.rb b/config/initializers/log_memory_consumption.rb new file mode 100644 index 0000000..cc3acd1 --- /dev/null +++ b/config/initializers/log_memory_consumption.rb @@ -0,0 +1,5 @@ +if Delayed::Backend::ActiveRecord::Job.table_exists? + jobs = Delayed::Backend::ActiveRecord::Job.all :conditions => ['handler LIKE ?', "%LogMemoryConsumptionJob%"] + jobs.map(&:destroy) if jobs.present? + Delayed::Backend::ActiveRecord::Job.enqueue(LogMemoryConsumptionJob.new) +end diff --git a/lib/log_memory_consumption_job.rb b/lib/log_memory_consumption_job.rb new file mode 100644 index 0000000..915275f --- /dev/null +++ b/lib/log_memory_consumption_job.rb @@ -0,0 +1,26 @@ +class LogMemoryConsumptionJob < Struct.new(:last_stat) + # Number of entries do display + N = 20 + # In seconds + PERIOD = 10 + + def perform + logpath = File.join(Rails.root, 'log', "#{ENV['RAILS_ENV']}_memory_consumption.log") + logger = Logger.new(logpath) + stats = Hash.new(0) + ObjectSpace.each_object {|o| stats[o.class.to_s] += 1} + i = 1 + + logger << "[#{Time.now.strftime('%F %T %z')}]\n" + stats.sort {|(k1,v1),(k2,v2)| v2 <=> v1}.each do |k,v| + logger << (sprintf "%-60s %10d", k, v) + logger << (sprintf " | delta %10d", (v - last_stat[k])) if last_stat && last_stat[k] + logger << "\n" + break if i > N + i += 1 + end + logger << "\n" + + Delayed::Job.enqueue(LogMemoryConsumptionJob.new(stats), 0, PERIOD.seconds.from_now) + end +end -- libgit2 0.21.2