Commit | Line | Data |
---|---|---|
7c40a30f | 1 | #!perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
4853fb50 | 6 | use Test::More; |
7 | BEGIN { | |
8 | plan skip_all => 'set TEST_MEMLEAK to enable this test' | |
9 | unless $ENV{TEST_MEMLEAK}; | |
10 | } | |
11 | ||
7c40a30f | 12 | use FindBin; |
a2e038a1 | 13 | use lib "$FindBin::Bin/lib"; |
7c40a30f | 14 | use Catalyst::Test 'TestApp'; |
7c40a30f | 15 | |
15f0ede8 | 16 | eval "use Proc::ProcessTable"; |
9b984642 | 17 | plan skip_all => 'Proc::ProcessTable required for this test' if $@; |
7c40a30f | 18 | |
3eca4f18 | 19 | eval "use HTTP::Body 0.03"; |
20 | plan skip_all => 'HTTP::Body >= 0.03 required for this test' if $@; | |
21 | ||
15f0ede8 | 22 | eval "use YAML"; |
23 | plan skip_all => 'YAML required for this test' if $@; | |
24 | ||
9b984642 | 25 | our $t = Proc::ProcessTable->new( cache_ttys => 1 ); |
010c814d | 26 | our ( $initial, $final ) = ( 0, 0 ); |
e63e8323 | 27 | our $tests = YAML::LoadFile("$FindBin::Bin/optional_stress.yml"); |
7c40a30f | 28 | |
010c814d | 29 | my $total_tests = 0; |
010c814d | 30 | |
13af225c | 31 | # let the user specify a single uri to test |
32 | my $user_test = shift; | |
33 | if ( $user_test ) { | |
34 | plan tests => 1; | |
35 | run_test( $user_test ); | |
36 | } | |
37 | # otherwise, run all tests | |
38 | else { | |
39 | map { $total_tests += scalar @{ $tests->{$_} } } keys %{$tests}; | |
40 | plan tests => $total_tests; | |
41 | ||
42 | foreach my $test_group ( keys %{$tests} ) { | |
43 | foreach my $test ( @{ $tests->{$test_group} } ) { | |
44 | run_test( $test ); | |
45 | } | |
010c814d | 46 | } |
47 | } | |
48 | ||
49 | sub run_test { | |
50 | my $uri = shift || die 'No URI given for test'; | |
7c40a30f | 51 | |
010c814d | 52 | print "TESTING $uri\n"; |
53 | ||
54 | # make a few requests to set initial memory size | |
55 | for ( 1 .. 3 ) { | |
56 | request( $uri ); | |
57 | } | |
58 | ||
9b984642 | 59 | $initial = size_of($$); |
60 | print "Initial Size: $initial\n"; | |
7c40a30f | 61 | |
010c814d | 62 | for ( 1 .. 500 ) { |
63 | request( $uri ); | |
7c40a30f | 64 | } |
65 | ||
9b984642 | 66 | $final = size_of($$); |
67 | print "Final Size: $final\n"; | |
7c40a30f | 68 | |
69 | if ( $final > $initial ) { | |
0d9dfa69 | 70 | print "Leaked: " . ($final - $initial) . "K\n"; |
7c40a30f | 71 | } |
72 | ||
010c814d | 73 | is( $final, $initial, "'$uri' memory is not leaking" ); |
7c40a30f | 74 | } |
010c814d | 75 | |
9b984642 | 76 | sub size_of { |
77 | my $pid = shift; | |
78 | ||
79 | foreach my $p ( @{ $t->table } ) { | |
80 | if ( $p->pid == $pid ) { | |
fe796093 | 81 | return $p->rss; |
9b984642 | 82 | } |
83 | } | |
84 | ||
85 | die "Pid $pid not found?"; | |
86 | } | |
87 |