blib/lib/Devel/StatProfiler/Utils.pm

filter: Statementstmt 81.6% 98 / 120 Branchbran 44.4% 32 / 72 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 95.4% 21 / 22 Podpod 0.0% 0 / 11 totaltotal 63.7% 153 / 240 CC 40 SCAR 20.0
CC
40
Cov
66%
_state_file
30.0
read_data
13.8
_write_and_rename
8.8
CRAP 7.4
« blib/lib/Devel/StatProfiler/EvalSourceStorage.pm ↑ Summary blib/lib/Devel/StatProfiler/SourceMap.pm »
1 package Devel::StatProfiler::Utils;
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 File::Spec::Functions ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 ▶use Fcntl qw(O_WRONLY O_CREAT O_EXCL);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 26 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 26 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 26 ▶use Digest::SHA qw(sha1 sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 26 our @EXPORT_OK = qw(
14 check_serializer
15 read_data
16 read_file
17 state_dir
18 state_file
19 state_file_shard
20 utf8_sha1
21 utf8_sha1_hex
22 write_data
23 write_data_any
24 write_data_part
25 write_file
26 );
27  
28 26 my ($SEREAL_ENCODER, $SEREAL_DECODER);
29  
30 7249 ▶sub state_dir {
Subroutine state_dir: called CC=5 SCAR=17.0
Pod undocumented
31 7249 my ($obj, $is_part) = @_;
32 7249 ▶ die "first parameter of state_dir() must be an object" unless ref $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj 0 7249 7249
33 die ref($obj), " passed to state_dir() is missing the root_dir attribute"
34 7249 ▶ unless $obj->{root_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"root_dir"} 0 7249 7249
35  
36 7249 ▶ if ($is_part) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_part) { } 3801 3448 7249
37 3801 ▶ File::Spec::Functions::catdir($obj->{parts_dir} // $obj->{root_dir}, '__state__', 'parts');
Condition33%1/3
$obj->{"parts_dir"} // $obj->{"root_dir"}
l!l&&r!l&&!r
380100
MC/DC0%0/2
$obj->{"parts_dir"} // $obj->{"root_dir"}
$obj->{"parts_dir"} $obj->{"root_dir"}
Truth table33%1/3
A // B
A= $obj->{"parts_dir"}B= $obj->{"root_dir"}
ABresult
1X1
011
000
38 } else {
39 3448 File::Spec::Functions::catdir($obj->{root_dir}, '__state__');
40 }
41 }
42  
43 0 ▶sub _state_file {
Subroutine _state_file: not called CC=5 SCAR=34.0
44 0 my ($obj, $is_part, $file) = @_;
45 0 ▶ die "first parameter of state_file() must be an object" unless ref $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj 0 0 0
46 die ref($obj), " passed to state_file() is missing the root_dir attribute"
47 0 ▶ unless $obj->{root_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"root_dir"} 0 0 0
48  
49 0 ▶ if ($is_part) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_part) { } 0 0 0
50 0 ▶ File::Spec::Functions::catfile($obj->{parts_dir} // $obj->{root_dir}, '__state__', 'parts', $file);
Condition0%0/3
$obj->{"parts_dir"} // $obj->{"root_dir"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj->{"parts_dir"} // $obj->{"root_dir"}
$obj->{"parts_dir"} $obj->{"root_dir"}
Truth table0%0/3
A // B
A= $obj->{"parts_dir"}B= $obj->{"root_dir"}
ABresult
1X1
011
000
51 } else {
52 0 File::Spec::Functions::catfile($obj->{root_dir}, '__state__', $file);
53 }
54 }
55  
56 2035 ▶sub state_file {
Subroutine state_file: called CC=7 SCAR=20.4
Pod undocumented
57 2035 my ($obj, $is_part, $file) = @_;
58 2035 ▶ die "first parameter of state_file() must be an object" unless ref $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj 0 2035 2035
59 die ref($obj), " passed to state_file() is missing the root_dir attribute"
60 2035 ▶ unless $obj->{root_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"root_dir"} 0 2035 2035
61 die ref($obj), " passed to state_file() is missing the shard attribute"
62 2035 ▶ unless $obj->{shard};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"shard"} 0 2035 2035
63  
64 2035 ▶ my $file_base = $file =~ s/\.\*$// ? "$file.$obj->{shard}.*" : "$file.$obj->{shard}";
Branch: 1 branch
BranchTrueFalseTotal
$file =~ s/\.\*$// ? : 20 2015 2035
65 2035 ▶ if ($is_part) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_part) { } 20 2015 2035
66 20 ▶ File::Spec::Functions::catfile($obj->{parts_dir} // $obj->{root_dir}, '__state__', 'parts', $file_base);
Condition33%1/3
$obj->{"parts_dir"} // $obj->{"root_dir"}
l!l&&r!l&&!r
2000
MC/DC0%0/2
$obj->{"parts_dir"} // $obj->{"root_dir"}
$obj->{"parts_dir"} $obj->{"root_dir"}
Truth table33%1/3
A // B
A= $obj->{"parts_dir"}B= $obj->{"root_dir"}
ABresult
1X1
011
000
67 } else {
68 2015 File::Spec::Functions::catfile($obj->{root_dir}, '__state__', $file_base);
69 }
70 }
71  
72 32132 ▶sub check_serializer {
Subroutine check_serializer: called CC=3 SCAR=16.7
Pod undocumented
73 32132 my ($serializer) = @_;
74  
75 32132 ▶ if ($serializer eq 'storable') {
Branch: 2 branches
BranchTrueFalseTotal
if ($serializer eq 'storable') { } 32132 0 32132
elsif ($serializer eq 'sereal') { } 0 0 0
76 32132 require Storable;
77 } elsif ($serializer eq 'sereal') {
78 0 require Sereal;
79  
80 0 $SEREAL_ENCODER = Sereal::Encoder->new({
81 snappy => 1,
82 });
83 0 $SEREAL_DECODER = Sereal::Decoder->new;
84 } else {
85 0 die "Unsupported serializer format '$serializer'";
86 }
87 }
88  
89 11830 ▶sub read_data {
Subroutine read_data: called CC=5 SCAR=26.2
Pod undocumented
90 11830 my ($serializer, $file) = @_;
91 11830 open my $fh, '<', $file;
92  
93 11830 ▶ if ($serializer eq 'storable') {
Branch: 2 branches
BranchTrueFalseTotal
if ($serializer eq 'storable') { } 11830 0 11830
elsif ($serializer eq 'sereal') { } 0 0 0
94 11830 return Storable::fd_retrieve($fh);
95 } elsif ($serializer eq 'sereal') {
96 0 my ($data, $read);
97  
98 0 ▶ 1 while ($read = $fh->read($data, 256 * 1024, length $data));
Branch: 1 branch
BranchTrueFalseTotal
$read = $fh->read($data, 262144, length $data) and do { '???' } 0 0 0
99 0 ▶ die "Error while reading Sereal-ized data from '$file'" if !defined $read;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $read 0 0 0
100  
101 0 return $SEREAL_DECODER->decode($data);
102 } else {
103 0 die "Unsupported serializer format '$serializer'";
104 }
105 }
106  
107 6058 ▶sub write_data_any {
Subroutine write_data_any: called CC=2 SCAR=6.9
Pod undocumented
108 6058 ▶ if (shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (shift()) { } 4558 1500 6058
109 4558 write_data_part(@_);
110 } else {
111 1500 write_data(@_);
112 }
113 }
114  
115 8440 ▶sub write_data_part {
Subroutine write_data_part: called CC=4 SCAR=14.2
Pod undocumented
116 8440 my ($obj, $dir, $file_base, $data) = @_;
117 8440 ▶ die "first parameter of write_data_part() must be an object" unless ref $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj 0 8440 8440
118 die ref($obj), " passed to write_data_part() is missing the serializer attribute"
119 8440 ▶ unless $obj->{serializer};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"serializer"} 0 8440 8440
120 die ref($obj), " passed to write_data_part() is missing the shard attribute"
121 8440 ▶ unless $obj->{shard};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"shard"} 0 8440 8440
122  
123 8440 my $subdir = File::Spec::Functions::catdir($dir, sprintf "%02x", $$ % 256);
124 8440 File::Path::mkpath([$subdir]);
125  
126 8440 my ($fh, $tmppath, $path) = _output_file($subdir, "$file_base.$obj->{shard}");
127 8440 _write_and_rename($obj->{serializer}, $fh, $tmppath, $path, $data);
128 }
129  
130 3438 ▶sub write_data {
Subroutine write_data: called CC=4 SCAR=14.2
Pod undocumented
131 3438 my ($obj, $dir, $file, $data) = @_;
132 3438 ▶ die "first parameter of write_data() must be an object" unless ref $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj 0 3438 3438
133 die ref($obj), " passed to write_data() is missing the serializer attribute"
134 3438 ▶ unless $obj->{serializer};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"serializer"} 0 3438 3438
135 die ref($obj), " passed to write_data() is missing the shard attribute"
136 3438 ▶ unless $obj->{shard};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"shard"} 0 3438 3438
137  
138 3438 my $file_base = "$file.$obj->{shard}";
139 3438 my $full_tmppath = File::Spec::Functions::catfile($dir, "_$file_base.tmp");
140 3438 my $full_path = File::Spec::Functions::catfile($dir, $file_base);
141 3438 open my $fh, '>', $full_tmppath;
142  
143 3438 _write_and_rename($obj->{serializer}, $fh, $full_tmppath, $full_path, $data);
144 }
145  
146 99 ▶sub write_file {
Subroutine write_file: called CC=3 SCAR=11.0
Pod undocumented
147 99 my ($dir, $file, $utf8, $data) = @_;
148 99 my ($fh, $tmppath, undef) = _output_file($dir, $file);
149  
150 99 ▶ binmode $fh, ':utf8' if $utf8;
Branch: 1 branch
BranchTrueFalseTotal
if $utf8 98 1 99
151 99 ▶ $fh->print($data) or die "Error while writing file data to '$file'";
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($data) or die "Error while writing file data to '${file}'" 99 0 99
152 99 close $fh;
153 99 rename $tmppath, File::Spec::Functions::catfile($dir, $file);
154 }
155  
156 65 ▶sub read_file {
Subroutine read_file: called CC=2 SCAR=6.9
Pod undocumented
157 65 my ($file, $utf8) = @_;
158 65 local $/;
159  
160 65 open my $fh, '<', $file;
161 65 ▶ binmode $fh, ':utf8' if $utf8;
Branch: 1 branch
BranchTrueFalseTotal
if $utf8 64 1 65
162 65 return readline $fh;
163 }
164  
165 11878 ▶sub _write_and_rename {
Subroutine _write_and_rename: called CC=5 SCAR=21.7
166 11878 my ($serializer, $fh, $tmppath, $path, $data) = @_;
167  
168 11878 ▶ if ($serializer eq 'storable') {
Branch: 2 branches
BranchTrueFalseTotal
if ($serializer eq 'storable') { } 11878 0 11878
elsif ($serializer eq 'sereal') { } 0 0 0
169 11878 ▶ Storable::nstore_fd($data, $fh)
Branch: 1 branch
BranchTrueFalseTotal
Storable::nstore_fd($data, $fh) or die "Internal error in Storable::nstore_fd to '${path}'" 11878 0 11878
170 or die "Internal error in Storable::nstore_fd to '$path'"
171 } elsif ($serializer eq 'sereal') {
172 0 ▶ $fh->print($SEREAL_ENCODER->encode($data))
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($SEREAL_ENCODER->encode($data)) or die "Error while writing Sereal-ized data to '${path}'" 0 0 0
173 or die "Error while writing Sereal-ized data to '$path'";
174 } else {
175 0 die "Unsupported serializer format '$serializer'";
176 }
177  
178 11878 close $fh;
179 11878 rename $tmppath, $path;
180 }
181  
182 8539 ▶sub _output_file {
Subroutine _output_file: called CC=4 SCAR=17.4
183 8539 my ($dir, $file_base) = @_;
184  
185 8539 for (;;) {
186 8539 my $suffix = int(rand(2 ** 31));
187 8539 my $tmppath = File::Spec::Functions::catfile($dir, "_$file_base.$suffix.tmp");
188 8539 my $path = File::Spec::Functions::catfile($dir, "$file_base.$suffix");
189  
190 8539 ▶ if (sysopen my $fh, $tmppath, O_WRONLY|O_CREAT|O_EXCL) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen my $fh, $tmppath, 193) 8539 0 8539
191 8539 ▶ if (-f $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path) 0 8539 8539
192 0 unlink $tmppath;
193 0 next;
194 }
195 8539 return ($fh, $tmppath, $path);
196 }
197 0 ▶ if ($! != Errno::EEXIST) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! != 17) 0 0 0
198 0 die "Error while opening report file: $!";
199 }
200 }
201  
202 0 die "Can't get here";
203 }
204  
205 312 ▶sub utf8_sha1 {
Subroutine utf8_sha1: called CC=2 SCAR=7.1
Pod undocumented
206 312 my ($value) = @_;
207  
208 312 ▶ utf8::encode($value) if utf8::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($value) 0 312 312
209  
210 312 return sha1($value);
211 }
212  
213 8369 ▶sub utf8_sha1_hex {
Subroutine utf8_sha1_hex: called CC=2 SCAR=7.1
Pod undocumented
214 8369 my ($value) = @_;
215  
216 8369 ▶ utf8::encode($value) if utf8::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($value) 0 8369 8369
217  
218 8369 return sha1_hex($value);
219 }
220  
221 26 1;
222  
223 __END__
« blib/lib/Devel/StatProfiler/EvalSourceStorage.pm ↑ Summary blib/lib/Devel/StatProfiler/SourceMap.pm »