bench/run-first-class-framed-callback-bench.pl

filter: Statementstmt 90.1% 229 / 254 Branchbran 58.1% 64 / 110 Conditioncond 61.1% 11 / 18 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 90.9% 30 / 33 Podpod n/a totaltotal 79.2% 336 / 424 CC 43 SCAR 21.0
CC
43
Cov
80%
run_case
23.2
write_all
6.7
current_rss_kib
4.5
CRAP 8.2
« blib/lib/Linux/Event/Framer/Delimiter.pm ↑ Summary bench/run-callback-batching-microbench.pl »
1 #!/usr/bin/env perl
2 5 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Socket qw(AF_UNIX SOCK_STREAM PF_UNSPEC);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC CLOCK_PROCESS_CPUTIME_ID);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use Linux::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Linux::Event::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Linux::Event::IO::Sock::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 my @payload_sizes = (16, 4_096, 65_536);
17 5 my @idle_connections = (0, 63);
18 5 my $target_mib = 16;
19 5 my $minimum_messages = 512;
20 5 my $repeats = 7;
21 5 my $warmup = 1;
22 5 my $json_path;
23 my $help;
24  
25 GetOptions(
26 5 ▶ 'payload-sizes=s' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
27 5 @payload_sizes = map { 0 + $_ } split /,/, $_[1];
28 },
29 5 ▶ 'idle-connections=s' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 5 @idle_connections = map { 0 + $_ } split /,/, $_[1];
31 },
32 5 ▶ 'target-mib=f' => \$target_mib,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("payload-sizes=s", sub { @payload_sizes = map({0 + $_;} split(/,/u, $_[1], 0)); } , "idle-connections=s", sub { @idle_connections = map({0 + $_;} split(/,/u, $_[1], 0)); } , "target-mib=f", \$target_mib, "minimum-messages=i", \$minimum_messages, "repeats=i", \$repeats, "warmup=i", \$warmup, "json=s", \$json_path, "help", \$help) or usage(1) 5 0 5
33 'minimum-messages=i' => \$minimum_messages,
34 'repeats=i' => \$repeats,
35 'warmup=i' => \$warmup,
36 'json=s' => \$json_path,
37 'help' => \$help,
38 ) or usage(1);
39 5 ▶usage(0) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 5 5
40  
41 5 ▶die "payload sizes must be positive\n" if grep { $_ < 1 } @payload_sizes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ < 1;} @payload_sizes 0 5 5
42 die "idle connection counts must be non-negative\n"
43 5 ▶ if grep { $_ < 0 } @idle_connections;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ < 0;} @idle_connections 0 5 5
44 5 ▶die "target-mib must be positive\n" if $target_mib <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $target_mib <= 0 0 5 5
45 5 ▶die "minimum-messages must be positive\n" if $minimum_messages < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $minimum_messages < 1 0 5 5
46 5 ▶die "repeats must be positive\n" if $repeats < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $repeats < 1 0 5 5
47 5 ▶die "warmup must be non-negative\n" if $warmup < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $warmup < 0 0 5 5
48  
49 {
50 package Linux::Event::Bench::CachedClosureStream;
51 5 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
52 5 ▶ use Linux::Event::Framer 'Delimiter', "\n";
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 5 our ($CAPTURE_A, $CAPTURE_B, $CAPTURE_C, $CAPTURE_D) = (1, 2, 3, 4);
55  
56 4 ▶ sub stream_tuning ($class) {
Subroutine stream_tuning: called CC=1 SCAR=0.0
57 4 return read_size => 65_536;
58 }
59  
60 4096 ▶ sub on_message ($stream, $message) {
Subroutine on_message: called CC=4 SCAR=14.3
61 4096 ▶ if ($message eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($message eq "") 0 4096 4096
62 0 $CAPTURE_A += $CAPTURE_B + $CAPTURE_C + $CAPTURE_D;
63 }
64 4096 my $state = $stream->data;
65 4096 ▶ die "payload mismatch\n" if $message ne $state->{payload};
Branch: 1 branch
BranchTrueFalseTotal
if $message ne $state->{"payload"} 0 4096 4096
66 4096 $state->{messages}++;
67 $state->{loop}->stop
68 4096 ▶ if $state->{messages} == $state->{expected_messages};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"messages"} == $state->{"expected_messages"} 4 4092 4096
69 4096 return;
70 }
71  
72 0 ▶ sub on_error ($stream, $error) {
Subroutine on_error: not called CC=1 SCAR=6.9
73 0 die "Stream error: $error\n";
74 }
75 }
76  
77 5 our ($PLAIN_A, $PLAIN_B, $PLAIN_C, $PLAIN_D) = (1, 2, 3, 4);
78  
79 3072 ▶my $plain_coderef = sub ($stream, $message) {
Subroutine __ANON__: called CC=4 SCAR=14.3
80 3072 ▶ if ($message eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($message eq "") 0 3072 3072
81 0 $PLAIN_A += $PLAIN_B + $PLAIN_C + $PLAIN_D;
82 }
83 3072 my $state = $stream->data;
84 3072 ▶ die "payload mismatch\n" if $message ne $state->{payload};
Branch: 1 branch
BranchTrueFalseTotal
if $message ne $state->{"payload"} 0 3072 3072
85 3072 $state->{messages}++;
86 $state->{loop}->stop
87 3072 ▶ if $state->{messages} == $state->{expected_messages};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"messages"} == $state->{"expected_messages"} 3 3069 3072
88 3072 return;
89 5 };
90  
91 2 ▶sub closure_one () {
Subroutine closure_one: called CC=1 SCAR=0.1
92 2 my $capture_a = 1;
93 2048 ▶ return sub ($stream, $message) {
Subroutine __ANON__: called CC=4 SCAR=14.3
94 2048 ▶ if ($message eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($message eq "") 0 2048 2048
95 0 $capture_a += $PLAIN_B + $PLAIN_C + $PLAIN_D;
96 }
97 2048 my $state = $stream->data;
98 2048 ▶ die "payload mismatch\n" if $message ne $state->{payload};
Branch: 1 branch
BranchTrueFalseTotal
if $message ne $state->{"payload"} 0 2048 2048
99 2048 $state->{messages}++;
100 $state->{loop}->stop
101 2048 ▶ if $state->{messages} == $state->{expected_messages};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"messages"} == $state->{"expected_messages"} 2 2046 2048
102 2048 return;
103 2 };
104 }
105  
106 1 ▶sub closure_four () {
Subroutine closure_four: called CC=1 SCAR=0.1
107 1 my ($capture_a, $capture_b, $capture_c, $capture_d) = (1, 2, 3, 4);
108 1024 ▶ return sub ($stream, $message) {
Subroutine __ANON__: called CC=4 SCAR=14.3
109 1024 ▶ if ($message eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($message eq "") 0 1024 1024
110 0 $capture_a += $capture_b + $capture_c + $capture_d;
111 }
112 1024 my $state = $stream->data;
113 1024 ▶ die "payload mismatch\n" if $message ne $state->{payload};
Branch: 1 branch
BranchTrueFalseTotal
if $message ne $state->{"payload"} 0 1024 1024
114 1024 $state->{messages}++;
115 $state->{loop}->stop
116 1024 ▶ if $state->{messages} == $state->{expected_messages};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"messages"} == $state->{"expected_messages"} 1 1023 1024
117 1024 return;
118 1 };
119 }
120  
121 my @case = (
122 4 ▶ { name => 'subclass_method', callback => sub { return undef } },
Subroutine __ANON__: called CC=1 SCAR=0.0
123 3 ▶ { name => 'constructor_coderef', callback => sub { return $plain_coderef } },
Subroutine __ANON__: called CC=1 SCAR=0.0
124 5 { name => 'closure_one', callback => \&closure_one },
125 { name => 'closure_four', callback => \&closure_four },
126 );
127  
128 5 my @configuration;
129 5 for my $payload_size (@payload_sizes) {
130 5 my $target_bytes = int($target_mib * 1_048_576);
131 5 my $messages = int($target_bytes / $payload_size);
132 5 ▶ $messages = $minimum_messages if $messages < $minimum_messages;
Branch: 1 branch
BranchTrueFalseTotal
if $messages < $minimum_messages 0 5 5
133 push @configuration, map {
134 5 +{
135 5 payload_size => $payload_size,
136 idle_connections => $_,
137 messages => $messages,
138 }
139 } @idle_connections;
140 }
141  
142 5 for (1 .. $warmup) {
143 0 for my $config (@configuration) {
144 0 my $warm_messages = int($config->{messages} / 10);
145 0 ▶ $warm_messages = 100 if $warm_messages < 100;
Branch: 1 branch
BranchTrueFalseTotal
if $warm_messages < 100 0 0 0
146 $warm_messages = $config->{messages}
147 0 ▶ if $warm_messages > $config->{messages};
Branch: 1 branch
BranchTrueFalseTotal
if $warm_messages > $config->{"messages"} 0 0 0
148 0 for my $case (@case) {
149 0 run_case($case, { %$config, messages => $warm_messages });
150 }
151 }
152 }
153  
154 5 my @raw;
155 5 for my $repeat (1 .. $repeats) {
156 5 for my $config_index (0 .. $#configuration) {
157 5 my $config = $configuration[$config_index];
158 5 my @ordered = rotate_cases(
159 \@case, $repeat - 1 + $config_index,
160 );
161 5 for my $case (@ordered) {
162 14 my $row = run_case($case, $config);
163 10 $row->{repeat} = $repeat;
164 10 push @raw, $row;
165 printf "%s bytes=%d idle=%d repeat=%d %.1f msg/s %.3f cpu us/msg\n",
166 $case->{name}, $config->{payload_size},
167 $config->{idle_connections}, $repeat,
168 10 $row->{messages_per_second}, $row->{cpu_us_per_message};
169 }
170 }
171 }
172  
173 1 my @summary;
174 1 for my $config (@configuration) {
175 1 for my $case (@case) {
176 my @rows = grep {
177 4 $_->{case} eq $case->{name}
178 && $_->{payload_size} == $config->{payload_size}
179 && $_->{idle_connections} == $config->{idle_connections}
180 16 ▶ } @raw;
Condition66%4/6
$_->{'case'} eq $case->{'name'} && $_->{'payload_size'} == $config->{'payload_size'} && $_->{'idle_connections'} == $config->{'idle_connections'}
!ll&&!rl&&r
1204
$_->{"case"} eq $case->{"name"} and $_->{"payload_size"} == $config->{"payload_size"}
!ll&&!rl&&r
1204
MC/DC33%1/3
$_->{'case'} eq $case->{'name'} && $_->{'payload_size'} == $config->{'payload_size'} && $_->{'idle_connections'} == $config->{'idle_connections'}
$_->{"case"} eq $case->{"name"} $_->{"payload_size"} == $config->{"payload_size"} $_->{'idle_connections'} == $config->{'idle_connections'}
Truth table50%2/4
A and B && C
A= $_->{"case"} eq $case->{"name"}B= $_->{"payload_size"} == $config->{"payload_size"}C= $_->{'idle_connections'} == $config->{'idle_connections'}
ABCresult
0XX0
10X0
1100
1111
181 my $summary = {
182 case => $case->{name},
183 payload_size => $config->{payload_size},
184 idle_connections => $config->{idle_connections},
185 messages => $config->{messages},
186 median_messages_per_second => median(
187 4 map { $_->{messages_per_second} } @rows,
188 ),
189 median_cpu_us_per_message => median(
190 4 map { $_->{cpu_us_per_message} } @rows,
191 ),
192 median_setup_us_per_stream => median(
193 4 map { $_->{setup_us_per_stream} } @rows,
194 ),
195 median_rss_after_setup_kib => median(
196 4 map { $_->{rss_after_setup_kib} } @rows,
197 ),
198 };
199 4 ▶ if ($case->{name} ne 'subclass_method') {
Branch: 1 branch
BranchTrueFalseTotal
if ($case->{"name"} ne "subclass_method") 3 1 4
200 3 my (@throughput_delta, @cpu_delta);
201 3 for my $row (@rows) {
202 my ($paired_method) = grep {
203 3 $_->{case} eq 'subclass_method'
204 && $_->{payload_size} == $config->{payload_size}
205 && $_->{idle_connections}
206 == $config->{idle_connections}
207 && $_->{repeat} == $row->{repeat}
208 12 ▶ } @raw;
Condition66%6/9
$_->{'case'} eq 'subclass_method' && $_->{'payload_size'} == $config->{'payload_size'} && $_->{'idle_connections'} == $config->{'idle_connections'} && $_->{'repeat'} == $row->{'repeat'}
!ll&&!rl&&r
903
$_->{"case"} eq "subclass_method" and $_->{"payload_size"} == $config->{"payload_size"} and $_->{"idle_connections"} == $config->{"idle_connections"}
!ll&&!rl&&r
903
$_->{"case"} eq "subclass_method" and $_->{"payload_size"} == $config->{"payload_size"}
!ll&&!rl&&r
903
MC/DC25%1/4
$_->{'case'} eq 'subclass_method' && $_->{'payload_size'} == $config->{'payload_size'} && $_->{'idle_connections'} == $config->{'idle_connections'} && $_->{'repeat'} == $row->{'repeat'}
$_->{"case"} eq "subclass_method" $_->{"payload_size"} == $config->{"payload_size"} $_->{"idle_connections"} == $config->{"idle_connections"} $_->{'repeat'} == $row->{'repeat'}
Truth table40%2/5
A and B and C && D
A= $_->{"case"} eq "subclass_method"B= $_->{"payload_size"} == $config->{"payload_size"}C= $_->{"idle_connections"} == $config->{"idle_connections"}D= $_->{'repeat'} == $row->{'repeat'}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
209 push @throughput_delta, 100 * (
210 $row->{messages_per_second}
211 3 / $paired_method->{messages_per_second} - 1
212 );
213 push @cpu_delta, 100 * (
214 $row->{cpu_us_per_message}
215 3 / $paired_method->{cpu_us_per_message} - 1
216 );
217 }
218 3 $summary->{throughput_delta_percent} = median(@throughput_delta);
219 3 $summary->{cpu_delta_percent} = median(@cpu_delta);
220 }
221 4 push @summary, $summary;
222 }
223 }
224  
225 1 say "\nFirst-class framed callback dispatch benchmark";
226 1 printf "target_mib=%.3f minimum_messages=%d warmup=%d repeats=%d\n",
227 $target_mib, $minimum_messages, $warmup, $repeats;
228 1 printf "%-20s %8s %6s %12s %12s %10s %10s\n",
229 'case', 'bytes', 'idle', 'msg/s', 'cpu us/msg', 'speed', 'cpu';
230 1 for my $row (@summary) {
231 printf "%-20s %8d %6d %12.1f %12.3f %9s %9s\n",
232 $row->{case}, $row->{payload_size}, $row->{idle_connections},
233 $row->{median_messages_per_second},
234 $row->{median_cpu_us_per_message},
235 defined($row->{throughput_delta_percent})
236 ? sprintf('%+.2f%%', $row->{throughput_delta_percent}) : '-',
237 defined($row->{cpu_delta_percent})
238 4 ▶ ? sprintf('%+.2f%%', $row->{cpu_delta_percent}) : '-';
Branch: 2 branches
BranchTrueFalseTotal
defined $row->{'throughput_delta_percent'} ? : 3 1 4
defined $row->{'cpu_delta_percent'} ? : 3 1 4
239 }
240  
241 1 ▶if (defined $json_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json_path) 1 0 1
242 1 my $report = {
243 benchmark => 'linux-event-first-class-framed-callback-dispatch',
244 benchmark_contract_version => 2,
245 linux_event_version => $Linux::Event::VERSION,
246 perl_version => 0 + $],
247 configuration => {
248 payload_sizes => \@payload_sizes,
249 idle_connections => \@idle_connections,
250 target_mib => 0 + $target_mib,
251 minimum_messages => $minimum_messages,
252 repeats => $repeats,
253 warmup => $warmup,
254 framer => 'Delimiter',
255 delimiter_bytes => 1,
256 read_size => 65_536,
257 },
258 raw => \@raw,
259 summary => \@summary,
260 };
261 1 ▶ open my $json, '>', $json_path or die "open $json_path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $json, ">", $json_path or die "open ${json_path}: $!\n" 1 0 1
262 1 print {$json} JSON::PP->new->canonical->pretty->encode($report);
263 1 ▶ close $json or die "close $json_path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $json or die "close ${json_path}: $!\n" 1 0 1
264 }
265  
266 14 ▶sub run_case ($case, $config) {
Subroutine run_case: called CC=19 SCAR=31.4
267 14 ▶ socketpair(my $receiver, my $sender,
Branch: 1 branch
BranchTrueFalseTotal
socketpair my $receiver, my $sender, 1, 1, 0 or die "socketpair: $!" 14 0 14
268 AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die "socketpair: $!";
269 14 ▶ pipe(my $gate_read, my $gate_write) or die "gate pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $gate_read, my $gate_write or die "gate pipe: $!" 14 0 14
270  
271 14 my $payload = 'x' x $config->{payload_size};
272 14 my $wire = $payload . "\n";
273 14 my $writer = fork();
274 14 ▶ die "writer fork: $!" if !defined $writer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $writer 0 14 14
275 14 ▶ if ($writer == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writer == 0) 4 10 14
276 4 close $receiver;
277 4 close $gate_write;
278 4 my $gate = '';
279 4 ▶ sysread($gate_read, $gate, 1) == 1 or exit 2;
Branch: 1 branch
BranchTrueFalseTotal
sysread($gate_read, $gate, 1) == 1 or exit 2 4 0 4
280 4 close $gate_read;
281 4 local $SIG{PIPE} = 'IGNORE';
282 4 ▶ eval { write_frames($sender, $wire, $config->{messages}); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { write_frames($sender, $wire, $config->{"messages"}); 1 } } or exit 3 4 0 4
283 or exit 3;
284 4 close $sender;
285 4 exit 0;
286 }
287  
288 10 close $sender;
289 10 close $gate_read;
290 10 my $loop = Linux::Event::Loop->new;
291 my $state = {
292 loop => $loop,
293 payload => $payload,
294 messages => 0,
295 expected_messages => $config->{messages},
296 10 };
297 10 my $callback = $case->{callback}->();
298 10 ▶ my %callback_option = defined($callback)
Branch: 1 branch
BranchTrueFalseTotal
defined $callback ? : 6 4 10
299 ? (on_message => $callback) : ();
300 10 my @streams;
301 my @idle_peers;
302 10 my $rss_before_setup = current_rss_kib();
303 10 my $setup_start = clock_gettime(CLOCK_MONOTONIC);
304 10 push @streams, Linux::Event::Bench::CachedClosureStream->new(
305 loop => $loop,
306 fh => $receiver,
307 data => $state,
308 %callback_option,
309 );
310 10 for (1 .. $config->{idle_connections}) {
311 0 ▶ socketpair(my $idle_stream, my $idle_peer,
Branch: 1 branch
BranchTrueFalseTotal
socketpair my $idle_stream, my $idle_peer, 1, 1, 0 or die "socketpair: $!" 0 0 0
312 AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die "socketpair: $!";
313 0 push @idle_peers, $idle_peer;
314 0 push @streams, Linux::Event::Bench::CachedClosureStream->new(
315 loop => $loop,
316 fh => $idle_stream,
317 data => $state,
318 %callback_option,
319 );
320 }
321 10 my $setup_end = clock_gettime(CLOCK_MONOTONIC);
322 10 my $rss_after_setup = current_rss_kib();
323  
324 10 my $wall_start = clock_gettime(CLOCK_MONOTONIC);
325 10 my $cpu_start = clock_gettime(CLOCK_PROCESS_CPUTIME_ID);
326 10 ▶ syswrite($gate_write, 'g') == 1 or die "release writer: $!";
Branch: 1 branch
BranchTrueFalseTotal
syswrite($gate_write, "g") == 1 or die "release writer: $!" 10 0 10
327 10 close $gate_write;
328  
329 0 ▶ local $SIG{ALRM} = sub { die "first-class framed callback benchmark timed out\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
330 10 alarm 120;
331 10 my $ok = eval { $loop->run; 1 };
332 10 my $error = $@;
333 10 alarm 0;
334 10 my $cpu_end = clock_gettime(CLOCK_PROCESS_CPUTIME_ID);
335 10 my $wall_end = clock_gettime(CLOCK_MONOTONIC);
336  
337 10 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 10 10
338 0 kill 'TERM', $writer;
339 0 waitpid($writer, 0);
340 0 die $error;
341 }
342 10 waitpid($writer, 0);
343 10 ▶ die "writer failed with status $?\n" if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 0 10 10
344  
345 10 my $active = $streams[0];
346 10 my $stats = $active->{xs_state}->stats;
347 die "received $state->{messages}, expected $config->{messages}\n"
348 10 ▶ if $state->{messages} != $config->{messages};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"messages"} != $config->{"messages"} 0 10 10
349 die "native parser emitted $stats->{frames_emitted}, expected $config->{messages}\n"
350 10 ▶ if $stats->{frames_emitted} != $config->{messages};
Branch: 1 branch
BranchTrueFalseTotal
if $stats->{"frames_emitted"} != $config->{"messages"} 0 10 10
351 die "native callback count is $stats->{message_callback_calls}, expected $config->{messages}\n"
352 10 ▶ if $stats->{message_callback_calls} != $config->{messages};
Branch: 1 branch
BranchTrueFalseTotal
if $stats->{"message_callback_calls"} != $config->{"messages"} 0 10 10
353  
354 10 my $elapsed = $wall_end - $wall_start;
355 10 my $cpu = $cpu_end - $cpu_start;
356 10 my $stream_count = 1 + $config->{idle_connections};
357 10 for my $stream (@streams) {
358 10 ▶ $stream->close if !$stream->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
unless $stream->is_closed 10 0 10
359 }
360 10 for my $peer (@idle_peers) {
361 0 close $peer;
362 }
363  
364 return {
365 case => $case->{name},
366 payload_size => $config->{payload_size},
367 idle_connections => $config->{idle_connections},
368 messages => $config->{messages},
369 elapsed_seconds => 0 + $elapsed,
370 receiver_cpu_seconds => 0 + $cpu,
371 messages_per_second => $config->{messages} / $elapsed,
372 cpu_us_per_message => $cpu * 1_000_000 / $config->{messages},
373 setup_us_per_stream => ($setup_end - $setup_start) * 1_000_000
374 / $stream_count,
375 rss_before_setup_kib => $rss_before_setup,
376 rss_after_setup_kib => $rss_after_setup,
377 rss_setup_delta_kib => $rss_after_setup - $rss_before_setup,
378 read_calls => $stats->{read_calls},
379 frames_emitted => $stats->{frames_emitted},
380 callback_calls => $stats->{message_callback_calls},
381 10 };
382 }
383  
384 4 ▶sub write_frames ($fh, $wire, $messages) {
Subroutine write_frames: called CC=4 SCAR=14.0
385 4 my $frames_per_chunk = int(65_536 / length($wire));
386 4 ▶ $frames_per_chunk = 1 if $frames_per_chunk < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $frames_per_chunk < 1 0 4 4
387 4 my $remaining = $messages;
388 4 ▶ while ($remaining) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining) 4 4 8
389 4 ▶ my $count = $remaining < $frames_per_chunk
Branch: 1 branch
BranchTrueFalseTotal
$remaining < $frames_per_chunk ? : 4 0 4
390 ? $remaining : $frames_per_chunk;
391 4 write_all($fh, $wire x $count);
392 4 $remaining -= $count;
393 }
394 4 return;
395 }
396  
397 4 ▶sub write_all ($fh, $bytes) {
Subroutine write_all: called CC=6 SCAR=19.0
398 4 my $offset = 0;
399 4 ▶ while ($offset < length($bytes)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $bytes) 4 4 8
400 4 my $written = syswrite($fh, $bytes, length($bytes) - $offset, $offset);
401 4 ▶ next if !defined($written) && $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $written and $!{"EINTR"} 0 4 4
Condition33%1/3
not defined $written and $!{"EINTR"}
!ll&&!rl&&r
400
MC/DC0%0/2
not defined $written and $!{"EINTR"}
not defined $written $!{"EINTR"}
Truth table33%1/3
not(A) and B
A= not defined $writtenB= $!{"EINTR"}
ABresult
0X0
100
111
402 4 ▶ die "writer syswrite: $!\n" if !defined $written;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $written 0 4 4
403 4 ▶ die "writer syswrite returned zero\n" if $written == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $written == 0 0 4 4
404 4 $offset += $written;
405 }
406 4 return;
407 }
408  
409 20 ▶sub current_rss_kib () {
Subroutine current_rss_kib: called CC=4 SCAR=15.0
410 20 ▶ open my $status, '<', '/proc/self/status' or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $status, "<", "/proc/self/status" or return 0 20 0 20
411 20 ▶ while (my $line = <$status>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $status)) 460 0 460
412 460 ▶ if ($line =~ /\AVmRSS:\s+(\d+)\s+kB\s*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\AVmRSS:\s+(\d+)\s+kB\s*\z/u) 20 440 460
413 20 close $status;
414 20 return 0 + $1;
415 }
416 }
417 0 close $status;
418 0 return 0;
419 }
420  
421 5 ▶sub rotate_cases ($cases, $offset) {
Subroutine rotate_cases: called CC=1 SCAR=0.0
422 5 my $count = @$cases;
423 5 $offset %= $count;
424 5 return (@$cases[$offset .. $count - 1], @$cases[0 .. $offset - 1]);
425 }
426  
427 22 ▶sub median (@values) {
Subroutine median: called CC=2 SCAR=9.2
428 0 @values = sort { $a <=> $b } @values;
429 22 ▶ return $values[int(@values / 2)] if @values % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @values % 2 22 0 22
430 0 return ($values[@values / 2 - 1] + $values[@values / 2]) / 2;
431 }
432  
433 0 ▶sub usage ($status) {
Subroutine usage: not called CC=1 SCAR=6.9
434 0 print <<'USAGE';
435 Usage:
436 perl -Iblib/lib -Iblib/arch bench/run-first-class-framed-callback-bench.pl [options]
437  
438 Options:
439 --payload-sizes=16,4096,65536
440 --idle-connections=0,63
441 --target-mib=16
442 --minimum-messages=512
443 --repeats=7
444 --warmup=1
445 --json=path
446  
447 The benchmark compares one cached subclass method CV with a cached constructor
448 coderef, a closure retaining one lexical, and a closure retaining four
449 lexicals. Every case uses the same native delimiter framer and the same
450 executed callback work. Cases rotate within each repeated configuration.
451 Receiver process CPU excludes the forked blocking writer.
452 USAGE
453 0 exit $status;
454 }
« blib/lib/Linux/Event/Framer/Delimiter.pm ↑ Summary bench/run-callback-batching-microbench.pl »