Example/Latency.pm untested

filter: Statementstmt 0.0% 0 / 49 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 89 CC 3 SCAR 12.0
CC
3
Cov
0%
clock_name
6.0
in_child
6.0
tick
2.0
CRAP 3.3
1 ########################################################################
2 # housekeeping
3 ########################################################################
4 package LinkedList::Single::Example::Latency v2.0.0;
5 use v5.40;
6  
7 use Exporter qw( import );
8 use Time::HiRes qw( clock_gettime CLOCK_MONOTONIC CLOCK_THREAD_CPUTIME_ID );
9  
10 our @EXPORT_OK
11 = qw
12 (
13 clock_floor
14 clock_name
15 in_child
16 sample
17 summary
18 row
19 tick
20 );
21  
22 ########################################################################
23 # why this is not best_of(): an average hides exactly what a latency
24 # bound is about. every call is timed on its own, and what is reported is
25 # how the calls are distributed (median, 99th, 99.9th percentile) and the
26 # one slowest call, with its position in the run so that a spike can be
27 # traced to what happened there (a reallocation, a split).
28 #
29 # the samples go into an array that has been extended to its final size
30 # before the first call is timed, so that the harness does not grow an
31 # array in the middle of a measurement and report it as the code's.
32 # the clock is monotonic.
33 ########################################################################
34  
35 ########################################################################
36 # the clock. wall time includes every moment the process is not running:
37 # the scheduler takes the cpu from it, and the longest call in any run is
38 # then a measure of the machine and not of the code (the empty loop
39 # below shows it: its slowest call is hundreds of microseconds). the
40 # default is the time this thread is actually running, which still counts
41 # a page fault, a reallocation or a memmove, and leaves out being
42 # descheduled. LATENCY_CLOCK=wall selects wall time.
43 ########################################################################
44  
45 my $wall = ( $ENV{ LATENCY_CLOCK } // '' ) eq 'wall';
46  
47 my $clock = $wall ? CLOCK_MONOTONIC : CLOCK_THREAD_CPUTIME_ID;
48  
49 sub tick()
50 {
51 clock_gettime( $clock )
52 }
53  
54 sub clock_name()
55 {
56 $wall ? 'wall clock' : 'thread cpu time'
57 }
58  
59 # time $count calls of $code->( $i ) one by one. returns the list of
60 # seconds taken.
61  
62 sub sample( $count, $code )
63 {
64 my @took;
65  
66 $#took = $count - 1;
67  
68 for my $i ( 0 .. $count - 1 )
69 {
70 my $t0 = tick();
71  
72 $code->( $i );
73  
74 $took[ $i ] = tick() - $t0;
75 }
76  
77 \@took
78 }
79  
80 # what the timing itself costs: the same loop around an empty call.
81 # the median of this is the smallest number any report can show.
82  
83 sub clock_floor( $count = 200_000 )
84 {
85 my $took = sample( $count, sub {} );
86  
87 summary( $took )
88 }
89  
90 # percentiles, the slowest call and where it was, in microseconds.
91  
92 sub summary( $took )
93 {
94 my @order = sort { $a <=> $b } @$took;
95 my $count = @order;
96  
97 my $at
98 = sub( $fraction )
99 {
100 $order[ int( $fraction * ( $count - 1 ) ) ]
101 };
102  
103 # the position of the slowest call.
104  
105 my $slow = 0;
106  
107 $took->[ $_ ] > $took->[ $slow ]
108 and $slow = $_
109 for 0 .. $#$took;
110  
111 my $total = 0;
112  
113 $total += $_ for @order;
114  
115 return
116 {
117 count => $count
118 , mean => 1e6 * $total / $count
119 , p50 => 1e6 * $at->( 0.50 )
120 , p99 => 1e6 * $at->( 0.99 )
121 , p999 => 1e6 * $at->( 0.999 )
122 , max => 1e6 * $order[ -1 ]
123 , at => $slow
124 }
125 }
126  
127 # $code run in a child process, which returns what it prints on one
128 # line: the numbers come back as a list. perl does not give freed heap
129 # back, so a run that follows another in the same process starts from
130 # the other's high-water mark; each case gets a process of its own.
131  
132 sub in_child( $code )
133 {
134 pipe my $r, my $w
135 or die "Botched pipe: $!\n";
136  
137 my $pid = fork // die "Botched fork: $!\n";
138  
139 if( ! $pid )
140 {
141 close $r;
142  
143 say $w join ' ' => $code->();
144  
145 close $w;
146  
147 exit 0;
148 }
149  
150 close $w;
151  
152 my $line = <$r>;
153  
154 close $r;
155 waitpid $pid, 0;
156  
157 defined $line
158 or die "Botched child: no result\n";
159  
160 chomp $line;
161  
162 split ' ', $line
163 }
164  
165 # one table row: the name and the five numbers of a summary.
166  
167 sub row( $name, $s )
168 {
169 sprintf '%-18s %9.2f %9.2f %9.2f %9.2f %11.1f %9d'
170 => $name
171 , $s->@{ qw( mean p50 p99 p999 max at ) }
172 }
173  
174 1
175 __END__