From d4faac5480f6416cf92301745a9a9572bc865061 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 11 Mar 2011 20:56:49 -0800 Subject: linux: method for dumping all TCP listener stats This is a work-in-progress and will probably be modified before the next release. --- test/test_linux_all_tcp_listen_stats.rb | 66 ++++++++++++++++++++++++++++ test/test_linux_all_tcp_listen_stats_leak.rb | 43 ++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 test/test_linux_all_tcp_listen_stats.rb create mode 100644 test/test_linux_all_tcp_listen_stats_leak.rb (limited to 'test') diff --git a/test/test_linux_all_tcp_listen_stats.rb b/test/test_linux_all_tcp_listen_stats.rb new file mode 100644 index 0000000..7a45b7b --- /dev/null +++ b/test/test_linux_all_tcp_listen_stats.rb @@ -0,0 +1,66 @@ +# -*- encoding: binary -*- +require 'test/unit' +require 'socket' +require 'raindrops' +require 'pp' +$stderr.sync = $stdout.sync = true + +class TestLinuxAllTcpListenStats < Test::Unit::TestCase + include Raindrops::Linux + TEST_ADDR = ENV['UNICORN_TEST_ADDR'] || '127.0.0.1' + + def test_print_all + puts "EVERYTHING" + pp Raindrops::Linux.all_tcp_listener_stats + puts("-" * 72) + end if $stdout.tty? + + def setup + @socks = [] + end + + def teardown + @socks.each { |io| io.closed? or io.close } + end + + def new_server + s = TCPServer.new TEST_ADDR, 0 + @socks << s + [ s, s.addr[1] ] + end + + def new_client(port) + s = TCPSocket.new("127.0.0.1", port) + @socks << s + s + end + + def new_accept(srv) + c = srv.accept + @socks << c + c + end + + def test_all_ports + srv, port = new_server + addr = "#{TEST_ADDR}:#{port}" + all = Raindrops::Linux.all_tcp_listener_stats + assert_equal [0,0], all[addr].to_a + + new_client(port) + all = Raindrops::Linux.all_tcp_listener_stats + assert_equal [0,1], all[addr].to_a + + new_client(port) + all = Raindrops::Linux.all_tcp_listener_stats + assert_equal [0,2], all[addr].to_a + + new_accept(srv) + all = Raindrops::Linux.all_tcp_listener_stats + assert_equal [1,1], all[addr].to_a + + new_accept(srv) + all = Raindrops::Linux.all_tcp_listener_stats + assert_equal [2,0], all[addr].to_a + end +end if RUBY_PLATFORM =~ /linux/ diff --git a/test/test_linux_all_tcp_listen_stats_leak.rb b/test/test_linux_all_tcp_listen_stats_leak.rb new file mode 100644 index 0000000..1bbdae3 --- /dev/null +++ b/test/test_linux_all_tcp_listen_stats_leak.rb @@ -0,0 +1,43 @@ +# -*- encoding: binary -*- +require 'test/unit' +require 'raindrops' +require 'socket' +require 'benchmark' +$stderr.sync = $stdout.sync = true + +class TestLinuxAllTcpListenStatsLeak < Test::Unit::TestCase + + TEST_ADDR = ENV['UNICORN_TEST_ADDR'] || '127.0.0.1' + + + def rss_kb + File.readlines("/proc/#$$/status").grep(/VmRSS:/)[0].split(/\s+/)[1].to_i + end + def test_leak + s = TCPServer.new(TEST_ADDR, 0) + start_kb = rss_kb + p [ :start_kb, start_kb ] + assert_nothing_raised do + p(Benchmark.measure { + 1000.times { Raindrops::Linux.all_tcp_listener_stats } + }) + end + cur_kb = rss_kb + p [ :cur_kb, cur_kb ] + now = Time.now.to_i + fin = now + 60 + assert_nothing_raised do + 1000000000.times { |i| + if (i % 1024) == 0 + now = Time.now.to_i + break if now > fin + end + Raindrops::Linux.all_tcp_listener_stats + } + end + cur_kb = rss_kb + p [ :cur_kb, cur_kb ] + ensure + s.close + end if ENV["STRESS"].to_i != 0 +end -- cgit v1.2.3-24-ge0c7