Skip to content

Commit

Permalink
Added job_rolling_avg and longest_job
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpeabody committed Jul 14, 2011
1 parent 3d9cff4 commit 9e0acdc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/resque/plugins/job_stats/duration.rb
Expand Up @@ -24,16 +24,23 @@ def around_perform_job_stats_duration(*payload)
yield
duration = Time.now - start

Resque.redis.multi do
Resque.redis.lpush(jobs_duration_key, duration)
Resque.redis.ltrim(jobs_duration_key, 0, job_durations_to_track)
end
Resque.redis.lpush(jobs_duration_key, duration)
Resque.redis.ltrim(jobs_duration_key, 0, job_durations_to_track)
end

def job_durations_to_track
100
end

def job_rolling_avg
job_times = job_durations
job_times.inject(0.0) {|s,j| s + j} / job_times.size
end

def longest_job
job_durations.max
end

end
end
end
Expand Down
2 changes: 2 additions & 0 deletions test/test_job_stats.rb
Expand Up @@ -66,6 +66,8 @@ def test_duration
assert_in_delta 0.3, SimpleJob.job_durations[0], 0.01
assert_in_delta 0.2, SimpleJob.job_durations[1], 0.01
assert_in_delta 0.1, SimpleJob.job_durations[2], 0.01
assert_in_delta 0.3, SimpleJob.longest_job, 0.01
assert_in_delta 0.2, SimpleJob.job_rolling_avg, 0.01
end

end

0 comments on commit 9e0acdc

Please sign in to comment.