bench/run-stream-watcher-state-bench.pl

filter: Statementstmt 94.5% 366 / 387 Branchbran 58.7% 74 / 126 Conditioncond 38.1% 8 / 21 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 93.1% 68 / 73 Podpod n/a totaltotal 83.5% 518 / 620 CC 81 SCAR 14.7
CC
81
Cov
84%
git_commit
21.7
run_case
11.0
queue_once
7.3
CRAP 4.4
« bench/run-performance-regression.pl ↑ Summary blib/lib/Linux/Event/IO/Sock/Listener.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 Errno qw(EAGAIN EWOULDBLOCK EINTR);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Fcntl qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 F_GETFD F_SETFD FD_CLOEXEC
11 F_GETFL F_SETFL O_NONBLOCK
12 1 );
13 1 ▶use FindBin qw($Bin);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use POSIX qw(strftime uname);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Socket qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
18 AF_UNIX SOCK_STREAM PF_UNSPEC
19 SOL_SOCKET SO_SNDBUF
20 1 );
21 1 ▶use Time::HiRes qw(time clock_gettime CLOCK_PROCESS_CPUTIME_ID);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use Linux::Event::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Linux::Event::IO::Sock::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Linux::Event::IO::Sock::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Linux::Event::TLS;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 {
29 package Linux::Event::Bench::WatcherState::Stream;
30 1 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 0 ▶ sub on_data ($stream, $bytes) { return }
Subroutine on_data: not called CC=1 SCAR=6.9
32 }
33  
34 {
35 1 package Linux::Event::Bench::WatcherState::TLSStream;
36 1 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 4 ▶ sub on_transport_ready ($stream) { $stream->data->{ready}++ }
Subroutine on_transport_ready: called CC=1 SCAR=0.0
38 0 ▶ sub on_data ($stream, $bytes) { return }
Subroutine on_data: not called CC=1 SCAR=6.9
39 1 ▶ sub on_eof ($stream) { $stream->data->{eof}++ }
Subroutine on_eof: called CC=1 SCAR=0.0
40 0 ▶ sub on_error ($stream, $error) {
Subroutine on_error: not called CC=2 SCAR=17.9
41 0 ▶ $stream->data->{error} //= "$error";
Condition0%0/2
$stream->data->{"error"} //= "$error"
l!l
00
MC/DC0%0/1
$stream->data->{"error"} //= "$error"
$stream->data->{"error"}
Truth table0%0/2
A
A= $stream->data->{"error"}
Aresult
11
00
42 0 $stream->loop->stop;
43 0 return;
44 }
45 }
46  
47 1 my @all_cases = qw(
48 watcher-read-toggle
49 xsstate-pause-toggle
50 stream-pause-resume
51 raw-register-cancel
52 stream-attach-detach
53 stream-half-close
54 stream-close
55 queued-write-drain
56 tls-handshake
57 tls-shutdown
58 );
59 1 my %known_case = map { $_ => 1 } @all_cases;
60  
61 1 my $contract_version = 1;
62 1 my $operations = 10_000;
63 1 my $pool_size = 256;
64 1 my $warmup = 1_000;
65 1 my $repeats = 7;
66 1 my $queue_bytes = 12_288;
67 1 my $send_buffer = 4_096;
68 1 my $tls_pairs = 64;
69 1 my $timeout = 10;
70 1 my @cases = @all_cases;
71 1 my $cert_file = "$Bin/../t/tls-certs/server-cert.pem";
72 1 my $key_file = "$Bin/../t/tls-certs/server-key.pem";
73 1 my $json_path;
74 my $help;
75  
76 GetOptions(
77 'operations=i' => \$operations,
78 'pool=i' => \$pool_size,
79 'warmup=i' => \$warmup,
80 'repeats=i' => \$repeats,
81 'queue-bytes=i' => \$queue_bytes,
82 'send-buffer=i' => \$send_buffer,
83 'tls-pairs=i' => \$tls_pairs,
84 'timeout=f' => \$timeout,
85 1 ▶ 'cases=s' => sub { @cases = split /,/, $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
86 1 ▶ 'cert-file=s' => \$cert_file,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("operations=i", \$operations, "pool=i", \$pool_size, "warmup=i", \$warmup, "repeats=i", \$repeats, "queue-bytes=i", \$queue_bytes, "send-buffer=i", \$send_buffer, "tls-pairs=i", \$tls_pairs, "timeout=f", \$timeout, "cases=s", sub { @cases = split(/,/u, $_[1], 0); } , "cert-file=s", \$cert_file, "key-file=s", \$key_file, "json=s", \$json_path, "help", \$help) or usage(2) 1 0 1
87 'key-file=s' => \$key_file,
88 'json=s' => \$json_path,
89 'help' => \$help,
90 ) or usage(2);
91  
92 1 ▶usage(0) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 1 1
93 1 ▶die "operations must be > 0\n" if $operations <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $operations <= 0 0 1 1
94 1 ▶die "pool must be > 0\n" if $pool_size <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pool_size <= 0 0 1 1
95 1 ▶die "warmup must be >= 0\n" if $warmup < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $warmup < 0 0 1 1
96 1 ▶die "repeats must be > 0\n" if $repeats <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $repeats <= 0 0 1 1
97 1 ▶die "queue-bytes must be > 0\n" if $queue_bytes <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $queue_bytes <= 0 0 1 1
98 1 ▶die "send-buffer must be > 0\n" if $send_buffer <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $send_buffer <= 0 0 1 1
99 1 ▶die "tls-pairs must be > 0\n" if $tls_pairs <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tls_pairs <= 0 0 1 1
100 1 ▶die "timeout must be > 0\n" if $timeout <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= 0 0 1 1
101 1 ▶die "at least one case is required\n" if !@cases;
Branch: 1 branch
BranchTrueFalseTotal
unless @cases 0 1 1
102 1 die "unknown case: $_\n" for grep { !$known_case{$_} } @cases;
103  
104 1 ▶if (grep { /^tls-/ } @cases) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^tls-/u;} @cases) 1 0 1
105 1 ▶ die "TLS certificate not found: $cert_file\n" if !-f $cert_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $cert_file 0 1 1
106 1 ▶ die "TLS private key not found: $key_file\n" if !-f $key_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $key_file 0 1 1
107 }
108  
109 1 say 'Linux::Event Stream watcher-state benchmark';
110 1 say "version=$Linux::Event::IO::Sock::Stream::VERSION perl=$^V pid=$$";
111 1 say "contract=$contract_version cases=" . join(',', @cases);
112 1 say "native_timing=enabled";
113  
114 1 my @records;
115 1 for my $repeat (1 .. $repeats) {
116 1 for my $case (rotated_cases($repeat)) {
117 10 my $row = run_case($case);
118 10 $row->{repeat} = $repeat;
119 10 push @records, $row;
120 printf "%s repeat=%d %.1f ops/s cpu=%.3f us/op ctl=%.3f/%.3f/%.3f mod=%.1f ns/op\n",
121 $case, $repeat, $row->{operations_per_second},
122 $row->{cpu_us_per_operation},
123 $row->{epoll_ctl_add_calls_per_operation},
124 $row->{epoll_ctl_mod_calls_per_operation},
125 $row->{epoll_ctl_del_calls_per_operation},
126 10 $row->{epoll_ctl_mod_ns_per_operation};
127 }
128 }
129  
130 1 my @summary;
131 1 say "\nMedian watcher-state summary";
132 1 printf "%-26s %14s %12s %9s %9s %9s %12s\n",
133 'case', 'ops/s', 'cpu us/op', 'add/op', 'mod/op', 'del/op', 'mod ns/op';
134 1 for my $case (@cases) {
135 10 my @set = grep { $_->{case} eq $case } @records;
136 my $row = {
137 benchmark_contract_version => $contract_version,
138 case => $case,
139 operation_unit => $set[0]{operation_unit},
140 operations => $set[0]{operations},
141 10 };
142 10 for my $key (qw(
143 operations_per_second cpu_us_per_operation
144 epoll_ctl_add_calls_per_operation
145 epoll_ctl_mod_calls_per_operation
146 epoll_ctl_del_calls_per_operation
147 epoll_ctl_add_ns_per_operation
148 epoll_ctl_mod_ns_per_operation
149 epoll_ctl_del_ns_per_operation
150 callback_calls_per_operation
151 ready_events_returned_per_operation
152 )) {
153 100 $row->{$key} = median(map { $_->{$key} } @set);
154 }
155 10 push @summary, $row;
156 printf "%-26s %14.1f %12.3f %9.3f %9.3f %9.3f %12.1f\n",
157 $case, $row->{operations_per_second}, $row->{cpu_us_per_operation},
158 $row->{epoll_ctl_add_calls_per_operation},
159 $row->{epoll_ctl_mod_calls_per_operation},
160 $row->{epoll_ctl_del_calls_per_operation},
161 10 $row->{epoll_ctl_mod_ns_per_operation};
162 }
163  
164 1 ▶if (defined $json_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json_path) 1 0 1
165 1 my $report = {
166 benchmark => 'linux-event-stream-watcher-state',
167 benchmark_contract_version => $contract_version,
168 generated_at => strftime('%Y-%m-%dT%H:%M:%SZ', gmtime),
169 environment => environment_info(),
170 configuration => {
171 operations => $operations,
172 pool => $pool_size,
173 warmup => $warmup,
174 repeats => $repeats,
175 queue_bytes => $queue_bytes,
176 send_buffer => $send_buffer,
177 tls_pairs => $tls_pairs,
178 timeout => $timeout,
179 cases => \@cases,
180 native_timing => JSON::PP::true,
181 },
182 records => \@records,
183 summary => \@summary,
184 notes => [
185 'Loop profiling is enabled so epoll_ctl nanoseconds are diagnostic, not release-throughput numbers.',
186 'watcher-read-toggle is the public native-registration floor for two real interest changes.',
187 'xsstate-pause-toggle isolates two Stream XSState boundary calls without epoll changes.',
188 'stream-pause-resume performs the public state and readiness transition pair.',
189 'raw-register-cancel and stream-attach-detach reuse pre-created socketpairs.',
190 'stream-half-close and stream-close time only terminal operations on pre-attached Streams.',
191 'queued-write-drain prefills the send buffer, forces EAGAIN, and validates exact byte delivery.',
192 'TLS rows count one client/server pair per operation and retain provider WANT counters.',
193 'Compare reports only when contract, configuration, build, kernel, Perl, and host match.',
194 ],
195 };
196  
197 1 my $dir = dirname($json_path);
198 1 ▶ make_path($dir) if $dir ne '.' && !-d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir ne "." and not -d $dir 0 1 1
Condition33%1/3
$dir ne "." and not -d $dir
!ll&&!rl&&r
010
MC/DC0%0/2
$dir ne "." and not -d $dir
$dir ne "." not -d $dir
Truth table33%1/3
A and not(B)
A= $dir ne "."B= not -d $dir
ABresult
0X0
100
111
199 1 ▶ 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" 1 0 1
200 1 print {$out} JSON::PP->new->canonical->pretty->encode($report);
201 1 ▶ close $out or die "close $json_path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $out or die "close ${json_path}: $!\n" 1 0 1
202 1 say "\nWrote $json_path";
203 }
204  
205 10 ▶sub run_case ($case) {
Subroutine run_case: called CC=11 SCAR=24.0
206 10 ▶ return run_watcher_read_toggle() if $case eq 'watcher-read-toggle';
Branch: 1 branch
BranchTrueFalseTotal
if $case eq "watcher-read-toggle" 1 9 10
207 9 ▶ return run_xsstate_pause_toggle() if $case eq 'xsstate-pause-toggle';
Branch: 1 branch
BranchTrueFalseTotal
if $case eq "xsstate-pause-toggle" 1 8 9
208 8 ▶ return run_stream_pause_resume() if $case eq 'stream-pause-resume';
Branch: 1 branch
BranchTrueFalseTotal
if $case eq "stream-pause-resume" 1 7 8
209 7 ▶ return run_lifecycle($case) if $case eq 'raw-register-cancel'
Branch: 1 branch
BranchTrueFalseTotal
if $case eq "raw-register-cancel" or $case eq "stream-attach-detach" 2 5 7
Condition100%3/3
$case eq "raw-register-cancel" or $case eq "stream-attach-detach"
l!l&&r!l&&!r
115
MC/DC100%2/2
$case eq "raw-register-cancel" or $case eq "stream-attach-detach"
$case eq "raw-register-cancel" $case eq "stream-attach-detach"
Truth table100%3/3
A or B
A= $case eq "raw-register-cancel"B= $case eq "stream-attach-detach"
ABresult
1X1
011
000
210 || $case eq 'stream-attach-detach';
211 5 ▶ return run_stream_half_close() if $case eq 'stream-half-close';
Branch: 1 branch
BranchTrueFalseTotal
if $case eq "stream-half-close" 1 4 5
212 4 ▶ return run_stream_close() if $case eq 'stream-close';
Branch: 1 branch
BranchTrueFalseTotal
if $case eq "stream-close" 1 3 4
213 3 ▶ return run_queued_write_drain() if $case eq 'queued-write-drain';
Branch: 1 branch
BranchTrueFalseTotal
if $case eq "queued-write-drain" 1 2 3
214 2 ▶ return run_tls_handshake() if $case eq 'tls-handshake';
Branch: 1 branch
BranchTrueFalseTotal
if $case eq "tls-handshake" 1 1 2
215 1 ▶ return run_tls_shutdown() if $case eq 'tls-shutdown';
Branch: 1 branch
BranchTrueFalseTotal
if $case eq "tls-shutdown" 1 0 1
216 0 die "unimplemented case: $case\n";
217 }
218  
219 1 ▶sub run_watcher_read_toggle () {
Subroutine run_watcher_read_toggle: called CC=1 SCAR=0.0
220 1 my $loop = Linux::Event::Loop->new;
221 1 my ($stream_fh, $peer_fh) = socket_pair();
222 1 my $watcher = raw_registration($loop, $stream_fh);
223 1 toggle_watcher($watcher, $warmup);
224 my $row = measure('watcher-read-toggle', 'toggle-cycle', $operations,
225 1 ▶ $loop, sub { toggle_watcher($watcher, $operations) });
Subroutine __ANON__: called CC=1 SCAR=0.0
226 1 $watcher->cancel;
227 1 close $stream_fh;
228 1 close $peer_fh;
229 1 return $row;
230 }
231  
232 1 ▶sub run_xsstate_pause_toggle () {
Subroutine run_xsstate_pause_toggle: called CC=1 SCAR=0.0
233 1 my $loop = Linux::Event::Loop->new;
234 1 my ($stream_fh, $peer_fh) = socket_pair();
235 1 my $stream = Linux::Event::Bench::WatcherState::Stream->new(
236 loop => $loop, fh => $stream_fh,
237 );
238 1 my $xs_state = $stream->{xs_state};
239 1 toggle_xsstate($xs_state, $warmup);
240 my $row = measure('xsstate-pause-toggle', 'toggle-cycle', $operations,
241 1 ▶ $loop, sub { toggle_xsstate($xs_state, $operations) });
Subroutine __ANON__: called CC=1 SCAR=0.0
242 1 my $detached = $stream->detach;
243 1 close $detached;
244 1 close $peer_fh;
245 1 return $row;
246 }
247  
248 1 ▶sub run_stream_pause_resume () {
Subroutine run_stream_pause_resume: called CC=1 SCAR=0.0
249 1 my $loop = Linux::Event::Loop->new;
250 1 my ($stream_fh, $peer_fh) = socket_pair();
251 1 my $stream = Linux::Event::Bench::WatcherState::Stream->new(
252 loop => $loop, fh => $stream_fh,
253 );
254 1 toggle_stream($stream, $warmup);
255 my $row = measure('stream-pause-resume', 'toggle-cycle', $operations,
256 1 ▶ $loop, sub { toggle_stream($stream, $operations) });
Subroutine __ANON__: called CC=1 SCAR=0.0
257 1 my $detached = $stream->detach;
258 1 close $detached;
259 1 close $peer_fh;
260 1 return $row;
261 }
262  
263 2 ▶sub run_lifecycle ($case) {
Subroutine run_lifecycle: called CC=3 SCAR=11.0
264 2 my $loop = Linux::Event::Loop->new;
265 2 my ($stream_fh, $peer_fh) = socket_pool($pool_size);
266 2 lifecycle_many($case, $loop, $stream_fh, $warmup);
267 my $row = measure($case, 'lifecycle', $operations, $loop,
268 2 ▶ sub { lifecycle_many($case, $loop, $stream_fh, $operations) });
Subroutine __ANON__: called CC=1 SCAR=0.0
269 2 close $_ for @$stream_fh;
270 2 close $_ for @$peer_fh;
271 2 return $row;
272 }
273  
274 1 ▶sub run_stream_half_close () {
Subroutine run_stream_half_close: called CC=3 SCAR=11.0
275 1 my $loop = Linux::Event::Loop->new;
276 1 my ($stream_fh, $peer_fh) = socket_pool($pool_size);
277 my @streams = map {
278 1 Linux::Event::Bench::WatcherState::Stream->new(
279 loop => $loop, fh => $_,
280 )
281 } @$stream_fh;
282 my $row = measure('stream-half-close', 'stream', scalar(@streams),
283 1 ▶ $loop, sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
284 1 $_->end for @streams;
285 die "plain Stream half-close did not complete synchronously\n"
286 1 ▶ if grep { !$_->is_write_ended } @streams;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $_->is_write_ended;} @streams 0 1 1
287 1 });
288 1 close $_->detach for @streams;
289 1 close $_ for @$peer_fh;
290 1 return $row;
291 }
292  
293 1 ▶sub run_stream_close () {
Subroutine run_stream_close: called CC=3 SCAR=11.0
294 1 my $loop = Linux::Event::Loop->new;
295 1 my ($stream_fh, $peer_fh) = socket_pool($pool_size);
296 my @streams = map {
297 1 Linux::Event::Bench::WatcherState::Stream->new(
298 loop => $loop, fh => $_,
299 )
300 } @$stream_fh;
301 my $row = measure('stream-close', 'stream', scalar(@streams),
302 1 ▶ $loop, sub { $_->close for @streams });
Subroutine __ANON__: called CC=2 SCAR=6.9
303 die "Stream close case left an object open\n"
304 1 ▶ if grep { !$_->is_closed } @streams;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $_->is_closed;} @streams 0 1 1
305 1 close $_ for @$peer_fh;
306 1 return $row;
307 }
308  
309 1 ▶sub run_queued_write_drain () {
Subroutine run_queued_write_drain: called CC=4 SCAR=13.9
310 1 my $loop = Linux::Event::Loop->new;
311 1 my (@streams, @peers);
312 1 for my $i (0 .. $pool_size - 1) {
313 2 my ($stream_fh, $peer_fh) = socket_pair(send_buffer => $send_buffer);
314 2 push @streams, Linux::Event::Bench::WatcherState::Stream->new(
315 loop => $loop, fh => $stream_fh,
316 );
317 2 push @peers, $peer_fh;
318 }
319 1 my $payload = 'x' x $queue_bytes;
320 1 queue_many($loop, \@streams, \@peers, $payload, $warmup);
321 1 my $before = sum_stream_stats(\@streams);
322 my $row = measure('queued-write-drain', 'queue-cycle', $operations,
323 1 ▶ $loop, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
324 1 queue_many($loop, \@streams, \@peers, $payload, $operations);
325 1 });
326 1 my $after = sum_stream_stats(\@streams);
327 1 add_extra_stats($row, 'stream', counter_delta($before, $after),
328 scalar($operations));
329 1 close $_->detach for @streams;
330 1 close $_ for @peers;
331 1 return $row;
332 }
333  
334 1 ▶sub run_tls_handshake () {
Subroutine run_tls_handshake: called CC=2 SCAR=6.9
335 1 my ($loop, $state, $spec) = tls_setup($tls_pairs);
336 1 my (@clients, @servers);
337 1 ▶ my $row = measure('tls-handshake', 'tls-pair', $tls_pairs, $loop, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
338 1 for my $item (@$spec) {
339 push @servers, Linux::Event::Bench::WatcherState::TLSStream->new(
340 loop => $loop, fh => $item->{server_fh}, data => $state,
341 transport => $item->{server_tls},
342 1 );
343 push @clients, Linux::Event::Bench::WatcherState::TLSStream->new(
344 loop => $loop, fh => $item->{client_fh}, data => $state,
345 transport => $item->{client_tls},
346 1 );
347 }
348 drive_until($loop, $state,
349 1 ▶ sub { $state->{ready} == $tls_pairs * 2 }, 'TLS handshake');
Subroutine __ANON__: called CC=1 SCAR=0.0
350 1 });
351 1 add_extra_stats($row, 'tls', sum_tls_stats($spec), $tls_pairs);
352 1 $_->close for @clients, @servers;
353 1 return $row;
354 }
355  
356 1 ▶sub run_tls_shutdown () {
Subroutine run_tls_shutdown: called CC=3 SCAR=11.0
357 1 my ($loop, $state, $spec) = tls_setup($tls_pairs);
358 1 my (@clients, @servers);
359 1 for my $item (@$spec) {
360 push @servers, Linux::Event::Bench::WatcherState::TLSStream->new(
361 loop => $loop, fh => $item->{server_fh}, data => $state,
362 transport => $item->{server_tls},
363 1 );
364 push @clients, Linux::Event::Bench::WatcherState::TLSStream->new(
365 loop => $loop, fh => $item->{client_fh}, data => $state,
366 transport => $item->{client_tls},
367 1 );
368 }
369 drive_until($loop, $state,
370 1 ▶ sub { $state->{ready} == $tls_pairs * 2 }, 'TLS setup handshake');
Subroutine __ANON__: called CC=1 SCAR=0.0
371 1 my $before = sum_tls_stats($spec);
372 1 ▶ my $row = measure('tls-shutdown', 'tls-pair', $tls_pairs, $loop, sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
373 1 $_->end for @clients;
374 drive_until($loop, $state,
375 1 ▶ sub { $state->{eof} == $tls_pairs }, 'TLS shutdown');
Subroutine __ANON__: called CC=1 SCAR=0.0
376 die "TLS shutdown left a client write side open\n"
377 1 ▶ if grep { !$_->is_write_ended } @clients;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $_->is_write_ended;} @clients 0 1 1
378 1 });
379 1 my $after = sum_tls_stats($spec);
380 1 add_extra_stats($row, 'tls', counter_delta($before, $after), $tls_pairs);
381 1 $_->close for @clients, @servers;
382 1 return $row;
383 }
384  
385 10 ▶sub measure ($case, $unit, $count, $loop, $code) {
Subroutine measure: called CC=3 SCAR=11.0
386 10 $loop->profile(1);
387 10 $loop->reset_stats;
388 10 my $wall_start = time;
389 10 my $cpu_start = clock_gettime(CLOCK_PROCESS_CPUTIME_ID);
390 10 $code->();
391 10 my $cpu_seconds = clock_gettime(CLOCK_PROCESS_CPUTIME_ID) - $cpu_start;
392 10 my $elapsed_seconds = time - $wall_start;
393 10 ▶ die "timer produced a non-positive interval for $case\n"
Branch: 1 branch
BranchTrueFalseTotal
if $elapsed_seconds <= 0 0 10 10
394 if $elapsed_seconds <= 0;
395  
396 10 my $stats = $loop->stats;
397 my $row = {
398 benchmark_contract_version => $contract_version,
399 case => $case,
400 operation_unit => $unit,
401 operations => $count,
402 elapsed_seconds => $elapsed_seconds,
403 cpu_seconds => $cpu_seconds,
404 operations_per_second => $count / $elapsed_seconds,
405 cpu_us_per_operation => ($cpu_seconds * 1_000_000) / $count,
406 loop_stats => {
407 10 map { $_ => 0 + $stats->{$_} } loop_stat_keys()
408 },
409 };
410 10 for my $key (loop_stat_keys()) {
411 110 $row->{"${key}_per_operation"} = $stats->{$key} / $count;
412 }
413 10 return $row;
414 }
415  
416 6 ▶sub raw_registration ($loop, $fh) {
Subroutine raw_registration: called CC=1 SCAR=0.0
417 6 my $watcher = $loop->watch_fd(
418 fileno($fh),
419 fh => $fh,
420 data => {},
421 read => \&_watch_ready,
422 write => \&_watch_ready,
423 error => \&_watch_ready,
424 _callback_data_arg => 1,
425 );
426 6 $watcher->disable_write;
427 6 return $watcher;
428 }
429  
430 0 ▶sub _watch_ready ($data) { return }
Subroutine _watch_ready: not called CC=1 SCAR=6.9
431  
432 2 ▶sub toggle_watcher ($watcher, $count) {
Subroutine toggle_watcher: called CC=2 SCAR=6.9
433 2 for (1 .. $count) {
434 5 $watcher->disable_read;
435 5 $watcher->enable_read;
436 }
437 }
438  
439 2 ▶sub toggle_xsstate ($xs_state, $count) {
Subroutine toggle_xsstate: called CC=2 SCAR=6.9
440 2 for (1 .. $count) {
441 5 $xs_state->_pause;
442 5 $xs_state->_resume;
443 }
444 }
445  
446 2 ▶sub toggle_stream ($stream, $count) {
Subroutine toggle_stream: called CC=2 SCAR=6.9
447 2 for (1 .. $count) {
448 5 $stream->pause_read;
449 5 $stream->resume_read;
450 }
451 }
452  
453 4 ▶sub lifecycle_many ($case, $loop, $stream_fh, $count) {
Subroutine lifecycle_many: called CC=4 SCAR=13.9
454 4 for my $i (0 .. $count - 1) {
455 10 my $fh = $stream_fh->[$i % @$stream_fh];
456 10 ▶ if ($case eq 'raw-register-cancel') {
Branch: 1 branch
BranchTrueFalseTotal
if ($case eq "raw-register-cancel") 5 5 10
457 5 raw_registration($loop, $fh)->cancel;
458 5 next;
459 }
460 5 my $stream = Linux::Event::Bench::WatcherState::Stream->new(fh => $fh);
461 5 $loop->add($stream);
462 5 my $detached = $stream->detach;
463 5 ▶ die "detach returned the wrong filehandle\n"
Branch: 1 branch
BranchTrueFalseTotal
if fileno $detached != fileno $fh 0 5 5
464 if fileno($detached) != fileno($fh);
465 }
466 }
467  
468 2 ▶sub queue_many ($loop, $streams, $peers, $payload, $count) {
Subroutine queue_many: called CC=2 SCAR=6.9
469 2 for my $i (0 .. $count - 1) {
470 5 queue_once($loop, $streams->[$i % @$streams],
471 $peers->[$i % @$peers], $payload);
472 }
473 }
474  
475 5 ▶sub queue_once ($loop, $stream, $peer, $payload) {
Subroutine queue_once: called CC=7 SCAR=19.9
476 5 ▶ die "queue cycle began with pending output\n" if $stream->pending_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if $stream->pending_bytes 0 5 5
477 5 my $prefilled = fill_send_buffer($stream->fh);
478 5 $stream->write($payload);
479 5 ▶ die "prefilled send buffer did not force queued output\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $stream->pending_bytes 0 5 5
480 if !$stream->pending_bytes;
481  
482 5 my $received = drain_peer($peer);
483 5 my $turns = 0;
484 5 ▶ while ($stream->pending_bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream->pending_bytes) 10 5 15
485 10 ▶ die "queued write did not drain after 1000 Loop turns\n"
Branch: 1 branch
BranchTrueFalseTotal
if ++$turns > 1000 0 10 10
486 if ++$turns > 1_000;
487 10 my $ready = $loop->run_once(1_000);
488 10 ▶ die "queued write timed out waiting for writable readiness\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $ready 0 10 10
489 if !$ready;
490 10 $received += drain_peer($peer);
491 }
492 5 $received += drain_peer($peer);
493 5 my $expected = $prefilled + length($payload);
494 5 ▶ die "queued write delivered $received bytes, expected $expected\n"
Branch: 1 branch
BranchTrueFalseTotal
if $received != $expected 0 5 5
495 if $received != $expected;
496 }
497  
498 5 ▶sub fill_send_buffer ($fh) {
Subroutine fill_send_buffer: called CC=6 SCAR=19.1
499 5 state $filler = 'f' x 65_536;
500 5 my $total = 0;
501 5 while (1) {
502 10 my $written = syswrite($fh, $filler);
503 10 ▶ if (defined $written) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $written) 5 5 10
504 5 ▶ die "send-buffer prefill made no progress\n" if $written == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $written == 0 0 5 5
505 5 $total += $written;
506 5 next;
507 }
508 5 ▶ next if $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 5 5
509 5 ▶ return $total if $! == EAGAIN || $! == EWOULDBLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 or $! == 11 5 0 5
Condition33%1/3
$! == 11 or $! == 11
l!l&&r!l&&!r
500
MC/DC0%0/2
$! == 11 or $! == 11
$! == 11 $! == 11
Truth table33%1/3
A or B
A= $! == 11B= $! == 11
ABresult
1X1
011
000
510 0 die "send-buffer prefill syswrite: $!\n";
511 }
512 }
513  
514 20 ▶sub drain_peer ($fh) {
Subroutine drain_peer: called CC=6 SCAR=19.2
515 20 my $total = 0;
516 20 while (1) {
517 35 my $read = sysread($fh, my $buffer, 65_536);
518 35 ▶ if (defined $read) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read) 15 20 35
519 15 ▶ die "queued write peer reached EOF\n" if $read == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $read == 0 0 15 15
520 15 $total += $read;
521 15 next;
522 }
523 20 ▶ next if $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 20 20
524 20 ▶ return $total if $! == EAGAIN || $! == EWOULDBLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 or $! == 11 20 0 20
Condition33%1/3
$! == 11 or $! == 11
l!l&&r!l&&!r
2000
MC/DC0%0/2
$! == 11 or $! == 11
$! == 11 $! == 11
Truth table33%1/3
A or B
A= $! == 11B= $! == 11
ABresult
1X1
011
000
525 0 die "queued write peer sysread: $!\n";
526 }
527 }
528  
529 2 ▶sub tls_setup ($count) {
Subroutine tls_setup: called CC=2 SCAR=6.9
530 2 my $loop = Linux::Event::Loop->new;
531 2 my $state = { ready => 0, eof => 0 };
532 2 my @spec;
533 2 for my $i (0 .. $count - 1) {
534 2 my ($client_fh, $server_fh) = socket_pair();
535 2 push @spec, {
536 client_fh => $client_fh,
537 server_fh => $server_fh,
538 client_tls => Linux::Event::TLS->client(
539 server_name => 'localhost', ca_file => $cert_file,
540 ),
541 server_tls => Linux::Event::TLS->server(
542 cert_file => $cert_file, key_file => $key_file,
543 ),
544 };
545 }
546 2 return ($loop, $state, \@spec);
547 }
548  
549 3 ▶sub drive_until ($loop, $state, $done, $label) {
Subroutine drive_until: called CC=4 SCAR=14.1
550 3 my $deadline = time + $timeout;
551 3 ▶ while (!$done->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not &$done()) 9 3 12
552 9 ▶ die "$label failed: $state->{error}\n" if defined $state->{error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $state->{"error"} 0 9 9
553 9 ▶ die "$label exceeded ${timeout}s\n" if time >= $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if &main::time() >= $deadline 0 9 9
554 9 $loop->run_once(100);
555 }
556 }
557  
558 4 ▶sub socket_pool ($count) {
Subroutine socket_pool: called CC=2 SCAR=6.9
559 4 my (@stream_fh, @peer_fh);
560 4 for my $i (0 .. $count - 1) {
561 8 my ($stream, $peer) = socket_pair();
562 8 push @stream_fh, $stream;
563 8 push @peer_fh, $peer;
564 }
565 4 return (\@stream_fh, \@peer_fh);
566 }
567  
568 15 ▶sub socket_pair (%opt) {
Subroutine socket_pair: called CC=4 SCAR=14.0
569 15 ▶ socketpair(my $stream, my $peer, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
Branch: 1 branch
BranchTrueFalseTotal
socketpair my $stream, my $peer, 1, 1, 0 or die "socketpair: $!" 15 0 15
570 or die "socketpair: $!";
571 15 prepare_fh($stream);
572 15 prepare_fh($peer);
573 15 ▶ if (defined $opt{send_buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"send_buffer"}) 2 13 15
574 setsockopt($stream, SOL_SOCKET, SO_SNDBUF,
575 2 ▶ pack('i', $opt{send_buffer}))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $stream, 1, 7, pack("i", $opt{"send_buffer"}) or die "setsockopt(SO_SNDBUF): $!\n" 2 0 2
576 or die "setsockopt(SO_SNDBUF): $!\n";
577 }
578 15 return ($stream, $peer);
579 }
580  
581 30 ▶sub prepare_fh ($fh) {
Subroutine prepare_fh: called CC=5 SCAR=17.2
582 30 my $status = fcntl($fh, F_GETFL, 0);
583 30 ▶ die "fcntl(F_GETFL): $!\n" if !defined $status;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status 0 30 30
584 30 ▶ fcntl($fh, F_SETFL, $status | O_NONBLOCK)
Branch: 1 branch
BranchTrueFalseTotal
fcntl $fh, 4, $status | 2048 or die "fcntl(F_SETFL): $!\n" 30 0 30
585 or die "fcntl(F_SETFL): $!\n";
586 30 my $descriptor = fcntl($fh, F_GETFD, 0);
587 30 ▶ die "fcntl(F_GETFD): $!\n" if !defined $descriptor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $descriptor 0 30 30
588 30 ▶ fcntl($fh, F_SETFD, $descriptor | FD_CLOEXEC)
Branch: 1 branch
BranchTrueFalseTotal
fcntl $fh, 2, $descriptor | 1 or die "fcntl(F_SETFD): $!\n" 30 0 30
589 or die "fcntl(F_SETFD): $!\n";
590 }
591  
592 20 ▶sub loop_stat_keys () {
Subroutine loop_stat_keys: called CC=1 SCAR=0.0
593 20 return qw(
594 epoll_wait_calls ready_events_returned callback_calls
595 epoll_ctl_add_calls epoll_ctl_mod_calls epoll_ctl_del_calls
596 epoll_wait_ns epoll_ctl_add_ns epoll_ctl_mod_ns epoll_ctl_del_ns
597 dispatch_ns
598 );
599 }
600  
601 2 ▶sub sum_stream_stats ($streams) {
Subroutine sum_stream_stats: called CC=3 SCAR=11.0
602 2 my %sum;
603 2 for my $stream (@$streams) {
604 4 my $stats = $stream->{xs_state}->stats;
605 4 $sum{$_} += $stats->{$_} for qw(
606 write_submit_calls write_ready_calls write_calls writev_calls
607 write_eagain_count empty_calls bytes_written
608 );
609 }
610 2 return \%sum;
611 }
612  
613 3 ▶sub sum_tls_stats ($spec) {
Subroutine sum_tls_stats: called CC=4 SCAR=13.9
614 3 my %sum;
615 3 for my $item (@$spec) {
616 3 for my $tls ($item->{client_tls}, $item->{server_tls}) {
617 6 my $stats = $tls->stats;
618 6 $sum{$_} += $stats->{$_} for qw(
619 handshake_calls handshake_successes read_calls write_calls
620 writev_calls shutdown_calls want_read_count want_write_count
621 interrupt_count error_count clean_eof_count unclean_eof_count
622 );
623 }
624 }
625 3 return \%sum;
626 }
627  
628 2 ▶sub counter_delta ($before, $after) {
Subroutine counter_delta: called CC=3 SCAR=12.0
629 2 ▶ return { map { $_ => ($after->{$_} // 0) - ($before->{$_} // 0) }
Condition50%2/4
$after->{$_} // 0
l!l
190
$before->{$_} // 0
l!l
190
MC/DC0%0/1
$after->{$_} // 0
$after->{$_}
MC/DC0%0/1
$before->{$_} // 0
$before->{$_}
Truth table50%1/2
A
A= $after->{$_}
Aresult
11
00
Truth table50%1/2
A
A= $before->{$_}
Aresult
11
00
630 keys %$after };
631 }
632  
633 3 ▶sub add_extra_stats ($row, $name, $stats, $count) {
Subroutine add_extra_stats: called CC=1 SCAR=0.0
634 3 $row->{"${name}_stats"} = $stats;
635 $row->{"${name}_stats_per_operation"} = {
636 3 map { $_ => $stats->{$_} / $count } keys %$stats
637 };
638 }
639  
640 1 ▶sub rotated_cases ($repeat) {
Subroutine rotated_cases: called CC=1 SCAR=0.0
641 1 my $shift = ($repeat - 1) % @cases;
642 1 return (@cases[$shift .. $#cases], @cases[0 .. $shift - 1]);
643 }
644  
645 100 ▶sub median (@values) {
Subroutine median: called CC=3 SCAR=12.7
646 100 ▶ die "median requires at least one value\n" if !@values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 100 100
647 0 @values = sort { $a <=> $b } @values;
648 100 my $middle = int(@values / 2);
649 100 ▶ return $values[$middle] if @values % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @values % 2 100 0 100
650 0 return ($values[$middle - 1] + $values[$middle]) / 2;
651 }
652  
653 1 ▶sub environment_info () {
Subroutine environment_info: called CC=1 SCAR=0.0
654 1 my ($sysname, $nodename, $release, $version, $machine) = uname();
655 return {
656 1 linux_event_version => $Linux::Event::IO::Sock::Stream::VERSION,
657 perl_version => "$^V",
658 perl_executable => $^X,
659 os => $^O,
660 uname => {
661 sysname => $sysname,
662 release => $release,
663 version => $version,
664 machine => $machine,
665 },
666 git_commit => git_commit(),
667 };
668 }
669  
670 1 ▶sub git_commit () {
Subroutine git_commit: called CC=5 SCAR=30.8
671 1 ▶ return undef if !-e "$Bin/../.git";
Branch: 1 branch
BranchTrueFalseTotal
unless -e "$main::Bin/../.git" 1 0 1
672 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
673 or return undef;
674 0 my $commit = <$git>;
675 0 my $ok = close $git;
676 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
677 0 chomp $commit;
678 0 return $commit;
679 }
680  
681 0 ▶sub usage ($status) {
Subroutine usage: not called CC=2 SCAR=17.9
682 0 ▶ my $fh = $status ? *STDERR : *STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 0 0 0
683 0 print {$fh} <<'USAGE';
684 Usage: perl -Mblib bench/run-stream-watcher-state-bench.pl [options]
685  
686 --operations=N operations for toggle/lifecycle/queue cases (default 10000)
687 --pool=N reusable or pre-attached plain Streams (default 256)
688 --warmup=N untimed operations for reusable cases (default 1000)
689 --repeats=N repeats per case (default 7)
690 --queue-bytes=N bytes submitted per forced queue cycle (default 12288)
691 --send-buffer=N requested queue-case SO_SNDBUF bytes (default 4096)
692 --tls-pairs=N client/server pairs in each TLS row (default 64)
693 --timeout=N catastrophic TLS timeout in seconds (default 10)
694 --cases=A,B comma-separated watcher-state cases
695 --cert-file=PATH TLS localhost certificate
696 --key-file=PATH TLS private key
697 --json=PATH write raw records and summaries as JSON
698 --help show this help
699  
700 Cases:
701 watcher-read-toggle, xsstate-pause-toggle, stream-pause-resume,
702 raw-register-cancel, stream-attach-detach, stream-half-close,
703 stream-close, queued-write-drain, tls-handshake, tls-shutdown
704 USAGE
705 0 exit $status;
706 }
« bench/run-performance-regression.pl ↑ Summary blib/lib/Linux/Event/IO/Sock/Listener.pm »