blib/lib/Time/HiRes/Sleep/Until.pm

filter: Statementstmt 94.7% 36 / 38 Branchbran 62.5% 5 / 8 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 90.9% 10 / 11 Podpod 100% 6 / 6 totaltotal 86.1% 62 / 72 CC 8 SCAR 9.4
CC
8
Cov
87%
sleep
6.0
mark
3.1
epoch
3.0
CRAP 2.6
↑ Summary
1 package Time::HiRes::Sleep::Until;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw{Package::New};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Time::HiRes qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Math::Round qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.10';
9  
10 =head1 NAME
11  
12 Time::HiRes::Sleep::Until - Provides common ways to sleep until...
13  
14 =head1 SYNOPSIS
15  
16 use Time::HiRes::Sleep::Until;
17 my $su = Time::HiRes::Sleep::Until->new;
18 my $slept = $su->epoch($epoch); # epoch is a calculated time + $seconds
19 my $slept = $su->mark(20); # sleep until 20 second mark of the clock :00, :20, or :40
20 my $slept = $su->second(45); # sleep until 45 seconds after the minute
21  
22  
23 =head1 DESCRIPTION
24  
25 Sleep Until provides sleep wrappers for sleep functions that I commonly need. These methods are simply wrappers around L<Time::HiRes> and L<Math::Round>.
26  
27 We use this package to make measurements at the same time within the minute for integration with RRDtool.
28  
29 =head1 USAGE
30  
31 use strict;
32 use warnings;
33 use DateTime;
34 use Time::HiRes::Sleep::Until;
35 my $su = Time::HiRes::Sleep::Until->new;
36 do {
37 print DateTime->now, "\n"; #make a measurment three times a minute
38 } while ($su->mark(20));
39  
40 Perl One liner
41  
42 perl -MTime::HiRes::Sleep::Until -e 'printf "Slept: %s\n", Time::HiRes::Sleep::Until->new->top'
43  
44 =head1 CONSTRUCTOR
45  
46 =head2 new
47  
48 use Time::HiRes::Sleep::Until;
49 my $su = Time::HiRes::Sleep::Until->new;
50  
51 =head1 METHODS
52  
53 =head2 epoch
54  
55 Sleep until provided epoch in float seconds.
56  
57 while ($CONTINUE) {
58 my $sleep_epoch = $su->time + 60/8;
59 do_work(); #run process that needs to run back to back but not more than 8 times per minute
60 $su->epoch($sleep_epoch); #sleep(7.5 - runtime). if runtime > 7.5 seconds does not sleep
61 }
62  
63 =cut
64  
65 8 ▶sub epoch {
Subroutine epoch: called CC=3 SCAR=11.0
Pod documented
66 8 my $self = shift;
67 8 ▶ my $epoch = shift || 0; #default is 1970-01-01 00:00
Condition50%1/2
shift() || 0
l!l
80
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
68 8 my $sleep = $epoch - Time::HiRes::time();
69 8 ▶ return $sleep <= 0 ? 0 : Time::HiRes::sleep($sleep);
Branch: 1 branch
BranchTrueFalseTotal
$sleep <= 0 ? : 1 7 8
70 }
71  
72 =head2 mark
73  
74 Sleep until next second mark;
75  
76 my $slept = $su->mark(20); # 20 second mark, i.e. 3 times a minute on the 20s
77 my $slept = $su->mark(10); # 10 second mark, i.e. 6 times a minute on the 10s
78 my $slept = $su->mark(6); # 6 second mark, i.e. 10 times a minute on 0,6,12,...
79  
80 =cut
81  
82 2 ▶sub mark {
Subroutine mark: called CC=3 SCAR=11.2
Pod documented
83 2 my $time = Time::HiRes::time();
84 2 my $self = shift;
85 2 ▶ my $mark = shift || 0;
Condition50%1/2
shift() || 0
l!l
20
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
86 2 ▶ die("Error: mark requires parameter to be greater than zero.") unless $mark > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $mark > 0 0 2 2
87 2 my $epoch = Math::Round::nhimult($mark => $time); #next mark
88 2 return $self->epoch($epoch);
89 }
90  
91 =head2 second
92  
93 Sleep until the provided seconds after the minute
94  
95 my $slept = $su->second(0); #sleep until top of minute
96 my $slept = $su->second(30); #sleep until bottom of minute
97  
98 =cut
99  
100 4 ▶sub second {
Subroutine second: called CC=3 SCAR=11.0
Pod documented
101 4 my $time = Time::HiRes::time();
102 4 my $self = shift;
103 4 ▶ my $second = shift || 0; #default is top of the minute
Condition100%2/2
shift() || 0
l!l
13
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
104 4 my $min_next = Math::Round::nhimult(60 => $time);
105 4 my $min_last = $min_next - 60;
106 4 ▶ return $time < $min_last + $second
Branch: 1 branch
BranchTrueFalseTotal
$time < $min_last + $second ? : 1 3 4
107 ? $self->epoch($min_last + $second)
108 : $self->epoch($min_next + $second);
109 }
110  
111 =head2 top
112  
113 Sleep until the top of the minute
114  
115 my $slept = $su->top; #alias for $su->second(0);
116  
117 =cut
118  
119 2 ▶sub top {
Subroutine top: called CC=1 SCAR=0.0
Pod documented
120 2 my $self = shift;
121 2 return $self->second(0);
122 }
123  
124 =head2 time
125  
126 Method to access Time::HiRes time without another import.
127  
128 =cut
129  
130 1 ▶sub time {return Time::HiRes::time()};
Subroutine time: called CC=1 SCAR=0.0
Pod documented
131  
132 =head2 sleep
133  
134 Method to access Time::HiRes sleep without another import.
135  
136 =cut
137  
138 0 ▶sub sleep {
Subroutine sleep: not called CC=2 SCAR=17.9
Pod documented
139 0 my $self = shift;
140 0 ▶ return @_ ? Time::HiRes::sleep($_[0]) : CORE::sleep();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
141 }
142  
143 =head1 LIMITATIONS
144  
145 The mathematics add a small amount of delay for which we do not account. Testing routinely passes with 100th of a second accuracy and typically with millisecond accuracy.
146  
147 =head1 BUGS
148  
149 Please log on GitHub
150  
151 =head1 AUTHOR
152  
153 Michael R. Davis
154  
155 =head1 COPYRIGHT
156  
157 MIT License
158  
159 Copyright (c) 2025 Michael R. Davis
160  
161 =head1 SEE ALSO
162  
163 L<Time::HiRes>, L<Math::Round>
164  
165 =cut
166  
167 1 1;
↑ Summary