bench/run-connect-microbench.pl

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