Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added connection stats that are mostly used for determining coverage …
…rates when being rate limited on a particular connection.
- Loading branch information
Hayes Davis
committed
Jan 31, 2011
1 parent
a880109
commit 206c985
Showing
2 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module Flamingo | ||
module Stats | ||
class Connection | ||
|
||
START_TIME = "conn:start:time" | ||
START_EVENT_COUNT = "conn:start:event_count" | ||
START_TWEET_COUNT = "conn:start:tweet_count" | ||
LIMIT_COUNT = "conn:limit:count" | ||
LIMIT_TIME = "conn:limit:time" | ||
COVERAGE = "conn:coverage" | ||
|
||
def connected! | ||
meta.set(START_TIME,Time.now.to_i) | ||
meta.set(START_EVENT_COUNT,event_stats.all_count) | ||
meta.set(START_TWEET_COUNT,event_stats.tweet_count) | ||
meta.set(COVERAGE,100) | ||
meta.delete(LIMIT_COUNT) | ||
meta.delete(LIMIT_TIME) | ||
end | ||
|
||
def limited!(count) | ||
meta.set(LIMIT_COUNT,count) | ||
meta.set(LIMIT_TIME,Time.now.to_i) | ||
meta.set(COVERAGE,coverage_rate) | ||
end | ||
|
||
def received_tweets | ||
event_stats.tweet_count - (meta.get(START_TWEET_COUNT) || 0) | ||
end | ||
|
||
def skipped_tweets | ||
meta.get(LIMIT_COUNT) || 0 | ||
end | ||
|
||
def received_events | ||
event_stats.all_count - (meta.get(START_EVENT_COUNT) || 0) | ||
end | ||
|
||
def coverage_rate | ||
received = received_tweets | ||
possible_tweets = received + skipped_tweets | ||
if possible_tweets == 0 | ||
0 | ||
else | ||
(received / possible_tweets.to_f)*100 | ||
end | ||
end | ||
|
||
def meta | ||
Flamingo.meta | ||
end | ||
|
||
def event_stats | ||
Flamingo.event_stats | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require File.join(File.dirname(__FILE__),"..","test_helper") | ||
|
||
class ConnectionTest < Test::Unit::TestCase | ||
|
||
include FlamingoTestCase | ||
|
||
def setup | ||
setup_flamingo | ||
@stats = Flamingo::Stats::Connection.new | ||
end | ||
|
||
def test_connected_resets_all_meta_keys | ||
Flamingo.event_stats.expects(:tweet_count=>500,:all_count=>502) | ||
Flamingo.meta.expects(:set).with("conn:start:time",kind_of(Integer)) | ||
Flamingo.meta.expects(:set).with("conn:start:tweet_count",500) | ||
Flamingo.meta.expects(:set).with("conn:start:event_count",502) | ||
Flamingo.meta.expects(:set).with("conn:coverage",100) | ||
Flamingo.meta.expects(:delete).with("conn:limit:count") | ||
Flamingo.meta.expects(:delete).with("conn:limit:time") | ||
@stats.connected! | ||
end | ||
|
||
def test_limited_sets_meta_keys | ||
Flamingo.meta.expects(:set).with("conn:limit:count",100) | ||
Flamingo.meta.expects(:set).with("conn:limit:time",kind_of(Integer)) | ||
Flamingo.meta.expects(:set).with("conn:coverage",0) | ||
@stats.limited!(100) | ||
end | ||
|
||
def test_limited_sets_coverage_rate | ||
# Assumes we started with 500 tweets | ||
Flamingo.meta.set("conn:start:tweet_count",500) | ||
# Now have 900 tweets | ||
Flamingo.event_stats.expects(:tweet_count).returns(900) | ||
@stats.limited!(100) | ||
assert_equal(80,Flamingo.meta.get("conn:coverage").to_f) | ||
end | ||
|
||
def test_received_tweets_calculates_difference_in_start_count_vs_current_count | ||
Flamingo.event_stats.expects(:tweet_count=>500,:all_count=>502) | ||
@stats.connected! | ||
Flamingo.event_stats.expects(:tweet_count=>1000) | ||
assert_equal(500,@stats.received_tweets) | ||
end | ||
|
||
def test_received_events_calculates_difference_in_start_count_vs_current_count | ||
Flamingo.event_stats.expects(:tweet_count=>500,:all_count=>502) | ||
@stats.connected! | ||
Flamingo.event_stats.expects(:all_count=>602) | ||
assert_equal(100,@stats.received_events) | ||
end | ||
|
||
def test_skipped_tweets_returns_0_if_never_limited | ||
assert_equal(0,@stats.skipped_tweets) | ||
end | ||
|
||
def test_skipped_tweets_reads_from_meta_limit_count_key | ||
Flamingo.meta.expects(:get).with("conn:limit:count").returns(100) | ||
assert_equal(100,@stats.skipped_tweets) | ||
end | ||
|
||
def test_coverage_rate_calculates_tweets_received_over_possible | ||
# Assumes we started with 500 tweets | ||
Flamingo.meta.expects(:get).with("conn:start:tweet_count").returns(500) | ||
# Were limited by 150 tweets | ||
Flamingo.meta.expects(:get).with("conn:limit:count").returns(150) | ||
# Now have 550 tweets | ||
Flamingo.event_stats.expects(:tweet_count).returns(550) | ||
# 50 received tweets (550-500), 200 possible tweets (150 limit + 50 received) | ||
assert_equal(25,@stats.coverage_rate) | ||
end | ||
|
||
def teardown | ||
teardown_flamingo | ||
end | ||
|
||
end |