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

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