bench/run-listen-microbench.pl

filter: Statementstmt 32.1% 45 / 140 Branchbran 4.7% 2 / 42 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 52.0% 13 / 25 Podpod n/a totaltotal 27.0% 60 / 222 CC 17 SCAR 29.4
CC
17
Cov
25%
one_run
72.0
manual_accept_ready
20.0
finish_if_done
12.0
CRAP 19.0
« blib/lib/Linux/Event/Stream.pm ↑ Summary bench/run-connect-microbench.pl »
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 Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶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 1 );
13 1 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Linux::Event::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Linux::Event::Listener;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Linux::Event::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 my @modes = qw(manual add loop);
20 1 my @clients = (1, 10, 100);
21 1 my $connections = 10_000;
22 1 my $repeats = 9;
23 1 my $timeout = 30;
24 1 my $help = 0;
25  
26 GetOptions(
27 0 ▶ 'modes=s' => sub { @modes = split /,/, $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 0 ▶ 'clients=s' => sub { @clients = map { 0 + $_ } split /,/, $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 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, "help", \$help) or usage(1) 1 0 1
30 'repeats=i' => \$repeats,
31 'timeout=f' => \$timeout,
32 'help' => \$help,
33 ) or usage(1);
34 1 ▶usage(0) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 1 0 1
35  
36 0 ▶die "connections must be positive\n" if $connections < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $connections < 1 0 0 0
37 0 ▶die "repeats must be positive\n" if $repeats < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $repeats < 1 0 0 0
38 0 ▶die "timeout must be positive\n" if $timeout <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= 0 0 0 0
39 0 ▶die "clients must be positive\n" if grep { $_ < 1 } @clients;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ < 1;} @clients 0 0 0
40 0 my %valid_mode = map { $_ => 1 } qw(manual add loop);
41 die "modes must contain only manual, add, or loop\n"
42 0 ▶ if grep { !$valid_mode{$_} } @modes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $valid_mode{$_};} @modes 0 0 0
43 0 my %seen_mode;
44 die "modes must not contain duplicates\n"
45 0 ▶ if grep { $seen_mode{$_}++ } @modes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$seen_mode{$_}++;} @modes 0 0 0
46  
47 {
48 package BenchAutomaticStream;
49 1 ▶ use parent 'Linux::Event::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 sub on_data ($stream, $bytes) { }
52  
53 0 ▶ sub on_ready ($stream) {
Subroutine on_ready: not called CC=1 SCAR=6.9
54 0 my $run = $stream->data;
55 0 $stream->close;
56 0 $run->{accepted}++;
57 0 main::finish_if_done($run);
58 }
59  
60 }
61  
62 {
63 0 package BenchAutomaticListener;
64 1 ▶ use parent 'Linux::Event::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 0 package BenchListenClient;
73 1 ▶ use parent 'Linux::Event::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 sub on_data ($stream, $bytes) { }
76  
77 0 ▶ sub on_ready ($stream) {
Subroutine on_ready: not called CC=1 SCAR=6.9
78 0 my $run = $stream->data;
79 0 main::enable_abortive_close($stream->fh, 'client');
80 0 $stream->close;
81 0 $run->{active}--;
82 0 $run->{completed}++;
83 0 main::launch_requests($run);
84 0 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 0 ▶sub enable_abortive_close ($fh, $role) {
Subroutine enable_abortive_close: not called CC=2 SCAR=17.9
93 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 "$role SO_LINGER: $!\n" 0 0 0
94 or die "$role SO_LINGER: $!\n";
95 0 return;
96 }
97  
98 0 ▶sub finish_if_done ($run) {
Subroutine finish_if_done: not called CC=3 SCAR=24.8
99 $run->{loop}->stop
100 if $run->{accepted} == $run->{connections}
101 0 ▶ && $run->{completed} == $run->{connections};
Branch: 1 branch
BranchTrueFalseTotal
if $run->{"accepted"} == $run->{"connections"} and $run->{"completed"} == $run->{"connections"} 0 0 0
Condition0%0/3
$run->{"accepted"} == $run->{"connections"} and $run->{"completed"} == $run->{"connections"}
!ll&&!rl&&r
000
MC/DC0%0/2
$run->{"accepted"} == $run->{"connections"} and $run->{"completed"} == $run->{"connections"}
$run->{"accepted"} == $run->{"connections"} $run->{"completed"} == $run->{"connections"}
Truth table0%0/3
A and B
A= $run->{"accepted"} == $run->{"connections"}B= $run->{"completed"} == $run->{"connections"}
ABresult
0X0
100
111
102 0 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 0 ▶sub launch_requests ($run) {
Subroutine launch_requests: not called CC=3 SCAR=24.8
118 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
119 && $run->{started} < $run->{connections}) {
120 0 $run->{started}++;
121 0 $run->{active}++;
122 BenchListenClient->connect(
123 loop => $run->{loop},
124 host => '127.0.0.1',
125 port => $run->{port},
126 0 timeout => $timeout,
127 data => $run,
128 );
129 }
130 0 return;
131 }
132  
133 0 ▶sub one_run ($mode, $client_count) {
Subroutine one_run: not called CC=8 SCAR=42.8
134 0 my $loop = Linux::Event::Loop->new;
135 0 $loop->enable_watcher_reclaim(1);
136 0 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 0 ▶ if ($mode eq 'manual') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'manual') { } 0 0 0
elsif ($mode eq 'add') { } 0 0 0
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 0 $run->{listener} = BenchAutomaticListener->new(
164 stream_class => 'BenchAutomaticStream',
165 host => '127.0.0.1', port => 0, data => $run,
166 );
167 0 $loop->add($run->{listener});
168 0 $run->{port} = $run->{listener}->port;
169 } else {
170 0 $run->{listener} = BenchAutomaticListener->new(
171 stream_class => 'BenchAutomaticStream',
172 loop => $loop, host => '127.0.0.1', port => 0, data => $run,
173 );
174 0 $run->{port} = $run->{listener}->port;
175 }
176  
177 0 my $wall_start = clock_gettime(CLOCK_MONOTONIC);
178 0 my ($user_start, $system_start) = (times)[0, 1];
179 0 launch_requests($run);
180 0 $loop->run;
181 0 my ($user_end, $system_end) = (times)[0, 1];
182 0 my $elapsed = clock_gettime(CLOCK_MONOTONIC) - $wall_start;
183 0 my $cpu = ($user_end - $user_start) + ($system_end - $system_start);
184  
185 0 ▶ if ($mode eq 'manual') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'manual') { } 0 0 0
186 0 $run->{listener_watcher}->cancel;
187 0 close $run->{listener_fh};
188 } else {
189 0 $run->{listener}->close;
190 }
191 return {
192 0 rate => $connections / $elapsed,
193 cpu_us => $cpu * 1_000_000 / $connections,
194 };
195 }
196  
197 0 ▶sub median (@values) {
Subroutine median: not called CC=2 SCAR=17.9
198 0 @values = sort { $a <=> $b } @values;
199 0 my $middle = int(@values / 2);
200 0 ▶ return @values % 2
Branch: 1 branch
BranchTrueFalseTotal
@values % 2 ? : 0 0 0
201 ? $values[$middle]
202 : ($values[$middle - 1] + $values[$middle]) / 2;
203 }
204  
205 0 say 'Median loopback TCP Listener lifecycle benchmark';
206 0 say "Linux::Event version $Linux::Event::Listener::VERSION";
207 0 printf "%-8s %8s %14s %14s\n",
208 qw(mode clients accepts/s cpu_us/accept);
209 0 for my $client_count (@clients) {
210 0 my %result;
211 0 for my $repeat (0 .. $repeats - 1) {
212 0 my $offset = $repeat % @modes;
213 0 my @order = (@modes[$offset .. $#modes], @modes[0 .. $offset - 1]);
214 0 push @{ $result{$_} }, one_run($_, $client_count) for @order;
215 }
216 0 for my $mode (@modes) {
217 printf "%-8s %8d %14.1f %14.3f\n",
218 $mode,
219 $client_count,
220 0 median(map { $_->{rate} } @{ $result{$mode} }),
221 0 median(map { $_->{cpu_us} } @{ $result{$mode} });
222 }
223 }
224  
225 1 ▶sub usage ($exit) {
Subroutine usage: called CC=1 SCAR=0.0
226 1 print <<'USAGE';
227 Usage: perl -Mblib bench/run-listen-microbench.pl [options]
228  
229 --modes=LIST manual,add,loop (default: all three)
230 --clients=LIST concurrent clients (default: 1,10,100)
231 --connections=N accepted connections per row (default: 10000)
232 --repeats=N median repetitions (default: 9)
233 --timeout=SECONDS catastrophic connection deadline (default: 30)
234 --help
235  
236 All rows acquire loopback TCP clients through MyStream->connect. Manual uses
237 explicit listener setup, Loop->watch, Perl accept, and close. Add uses detached
238 Listener construction followed by Loop->add. Loop supplies loop => directly to
239 Listener->new. Both Listener rows construct and close the same minimal Stream
240 subclass. Every successfully connected client
241 uses equal abortive teardown so repeated rows do not exhaust the host with
242 client-side TIME_WAIT sockets. Repeat execution order rotates to reduce bias;
243 use a repeat count divisible by three for balanced execution order.
244 USAGE
245 1 exit $exit;
246 }
« blib/lib/Linux/Event/Stream.pm ↑ Summary bench/run-connect-microbench.pl »