Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
added requests per second to stats
Browse files Browse the repository at this point in the history
  • Loading branch information
arya committed Jul 20, 2009
1 parent afe6031 commit fc6c5e5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/pandemic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'pandemic/util'
require 'pandemic/connection_pool'
require 'pandemic/mutex_counter'
require 'pandemic/requests_per_second'

require 'pandemic/server_side/config'
require 'pandemic/server_side/client'
Expand Down
31 changes: 31 additions & 0 deletions lib/pandemic/requests_per_second.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Pandemic
class RequestsPerSecond
def initialize(sample_size = 10)
@hits = Array.new(sample_size + 2)
@last_hit_at = nil
end

def hit(now = Time.now.to_i)
key = now % @hits.size
if @hits[key].nil? || @hits[key][0] != now
@hits[key] = [now, 0]
end
@hits[key][1] += 1
@last_hit_at = now
end

def rps(now = Time.now.to_i)
return 0 if @last_hit_at.nil?
entries_to_go_back = @hits.size - (now - @last_hit_at) - 2
return 0 if entries_to_go_back <= 0
sum = 0
entries_to_go_back.times do |i|
now -= 1
if @hits[now % @hits.size] && @hits[now % @hits.size][0] == now
sum += @hits[now % @hits.size][1]
end
end
return sum.to_f / (@hits.size - 2)
end
end
end
8 changes: 8 additions & 0 deletions lib/pandemic/server_side/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def initialize(bind_to)
@clients_mutex = Mutex.new
@num_jobs_processed = MutexCounter.new
@num_jobs_entered = MutexCounter.new
@requests_per_second = RequestsPerSecond.new(10)

@peers = with_mutex({})
@servers = Config.servers
Expand Down Expand Up @@ -148,6 +149,8 @@ def handle_client_request(request)
end
end

@requests_per_second.hit

debug("Waiting for responses")
request.wait_for_responses

Expand Down Expand Up @@ -211,6 +214,8 @@ def print_stats(connection)
str << "Late Responses: #{stats[:late_responses]}"
str << "Total Jobs Processed: #{stats[:total_jobs_processed]}"
str << "Pending Jobs: #{stats[:jobs_pending]}"
str << "Requests per Second (10 sec): #{"%.1f" % stats[:rps_10]}"
str << "Requests per Second (lifetime): #{"%.1f" % stats[:rps_lifetime]}"
connection.puts(str.join("\n"))
end while (s = connection.gets) && (s.strip == "stats" || s.strip == "")
connection.close if connection && !connection.closed?
Expand Down Expand Up @@ -252,8 +257,11 @@ def collect_stats
pending + (client.received_requests - client.responded_requests.to_i)
end
end
results[:rps_10] = @requests_per_second.rps

results[:uptime] = Time.now - @running_since
results[:rps_lifetime] = results[:num_requests] / results[:uptime]

results
end

Expand Down

0 comments on commit fc6c5e5

Please sign in to comment.