Skip to content

Instantly share code, notes, and snippets.

@metade
Created November 24, 2010 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metade/714608 to your computer and use it in GitHub Desktop.
Save metade/714608 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'pp'
DURATION = 60
INTERVAL = 3
SPEEDS = {
:off => 0,
:slow => 0.3334,
:medium => 0.75,
:fast => 1
}
$count = 0
$current_speed = :off
while true
now = Time.now
messages = (0..rand(100)).map { |i| now + rand(DURATION) }.sort
histogram = Array.new(DURATION/INTERVAL, 0)
messages.each do |time|
delta = time - now
segment = (delta/INTERVAL).floor
histogram[segment] += 1
end
p histogram
while histogram.any?
msgs = histogram.shift
$count += msgs
distance = (SPEEDS[$current_speed] * INTERVAL).floor
$count -= distance
$count = 0 if $count < 0 # huh?
$current_speed = case $count
when 0 then :off
when 1..2 then :slow
when 3..5 then :medium
when 5..100 then :fast
else :apeshit
end
puts "#{$count}:\t+#{msgs}\t-#{distance}\t#{$current_speed}"
sleep INTERVAL
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment