bench/run-listen-microbench.pl

filter: Statementstmt 86.4% 121 / 140 Branchbran 45.2% 19 / 42 Conditioncond 66.6% 6 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 88.0% 22 / 25 Podpod n/a totaltotal 77.4% 172 / 222 CC 17 SCAR 17.6
CC
17
Cov
76%
manual_accept_ready
20.0
one_run
13.9
finish_if_done
3.0
CRAP 5.8
« bench/run-signal-microbench.pl ↑ Summary blib/lib/Linux/Event/Framer/Fixed.pm »
1 #!/usr/bin/env perl
2 2 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Socket qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
8 AF_INET INADDR_LOOPBACK
9 SOCK_STREAM SOCK_NONBLOCK SOCK_CLOEXEC
10 SOL_SOCKET SO_LINGER SO_REUSEADDR
11 pack_sockaddr_in unpack_sockaddr_in
12 2 );
13 2 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Linux::Event::IO::Sock::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Linux::Event::IO::Sock::Listener;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Linux::Event::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 my @modes = qw(manual add loop);
20 2 my @clients = (1, 10, 100);
21 2 my $connections = 10_000;
22 2 my $repeats = 9;
23 2 my $timeout = 30;
24 2 my $help = 0;
25  
26 GetOptions(
27 1 ▶ 'modes=s' => sub { @modes = split /,/, $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 1 ▶ 'clients=s' => sub { @clients = map { 0 + $_ } split /,/, $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 2 ▶ '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, "help", \$help) or usage(1) 2 0 2
30 'repeats=i' => \$repeats,
31 'timeout=f' => \$timeout,
32 'help' => \$help,
33 ) or usage(1);
34 2 ▶usage(0) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 1 1 2
35  
36 1 ▶die "connections must be positive\n" if $connections < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $connections < 1 0 1 1
37 1 ▶die "repeats must be positive\n" if $repeats < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $repeats < 1 0 1 1
38 1 ▶die "timeout must be positive\n" if $timeout <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= 0 0 1 1
39 1 ▶die "clients must be positive\n" if grep { $_ < 1 } @clients;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ < 1;} @clients 0 1 1
40 1 my %valid_mode = map { $_ => 1 } qw(manual add loop);
41 die "modes must contain only manual, add, or loop\n"
42 1 ▶ if grep { !$valid_mode{$_} } @modes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $valid_mode{$_};} @modes 0 1 1
43 1 my %seen_mode;
44 die "modes must not contain duplicates\n"
45 1 ▶ if grep { $seen_mode{$_}++ } @modes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$seen_mode{$_}++;} @modes 0 1 1
46  
47 {
48 package BenchAutomaticStream;
49 2 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 sub on_data ($stream, $bytes) { }
52  
53 8 ▶ sub on_ready ($stream) {
Subroutine on_ready: called CC=1 SCAR=0.0
54 8 my $run = $stream->data;
55 8 $stream->close;
56 8 $run->{accepted}++;
57 8 main::finish_if_done($run);
58 }
59  
60 }
61  
62 {
63 1 package BenchAutomaticListener;
64 2 ▶ use parent 'Linux::Event::IO::Sock::Listener';
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 0 ▶ sub on_error ($listener, $error) {
Subroutine on_error: not called CC=1 SCAR=6.9
67 0 die "benchmark listener failed: $error\n";
68 }
69 }
70  
71 {
72 1 package BenchListenClient;
73 2 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 sub on_data ($stream, $bytes) { }
76  
77 8 ▶ sub on_ready ($stream) {
Subroutine on_ready: called CC=1 SCAR=0.0
78 8 my $run = $stream->data;
79 8 main::enable_abortive_close($stream->fh, 'client');
80 8 $stream->close;
81 8 $run->{active}--;
82 8 $run->{completed}++;
83 8 main::launch_requests($run);
84 8 main::finish_if_done($run);
85 }
86  
87 0 ▶ sub on_error ($stream, $error) {
Subroutine on_error: not called CC=1 SCAR=6.9
88 0 die "benchmark connection failed: $error\n";
89 }
90 }
91  
92 1 ▶sub enable_abortive_close ($fh, $role) {
Subroutine enable_abortive_close: called CC=2 SCAR=7.1
93 8 ▶ 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 "$role SO_LINGER: $!\n" 8 0 8
94 or die "$role SO_LINGER: $!\n";
95 8 return;
96 }
97  
98 16 ▶sub finish_if_done ($run) {
Subroutine finish_if_done: called CC=3 SCAR=11.0
99 $run->{loop}->stop
100 if $run->{accepted} == $run->{connections}
101 16 ▶ && $run->{completed} == $run->{connections};
Branch: 1 branch
BranchTrueFalseTotal
if $run->{"accepted"} == $run->{"connections"} and $run->{"completed"} == $run->{"connections"} 2 14 16
Condition100%3/3
$run->{"accepted"} == $run->{"connections"} and $run->{"completed"} == $run->{"connections"}
!ll&&!rl&&r
1042
MC/DC100%2/2
$run->{"accepted"} == $run->{"connections"} and $run->{"completed"} == $run->{"connections"}
$run->{"accepted"} == $run->{"connections"} $run->{"completed"} == $run->{"connections"}
Truth table100%3/3
A and B
A= $run->{"accepted"} == $run->{"connections"}B= $run->{"completed"} == $run->{"connections"}
ABresult
0X0
100
111
102 16 return;
103 }
104  
105 0 ▶sub manual_accept_ready ($registration) {
Subroutine manual_accept_ready: not called CC=4 SCAR=30.0
106 0 my $run = $registration->data;
107 0 ▶ while (accept(my $client, $run->{listener_fh})) {
Branch: 1 branch
BranchTrueFalseTotal
if (accept my $client, $run->{"listener_fh"}) 0 0 0
108 0 close $client;
109 0 $run->{accepted}++;
110 }
111 die "manual accept failed: $!\n"
112 0 ▶ if !$!{EAGAIN} && !$!{EWOULDBLOCK};
Branch: 1 branch
BranchTrueFalseTotal
unless $!{"EAGAIN"} or $!{"EWOULDBLOCK"} 0 0 0
Condition0%0/3
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
$!{"EAGAIN"} $!{"EWOULDBLOCK"}
Truth table0%0/3
A or B
A= $!{"EAGAIN"}B= $!{"EWOULDBLOCK"}
ABresult
1X1
011
000
113 0 finish_if_done($run);
114 0 return;
115 }
116  
117 10 ▶sub launch_requests ($run) {
Subroutine launch_requests: called CC=3 SCAR=11.0
118 10 ▶ while ($run->{active} < $run->{clients}
Branch: 1 branch
BranchTrueFalseTotal
if ($run->{"active"} < $run->{"clients"} and $run->{"started"} < $run->{"connections"}) 8 10 18
Condition100%3/3
$run->{"active"} < $run->{"clients"} and $run->{"started"} < $run->{"connections"}
!ll&&!rl&&r
648
MC/DC100%2/2
$run->{"active"} < $run->{"clients"} and $run->{"started"} < $run->{"connections"}
$run->{"active"} < $run->{"clients"} $run->{"started"} < $run->{"connections"}
Truth table100%3/3
A and B
A= $run->{"active"} < $run->{"clients"}B= $run->{"started"} < $run->{"connections"}
ABresult
0X0
100
111
119 && $run->{started} < $run->{connections}) {
120 8 $run->{started}++;
121 8 $run->{active}++;
122 BenchListenClient->connect(
123 loop => $run->{loop},
124 host => '127.0.0.1',
125 port => $run->{port},
126 8 timeout => $timeout,
127 data => $run,
128 );
129 }
130 10 return;
131 }
132  
133 2 ▶sub one_run ($mode, $client_count) {
Subroutine one_run: called CC=8 SCAR=26.3
134 2 my $loop = Linux::Event::Loop->new;
135 2 $loop->enable_watcher_reclaim(1);
136 2 my $run = {
137 mode => $mode,
138 clients => $client_count,
139 connections => $connections,
140 loop => $loop,
141 started => 0,
142 active => 0,
143 completed => 0,
144 accepted => 0,
145 };
146 2 ▶ if ($mode eq 'manual') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'manual') { } 0 2 2
elsif ($mode eq 'add') { } 1 1 2
147 0 ▶ socket(my $listener, AF_INET,
Branch: 1 branch
BranchTrueFalseTotal
socket my $listener, 2, 526337, 0 or die "manual listener socket: $!\n" 0 0 0
148 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)
149 or die "manual listener socket: $!\n";
150 0 ▶ setsockopt($listener, SOL_SOCKET, SO_REUSEADDR, pack('i', 1))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $listener, 1, 2, "\1\0\0\0" or die "manual listener setsockopt: $!\n" 0 0 0
151 or die "manual listener setsockopt: $!\n";
152 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 "manual listener bind: $!\n" 0 0 0
153 or die "manual listener bind: $!\n";
154 0 ▶ listen($listener, 4096) or die "manual listener listen: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
listen $listener, 4096 or die "manual listener listen: $!\n" 0 0 0
155 0 ($run->{port}) = unpack_sockaddr_in(getsockname($listener));
156 0 $run->{listener_fh} = $listener;
157 0 $run->{listener_watcher} = $loop->watch(
158 fh => $listener,
159 data => $run,
160 read => \&manual_accept_ready,
161 );
162 } elsif ($mode eq 'add') {
163 1 $run->{listener} = BenchAutomaticListener->new(
164 stream => {
165 class => 'BenchAutomaticStream',
166 data => $run,
167 },
168 host => '127.0.0.1', port => 0,
169 );
170 1 $loop->add($run->{listener});
171 1 $run->{port} = $run->{listener}->port;
172 } else {
173 1 $run->{listener} = BenchAutomaticListener->new(
174 stream => {
175 class => 'BenchAutomaticStream',
176 data => $run,
177 },
178 loop => $loop, host => '127.0.0.1', port => 0,
179 );
180 1 $run->{port} = $run->{listener}->port;
181 }
182  
183 2 my $wall_start = clock_gettime(CLOCK_MONOTONIC);
184 2 my ($user_start, $system_start) = (times)[0, 1];
185 2 launch_requests($run);
186 2 $loop->run;
187 2 my ($user_end, $system_end) = (times)[0, 1];
188 2 my $elapsed = clock_gettime(CLOCK_MONOTONIC) - $wall_start;
189 2 my $cpu = ($user_end - $user_start) + ($system_end - $system_start);
190  
191 2 ▶ if ($mode eq 'manual') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'manual') { } 0 2 2
192 0 $run->{listener_watcher}->cancel;
193 0 close $run->{listener_fh};
194 } else {
195 2 $run->{listener}->close;
196 }
197 return {
198 2 rate => $connections / $elapsed,
199 cpu_us => $cpu * 1_000_000 / $connections,
200 };
201 }
202  
203 4 ▶sub median (@values) {
Subroutine median: called CC=2 SCAR=7.6
204 0 @values = sort { $a <=> $b } @values;
205 4 my $middle = int(@values / 2);
206 4 ▶ return @values % 2
Branch: 1 branch
BranchTrueFalseTotal
@values % 2 ? : 4 0 4
207 ? $values[$middle]
208 : ($values[$middle - 1] + $values[$middle]) / 2;
209 }
210  
211 1 say 'Median loopback TCP Listener lifecycle benchmark';
212 1 say "Linux::Event version $Linux::Event::IO::Sock::Listener::VERSION";
213 1 printf "%-8s %8s %14s %14s\n",
214 qw(mode clients accepts/s cpu_us/accept);
215 1 for my $client_count (@clients) {
216 1 my %result;
217 1 for my $repeat (0 .. $repeats - 1) {
218 1 my $offset = $repeat % @modes;
219 1 my @order = (@modes[$offset .. $#modes], @modes[0 .. $offset - 1]);
220 1 push @{ $result{$_} }, one_run($_, $client_count) for @order;
221 }
222 1 for my $mode (@modes) {
223 printf "%-8s %8d %14.1f %14.3f\n",
224 $mode,
225 $client_count,
226 2 median(map { $_->{rate} } @{ $result{$mode} }),
227 2 median(map { $_->{cpu_us} } @{ $result{$mode} });
228 }
229 }
230  
231 1 ▶sub usage ($exit) {
Subroutine usage: called CC=1 SCAR=0.0
232 1 print <<'USAGE';
233 Usage: perl -Mblib bench/run-listen-microbench.pl [options]
234  
235 --modes=LIST manual,add,loop (default: all three)
236 --clients=LIST concurrent clients (default: 1,10,100)
237 --connections=N accepted connections per row (default: 10000)
238 --repeats=N median repetitions (default: 9)
239 --timeout=SECONDS catastrophic connection deadline (default: 30)
240 --help
241  
242 All rows acquire loopback TCP clients through MySocket->connect. Manual uses
243 explicit listener setup, Loop->watch, Perl accept, and close. Add uses detached
244 Listener construction followed by Loop->add. Loop supplies loop => directly to
245 Listener->new. Both Listener rows construct and close the same minimal Stream
246 subclass. Every successfully connected client
247 uses equal abortive teardown so repeated rows do not exhaust the host with
248 client-side TIME_WAIT sockets. Repeat execution order rotates to reduce bias;
249 use a repeat count divisible by three for balanced execution order.
250 USAGE
251 1 exit $exit;
252 }
« bench/run-signal-microbench.pl ↑ Summary blib/lib/Linux/Event/Framer/Fixed.pm »