license: use LGPLv2.1 or later (was LGPL (2.1|3.0)-only)
[raindrops.git] / test / test_linux_all_tcp_listen_stats_leak.rb
blob73682203c1f480cbf283be9437ea31252d9cefdf
1 # -*- encoding: binary -*-
2 require 'test/unit'
3 require 'raindrops'
4 require 'socket'
5 require 'benchmark'
6 $stderr.sync = $stdout.sync = true
8 class TestLinuxAllTcpListenStatsLeak < Test::Unit::TestCase
10   TEST_ADDR = ENV['UNICORN_TEST_ADDR'] || '127.0.0.1'
13   def rss_kb
14     File.readlines("/proc/#$$/status").grep(/VmRSS:/)[0].split(/\s+/)[1].to_i
15   end
16   def test_leak
17     s = TCPServer.new(TEST_ADDR, 0)
18     start_kb = rss_kb
19     p [ :start_kb, start_kb ]
20     assert_nothing_raised do
21       p(Benchmark.measure {
22         1000.times { Raindrops::Linux.all_tcp_listener_stats }
23       })
24     end
25     cur_kb = rss_kb
26     p [ :cur_kb, cur_kb ]
27     now = Time.now.to_i
28     fin = now + 60
29     assert_nothing_raised do
30       1000000000.times { |i|
31         if (i % 1024) == 0
32           now = Time.now.to_i
33           break if now > fin
34         end
35         Raindrops::Linux.all_tcp_listener_stats
36       }
37     end
38     cur_kb = rss_kb
39     p [ :cur_kb, cur_kb ]
40     ensure
41       s.close
42   end
43 end if ENV["STRESS"].to_i != 0