bench/run-timer-microbench.pl

filter: Statementstmt 95.6% 152 / 159 Branchbran 50.0% 25 / 50 Conditioncond 40.0% 8 / 20 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 93.9% 31 / 33 Podpod n/a totaltotal 78.9% 217 / 275 CC 29 SCAR 13.8
CC
29
Cov
81%
timed
10.4
measurement
8.6
process_cpu_clock
6.0
CRAP 4.0
« bench/run-resolver-microbench.pl ↑ Summary blib/lib/Linux/Event/_Resolver.pm »
1 #!/usr/bin/env perl
2 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use FindBin qw($Bin);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON::PP qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC CLOCK_PROCESS_CPUTIME_ID);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use lib "$Bin/../blib/lib", "$Bin/../blib/arch", "$Bin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Linux::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Linux::Event::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Linux::Event::Kernel::Timer;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 {
20 package Linux::Event::Bench::Timer;
21 1 ▶ use parent 'Linux::Event::Kernel::Timer';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶ sub on_timer ($timer) {
Subroutine on_timer: called CC=2 SCAR=7.1
23 2 my $state = $timer->data;
24 2 $state->{completed}++;
25 $state->{loop}->stop
26 2 ▶ if $state->{completed} == $state->{target};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"completed"} == $state->{"target"} 2 0 2
27 }
28 }
29  
30 1 my @counts = (1_000, 10_000, 100_000);
31 1 my $repeats = 5;
32 1 my $json_path = 'bench/results/timer-microbench.json';
33 1 my $cpu_clock = 'auto';
34 1 my $help = 0;
35  
36 GetOptions(
37 1 ▶ 'counts=s' => sub { @counts = split /,/, $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 1 ▶ 'repeats=i' => \$repeats,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("counts=s", sub { @counts = split(/,/u, $_[1], 0); } , "repeats=i", \$repeats, "json=s", \$json_path, "cpu-clock=s", \$cpu_clock, "help", \$help) or usage(2) 1 0 1
39 'json=s' => \$json_path,
40 'cpu-clock=s' => \$cpu_clock,
41 'help' => \$help,
42 ) or usage(2);
43 1 ▶usage(0) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 1 1
44 die "counts must be positive integers\n"
45 1 ▶ if !@counts || grep { !/\A[1-9]\d*\z/ } @counts;
Branch: 1 branch
BranchTrueFalseTotal
if not @counts or grep {not /\A[1-9]\d*\z/u;} @counts 0 1 1
Condition33%1/3
not @counts or grep {not /\A[1-9]\d*\z/u;} @counts
l!l&&r!l&&!r
001
MC/DC0%0/2
not @counts or grep {not /\A[1-9]\d*\z/u;} @counts
not @counts grep {not /\A[1-9]\d*\z/u;} @counts
Truth table33%1/3
not(A) or B
A= not @countsB= grep {not /\A[1-9]\d*\z/u;} @counts
ABresult
1X1
011
000
46 1 ▶die "repeats must be positive\n" if $repeats < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $repeats < 1 0 1 1
47 1 ▶die "cpu-clock must be auto or times\n"
Branch: 1 branch
BranchTrueFalseTotal
if $cpu_clock ne "auto" and $cpu_clock ne "times" 0 1 1
Condition33%1/3
$cpu_clock ne "auto" and $cpu_clock ne "times"
!ll&&!rl&&r
010
MC/DC0%0/2
$cpu_clock ne "auto" and $cpu_clock ne "times"
$cpu_clock ne "auto" $cpu_clock ne "times"
Truth table33%1/3
A and B
A= $cpu_clock ne "auto"B= $cpu_clock ne "times"
ABresult
0X0
100
111
48 if $cpu_clock ne 'auto' && $cpu_clock ne 'times';
49  
50 1 my @modes = qw(lifecycle reschedule expiration);
51 1 my @records;
52 1 say "Linux::Event Timer microbenchmark version=$Linux::Event::VERSION";
53 1 say 'counts=' . join(',', @counts) . " repeats=$repeats";
54  
55 1 for my $count (@counts) {
56 1 ▶ warmup($count < 1_000 ? $count : 1_000);
Branch: 1 branch
BranchTrueFalseTotal
$count < 1000 ? : 1 0 1
57 1 for my $repeat (1 .. $repeats) {
58 1 my $offset = ($repeat - 1) % @modes;
59 1 my @order = (@modes[$offset .. $#modes], @modes[0 .. $offset - 1]);
60 1 for my $mode (@order) {
61 3 my $row = run_mode($mode, $count);
62 3 $row->{mode} = $mode;
63 3 $row->{count} = 0 + $count;
64 3 $row->{repeat} = $repeat;
65 3 push @records, $row;
66 printf "%-12s timers=%7d repeat=%d %12.1f/s %9s cpu us/op [%s]\n",
67 $mode, $count, $repeat,
68 $row->{operations_per_second},
69 measurement_text($row->{cpu_us_per_operation}),
70 3 $row->{cpu_clock};
71 }
72 }
73 }
74  
75 1 my @summary;
76 1 for my $count (@counts) {
77 1 for my $mode (@modes) {
78 my @set = grep {
79 3 ▶ $_->{count} == $count && $_->{mode} eq $mode
Condition66%2/3
$_->{'count'} == $count && $_->{'mode'} eq $mode
!ll&&!rl&&r
063
MC/DC50%1/2
$_->{'count'} == $count && $_->{'mode'} eq $mode
$_->{'count'} == $count $_->{'mode'} eq $mode
Truth table66%2/3
A && B
A= $_->{'count'} == $countB= $_->{'mode'} eq $mode
ABresult
0X0
100
111
80 } @records;
81 push @summary, {
82 mode => $mode,
83 count => 0 + $count,
84 operations_per_second => median(
85 3 map { $_->{operations_per_second} } @set
86 ),
87 cpu_us_per_operation => median_optional(
88 3 map { $_->{cpu_us_per_operation} } @set
89 ),
90 cpu_clock_sources => [
91 3 sort keys %{ { map { $_->{cpu_clock} => 1 } @set } }
92 ],
93 };
94 }
95 }
96  
97 1 say "\nMedian summary";
98 1 printf "%-12s %9s %14s %14s\n", 'mode', 'timers', 'operations/s', 'cpu us/op';
99 1 for my $row (@summary) {
100 printf "%-12s %9d %14.1f %14s\n",
101 $row->{mode}, $row->{count},
102 $row->{operations_per_second},
103 3 measurement_text($row->{cpu_us_per_operation});
104 }
105  
106 my $report = {
107 benchmark => 'linux-event-timer-microbench',
108 benchmark_contract_version => 2,
109 linux_event_version => "$Linux::Event::VERSION",
110 configuration => {
111 1 counts => [map { 0 + $_ } @counts],
112 repeats => $repeats,
113 modes => \@modes,
114 cpu_clock => $cpu_clock,
115 },
116 records => \@records,
117 summary => \@summary,
118 };
119 1 write_json($json_path, $report);
120 1 say "\nWrote $json_path";
121  
122 1 ▶sub warmup ($count) {
Subroutine warmup: called CC=1 SCAR=0.0
123 1 timer_lifecycle($count);
124 1 timer_reschedule($count);
125 1 timer_expiration($count);
126 1 return;
127 }
128  
129 3 ▶sub run_mode ($mode, $count) {
Subroutine run_mode: called CC=3 SCAR=11.0
130 3 ▶ return timer_lifecycle($count) if $mode eq 'lifecycle';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "lifecycle" 1 2 3
131 2 ▶ return timer_reschedule($count) if $mode eq 'reschedule';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "reschedule" 1 1 2
132 1 return timer_expiration($count);
133 }
134  
135 2 ▶sub timer_lifecycle ($count) {
Subroutine timer_lifecycle: called CC=1 SCAR=0.0
136 2 my $loop = Linux::Event::Loop->new;
137 2 ▶ my ($wall, $cpu, $cpu_source) = timed(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
138 2 for (1 .. $count) {
139 2 my $timer = $loop->add(
140 Linux::Event::Bench::Timer->new(after => 3_600)
141 );
142 2 $timer->cancel;
143 }
144 2 });
145 2 return measurement($count, $wall, $cpu, $cpu_source);
146 }
147  
148 2 ▶sub timer_reschedule ($count) {
Subroutine timer_reschedule: called CC=2 SCAR=6.9
149 2 my $loop = Linux::Event::Loop->new;
150 my @timers = map {
151 2 $loop->add(Linux::Event::Bench::Timer->new(after => 3_600))
152 } 1 .. $count;
153 2 my $at = 0;
154 2 ▶ my ($wall, $cpu, $cpu_source) = timed(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
155 2 for my $timer (@timers) {
156 2 $timer->reschedule(after => 3_600 + (++$at % 100) / 1_000);
157 }
158 2 });
159 2 $_->cancel for @timers;
160 2 return measurement($count, $wall, $cpu, $cpu_source);
161 }
162  
163 2 ▶sub timer_expiration ($count) {
Subroutine timer_expiration: called CC=3 SCAR=11.0
164 2 my $loop = Linux::Event::Loop->new;
165 2 my $state = { loop => $loop, completed => 0, target => $count };
166 2 for (1 .. $count) {
167 2 $loop->add(Linux::Event::Bench::Timer->new(
168 after => 0,
169 data => $state,
170 ));
171 }
172 2 ▶ my ($wall, $cpu, $cpu_source) = timed(sub { $loop->run });
Subroutine __ANON__: called CC=1 SCAR=0.0
173 die "expiration completed $state->{completed} of $count Timers\n"
174 2 ▶ if $state->{completed} != $count;
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"completed"} != $count 0 2 2
175 2 return measurement($count, $wall, $cpu, $cpu_source);
176 }
177  
178 6 ▶sub timed ($code) {
Subroutine timed: called CC=8 SCAR=23.4
179 6 my $wall_start = clock_gettime(CLOCK_MONOTONIC);
180 6 ▶ my $cpu_start = $cpu_clock eq 'auto' ? process_cpu_clock() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cpu_clock eq 'auto' ? : 0 6 6
181 6 my $times_start = process_times();
182 6 $code->();
183 6 my $wall = clock_gettime(CLOCK_MONOTONIC) - $wall_start;
184 6 ▶ my $cpu_end = $cpu_clock eq 'auto' ? process_cpu_clock() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cpu_clock eq 'auto' ? : 0 6 6
185 6 my $times_cpu = process_times() - $times_start;
186 6 ▶ my $process_cpu = defined($cpu_start) && defined($cpu_end)
Branch: 1 branch
BranchTrueFalseTotal
defined $cpu_start && defined $cpu_end ? : 0 6 6
Condition33%1/3
defined $cpu_start and defined $cpu_end
!ll&&!rl&&r
600
MC/DC0%0/2
defined $cpu_start and defined $cpu_end
defined $cpu_start defined $cpu_end
Truth table33%1/3
A and B
A= defined $cpu_startB= defined $cpu_end
ABresult
0X0
100
111
187 ? $cpu_end - $cpu_start
188 : undef;
189  
190 6 ▶ return ($wall, $process_cpu, 'clock_gettime')
Branch: 1 branch
BranchTrueFalseTotal
if defined $process_cpu and $process_cpu > 0 0 6 6
Condition33%1/3
defined $process_cpu and $process_cpu > 0
!ll&&!rl&&r
600
MC/DC0%0/2
defined $process_cpu and $process_cpu > 0
defined $process_cpu $process_cpu > 0
Truth table33%1/3
A and B
A= defined $process_cpuB= $process_cpu > 0
ABresult
0X0
100
111
191 if defined($process_cpu) && $process_cpu > 0;
192 6 ▶ return ($wall, $times_cpu, 'times') if $times_cpu > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $times_cpu > 0 0 6 6
193 6 return ($wall, undef, 'unavailable');
194 }
195  
196 6 ▶sub measurement ($operations, $wall, $cpu, $cpu_source = undef) {
Subroutine measurement: called CC=6 SCAR=21.5
197 6 ▶ die "benchmark wall clock returned a non-positive duration\n"
Branch: 1 branch
BranchTrueFalseTotal
if $wall <= 0 0 6 6
198 if $wall <= 0;
199 6 ▶ $cpu_source //= defined($cpu) ? 'clock_gettime' : 'unavailable';
Branch: 1 branch
BranchTrueFalseTotal
defined $cpu ? : 0 0 0
Condition50%1/2
$cpu_source //= defined $cpu ? "clock_gettime" : "unavailable"
l!l
60
MC/DC0%0/1
$cpu_source //= defined $cpu ? "clock_gettime" : "unavailable"
$cpu_source
Truth table50%1/2
A
A= $cpu_source
Aresult
11
00
200 return {
201 6 ▶ operations => $operations,
Branch: 1 branch
BranchTrueFalseTotal
defined $cpu ? : 0 6 6
202 elapsed_seconds => $wall,
203 cpu_seconds => $cpu,
204 operations_per_second => $operations / $wall,
205 cpu_us_per_operation => defined($cpu)
206 ? $cpu * 1_000_000 / $operations
207 : undef,
208 cpu_clock => $cpu_source,
209 };
210 }
211  
212 0 ▶sub process_cpu_clock () {
Subroutine process_cpu_clock: not called CC=2 SCAR=17.9
213 0 my $value = eval { clock_gettime(CLOCK_PROCESS_CPUTIME_ID) };
214 0 ▶ return defined($value) ? 0 + $value : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
215 }
216  
217 12 ▶sub process_times () {
Subroutine process_times: called CC=1 SCAR=0.0
218 12 my ($user, $system) = times;
219 12 return $user + $system;
220 }
221  
222 3 ▶sub median (@values) {
Subroutine median: called CC=2 SCAR=7.6
223 0 @values = sort { $a <=> $b } @values;
224 3 my $middle = int(@values / 2);
225 3 ▶ return @values % 2
Branch: 1 branch
BranchTrueFalseTotal
@values % 2 ? : 3 0 3
226 ? $values[$middle]
227 : ($values[$middle - 1] + $values[$middle]) / 2;
228 }
229  
230 3 ▶sub median_optional (@values) {
Subroutine median_optional: called CC=2 SCAR=7.6
231 3 @values = grep { defined } @values;
232 3 ▶ return undef if !@values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 3 0 3
233 0 return median(@values);
234 }
235  
236 6 ▶sub measurement_text ($value) {
Subroutine measurement_text: called CC=2 SCAR=7.6
237 6 ▶ return defined($value) ? sprintf('%.3f', $value) : 'n/a';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 6 6
238 }
239  
240 1 ▶sub write_json ($path, $document) {
Subroutine write_json: called CC=5 SCAR=17.5
241 1 my $directory = dirname($path);
242 1 ▶ make_path($directory) if $directory ne '.' && !-d $directory;
Branch: 1 branch
BranchTrueFalseTotal
if $directory ne "." and not -d $directory 0 1 1
Condition33%1/3
$directory ne "." and not -d $directory
!ll&&!rl&&r
010
MC/DC0%0/2
$directory ne "." and not -d $directory
$directory ne "." not -d $directory
Truth table33%1/3
A and not(B)
A= $directory ne "."B= not -d $directory
ABresult
0X0
100
111
243 1 ▶ open my $fh, '>', $path or die "open $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $path or die "open ${path}: $!\n" 1 0 1
244 1 print {$fh} JSON::PP->new->canonical->pretty->encode($document);
245 1 ▶ close $fh or die "close $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "close ${path}: $!\n" 1 0 1
246 1 return;
247 }
248  
249 0 ▶sub usage ($status) {
Subroutine usage: not called CC=1 SCAR=6.9
250 0 print <<'USAGE';
251 Usage:
252 perl -Mblib bench/run-timer-microbench.pl [options]
253  
254 Options:
255 --counts LIST comma-separated Timer counts (default 1000,10000,100000)
256 --repeats N repeats per mode and count (default 5)
257 --json PATH JSON output path
258 --cpu-clock NAME auto (default) or times
259 USAGE
260 0 exit $status;
261 }
« bench/run-resolver-microbench.pl ↑ Summary blib/lib/Linux/Event/_Resolver.pm »