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