bench/run-first-class-callback-construction-bench.pl

filter: Statementstmt 92.7% 167 / 180 Branchbran 65.2% 30 / 46 Conditioncond 75.0% 9 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 76.9% 30 / 39 Podpod n/a totaltotal 84.5% 241 / 285 CC 12 SCAR 6.4
CC
12
Cov
87%
usage
6.0
paired_delta
3.0
finish_if_done
3.0
CRAP 1.9
« bench/run-process-microbench.pl ↑ Summary blib/lib/Linux/Event/Error.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 Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use POSIX qw(strftime uname);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Socket qw(SOL_SOCKET SO_LINGER);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Linux::Event::IO::Sock::Listener;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Linux::Event::IO::Sock::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Linux::Event::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 my @styles = qw(subclass_method listener_shared_closure fresh_closure);
19 1 my $clients = 100;
20 1 my $connections = 10_000;
21 1 my $repeats = 9;
22 1 my $timeout = 30;
23 1 my $json_path;
24 my $help;
25  
26 GetOptions(
27 0 ▶ 'styles=s' => sub { @styles = split /,/, $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 1 ▶ 'clients=i' => \$clients,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("styles=s", sub { @styles = split(/,/u, $_[1], 0); } , "clients=i", \$clients, "connections=i", \$connections, "repeats=i", \$repeats, "timeout=f", \$timeout, "json=s", \$json_path, "help", \$help) or usage(2) 1 0 1
29 'connections=i' => \$connections,
30 'repeats=i' => \$repeats,
31 'timeout=f' => \$timeout,
32 'json=s' => \$json_path,
33 'help' => \$help,
34 ) or usage(2);
35 1 ▶usage(0) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 1 1
36  
37 1 my %known = map { $_ => 1 } qw(
38 subclass_method listener_shared_closure fresh_closure
39 );
40 1 die "unknown callback style: $_\n" for grep { !$known{$_} } @styles;
41 die "callback styles must not repeat\n"
42 1 ▶ if do { my %seen; grep { $seen{$_}++ } @styles };
Branch: 1 branch
BranchTrueFalseTotal
if do { my %seen; grep {$seen{$_}++;} @styles } 0 1 1
43 1 ▶die "clients must be positive\n" if $clients < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $clients < 1 0 1 1
44 1 ▶die "connections must be positive\n" if $connections < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $connections < 1 0 1 1
45 1 ▶die "repeats must be positive\n" if $repeats < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $repeats < 1 0 1 1
46 1 ▶die "timeout must be positive\n" if $timeout <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= 0 0 1 1
47  
48 1 our $FRESH_CLOSURES = 0;
49 0 ▶our $SHARED_CALLBACK = sub ($stream, $bytes) { return };
Subroutine __ANON__: not called CC=1 SCAR=1.2
50  
51 {
52 package Linux::Event::Bench::CallbackMethodStream;
53 1 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 0 ▶ sub on_data ($stream, $bytes) { return }
Subroutine on_data: not called CC=1 SCAR=6.9
55 20 ▶ sub on_ready ($stream) { main::accepted_ready($stream) }
Subroutine on_ready: called CC=1 SCAR=0.0
56 }
57  
58 {
59 1 package Linux::Event::Bench::CallbackClosureStream;
60 1 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 20 ▶ sub on_ready ($stream) { main::accepted_ready($stream) }
Subroutine on_ready: called CC=1 SCAR=0.0
62 }
63  
64 {
65 1 package Linux::Event::Bench::CallbackFreshStream;
66 1 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
67 0 ▶ sub on_data ($stream, $bytes) { return }
Subroutine on_data: not called CC=1 SCAR=6.9
68 20 ▶ sub new ($class, %option) {
Subroutine new: called CC=1 SCAR=0.2
69 20 $main::FRESH_CLOSURES++;
70 0 ▶ $option{on_data} = sub ($stream, $bytes) { return };
Subroutine __ANON__: not called CC=1 SCAR=1.2
71 20 return $class->SUPER::new(%option);
72 }
73 20 ▶ sub on_ready ($stream) { main::accepted_ready($stream) }
Subroutine on_ready: called CC=1 SCAR=0.0
74 }
75  
76 {
77 1 package Linux::Event::Bench::CallbackListener;
78 1 ▶ use parent 'Linux::Event::IO::Sock::Listener';
Subroutine BEGIN: called CC=1 SCAR=0.0
79 0 ▶ sub on_error ($listener, $error) {
Subroutine on_error: not called CC=1 SCAR=6.9
80 0 die "benchmark listener failed: $error\n";
81 }
82 }
83  
84 {
85 1 package Linux::Event::Bench::CallbackClient;
86 1 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 0 ▶ sub on_data ($stream, $bytes) { return }
Subroutine on_data: not called CC=1 SCAR=6.9
88 60 ▶ sub on_ready ($stream) {
Subroutine on_ready: called CC=1 SCAR=0.0
89 60 my $run = $stream->data;
90 60 main::abortive_close($stream->fh);
91 60 $stream->close;
92 60 $run->{active}--;
93 60 $run->{completed}++;
94 60 main::launch_clients($run);
95 60 main::finish_if_done($run);
96 }
97 0 ▶ sub on_error ($stream, $error) {
Subroutine on_error: not called CC=1 SCAR=6.9
98 0 die "benchmark client failed: $error\n";
99 }
100 }
101  
102 1 ▶sub abortive_close ($fh) {
Subroutine abortive_close: called CC=2 SCAR=7.2
103 60 ▶ setsockopt($fh, SOL_SOCKET, SO_LINGER, pack('ii', 1, 0))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $fh, 1, 13, "\1\0\0\0\0\0\0\0" or die "client SO_LINGER: $!\n" 60 0 60
104 or die "client SO_LINGER: $!\n";
105 }
106  
107 60 ▶sub accepted_ready ($stream) {
Subroutine accepted_ready: called CC=1 SCAR=0.0
108 60 my $run = $stream->data;
109 60 $stream->close;
110 60 $run->{accepted}++;
111 60 finish_if_done($run);
112 }
113  
114 120 ▶sub finish_if_done ($run) {
Subroutine finish_if_done: called CC=3 SCAR=11.0
115 $run->{loop}->stop
116 if $run->{accepted} == $connections
117 120 ▶ && $run->{completed} == $connections;
Branch: 1 branch
BranchTrueFalseTotal
if $run->{"accepted"} == $connections and $run->{"completed"} == $connections 3 117 120
Condition100%3/3
$run->{"accepted"} == $connections and $run->{"completed"} == $connections
!ll&&!rl&&r
11163
MC/DC100%2/2
$run->{"accepted"} == $connections and $run->{"completed"} == $connections
$run->{"accepted"} == $connections $run->{"completed"} == $connections
Truth table100%3/3
A and B
A= $run->{"accepted"} == $connectionsB= $run->{"completed"} == $connections
ABresult
0X0
100
111
118 }
119  
120 63 ▶sub launch_clients ($run) {
Subroutine launch_clients: called CC=3 SCAR=11.0
121 63 ▶ while ($run->{active} < $clients && $run->{started} < $connections) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run->{"active"} < $clients and $run->{"started"} < $connections) 60 63 123
Condition100%3/3
$run->{"active"} < $clients and $run->{"started"} < $connections
!ll&&!rl&&r
57660
MC/DC100%2/2
$run->{"active"} < $clients and $run->{"started"} < $connections
$run->{"active"} < $clients $run->{"started"} < $connections
Truth table100%3/3
A and B
A= $run->{"active"} < $clientsB= $run->{"started"} < $connections
ABresult
0X0
100
111
122 60 $run->{active}++;
123 60 $run->{started}++;
124 Linux::Event::Bench::CallbackClient->connect(
125 loop => $run->{loop},
126 host => '127.0.0.1', port => $run->{port},
127 60 timeout => $timeout, data => $run,
128 );
129 }
130 }
131  
132 3 ▶sub run_style ($style, $repeat) {
Subroutine run_style: called CC=3 SCAR=11.0
133 3 my $loop = Linux::Event::Loop->new;
134 3 $loop->enable_watcher_reclaim(1);
135 3 my $run = {
136 loop => $loop, started => 0, active => 0,
137 completed => 0, accepted => 0,
138 };
139 3 my ($stream_class, %callback);
140 3 ▶ if ($style eq 'subclass_method') {
Branch: 2 branches
BranchTrueFalseTotal
if ($style eq 'subclass_method') { } 1 2 3
elsif ($style eq 'listener_shared_closure') { } 1 1 2
141 1 $stream_class = 'Linux::Event::Bench::CallbackMethodStream';
142 } elsif ($style eq 'listener_shared_closure') {
143 1 $stream_class = 'Linux::Event::Bench::CallbackClosureStream';
144 1 $callback{on_data} = $SHARED_CALLBACK;
145 } else {
146 1 $stream_class = 'Linux::Event::Bench::CallbackFreshStream';
147 }
148  
149 3 my $fresh_before = $FRESH_CLOSURES;
150 3 my $listener = Linux::Event::Bench::CallbackListener->new(
151 loop => $loop, host => '127.0.0.1', port => 0,
152 stream => {
153 class => $stream_class,
154 data => $run,
155 %callback,
156 },
157 );
158 3 $run->{port} = $listener->port;
159  
160 3 my $wall_start = clock_gettime(CLOCK_MONOTONIC);
161 3 my ($user_start, $system_start) = (times)[0, 1];
162 3 launch_clients($run);
163 3 $loop->run;
164 3 my ($user_end, $system_end) = (times)[0, 1];
165 3 my $elapsed = clock_gettime(CLOCK_MONOTONIC) - $wall_start;
166 3 my $cpu = ($user_end - $user_start) + ($system_end - $system_start);
167 3 $listener->close;
168  
169 return {
170 style => $style,
171 repeat => $repeat,
172 accepted => $run->{accepted},
173 3 elapsed_seconds => $elapsed,
174 parent_cpu_seconds => $cpu,
175 accepts_per_second => $connections / $elapsed,
176 parent_cpu_us_per_accept => $cpu * 1_000_000 / $connections,
177 fresh_closures_created => $FRESH_CLOSURES - $fresh_before,
178 };
179 }
180  
181 10 ▶sub median (@value) {
Subroutine median: called CC=2 SCAR=7.6
182 0 @value = sort { $a <=> $b } @value;
183 10 my $middle = int(@value / 2);
184 10 ▶ return @value % 2 ? $value[$middle]
Branch: 1 branch
BranchTrueFalseTotal
@value % 2 ? : 10 0 10
185 : ($value[$middle - 1] + $value[$middle]) / 2;
186 }
187  
188 4 ▶sub paired_delta ($rows, $style, $field) {
Subroutine paired_delta: called CC=3 SCAR=11.1
189 my %baseline = map {
190 4 $_->{repeat} => $_->{$field}
191 4 } grep { $_->{style} eq 'subclass_method' } @$rows;
192 my @delta = map {
193 4 100 * ($_->{$field} / $baseline{$_->{repeat}} - 1)
194 } grep {
195 4 $_->{style} eq $style && $baseline{$_->{repeat}}
196 12 ▶ } @$rows;
Condition66%2/3
$_->{'style'} eq $style && $baseline{$_->{'repeat'}}
!ll&&!rl&&r
804
MC/DC50%1/2
$_->{'style'} eq $style && $baseline{$_->{'repeat'}}
$_->{'style'} eq $style $baseline{$_->{'repeat'}}
Truth table66%2/3
A && B
A= $_->{'style'} eq $styleB= $baseline{$_->{'repeat'}}
ABresult
0X0
100
111
197 4 ▶ return @delta ? median(@delta) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@delta ? : 4 0 4
198 }
199  
200 1 say 'Accepted Stream first-class callback construction benchmark';
201 1 say "clients=$clients connections=$connections repeats=$repeats";
202 1 my @row;
203 1 for my $repeat (1 .. $repeats) {
204 1 my $shift = ($repeat - 1) % @styles;
205 1 my @order = (@styles[$shift .. $#styles], @styles[0 .. $shift - 1]);
206 1 for my $style (@order) {
207 3 my $row = run_style($style, $repeat);
208 3 push @row, $row;
209 printf "%s repeat=%d %.1f accepts/s cpu=%.3f us/accept fresh=%d\n",
210 $style, $repeat, $row->{accepts_per_second},
211 3 $row->{parent_cpu_us_per_accept}, $row->{fresh_closures_created};
212 }
213 }
214  
215 1 my @summary;
216 1 say "\nPaired median summary";
217 1 printf "%-28s %14s %16s %12s %12s\n",
218 'style', 'accepts/s', 'cpu us/accept', 'speed delta', 'cpu delta';
219 1 for my $style (@styles) {
220 3 my @set = grep { $_->{style} eq $style } @row;
221 3 ▶ my $speed_delta = $style eq 'subclass_method'
Branch: 1 branch
BranchTrueFalseTotal
$style eq 'subclass_method' ? : 1 2 3
222 ? undef : paired_delta(\@row, $style, 'accepts_per_second');
223 3 ▶ my $cpu_delta = $style eq 'subclass_method'
Branch: 1 branch
BranchTrueFalseTotal
$style eq 'subclass_method' ? : 1 2 3
224 ? undef : paired_delta(\@row, $style, 'parent_cpu_us_per_accept');
225 my $summary = {
226 style => $style,
227 3 accepts_per_second => median(map { $_->{accepts_per_second} } @set),
228 parent_cpu_us_per_accept => median(
229 3 map { $_->{parent_cpu_us_per_accept} } @set
230 ),
231 throughput_delta_percent => $speed_delta,
232 parent_cpu_delta_percent => $cpu_delta,
233 };
234 3 push @summary, $summary;
235 printf "%-28s %14.1f %16.3f %11s%% %11s%%\n",
236 $style, $summary->{accepts_per_second},
237 $summary->{parent_cpu_us_per_accept},
238 3 ▶ defined($speed_delta) ? sprintf('%.2f', $speed_delta) : 'baseline',
Branch: 2 branches
BranchTrueFalseTotal
defined $speed_delta ? : 2 1 3
defined $cpu_delta ? : 2 1 3
239 defined($cpu_delta) ? sprintf('%.2f', $cpu_delta) : 'baseline';
240 }
241  
242 1 ▶if (defined $json_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json_path) 1 0 1
243 1 my ($sysname, undef, $release, $version, $machine) = uname();
244 1 my $report = {
245 benchmark => 'linux-event-first-class-callback-construction',
246 benchmark_contract_version => 1,
247 generated_at => strftime('%Y-%m-%dT%H:%M:%SZ', gmtime),
248 environment => {
249 linux_event_version => $Linux::Event::IO::Sock::Stream::VERSION,
250 perl_version => "$^V", os => $^O,
251 uname => { sysname => $sysname, release => $release,
252 version => $version, machine => $machine },
253 },
254 configuration => { clients => $clients, connections => $connections,
255 repeats => $repeats, styles => \@styles },
256 raw => \@row,
257 summary => \@summary,
258 notes => [
259 'Parent process CPU per accept is the primary construction metric.',
260 'listener_shared_closure passes one Listener-held CV to every accepted Stream.',
261 'fresh_closure deliberately uses a benchmark-only new wrapper to allocate one CV per accepted Stream.',
262 ],
263 };
264 1 my $directory = dirname($json_path);
265 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
266 1 ▶ open my $output, '>', $json_path or die "open $json_path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $output, ">", $json_path or die "open ${json_path}: $!\n" 1 0 1
267 1 print {$output} JSON::PP->new->canonical->pretty->encode($report);
268 1 ▶ close $output or die "close $json_path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $output or die "close ${json_path}: $!\n" 1 0 1
269 1 say "\nWrote $json_path";
270 }
271  
272 0 ▶sub usage ($status) {
Subroutine usage: not called CC=2 SCAR=17.9
273 0 ▶ my $fh = $status ? *STDERR : *STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 0 0 0
274 0 print {$fh} <<'USAGE';
275 Usage: perl -Mblib bench/run-first-class-callback-construction-bench.pl [options]
276  
277 --styles=LIST subclass_method,listener_shared_closure,fresh_closure
278 --clients=N concurrent clients (default: 100)
279 --connections=N accepted connections per row (default: 10000)
280 --repeats=N paired repetitions (default: 9)
281 --timeout=SECONDS catastrophic client deadline (default: 30)
282 --json=PATH write raw rows and paired summary as JSON
283 --help
284  
285 Run order rotates within each repeat. The fresh-closure case is deliberately a
286 diagnostic; production Listener propagation reuses one callback CV.
287 USAGE
288 0 exit $status;
289 }
« bench/run-process-microbench.pl ↑ Summary blib/lib/Linux/Event/Error.pm »