bench/run-connect-microbench.pl

filter: Statementstmt 28.5% 52 / 182 Branchbran 3.1% 2 / 64 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 51.7% 15 / 29 Podpod n/a totaltotal 23.0% 69 / 300 CC 25 SCAR 28.4
CC
25
Cov
21%
one_run
56.0
launch_requests
30.0
git_commit
30.0
CRAP 17.1
« bench/run-listen-microbench.pl ↑ Summary blib/lib/Linux/Event/_SocketConfig.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 POSIX qw(strftime uname);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Socket qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 AF_INET INADDR_LOOPBACK
14 SOCK_STREAM SOCK_NONBLOCK SOCK_CLOEXEC
15 SOL_SOCKET SO_ERROR SO_LINGER SO_REUSEADDR SOMAXCONN
16 pack_sockaddr_in unpack_sockaddr_in
17 1 );
18 1 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Linux::Event::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Linux::Event::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 my @modes = qw(manual add loop);
24 1 my @clients = (1, 10, 100);
25 1 my $connections = 10_000;
26 1 my $repeats = 6;
27 1 my $timeout = 30;
28 1 my $json_path;
29 1 my $help = 0;
30  
31 GetOptions(
32 0 ▶ 'modes=s' => sub { @modes = split /,/, $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 ▶ 'clients=s' => sub { @clients = map { 0 + $_ } split /,/, $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 1 ▶ 'connections=i' => \$connections,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("modes=s", sub { @modes = split(/,/u, $_[1], 0); } , "clients=s", sub { @clients = map({0 + $_;} split(/,/u, $_[1], 0)); } , "connections=i", \$connections, "repeats=i", \$repeats, "timeout=f", \$timeout, "json=s", \$json_path, "help", \$help) or usage(1) 1 0 1
35 'repeats=i' => \$repeats,
36 'timeout=f' => \$timeout,
37 'json=s' => \$json_path,
38 'help' => \$help,
39 ) or usage(1);
40 1 ▶usage(0) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 1 0 1
41  
42 0 ▶die "connections must be positive\n" if $connections < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $connections < 1 0 0 0
43 0 ▶die "repeats must be positive\n" if $repeats < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $repeats < 1 0 0 0
44 0 ▶die "timeout must be positive\n" if $timeout <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= 0 0 0 0
45 0 ▶die "clients must be positive\n" if grep { $_ < 1 } @clients;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ < 1;} @clients 0 0 0
46 0 my %valid_mode = map { $_ => 1 } qw(manual add loop);
47 die "modes must contain only manual, add, or loop\n"
48 0 ▶ if grep { !$valid_mode{$_} } @modes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $valid_mode{$_};} @modes 0 0 0
49 0 my %seen_mode;
50 die "modes must not contain duplicates\n"
51 0 ▶ if grep { $seen_mode{$_}++ } @modes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$seen_mode{$_}++;} @modes 0 0 0
52  
53 {
54 package BenchConnectStream;
55 1 ▶ use parent 'Linux::Event::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 sub on_data ($stream, $bytes) { }
58  
59 0 ▶ sub on_ready ($stream) {
Subroutine on_ready: not called CC=1 SCAR=6.9
60 0 main::enable_abortive_close($stream->fh);
61 0 $stream->close;
62 0 main::complete_request($stream->data);
63 }
64  
65 0 ▶ sub on_error ($stream, $error) {
Subroutine on_error: not called CC=1 SCAR=6.9
66 0 die "benchmark connection failed: $error\n";
67 }
68 }
69  
70 0 ▶sub complete_request ($run) {
Subroutine complete_request: not called CC=1 SCAR=6.9
71 0 $run->{active}--;
72 0 $run->{completed}++;
73 0 launch_requests($run);
74 0 finish_if_done($run);
75 0 return;
76 }
77  
78 0 ▶sub manual_connected ($registration) {
Subroutine manual_connected: not called CC=3 SCAR=24.8
79 0 my $run = $registration->data;
80 0 my $fh = $registration->fh;
81 0 my $packed = getsockopt($fh, SOL_SOCKET, SO_ERROR);
82 0 ▶ my $errno = defined($packed) ? unpack('i', $packed) : 0 + $!;
Branch: 1 branch
BranchTrueFalseTotal
defined $packed ? : 0 0 0
83 0 ▶ if ($errno) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errno) 0 0 0
84 0 local $! = $errno;
85 0 die "manual benchmark connection failed: $!\n";
86 }
87 0 $registration->cancel;
88 0 enable_abortive_close($fh);
89 0 close $fh;
90 0 complete_request($run);
91 0 return;
92 }
93  
94 0 ▶sub launch_manual ($run) {
Subroutine launch_manual: not called CC=4 SCAR=30.0
95 0 ▶ socket(my $fh, AF_INET,
Branch: 1 branch
BranchTrueFalseTotal
socket my $fh, 2, 526337, 0 or die "manual client socket: $!\n" 0 0 0
96 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)
97 or die "manual client socket: $!\n";
98 my $connected = connect(
99 0 $fh, pack_sockaddr_in($run->{port}, INADDR_LOOPBACK),
100 );
101 0 ▶ die "manual client connect: $!\n" if !$connected && !$!{EINPROGRESS};
Branch: 1 branch
BranchTrueFalseTotal
unless $connected or $!{"EINPROGRESS"} 0 0 0
Condition0%0/3
$connected or $!{"EINPROGRESS"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$connected or $!{"EINPROGRESS"}
$connected $!{"EINPROGRESS"}
Truth table0%0/3
A or B
A= $connectedB= $!{"EINPROGRESS"}
ABresult
1X1
011
000
102 $run->{loop}->watch(
103 0 fh => $fh,
104 data => $run,
105 write => \&manual_connected,
106 error => \&manual_connected,
107 );
108 0 return;
109 }
110  
111 0 ▶sub enable_abortive_close ($fh) {
Subroutine enable_abortive_close: not called CC=2 SCAR=17.9
112 0 ▶ 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" 0 0 0
113 or die "client SO_LINGER: $!\n";
114 0 return;
115 }
116  
117 0 ▶sub finish_if_done ($run) {
Subroutine finish_if_done: not called CC=3 SCAR=24.8
118 $run->{loop}->stop
119 if $run->{completed} == $run->{connections}
120 0 ▶ && $run->{accepted} == $run->{connections};
Branch: 1 branch
BranchTrueFalseTotal
if $run->{"completed"} == $run->{"connections"} and $run->{"accepted"} == $run->{"connections"} 0 0 0
Condition0%0/3
$run->{"completed"} == $run->{"connections"} and $run->{"accepted"} == $run->{"connections"}
!ll&&!rl&&r
000
MC/DC0%0/2
$run->{"completed"} == $run->{"connections"} and $run->{"accepted"} == $run->{"connections"}
$run->{"completed"} == $run->{"connections"} $run->{"accepted"} == $run->{"connections"}
Truth table0%0/3
A and B
A= $run->{"completed"} == $run->{"connections"}B= $run->{"accepted"} == $run->{"connections"}
ABresult
0X0
100
111
121 0 return;
122 }
123  
124 0 ▶sub accept_ready ($watcher) {
Subroutine accept_ready: not called CC=2 SCAR=17.9
125 0 my $run = $watcher->data;
126 0 ▶ while (accept(my $peer, $run->{listener})) {
Branch: 1 branch
BranchTrueFalseTotal
if (accept my $peer, $run->{"listener"}) 0 0 0
127 0 close $peer;
128 0 $run->{accepted}++;
129 }
130 0 finish_if_done($run);
131 0 return;
132 }
133  
134 0 ▶sub launch_requests ($run) {
Subroutine launch_requests: not called CC=5 SCAR=34.0
135 0 ▶ while ($run->{active} < $run->{clients}
Branch: 1 branch
BranchTrueFalseTotal
if ($run->{"active"} < $run->{"clients"} and $run->{"started"} < $run->{"connections"}) 0 0 0
Condition0%0/3
$run->{"active"} < $run->{"clients"} and $run->{"started"} < $run->{"connections"}
!ll&&!rl&&r
000
MC/DC0%0/2
$run->{"active"} < $run->{"clients"} and $run->{"started"} < $run->{"connections"}
$run->{"active"} < $run->{"clients"} $run->{"started"} < $run->{"connections"}
Truth table0%0/3
A and B
A= $run->{"active"} < $run->{"clients"}B= $run->{"started"} < $run->{"connections"}
ABresult
0X0
100
111
136 && $run->{started} < $run->{connections}) {
137 0 $run->{started}++;
138 0 $run->{active}++;
139 0 ▶ if ($run->{mode} eq 'manual') {
Branch: 2 branches
BranchTrueFalseTotal
if ($run->{'mode'} eq 'manual') { } 0 0 0
elsif ($run->{'mode'} eq 'loop') { } 0 0 0
140 0 launch_manual($run);
141 } elsif ($run->{mode} eq 'loop') {
142 BenchConnectStream->connect(
143 loop => $run->{loop},
144 host => '127.0.0.1',
145 port => $run->{port},
146 0 timeout => $timeout,
147 data => $run,
148 );
149 } else {
150 my $stream = BenchConnectStream->connect(
151 host => '127.0.0.1',
152 port => $run->{port},
153 0 timeout => $timeout,
154 data => $run,
155 );
156 0 $run->{loop}->add($stream);
157 }
158 }
159 0 return;
160 }
161  
162 0 ▶sub one_run ($mode, $client_count) {
Subroutine one_run: not called CC=7 SCAR=40.3
163 0 ▶ socket(my $listener, AF_INET,
Branch: 1 branch
BranchTrueFalseTotal
socket my $listener, 2, 526337, 0 or die "listener socket: $!\n" 0 0 0
164 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)
165 or die "listener socket: $!\n";
166 0 ▶ setsockopt($listener, SOL_SOCKET, SO_REUSEADDR, pack('i', 1))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $listener, 1, 2, "\1\0\0\0" or die "setsockopt SO_REUSEADDR: $!\n" 0 0 0
167 or die "setsockopt SO_REUSEADDR: $!\n";
168 0 ▶ bind($listener, pack_sockaddr_in(0, INADDR_LOOPBACK))
Branch: 1 branch
BranchTrueFalseTotal
bind $listener, pack_sockaddr_in(0, "\177\0\0\1") or die "listener bind: $!\n" 0 0 0
169 or die "listener bind: $!\n";
170 0 ▶ listen($listener, SOMAXCONN) or die "listener listen: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
listen $listener, 4096 or die "listener listen: $!\n" 0 0 0
171 0 my ($port) = unpack_sockaddr_in(getsockname($listener));
172  
173 0 my $loop = Linux::Event::Loop->new;
174 0 $loop->enable_watcher_reclaim(1);
175 0 my $run = {
176 mode => $mode,
177 clients => $client_count,
178 connections => $connections,
179 loop => $loop,
180 listener => $listener,
181 port => $port,
182 started => 0,
183 active => 0,
184 completed => 0,
185 accepted => 0,
186 };
187 0 my $accept_watcher = $loop->watch(
188 fh => $listener,
189 data => $run,
190 read => \&accept_ready,
191 );
192  
193 0 my $wall_start = clock_gettime(CLOCK_MONOTONIC);
194 0 my ($user_start, $system_start) = (times)[0, 1];
195 0 launch_requests($run);
196 0 $loop->run;
197 0 my ($user_end, $system_end) = (times)[0, 1];
198 0 my $elapsed = clock_gettime(CLOCK_MONOTONIC) - $wall_start;
199 0 my $cpu = ($user_end - $user_start) + ($system_end - $system_start);
200  
201 0 $accept_watcher->cancel;
202 0 close $listener;
203 die "benchmark completed $run->{completed} of $connections connections\n"
204 0 ▶ if $run->{completed} != $connections;
Branch: 1 branch
BranchTrueFalseTotal
if $run->{"completed"} != $connections 0 0 0
205 die "benchmark accepted $run->{accepted} of $connections connections\n"
206 0 ▶ if $run->{accepted} != $connections;
Branch: 1 branch
BranchTrueFalseTotal
if $run->{"accepted"} != $connections 0 0 0
207 return {
208 rate => $connections / $elapsed,
209 cpu_us => $cpu * 1_000_000 / $connections,
210 accepted => $run->{accepted},
211 completed => $run->{completed},
212 0 };
213 }
214  
215 0 ▶sub median (@values) {
Subroutine median: not called CC=2 SCAR=17.9
216 0 @values = sort { $a <=> $b } @values;
217 0 my $middle = int(@values / 2);
218 0 ▶ return @values % 2
Branch: 1 branch
BranchTrueFalseTotal
@values % 2 ? : 0 0 0
219 ? $values[$middle]
220 : ($values[$middle - 1] + $values[$middle]) / 2;
221 }
222  
223 0 say 'Median loopback TCP Stream connection lifecycle benchmark';
224 0 say "Linux::Event version $Linux::Event::Stream::VERSION";
225 0 printf "%-8s %8s %14s %14s\n",
226 qw(mode clients connects/s cpu_us/connect);
227 0 my @records;
228 my @summary;
229 0 for my $client_count (@clients) {
230 0 my %result;
231 0 for my $repeat (0 .. $repeats - 1) {
232 0 my $offset = $repeat % @modes;
233 0 my @order = (@modes[$offset .. $#modes], @modes[0 .. $offset - 1]);
234 0 for my $position (0 .. $#order) {
235 0 my $mode = $order[$position];
236 0 my $row = one_run($mode, $client_count);
237 0 $row->{mode} = $mode;
238 0 $row->{clients} = $client_count;
239 0 $row->{repeat} = $repeat + 1;
240 0 $row->{order_position} = $position + 1;
241 0 push @{ $result{$mode} }, $row;
242 0 push @records, $row;
243 }
244 }
245 0 for my $mode (@modes) {
246 my $row = {
247 mode => $mode,
248 clients => $client_count,
249 connects_per_second => median(
250 0 map { $_->{rate} } @{ $result{$mode} }
251 ),
252 cpu_us_per_connect => median(
253 0 map { $_->{cpu_us} } @{ $result{$mode} }
254 ),
255 };
256 0 push @summary, $row;
257 printf "%-8s %8d %14.1f %14.3f\n",
258 $mode,
259 $client_count,
260 $row->{connects_per_second},
261 0 $row->{cpu_us_per_connect};
262 }
263 }
264  
265 0 ▶if (defined $json_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json_path) 0 0 0
266 0 my @uname = uname();
267 0 my $report = {
268 benchmark => 'linux-event-connect-lifecycle',
269 benchmark_contract_version => 1,
270 generated_at => strftime('%Y-%m-%dT%H:%M:%SZ', gmtime),
271 environment => {
272 linux_event_version => $Linux::Event::Stream::VERSION,
273 perl => "$^V",
274 uname => \@uname,
275 git_commit => git_commit(),
276 },
277 configuration => {
278 modes => \@modes,
279 clients => \@clients,
280 connections => $connections,
281 repeats => $repeats,
282 timeout => $timeout,
283 execution_order => 'balanced cyclic rotation',
284 teardown => 'abortive connected-client close',
285 },
286 records => \@records,
287 summary => \@summary,
288 notes => [
289 'Every row performs nonblocking TCP connection setup and teardown.',
290 'Manual is a raw nonblocking socket and Loop registration baseline.',
291 'Add uses detached MyStream->connect followed by Loop->add.',
292 'Loop supplies loop => directly to MyStream->connect.',
293 'The timeout is a per-request catastrophic deadline, not the measured row duration.',
294 'Compare only results with the same benchmark contract and configuration.',
295 ],
296 };
297 0 my $dir = dirname($json_path);
298 0 ▶ make_path($dir) if $dir ne '.' && !-d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir ne "." and not -d $dir 0 0 0
Condition0%0/3
$dir ne "." and not -d $dir
!ll&&!rl&&r
000
MC/DC0%0/2
$dir ne "." and not -d $dir
$dir ne "." not -d $dir
Truth table0%0/3
A and not(B)
A= $dir ne "."B= not -d $dir
ABresult
0X0
100
111
299 0 ▶ open my $out, '>', $json_path or die "open $json_path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", $json_path or die "open ${json_path}: $!\n" 0 0 0
300 0 print {$out} JSON::PP->new->canonical->pretty->encode($report);
301 0 ▶ close $out or die "close $json_path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $out or die "close ${json_path}: $!\n" 0 0 0
302 0 say "Wrote $json_path";
303 }
304  
305 0 ▶sub git_commit () {
Subroutine git_commit: not called CC=5 SCAR=34.0
306 0 ▶ return undef if !-e "$Bin/../.git";
Branch: 1 branch
BranchTrueFalseTotal
unless -e "$main::Bin/../.git" 0 0 0
307 0 ▶ open my $git, '-|', 'git', '-C', "$Bin/..", 'rev-parse', 'HEAD'
Branch: 1 branch
BranchTrueFalseTotal
open my $git, "-|", "git", "-C", "$main::Bin/..", "rev-parse", "HEAD" or return undef 0 0 0
308 or return undef;
309 0 my $commit = <$git>;
310 0 my $ok = close $git;
311 0 ▶ return undef if !$ok || !defined $commit;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok and defined $commit 0 0 0
Condition0%0/3
$ok and defined $commit
!ll&&!rl&&r
000
MC/DC0%0/2
$ok and defined $commit
$ok defined $commit
Truth table0%0/3
A and B
A= $okB= defined $commit
ABresult
0X0
100
111
312 0 chomp $commit;
313 0 return $commit;
314 }
315  
316 1 ▶sub usage ($exit) {
Subroutine usage: called CC=1 SCAR=0.0
317 1 print <<'USAGE';
318 Usage: perl -Mblib bench/run-connect-microbench.pl [options]
319  
320 --modes=LIST manual,add,loop (default: all three)
321 --clients=LIST concurrent requests (default: 1,10,100)
322 --connections=N completed connections per row (default: 10000)
323 --repeats=N median repetitions (default: 6)
324 --timeout=SECONDS catastrophic connection deadline (default: 30)
325 --json=PATH write raw records and summaries as JSON
326 --help
327  
328 Manual uses a raw nonblocking socket plus an opaque Loop registration. Add uses
329 detached MyStream->connect followed by Loop->add. Loop passes loop => directly
330 to MyStream->connect. Both public Stream rows preserve one object across
331 connection acquisition, readiness, and close. Every successfully connected
332 client uses equal abortive teardown so repeated rows do not exhaust
333 the host with client-side TIME_WAIT sockets. Repeat execution order rotates to
334 reduce bias; use a repeat count divisible by three for balanced execution.
335 USAGE
336 1 exit $exit;
337 }
« bench/run-listen-microbench.pl ↑ Summary blib/lib/Linux/Event/_SocketConfig.pm »