File Coverage

blib/lib/Git/Deploy/Timing.pm
Criterion Covered Total %
statement 15 42 35.7
branch 0 12 0.0
condition n/a
subroutine 5 8 62.5
pod 0 3 0.0
total 20 65 30.7


line stmt bran cond sub pod time code
1             package Git::Deploy::Timing;
2 1     1   1461 use strict;
  1         2  
  1         55  
3 1     1   6 use warnings FATAL => "all";
  1         2  
  1         53  
4 1     1   18 use Exporter 'import';
  1         2  
  1         38  
5 1     1   1318 use Time::HiRes;
  1         12003  
  1         5  
6              
7             our @EXPORT = qw(
8             push_timings
9             should_write_timings
10             write_timings
11             );
12              
13             our (@timings, $write_timings, @real_argv);
14             BEGIN {
15             # @timings is a set of 4-tuples: [ $tag, $time_stamp, $time_since_last_step, $time_since_start_tag ]
16 1     1   269 @timings= (
17             [
18             'gdt_start', # tagname
19             $^T, # process start time (set by Perl at perl startup)
20             -1, # time since last step (-1 == Not Applicable)
21             -1, # time since start tag - only relevant on _end tags (-1 == Not Applicable)
22             ]
23             );
24             # if this is true then we will write a timings file at process conclusion
25 1         3 $write_timings= 0;
26 1         923 @real_argv= @ARGV;
27             }
28              
29             sub should_write_timings {
30 0     0 0   $write_timings= 1;
31             }
32              
33             sub push_timings {
34 0     0 0   my $tag= shift;
35 0           $tag =~ s/[^a-zA-Z0-9_]+/_/g; # strip any bogosity from the tag
36 0           my $time= Time::HiRes::time();
37 0           my $elapsed= -1;
38 0 0         if ($tag=~/_end\z/) {
39 0           (my $start= $tag)=~s/_end\z/_start/;
40 0           foreach my $timing (@timings) {
41 0 0         next unless $timing->[0] eq $start;
42 0           $elapsed= $time - $timing->[1];
43 0           last;
44             }
45             }
46 0           push @timings, [ $tag, $time, $time - $timings[-1][1], $elapsed ];
47             }
48              
49             sub write_timings {
50 0 0   0 0   return unless $write_timings;
51             # Do we even have to write the timing data?
52 0           require Git::Deploy;
53 0 0         return unless Git::Deploy::get_config_bool("log-timing-data",'false');
54             # Where do we write it?
55 0           my $log_directory;
56 0 0         unless ( $log_directory = Git::Deploy::log_directory() ) {
57 0           warn "Not writing timing data: 'log_directory' has not been configured.";
58 0           return;
59             }
60              
61 0           my $timing_file= "$log_directory/timing_gdt-$timings[0][1].txt";
62             open my $fh, '>', $timing_file
63 0 0         or do {
64 0           warn "Not writing timing data: failed to open timing file '$timing_file': $!";
65 0           return;
66             };
67 0           print $fh "# ". join("\t",$0,@real_argv),"\n";
68 0           for my $timing (@timings) {
69 0           print $fh join("\t",@$timing),"\n";
70             }
71 0           close $fh;
72             }
73              
74             1;