blib/lib/Devel/StatProfiler/EvalSourceStorage.pm

filter: Statementstmt 94.0% 110 / 117 Branchbran 71.8% 23 / 32 Conditioncond 50.0% 7 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 4 totaltotal 81.3% 157 / 193 CC 28 SCAR 21.0
CC
28
Cov
86%
pack_files
18.5
_load_manifest
6.0
_total_size
6.0
CRAP 8.2
« blib/lib/Devel/StatProfiler/Aggregator.pm ↑ Summary blib/lib/Devel/StatProfiler/Utils.pm »
1 package Devel::StatProfiler::EvalSourceStorage;
2  
3 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use autodie qw(open close rename);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 26 ▶use Devel::StatProfiler::Utils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
8 read_file
9 write_file
10 26 );
11 26 ▶use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 26 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 26 ▶use File::Glob qw(bsd_glob);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1960 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
17 1960 my ($class, %opts) = @_;
18 my $self = bless {
19 base_dir => $opts{base_dir},
20 1960 pack_files => [],
21 source_map => {},
22 manifest => '',
23 }, $class;
24  
25 1960 return $self;
26 }
27  
28 85 ▶sub get_source_by_hash {
Subroutine get_source_by_hash: called CC=3 SCAR=11.0
Pod undocumented
29 85 my ($self, $hash) = @_;
30 85 my $source = $self->_get_source_by_hash($hash);
31  
32 85 ▶ return $source if defined $source;
Branch: 1 branch
BranchTrueFalseTotal
if defined $source 84 1 85
33  
34 # we should only get here because of manifest lazy loading
35 1 $self->_load_manifest();
36 1 ▶ return $self->_get_source_by_hash($hash) // '';
Condition50%1/2
$self->_get_source_by_hash($hash) // ""
l!l
10
MC/DC0%0/1
$self->_get_source_by_hash($hash) // ""
$self->_get_source_by_hash($hash)
Truth table50%1/2
A
A= $self->_get_source_by_hash($hash)
Aresult
11
00
37 }
38  
39 86 ▶sub _get_source_by_hash {
Subroutine _get_source_by_hash: called CC=3 SCAR=11.0
40 86 my ($self, $hash) = @_;
41 86 my $packed = pack "H*", $hash;
42  
43 86 ▶ if (my $pack_index = $self->{source_map}{$packed}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pack_index = $self->{'source_map'}{$packed}) { } 21 65 86
44 21 return $self->_get_source_from_pack($hash, $self->{pack_files}[$pack_index]);
45 } else {
46 my $path = File::Spec::Functions::catfile(
47 $self->{base_dir},
48 65 substr($hash, 0, 2),
49 substr($hash, 2, 2),
50 substr($hash, 4)
51 );
52  
53 65 ▶ return -f $path ? read_file($path, 'use_utf8') : undef;
Branch: 1 branch
BranchTrueFalseTotal
-f $path ? : 64 1 65
54 }
55 }
56  
57 120 ▶sub add_source_string {
Subroutine add_source_string: called CC=2 SCAR=6.9
Pod undocumented
58 120 my ($self, $hash) = @_;
59 my $source_subdir = File::Spec::Functions::catdir(
60 $self->{base_dir},
61 120 substr($hash, 0, 2),
62 substr($hash, 2, 2),
63 );
64  
65 120 File::Path::mkpath([$source_subdir]);
66 120 ▶ write_file($source_subdir, substr($hash, 4), 'use_utf8', $_[2])
Branch: 1 branch
BranchTrueFalseTotal
unless -e File::Spec::Functions::catfile($source_subdir, substr($hash, 4)) 98 22 120
67 unless -e File::Spec::Functions::catfile($source_subdir, substr($hash, 4));
68 }
69  
70 1 ▶sub pack_files {
Subroutine pack_files: called CC=15 SCAR=29.2
Pod undocumented
71 1 my ($self, $force) = @_;
72  
73 1 $self->_load_manifest();
74  
75 my $glob = File::Spec::Functions::catfile(
76 $self->{base_dir},
77 1 '*',
78 '*',
79 '*'
80 );
81 1 my @loose_files = grep !/\.tmp$/, bsd_glob($glob);
82 1 ▶ return if !$force &&
Branch: 1 branch
BranchTrueFalseTotal
if not $force and @loose_files < 10000 and _total_size(\@loose_files) < 10000000 0 1 1
Condition33%2/6
not $force and @loose_files < 10000 and _total_size(\@loose_files) < 10000000
!ll&&!rl&&r
100
not $force and @loose_files < 10000
!ll&&!rl&&r
100
MC/DC0%0/3
not $force and @loose_files < 10000 and _total_size(\@loose_files) < 10000000
not $force @loose_files < 10000 _total_size(\@loose_files) < 10000000
Truth table25%1/4
not(A) and B and C
A= not $forceB= @loose_files < 10000C= _total_size(\@loose_files) < 10000000
ABCresult
0XX0
10X0
1100
1111
83 @loose_files < 10_000 &&
84 _total_size(\@loose_files) < 10_000_000;
85 1 my ($archive, $estimated_size);
86  
87 1 for my $fullpath (@loose_files) {
88 21 my $relpath = File::Spec::Functions::abs2rel($fullpath, $self->{base_dir});
89 21 ▶ next unless $relpath =~ m{^[0-9a-fA-F/\\]+$};
Branch: 1 branch
BranchTrueFalseTotal
unless $relpath =~ m(^[0-9a-fA-F/\\]+$) 0 21 21
90 21 my $hash = $relpath =~ tr{/\\}{}dr;
91 21 my $packed = pack "H*", $hash;
92  
93 21 ▶ next if $self->{source_map}{$packed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"source_map"}{$packed} 0 21 21
94 21 ▶ if ($archive && $estimated_size >= 100_000_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($archive and $estimated_size >= 100000000) 0 21 21
Condition66%2/3
$archive and $estimated_size >= 100000000
!ll&&!rl&&r
1200
MC/DC0%0/2
$archive and $estimated_size >= 100000000
$archive $estimated_size >= 100000000
Truth table66%2/3
A and B
A= $archiveB= $estimated_size >= 100000000
ABresult
0X0
100
111
95 0 $self->_save_archive($archive);
96 0 $archive = $estimated_size = undef;
97 }
98 21 ▶ $archive ||= Archive::Zip->new;
Condition66%2/3
$archive ||= "Archive::Zip"->new
l!l&&r!l&&!r
2010
MC/DC0%0/2
$archive ||= "Archive::Zip"->new
$archive "Archive::Zip"->new
Truth table66%2/3
A ||= B
A= $archiveB= "Archive::Zip"->new
ABresult
1X1
011
000
99 21 my $size = -s $fullpath;
100 21 ▶ my $compression = $size >= 200 ? 6 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$size >= 200 ? : 20 1 21
101 21 $archive->addFile($fullpath, $relpath, $compression);
102 21 ▶ $estimated_size += $compression ? $size / 10 : $size;
Branch: 1 branch
BranchTrueFalseTotal
$compression ? : 20 1 21
103 }
104  
105 1 ▶ if ($archive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($archive) 1 0 1
106 1 $self->_save_archive($archive);
107 }
108  
109 1 $self->_build_manifest();
110  
111 1 unlink @loose_files;
112 # will only remove empty directories
113 1 rmdir $_ for bsd_glob(File::Spec::Functions::catdir(
114 $self->{base_dir},
115 '*',
116 '*',
117 ));
118 1 rmdir $_ for bsd_glob(File::Spec::Functions::catdir(
119 $self->{base_dir},
120 '*',
121 ));
122 }
123  
124 2 ▶sub _load_manifest {
Subroutine _load_manifest: called CC=6 SCAR=18.0
125 2 my ($self) = @_;
126 my $manifest = File::Spec::Functions::catfile(
127 $self->{base_dir},
128 2 'archives',
129 'manifest',
130 );
131  
132 2 ▶ if (!-f $manifest) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $manifest) { } 1 1 2
133 1 $self->{pack_files} = [];
134 1 $self->{source_map} = {};
135 1 $self->{manifest} = '';
136 } else {
137 1 my $contents = read_file($manifest);
138  
139 1 ▶ return if $self->{manifest} eq 'contents';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"manifest"} eq "contents" 0 1 1
140  
141 1 my @files = split /\n/, $contents;
142 1 my %source_map;
143 my @archives = grep $_, map {
144 1 my $fullpath = File::Spec::Functions::catfile(
145 $self->{base_dir},
146 1 'archives',
147 $_,
148 );
149  
150 1 ▶ if (-f $fullpath) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $fullpath) { } 1 0 1
151 1 Archive::Zip->new($fullpath);
152 } else {
153 undef
154 0 }
155 } @files;
156  
157 1 for my $i (0 .. $#archives) {
158 1 my $archive = $archives[$i];
159  
160 1 for my $member ($archive->memberNames) {
161 21 my $hash = $member =~ tr{/}{}dr;
162  
163 21 $source_map{pack 'H*', $hash} = $i + 1;
164 }
165 }
166  
167 1 $self->{pack_files} = [undef, @archives];
168 1 $self->{source_map} = \%source_map;
169 1 $self->{manifest} = $contents;
170 }
171 }
172  
173 1 ▶sub _build_manifest {
Subroutine _build_manifest: called CC=1 SCAR=0.0
174 1 my ($self) = @_;
175 my $archives_dir = File::Spec::Functions::catfile(
176 $self->{base_dir},
177 1 'archives',
178 );
179 1 my @zip_files = bsd_glob(File::Spec::Functions::catfile(
180 $archives_dir,
181 '*.zip',
182 ));
183  
184 my $manifest = join "\n", sort map {
185 1 File::Spec::Functions::abs2rel($_, $archives_dir)
186 } @zip_files;
187  
188 1 write_file($archives_dir, 'manifest', undef, $manifest)
189 }
190  
191 1 ▶sub _save_archive {
Subroutine _save_archive: called CC=3 SCAR=11.0
192 1 my ($self, $archive) = @_;
193 my $archives_dir = File::Spec::Functions::catfile(
194 $self->{base_dir},
195 1 'archives',
196 );
197  
198 1 for (;;) {
199 1 my $target = sprintf 'pack%06d.zip', rand(100000);
200 1 my $name = File::Spec::Functions::catfile(
201 $archives_dir,
202 $target,
203 );
204 1 my $tmpname = File::Spec::Functions::catfile(
205 $archives_dir,
206 $target . '.tmp',
207 );
208  
209 1 ▶ next if -f $name;
Branch: 1 branch
BranchTrueFalseTotal
if -f $name 0 1 1
210 1 File::Path::mkpath($archives_dir);
211 1 my $status = $archive->writeToFileNamed($tmpname);
212 1 ▶ die "Error while writing pack" if $status != AZ_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $status != 0 0 1 1
213  
214 1 rename($tmpname, $name);
215 1 last;
216 }
217 }
218  
219 21 ▶sub _get_source_from_pack {
Subroutine _get_source_from_pack: called CC=1 SCAR=0.0
220 21 my ($self, $hash, $archive) = @_;
221 21 my $path = join '/',
222 substr($hash, 0, 2),
223 substr($hash, 2, 2),
224 substr($hash, 4);
225  
226 21 return $archive->contents($path);
227 }
228  
229 0 ▶sub _total_size {
Subroutine _total_size: not called CC=2 SCAR=17.9
230 0 my ($files) = @_;
231 0 my $total = 0;
232  
233 0 $total += -s $_ for @$files;
234  
235 0 return $total;
236 }
237  
238 26 1;
239  
240 __END__
« blib/lib/Devel/StatProfiler/Aggregator.pm ↑ Summary blib/lib/Devel/StatProfiler/Utils.pm »