Created
November 3, 2009 07:26
-
-
Save bpot/224866 to your computer and use it in GitHub Desktop.
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
# mixin to measure up/down rate for an EM::Connection | |
module RateMeasure | |
RATE_PERIOD = 5 | |
def post_init | |
setup_meters | |
super | |
end | |
def receive_data data | |
update_meter(data.length, @download_meter) | |
super | |
end | |
def send_data data | |
update_meter(data.length, @upload_meter) | |
super | |
end | |
def up_rate | |
@upload_meter[:rate] / 1024.0 | |
end | |
def down_rate | |
@download_meter[:rate] / 1024.0 | |
end | |
private | |
def setup_meters | |
now = Time.now - 0.1 | |
@upload_meter = { | |
:rate => 0.0, | |
:total => 0, | |
:ratesince => now, | |
:last => now | |
} | |
@download_meter = { | |
:rate => 0.0, | |
:total => 0, | |
:ratesince => now, | |
:last => now | |
} | |
end | |
def update_meter(amount, meter) | |
now = Time.now | |
meter[:total] += amount | |
meter[:rate] = (meter[:rate] * (meter[:last] - meter[:ratesince]) + amount)/(now - meter[:ratesince]) | |
meter[:last] = now | |
if meter[:ratesince] < now - RATE_PERIOD | |
meter[:ratesince] = now - RATE_PERIOD | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment