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

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