line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Memchmark; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
21365
|
use 5.008; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
41
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
35
|
|
8
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
76
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
require Exporter; |
11
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw(memchmark cmpthese); |
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
115
|
|
15
|
1
|
|
|
1
|
|
1476
|
use Proc::ProcessTable; |
|
1
|
|
|
|
|
11331
|
|
|
1
|
|
|
|
|
65
|
|
16
|
1
|
|
|
1
|
|
779
|
use POSIX qw(:sys_wait_h); |
|
1
|
|
|
|
|
7417
|
|
|
1
|
|
|
|
|
7
|
|
17
|
1
|
|
|
1
|
|
2356
|
use Time::HiRes qw(usleep); |
|
1
|
|
|
|
|
21125
|
|
|
1
|
|
|
|
|
9
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub _find_process { |
20
|
0
|
|
0
|
0
|
|
|
my $pid = shift || $$; |
21
|
0
|
|
|
|
|
|
my $p=Proc::ProcessTable->new; |
22
|
0
|
|
|
|
|
|
for (@{$p->table}) { |
|
0
|
|
|
|
|
|
|
23
|
0
|
0
|
|
|
|
|
return $_ if $_->pid == $pid; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub memchmark (&) { |
28
|
0
|
|
|
0
|
1
|
|
my $sub = shift; |
29
|
0
|
0
|
|
|
|
|
ref $sub eq 'CODE' |
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
|
0
|
0
|
|
|
|
if (defined $pid and $pid==0) { |
35
|
0
|
|
|
|
|
|
eval { &$sub() }; |
|
0
|
|
|
|
|
|
|
36
|
0
|
0
|
|
|
|
|
$@ and print STDERR $@; |
37
|
0
|
|
|
|
|
|
sleep 1; |
38
|
0
|
|
|
|
|
|
exit(0); |
39
|
|
|
|
|
|
|
} |
40
|
0
|
0
|
|
|
|
|
defined $pid or croak "unable to fork"; |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
my $ecode; |
43
|
0
|
|
|
|
|
|
my $size1 = $size0; |
44
|
0
|
|
|
|
|
|
do { |
45
|
0
|
|
|
|
|
|
usleep(100000); |
46
|
0
|
|
|
|
|
|
my $size = _find_process($pid)->size; |
47
|
0
|
0
|
|
|
|
|
$size1 = $size if $size > $size1; |
48
|
0
|
|
|
|
|
|
$ecode = waitpid($pid, WNOHANG); |
49
|
|
|
|
|
|
|
} until $ecode > 0; |
50
|
0
|
|
|
|
|
|
return $size1-$size0; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub cmpthese { |
54
|
0
|
|
|
0
|
1
|
|
my %test = @_; |
55
|
0
|
|
0
|
0
|
|
|
my $init = delete $test{-init} || sub {}; |
|
0
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
my $size0 = &memchmark($init); |
57
|
0
|
|
|
|
|
|
my %size; |
58
|
0
|
|
|
|
|
|
for my $test (sort keys %test) { |
59
|
0
|
0
|
|
|
|
|
$test=~/^-/ and croak "invalid test name"; |
60
|
0
|
|
|
|
|
|
$size{$test} = &memchmark($test{$test}) - $size0; |
61
|
0
|
|
|
|
|
|
print "test: $test, memory used: $size{$test} bytes\n"; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
1; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
__END__ |