File Coverage

blib/lib/Test2/Plugin/MemUsage.pm
Criterion Covered Total %
statement 28 28 100.0
branch 8 16 50.0
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 42 51 82.3


line stmt bran cond sub pod time code
1             package Test2::Plugin::MemUsage;
2 1     1   70894 use strict;
  1         3  
  1         28  
3 1     1   4 use warnings;
  1         2  
  1         40  
4              
5             our $VERSION = '0.002002';
6              
7 1     1   7 use Test2::API qw/test2_add_callback_exit/;
  1         1  
  1         523  
8              
9             my $ADDED_HOOK = 0;
10              
11             sub import {
12 1 50   1   31 return if $ADDED_HOOK++;
13              
14 1         5 test2_add_callback_exit(\&send_mem_event);
15             }
16              
17 1     1   6543 sub proc_file { "/proc/$$/status" }
18              
19             sub send_mem_event {
20 1     1 0 427 my ($ctx, $real, $new) = @_;
21              
22 1         4 my $file = proc_file();
23 1 50       25 return unless -f $file;
24              
25 1         3 my $stats;
26             {
27 1 50       2 open(my $fh, '<', $file) or die "Could not open file '$file' (<): $!";
  1         38  
28 1         6 local $/;
29 1         28 $stats = <$fh>;
30 1 50       17 close($fh) or die "Could not close file '$file': $!";
31             }
32              
33 1 50       5 return unless $stats;
34              
35 1         2 my %mem;
36 1 50       14 $mem{peak} = [$1, $2] if $stats =~ m/VmPeak:\s+(\d+) (\S+)/;
37 1 50       10 $mem{size} = [$1, $2] if $stats =~ m/VmSize:\s+(\d+) (\S+)/;
38 1 50       8 $mem{rss} = [$1, $2] if $stats =~ m/VmRSS:\s+(\d+) (\S+)/;
39 1         8 $mem{details} = "rss: $mem{rss}->[0]$mem{rss}->[1]\nsize: $mem{size}->[0]$mem{size}->[1]\npeak: $mem{peak}->[0]$mem{peak}->[1]";
40              
41             $ctx->send_ev2(
42             memory => \%mem,
43             about => {package => __PACKAGE__, details => $mem{details}},
44             info => [{tag => 'MEMORY', details => $mem{details}}],
45              
46             harness_job_fields => [
47             {name => 'mem_rss', details => $mem{rss}->[0] . $mem{rss}->[1]},
48             {name => 'mem_size', details => $mem{size}->[0] . $mem{size}->[1]},
49 1         20 {name => 'mem_peak', details => $mem{peak}->[0] . $mem{peak}->[1]},
50             ],
51             );
52             }
53              
54             1;
55              
56             __END__