Brackup/BackupStats.pm

filter: Statementstmt 32.8% 25 / 76 Branchbran 20.5% 7 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 58.3% 7 / 12 Podpod 0.0% 0 / 7 totaltotal 30.2% 39 / 129 CC 18 SCAR 34.1
CC
18
Cov
29%
print
90.0
reset
6.0
next
6.0
CRAP 30.3
« Brackup/GPGProcess.pm ↑ Summary Brackup/Backup.pm »
1 package Brackup::BackupStats;
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 5 ▶sub new {
Subroutine new: called CC=3 SCAR=12.3
Pod undocumented
5 5 my $class = shift;
6 5 my %opts = @_;
7 5 ▶ croak("Unknown options: " . join(', ', keys %opts)) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 5 5
8  
9 5 my $self = {
10 start_time => time,
11 ts => Brackup::BackupStats::Data->new,
12 data => Brackup::BackupStats::Data->new,
13 };
14  
15 5 ▶ if (eval { require GTop }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require GTop } }) 0 5 5
16 0 $self->{gtop} = GTop->new;
17 0 $self->{gtop_max} = 0;
18 0 $self->{gtop_data} = Brackup::BackupStats::Data->new;
19 }
20  
21 5 return bless $self, $class;
22 }
23  
24 0 ▶sub print {
Subroutine print: not called CC=9 SCAR=45.0
Pod undocumented
25 0 my $self = shift;
26 0 my $stats_file = shift;
27
28 # Reset iterators
29 0 $self->reset;
30  
31 0 my $fh;
32 0 ▶ if ($stats_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stats_file) { } 0 0 0
33 0 ▶ open $fh, ">$stats_file"
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">$stats_file" or die "Failed to open stats file '${stats_file}': $!" 0 0 0
34 or die "Failed to open stats file '$stats_file': $!";
35 }
36 else {
37 0 $fh = *STDOUT;
38 }
39  
40 0 ▶ my $hash = $stats_file ? '' : '# ';
Branch: 1 branch
BranchTrueFalseTotal
$stats_file ? : 0 0 0
41 0 print $fh "${hash}BACKUPS STATS:\n";
42 0 print $fh "${hash}\n";
43  
44 0 my $start_time = $self->{start_time};
45 0 my $end_time = time;
46 0 my $fmt = "${hash}%-37s %s\n";
47 0 printf $fh $fmt, 'Start Time:', scalar localtime $start_time;
48 0 printf $fh $fmt, 'End Time:', scalar localtime $end_time;
49  
50 0 my $ts = $start_time;
51 0 ▶ while (my ($label, $next_ts) = $self->{ts}->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->{"ts"}->next) 0 0 0
52 0 printf $fh $fmt, "$label Time:", ($next_ts - $ts) . 's';
53 0 $ts = $next_ts;
54 }
55 0 printf $fh $fmt, 'Total Run Time:', ($end_time - $start_time) . 's';
56 0 print $fh "${hash}\n";
57  
58 0 ▶ if (my $gtop_data = $self->{gtop_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $gtop_data = $self->{'gtop_data'}) { } 0 0 0
59 0 ▶ while (my ($label, $size) = $gtop_data->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $gtop_data->next) 0 0 0
60 0 printf $fh $fmt,
61 "Post $label Memory Usage:", sprintf('%0.1f MB', $size / (1024 * 1024));
62 }
63 printf $fh $fmt,
64 0 'Peak Memory Usage:', sprintf('%0.1f MB', $self->{gtop_max} / (1024 * 1024));
65 0 print $fh "${hash}\n";
66 } else {
67 0 print $fh "${hash}GTop not installed, memory usage stats disabled\n";
68 0 print $fh "${hash}\n";
69 }
70  
71 0 my $data = $self->{data};
72 0 ▶ while (my ($key, $value) = $data->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $data->next) 0 0 0
73 0 printf $fh $fmt, $key, $value;
74 }
75 0 ▶ print $fh "\n" if $stats_file;
Branch: 1 branch
BranchTrueFalseTotal
if $stats_file 0 0 0
76 }
77  
78 # Check/record max memory usage
79 48 ▶sub check_maxmem {
Subroutine check_maxmem: called CC=3 SCAR=16.5
Pod undocumented
80 48 my $self = shift;
81 48 ▶ return unless $self->{gtop};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"gtop"} 48 0 48
82 0 my $mem = $self->{gtop}->proc_mem($$)->size;
83 0 ▶ $self->{gtop_max} = $mem if $mem > $self->{gtop_max};
Branch: 1 branch
BranchTrueFalseTotal
if $mem > $self->{"gtop_max"} 0 0 0
84 }
85  
86 # Record current time (and memory, if applicable) against $label
87 20 ▶sub timestamp {
Subroutine timestamp: called CC=2 SCAR=8.4
Pod undocumented
88 20 my ($self, $label) = @_;
89 20 $self->{ts}->set($label => time);
90 20 ▶ return unless $self->{gtop};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"gtop"} 20 0 20
91 0 $self->{gtop_data}->set($label => $self->{gtop}->proc_mem($$)->size);
92 0 $self->check_maxmem;
93 }
94  
95 20 ▶sub set {
Subroutine set: called CC=2 SCAR=6.9
Pod undocumented
96 20 my $self = shift;
97 20 ▶ $self->{data}->set(shift, shift) while @_ >= 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and do { $self->{"data"}->set(shift(), shift()) } 20 20 40
98 }
99  
100 0 ▶sub reset {
Subroutine reset: not called CC=2 SCAR=17.9
Pod undocumented
101 0 my $self = shift;
102 0 $self->{ts}->reset;
103 0 $self->{data}->reset;
104 0 ▶ $self->{gtop_data}->reset if $self->{gtop_data};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"gtop_data"} 0 0 0
105 }
106  
107 0 ▶sub note_stored_chunk {
Subroutine note_stored_chunk: not called CC=1 SCAR=6.9
Pod undocumented
108 0 my ($self, $chunk) = @_;
109 }
110  
111 package Brackup::BackupStats::Data;
112  
113 10 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
114 10 my $class = shift;
115 10 return bless {
116 index => 0,
117 list => [], # ordered list of data keys
118 data => {},
119 }, $class;
120 }
121  
122 40 ▶sub set {
Subroutine set: called CC=2 SCAR=7.0
123 40 my ($self, $key, $value) = @_;
124 40 ▶ die "data key '$key' exists" if exists $self->{data}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"data"}{$key} 0 40 40
125 40 push @{$self->{list}}, $key;
126 40 $self->{data}->{$key} = $value;
127 }
128  
129 # Iterator interface, returning ($key, $value)
130 0 ▶sub next {
Subroutine next: not called CC=2 SCAR=17.9
131 0 my $self = shift;
132 0 ▶ return () unless $self->{index} <= $#{$self->{list}};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"index"} <= $#{$$self{"list"};} 0 0 0
133 0 my $key = $self->{list}->[$self->{index}++];
134 0 return ($key, $self->{data}->{$key});
135 }
136  
137 # Reset/rewind iterator
138 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
139 0 my $self = shift;
140 0 $self->{index} = 0;
141 }
142  
143 13 1;
144  
145 # vim:sw=4
« Brackup/GPGProcess.pm ↑ Summary Brackup/Backup.pm »