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