Switched to Module::Install
[catagits/Catalyst-Runtime.git] / t / optional_memleak.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use FindBin;
7 use lib "$FindBin::Bin/../live/lib";
8
9 use Test::More;
10 use Catalyst::Test 'TestApp';
11 use YAML;
12 eval "use GTop";
13
14 plan skip_all => 'set TEST_MEMLEAK to enable this test'
15     unless $ENV{TEST_MEMLEAK};
16 plan skip_all => 'GTop required for this test' if $@;
17
18 eval "use HTTP::Body 0.03";
19 plan skip_all => 'HTTP::Body >= 0.03 required for this test' if $@;
20
21 our $gtop = GTop->new;
22 our ( $initial, $final ) = ( 0, 0 ); 
23 our $tests = YAML::LoadFile("$FindBin::Bin/stress.yml");
24
25 my $total_tests = 0;
26
27 # let the user specify a single uri to test
28 my $user_test = shift;
29 if ( $user_test ) {
30     plan tests => 1;
31     run_test( $user_test );
32 }
33 # otherwise, run all tests
34 else {
35     map { $total_tests += scalar @{ $tests->{$_} } } keys %{$tests};
36     plan tests => $total_tests;
37     
38     foreach my $test_group ( keys %{$tests} ) {
39         foreach my $test ( @{ $tests->{$test_group} } ) {
40             run_test( $test );
41         }
42     }
43 }
44
45 sub run_test {
46     my $uri = shift || die 'No URI given for test';
47     
48     print "TESTING $uri\n";
49     
50     # make a few requests to set initial memory size
51     for ( 1 .. 3 ) {
52         request( $uri );
53     }
54     
55     $initial = $gtop->proc_mem($$)->size;
56     print "Initial Size: " . GTop::size_string($initial) . "\n";
57     
58     for ( 1 .. 500 ) {
59         request( $uri );
60     }
61     
62     $final = $gtop->proc_mem($$)->size;
63     print "Final Size:   " . GTop::size_string($final) . "\n";
64     
65     if ( $final > $initial ) {
66         print "Leaked Bytes: " . GTop::size_string($final - $initial) . "\n";
67     }
68     
69     is( $final, $initial, "'$uri' memory is not leaking" );
70 }
71