Class: SidekiqJobStats::MemoryUsage

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_job_stats/memory_usage.rb

Constant Summary collapse

MEMORY_PERIOD_EXPIRE =
{:day => 60 * 60 * 24, :month => 60 * 60 * 24 * 30}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_class) ⇒ MemoryUsage

Returns a new instance of MemoryUsage.



7
8
9
# File 'lib/sidekiq_job_stats/memory_usage.rb', line 7

def initialize(job_class)
  @job_class = job_class
end

Instance Attribute Details

#job_classObject

Returns the value of attribute job_class.



5
6
7
# File 'lib/sidekiq_job_stats/memory_usage.rb', line 5

def job_class
  @job_class
end

Instance Method Details

#job_memory_usage_avg_dayObject



33
34
35
36
# File 'lib/sidekiq_job_stats/memory_usage.rb', line 33

def job_memory_usage_avg_day
  _total, _count, _peak, avg = job_memory_usage_per(:day)
  avg
end

#job_memory_usage_avg_monthObject



38
39
40
41
# File 'lib/sidekiq_job_stats/memory_usage.rb', line 38

def job_memory_usage_avg_month
  _total, _count, _peak, avg = job_memory_usage_per(:month)
  avg
end

#job_memory_usage_increx_per(memory_usage, period) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sidekiq_job_stats/memory_usage.rb', line 18

def job_memory_usage_increx_per(memory_usage, period)
  total, count, peak, _avg = job_memory_usage_per(period)
  Sidekiq.redis do |conn|
    conn.set(
      job_memory_usage_period_key(period),
      [
        total.to_f + memory_usage,
        count.to_i + 1,
        [peak.to_f, memory_usage].max,
        ((total.to_f + memory_usage)/(count.to_i + 1)).round(2)
      ].to_json)
    conn.expire(job_memory_usage_period_key(period), MEMORY_PERIOD_EXPIRE[period])
  end
end

#job_memory_usage_per(period) ⇒ Object



53
54
55
56
57
# File 'lib/sidekiq_job_stats/memory_usage.rb', line 53

def job_memory_usage_per(period)
  Sidekiq.redis do |conn|
    JSON.parse(conn.get(job_memory_usage_period_key(period)) || "null")
  end
end

#job_memory_usage_period_key(period) ⇒ Object



59
60
61
# File 'lib/sidekiq_job_stats/memory_usage.rb', line 59

def job_memory_usage_period_key(period)
  "#{jobs_memory_usage_key}:#{period}:#{Time.now.send(period)}"
end

#jobs_memory_usage_keyObject



63
64
65
# File 'lib/sidekiq_job_stats/memory_usage.rb', line 63

def jobs_memory_usage_key
  "stats:jobs:#{job_class}:memory_usage"
end

#peak_memory_usage_dayObject



43
44
45
46
# File 'lib/sidekiq_job_stats/memory_usage.rb', line 43

def peak_memory_usage_day
  _total, _count, peak, _avg = job_memory_usage_per(:day)
  peak
end

#peak_memory_usage_monthObject



48
49
50
51
# File 'lib/sidekiq_job_stats/memory_usage.rb', line 48

def peak_memory_usage_month
  _total, _count, peak, _avg = job_memory_usage_per(:month)
  peak
end

#trackObject



11
12
13
14
15
16
# File 'lib/sidekiq_job_stats/memory_usage.rb', line 11

def track
  memory_usage = GetProcessMem.new.mb.round(2)

  job_memory_usage_increx_per(memory_usage, :day)
  job_memory_usage_increx_per(memory_usage, :month)
end