blib/lib/Memchmark.pm

filter: Statementstmt 43.8% 25 / 57 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 63.6% 7 / 11 Podpod 100% 2 / 2 totaltotal 33.6% 34 / 101 CC 14 SCAR 34.4
CC
14
Cov
30%
memchmark
72.0
_find_process
20.0
cmpthese
20.0
CRAP 31.0
↑ Summary
1 package Memchmark;
2  
3 1 our $VERSION = '0.01';
4  
5 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 require Exporter;
11 1 our @ISA = qw(Exporter);
12 1 our @EXPORT_OK = qw(memchmark cmpthese);
13  
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Proc::ProcessTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use POSIX qw(:sys_wait_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Time::HiRes qw(usleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 0 ▶sub _find_process {
Subroutine _find_process: not called CC=4 SCAR=30.0
20 0 ▶ my $pid = shift || $$;
Condition0%0/3
shift() || $$
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $$
shift() $$
Truth table0%0/3
A || B
A= shift()B= $$
ABresult
1X1
011
000
21 0 my $p=Proc::ProcessTable->new;
22 0 for (@{$p->table}) {
23 0 ▶ return $_ if $_->pid == $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $_->pid == $pid 0 0 0
24 }
25 }
26  
27 0 ▶sub memchmark (&) {
Subroutine memchmark: not called CC=8 SCAR=42.8
Pod documented
28 0 my $sub = shift;
29 0 ▶ ref $sub eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $sub eq "CODE" or croak("invalid type for memchmark arg ($sub), CODE ref expected") 0 0 0
30 or croak "invalid type for memchmark arg ($sub), CODE ref expected";
31 0 my $size0 = _find_process->size;
32 0 my $pid = fork;
33  
34 0 ▶ if (defined $pid and $pid==0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pid and $pid == 0) 0 0 0
Condition0%0/3
defined $pid and $pid == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $pid and $pid == 0
defined $pid $pid == 0
Truth table0%0/3
A and B
A= defined $pidB= $pid == 0
ABresult
0X0
100
111
35 0 eval { &$sub() };
36 0 ▶ $@ and print STDERR $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and print STDERR $@ 0 0 0
37 0 sleep 1;
38 0 exit(0);
39 }
40 0 ▶ defined $pid or croak "unable to fork";
Branch: 1 branch
BranchTrueFalseTotal
defined $pid or croak("unable to fork") 0 0 0
41  
42 0 my $ecode;
43 0 my $size1 = $size0;
44 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$ecode > 0 or do { do { &usleep(100000); my $size = _find_process($pid)->size; $size1 = $size if $size > $size1; $ecode = waitpid($pid, 1) } } 0 0 0
45 0 usleep(100000);
46 0 my $size = _find_process($pid)->size;
47 0 ▶ $size1 = $size if $size > $size1;
Branch: 1 branch
BranchTrueFalseTotal
if $size > $size1 0 0 0
48 0 $ecode = waitpid($pid, WNOHANG);
49 } until $ecode > 0;
50 0 return $size1-$size0;
51 }
52  
53 0 ▶sub cmpthese {
Subroutine cmpthese: not called CC=4 SCAR=30.0
Pod documented
54 0 my %test = @_;
55 0 ▶ my $init = delete $test{-init} || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
delete $test{'-init'} || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $test{'-init'} || sub { }
delete $test{'-init'} sub { }
Truth table0%0/3
A || B
A= delete $test{'-init'}B= sub { }
ABresult
1X1
011
000
56 0 my $size0 = &memchmark($init);
57 0 my %size;
58 0 for my $test (sort keys %test) {
59 0 ▶ $test=~/^-/ and croak "invalid test name";
Branch: 1 branch
BranchTrueFalseTotal
$test =~ /^-/ and croak("invalid test name") 0 0 0
60 0 $size{$test} = &memchmark($test{$test}) - $size0;
61 0 print "test: $test, memory used: $size{$test} bytes\n";
62 }
63 }
64  
65 1 1;
66  
67 __END__
↑ Summary