| 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() || $$
MC/DC0%0/2
shift() || $$
shift()
$$
Truth table0%0/3
A || B
A= shift()B= $$
| |||||||||||||||||||||||||||||
| 21 | 0 | my $p=Proc::ProcessTable->new; | |||||||||||||||||||||||||||
| 22 | 0 | for (@{$p->table}) { | |||||||||||||||||||||||||||
| 23 | 0 | ▶ | return $_ if $_->pid == $pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $pid and $pid == 0
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
| |||||||||||||||||||||||||||||
| 35 | 0 | eval { &$sub() }; | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | $@ and print STDERR $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 38 | 0 | exit(0); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 0 | ▶ | defined $pid or croak "unable to fork"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | my $ecode; | |||||||||||||||||||||||||||
| 43 | 0 | my $size1 = $size0; | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | usleep(100000); | |||||||||||||||||||||||||||
| 46 | 0 | my $size = _find_process($pid)->size; | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | $size1 = $size if $size > $size1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 {
}
MC/DC0%0/2
delete $test{'-init'} || sub {
}
delete $test{'-init'}
sub {
}
Truth table0%0/3
A || B
A= delete $test{'-init'}B= sub {
}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||