Created
November 15, 2013 20:33
-
-
Save justindossey/7491093 to your computer and use it in GitHub Desktop.
Non-leaking version of bunny logger memory leak test
This file contains hidden or 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
require 'bunny' | |
require 'logger' | |
class TestLoggerSubscribeMemleak | |
def initialize(msg_count=10_000) | |
@messages_to_write = msg_count | |
@message_count = msg_count | |
@connection = Bunny.new(:user=>"jbd_dev_reader", :pass=>"123456", :host=>"localhost", :vhost=>"jbd", :port=>5672) | |
@connection.start | |
@channel = @connection.create_channel | |
@exchange = @channel.exchange('test_memleak', :type => :direct, :durable => false) | |
@queue = @channel.queue('test_memleak', :durable => false, :auto_delete => false) | |
@queue.bind(@exchange) | |
@log = File.open("/tmp/test_memleak.log","a") | |
@pong = 'PONG' | |
end | |
def run | |
print_memory_use "Before start: " | |
write_many_messages | |
sleep 1 | |
print_memory_use "Post-write 1: " | |
pop_all | |
print_memory_use "Post-pop_all: " | |
sleep 1 | |
write_many_messages | |
print_memory_use "Post-write 2: " | |
sleep 1 | |
do_subscribe | |
sleep 1 | |
print_memory_use "Post-subscribe: " | |
end | |
def write_many_messages | |
@queue.purge | |
@messages_to_write.times {|t| @exchange.publish("HI#{t}") } | |
end | |
def print_memory_use(prepend='') | |
memory_usage = `ps -o rss= -p #{$$}`.to_i | |
@message_count = @queue.status[:message_count] | |
$stderr.puts("#{prepend} RSS: #{memory_usage}; Pending Messages: #{@message_count}") | |
end | |
def pop_all | |
nils=0 | |
loop do | |
delivery_info, properties, payload = @queue.pop | |
if payload.nil? | |
nils += 1 | |
else | |
@log.puts(@pong) | |
end | |
if nils > 10 | |
break | |
end | |
end | |
end | |
def do_subscribe | |
messages_remaining = @message_count | |
consumer = @queue.subscribe(:block => false) do |delivery_info, properties, payload| | |
@log.puts(@pong) | |
messages_remaining -= 1 | |
end | |
while messages_remaining > 0 | |
sleep 0.5 | |
end | |
consumer.cancel | |
end | |
end | |
if $0 == __FILE__ | |
tester = TestLoggerSubscribeMemleak.new | |
tester.run | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment