blib/lib/Test/Time/HiRes.pm

filter: Statementstmt 96.0% 72 / 75 Branchbran 79.1% 19 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 2 totaltotal 91.4% 107 / 117 CC 13 SCAR 8.5
CC
13
Cov
92%
__ANON__
4.2
__ANON__
3.1
import
3.0
CRAP 2.3
↑ Summary
1 package Test::Time::HiRes;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Test::Time;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = '0.05';
11  
12 3 our $time = 0; # epoch in microseconds
13 3 our $seconds = 0; # i.e. standard epoch
14  
15 3 my $in_effect = 1;
16 3 my $imported = 0;
17  
18 37 ▶sub in_effect {
Subroutine in_effect: called CC=1 SCAR=0.0
Pod undocumented
19 37 $in_effect;
20 }
21  
22 7 ▶sub set_time {
Subroutine set_time: called CC=1 SCAR=0.0
Pod undocumented
23 7 my ( $class, $arg ) = @_;
24  
25 7 $Test::Time::time = $seconds = int($arg); # epoch time in seconds
26 7 $time = $arg * 1_000_000; # epoch time in microseconds
27 }
28  
29 # synchronise times so time is correct whether using sleep() or usleep().
30 # - assume time only goes forwards
31 # - take the highest as current epoch time
32 40 ▶sub _synchronise_times {
Subroutine _synchronise_times: called CC=3 SCAR=11.0
33  
34 40 ▶ if ( $seconds < $Test::Time::time ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Test::Time::HiRes::seconds < $Test::Time::time) { } 3 37 40
elsif ($Test::Time::HiRes::seconds > $Test::Time::time) { } 1 36 37
35  
36 # update seconds from Test::Time, but keep the fractional microsecond part
37 3 my $microseconds = _microseconds(); # part after DP
38 3 $seconds = $Test::Time::time;
39 3 $time = ( $seconds * 1_000_000 ) + $microseconds;
40 }
41 elsif ( $seconds > $Test::Time::time ) {
42 1 $Test::Time::time = $seconds;
43 }
44 }
45  
46 13 ▶sub _microseconds {
Subroutine _microseconds: called CC=2 SCAR=7.2
47 13 ▶ return 0 unless $time;
Branch: 1 branch
BranchTrueFalseTotal
unless $Test::Time::HiRes::time 0 13 13
48 13 return $time % 1_000_000;
49 }
50  
51 23 ▶sub _float {
Subroutine _float: called CC=2 SCAR=7.1
52 23 ▶ return 0 unless $time;
Branch: 1 branch
BranchTrueFalseTotal
unless $Test::Time::HiRes::time 0 23 23
53 23 my $t = $time / 1_000_000;
54 23 return sprintf( "%.6f", $t ) + 0;
55 }
56  
57 4 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
58 4 my ( $class, %opts ) = @_;
59  
60 4 $in_effect = 1;
61 4 Test::Time->import; # make sure Test::Time is enabled, in case
62 # there was a call to ->unimport earlier
63  
64 4 ▶ return if $imported;
Branch: 1 branch
BranchTrueFalseTotal
if $imported 1 3 4
65  
66 # If time set on import then use it and update
67 # Test::Time, otherwise use $Test::Time::time
68 3 ▶ if ( defined $opts{time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'time'}) { } 2 1 3
69 2 $class->set_time( $opts{time} );
70 }
71 else {
72 1 $seconds = $Test::Time::time;
73 1 $time = $seconds * 1_000_000;
74 }
75  
76 3 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 # keep copies of the original subroutines
79 3 my $sub_time = \&Time::HiRes::time;
80 3 my $sub_usleep = \&Time::HiRes::usleep;
81 3 my $sub_gettimeofday = \&Time::HiRes::gettimeofday;
82  
83 16 ▶ *Time::HiRes::time = sub() {
Subroutine __ANON__: called CC=2 SCAR=6.9
84 16 ▶ if (in_effect) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_effect()) { } 15 1 16
85 15 _synchronise_times();
86  
87 15 return _float();
88 }
89 else {
90 1 return $sub_time->();
91 }
92 3 };
93  
94 5 ▶ *Time::HiRes::usleep = sub($) {
Subroutine __ANON__: called CC=4 SCAR=14.3
95  
96 5 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 5 5
97 0 return $sub_usleep->(); # always give "no argument" error
98 }
99  
100 5 ▶ if (in_effect) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_effect()) { } 5 0 5
101 5 my $sleep = shift;
102  
103 5 _synchronise_times();
104  
105 5 ▶ return 0 unless $sleep;
Branch: 1 branch
BranchTrueFalseTotal
unless $sleep 1 4 5
106  
107 4 $time = $time + $sleep;
108 4 $seconds = int( $time / 1_000_000 );
109  
110 4 _synchronise_times();
111  
112 4 note "sleep $sleep";
113  
114 4 return $sleep;
115 }
116 else {
117 0 return $sub_usleep->(shift);
118 }
119 3 };
120  
121 16 ▶ *Time::HiRes::gettimeofday = sub() {
Subroutine __ANON__: called CC=3 SCAR=11.4
122  
123 16 ▶ if (in_effect) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_effect()) { } 16 0 16
124 16 _synchronise_times();
125  
126 16 ▶ return wantarray ? ( $seconds, _microseconds() ) : _float();
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 8 16
127 }
128 else {
129 0 return $sub_gettimeofday->();
130 }
131 3 };
132  
133 3 $imported++;
134 }
135  
136 1 ▶sub unimport {
Subroutine unimport: called CC=1 SCAR=0.0
137 1 $in_effect = 0;
138 1 Test::Time->unimport();
139 }
140  
141 3 1;
142  
143 __END__
↑ Summary