d5de6ff86d39ce37ecc0222bfc2e8e7d95856c84
[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 our $gtop = GTop->new;
19 our ( $initial, $final ) = ( 0, 0 ); 
20 our $tests = YAML::LoadFile("$FindBin::Bin/stress.yml");
21
22 my $total_tests = 0;
23 map { $total_tests += scalar @{ $tests->{$_} } } keys %{$tests};
24 plan tests => $total_tests;
25
26 foreach my $test_group ( keys %{$tests} ) {
27     foreach my $test ( @{ $tests->{$test_group} } ) {
28         run_test( $test );
29     }
30 }
31
32 sub run_test {
33     my $uri = shift || die 'No URI given for test';
34     
35     print "TESTING $uri\n";
36     
37     # make a few requests to set initial memory size
38     for ( 1 .. 3 ) {
39         request( $uri );
40     }
41     
42     $initial = $gtop->proc_mem($$)->size;
43     print "Initial Size: " . GTop::size_string($initial) . "\n";
44     
45     for ( 1 .. 500 ) {
46         request( $uri );
47     }
48     
49     $final = $gtop->proc_mem($$)->size;
50     print "Final Size:   " . GTop::size_string($final) . "\n";
51     
52     if ( $final > $initial ) {
53         print "Leaked Bytes: " . GTop::size_string($final - $initial) . "\n";
54     }
55     
56     is( $final, $initial, "'$uri' memory is not leaking" );
57 }
58