bench/run-process-pipe-drain-bench.pl

filter: Statementstmt 90.9% 150 / 165 Branchbran 48.2% 27 / 56 Conditioncond 44.4% 12 / 27 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 89.2% 25 / 28 Podpod n/a totaltotal 73.6% 215 / 292 CC 17 SCAR 16.8
CC
17
Cov
76%
run_once
14.7
on_timer
6.0
on_exit
4.5
CRAP 5.4
« blib/lib/Linux/Event/Framer/Fixed.pm ↑ Summary bench/run-framer-send-bench.pl »
1 #!/usr/bin/env perl
2 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::HiRes qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
9 clock_gettime CLOCK_MONOTONIC CLOCK_PROCESS_CPUTIME_ID
10 1 );
11  
12 1 ▶use Linux::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Linux::Event::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Linux::Event::Kernel::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Linux::Event::Kernel::Timer;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 my @engines = qw(perl native);
18 1 my @streams = qw(stdout stderr both);
19 1 my @workers = (1, 8, 32);
20 1 my @read_sizes = (4_096, 65_536);
21 1 my $bytes_per_stream = 16 * 1024 * 1024;
22 1 my $max_reads_per_tick = 64;
23 1 my $heartbeat_us = 0;
24 1 my $warmups = 1;
25 1 my $repeats = 7;
26 1 my $json_path;
27 my $help;
28  
29 GetOptions(
30 1 ▶ 'engines=s' => sub { @engines = split /,/, $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 1 ▶ 'streams=s' => sub { @streams = split /,/, $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 1 ▶ 'workers=s' => sub { @workers = map { 0 + $_ } split /,/, $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 1 ▶ 'read-sizes=s' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
34 1 @read_sizes = map { 0 + $_ } split /,/, $_[1];
35 },
36 1 ▶ 'bytes-per-stream=i' => \$bytes_per_stream,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("engines=s", sub { @engines = split(/,/u, $_[1], 0); } , "streams=s", sub { @streams = split(/,/u, $_[1], 0); } , "workers=s", sub { @workers = map({0 + $_;} split(/,/u, $_[1], 0)); } , "read-sizes=s", sub { @read_sizes = map({0 + $_;} split(/,/u, $_[1], 0)); } , "bytes-per-stream=i", \$bytes_per_stream, "max-reads-per-tick=i", \$max_reads_per_tick, "heartbeat-us=i", \$heartbeat_us, "warmups=i", \$warmups, "repeats=i", \$repeats, "json=s", \$json_path, "help", \$help) or usage(1) 1 0 1
37 'max-reads-per-tick=i' => \$max_reads_per_tick,
38 'heartbeat-us=i' => \$heartbeat_us,
39 'warmups=i' => \$warmups,
40 'repeats=i' => \$repeats,
41 'json=s' => \$json_path,
42 'help' => \$help,
43 ) or usage(1);
44 1 ▶usage(0) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 1 1
45  
46 1 my %valid_engine = map { $_ => 1 } qw(perl native);
47 1 my %valid_stream = map { $_ => 1 } qw(stdout stderr both);
48 die "engines must contain perl and/or native\n"
49 1 ▶ if !@engines || grep { !$valid_engine{$_} } @engines;
Branch: 1 branch
BranchTrueFalseTotal
if not @engines or grep {not $valid_engine{$_};} @engines 0 1 1
Condition33%1/3
not @engines or grep {not $valid_engine{$_};} @engines
l!l&&r!l&&!r
001
MC/DC0%0/2
not @engines or grep {not $valid_engine{$_};} @engines
not @engines grep {not $valid_engine{$_};} @engines
Truth table33%1/3
not(A) or B
A= not @enginesB= grep {not $valid_engine{$_};} @engines
ABresult
1X1
011
000
50 die "streams must contain stdout, stderr, and/or both\n"
51 1 ▶ if !@streams || grep { !$valid_stream{$_} } @streams;
Branch: 1 branch
BranchTrueFalseTotal
if not @streams or grep {not $valid_stream{$_};} @streams 0 1 1
Condition33%1/3
not @streams or grep {not $valid_stream{$_};} @streams
l!l&&r!l&&!r
001
MC/DC0%0/2
not @streams or grep {not $valid_stream{$_};} @streams
not @streams grep {not $valid_stream{$_};} @streams
Truth table33%1/3
not(A) or B
A= not @streamsB= grep {not $valid_stream{$_};} @streams
ABresult
1X1
011
000
52 1 ▶die "workers must be positive\n" if !@workers || grep { $_ < 1 } @workers;
Branch: 1 branch
BranchTrueFalseTotal
if not @workers or grep {$_ < 1;} @workers 0 1 1
Condition33%1/3
not @workers or grep {$_ < 1;} @workers
l!l&&r!l&&!r
001
MC/DC0%0/2
not @workers or grep {$_ < 1;} @workers
not @workers grep {$_ < 1;} @workers
Truth table33%1/3
not(A) or B
A= not @workersB= grep {$_ < 1;} @workers
ABresult
1X1
011
000
53 die "read sizes must be positive\n"
54 1 ▶ if !@read_sizes || grep { $_ < 1 } @read_sizes;
Branch: 1 branch
BranchTrueFalseTotal
if not @read_sizes or grep {$_ < 1;} @read_sizes 0 1 1
Condition33%1/3
not @read_sizes or grep {$_ < 1;} @read_sizes
l!l&&r!l&&!r
001
MC/DC0%0/2
not @read_sizes or grep {$_ < 1;} @read_sizes
not @read_sizes grep {$_ < 1;} @read_sizes
Truth table33%1/3
not(A) or B
A= not @read_sizesB= grep {$_ < 1;} @read_sizes
ABresult
1X1
011
000
55 1 ▶die "bytes-per-stream must be positive\n" if $bytes_per_stream < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $bytes_per_stream < 1 0 1 1
56 1 ▶die "max-reads-per-tick must be positive\n" if $max_reads_per_tick < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $max_reads_per_tick < 1 0 1 1
57 1 ▶die "heartbeat-us must be nonnegative\n" if $heartbeat_us < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $heartbeat_us < 0 0 1 1
58 1 ▶die "warmups must be nonnegative\n" if $warmups < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $warmups < 0 0 1 1
59 1 ▶die "repeats must be positive\n" if $repeats < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $repeats < 1 0 1 1
60  
61 1 my $CHILD = <<'CHILD';
62 use v5.36;
63 use strict;
64 use warnings;
65 my ($bytes, $mode) = @ARGV;
66 my $gate = '';
67 exit 91 if sysread(STDIN, $gate, 1) != 1;
68 my $chunk = 'x' x 65_536;
69 my ($stdout_left, $stderr_left) = (
70 $mode eq 'stderr' ? 0 : $bytes,
71 $mode eq 'stdout' ? 0 : $bytes,
72 );
73 while ($stdout_left || $stderr_left) {
74 if ($stdout_left) {
75 my $length = $stdout_left < length($chunk)
76 ? $stdout_left : length($chunk);
77 my $offset = 0;
78 while ($offset < $length) {
79 my $count = syswrite(STDOUT, $chunk, $length - $offset, $offset);
80 exit 92 if !defined $count;
81 $offset += $count;
82 }
83 $stdout_left -= $length;
84 }
85 if ($stderr_left) {
86 my $length = $stderr_left < length($chunk)
87 ? $stderr_left : length($chunk);
88 my $offset = 0;
89 while ($offset < $length) {
90 my $count = syswrite(STDERR, $chunk, $length - $offset, $offset);
91 exit 93 if !defined $count;
92 $offset += $count;
93 }
94 $stderr_left -= $length;
95 }
96 }
97  
98 CHILD
99  
100 {
101 package BenchPipeHeartbeat;
102 1 ▶ use parent 'Linux::Event::Kernel::Timer';
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 0 ▶ sub on_timer ($timer) {
Subroutine on_timer: not called CC=2 SCAR=17.9
105 0 my $run = $timer->data;
106 0 my $now = main::now();
107 0 my $gap = $now - $run->{heartbeat_last};
108 $run->{heartbeat_max_gap} = $gap
109 0 ▶ if $gap > $run->{heartbeat_max_gap};
Branch: 1 branch
BranchTrueFalseTotal
if $gap > $run->{"heartbeat_max_gap"} 0 0 0
110 0 $run->{heartbeat_last} = $now;
111 0 $run->{heartbeat_callbacks}++;
112 0 $run->{heartbeat_expirations} += $timer->expirations;
113 }
114 }
115  
116 {
117 1 package BenchPipeProcess;
118 1 ▶ use parent 'Linux::Event::Kernel::Process';
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 64 ▶ sub on_stdout ($process, $bytes) {
Subroutine on_stdout: called CC=1 SCAR=0.0
121 64 my $run = $process->data;
122 64 $run->{stdout_bytes} += length($bytes);
123 64 $run->{stdout_callbacks}++;
124 }
125  
126 64 ▶ sub on_stderr ($process, $bytes) {
Subroutine on_stderr: called CC=1 SCAR=0.0
127 64 my $run = $process->data;
128 64 $run->{stderr_bytes} += length($bytes);
129 64 $run->{stderr_callbacks}++;
130 }
131  
132 2 ▶ sub on_stdout_eof ($process) { $process->data->{stdout_eof}++ }
Subroutine on_stdout_eof: called CC=1 SCAR=0.0
133 2 ▶ sub on_stderr_eof ($process) { $process->data->{stderr_eof}++ }
Subroutine on_stderr_eof: called CC=1 SCAR=0.0
134  
135 2 ▶ sub on_exit ($process) {
Subroutine on_exit: called CC=4 SCAR=15.0
136 2 my $run = $process->data;
137 2 ▶ die "pipe benchmark child did not exit successfully\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $process->exit_code or $process->exit_code != 0 0 2 2
Condition33%1/3
not defined $process->exit_code or $process->exit_code != 0
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $process->exit_code or $process->exit_code != 0
not defined $process->exit_code $process->exit_code != 0
Truth table33%1/3
not(A) or B
A= not defined $process->exit_codeB= $process->exit_code != 0
ABresult
1X1
011
000
138 if !defined($process->exit_code) || $process->exit_code != 0;
139 2 $run->{completed}++;
140 2 ▶ if ($run->{completed} == $run->{workers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run->{"completed"} == $run->{"workers"}) 2 0 2
141 2 $run->{finished} = main::now();
142 2 $run->{loop}->stop;
143 }
144 }
145  
146 0 ▶ sub on_error ($process, $error) { die "$error\n" }
Subroutine on_error: not called CC=1 SCAR=6.9
147 }
148  
149 1 ▶sub now () { clock_gettime(CLOCK_MONOTONIC) }
Subroutine now: called CC=1 SCAR=0.0
150 4 ▶sub cpu_now () { clock_gettime(CLOCK_PROCESS_CPUTIME_ID) }
Subroutine cpu_now: called CC=1 SCAR=0.0
151  
152 8 ▶sub median (@values) {
Subroutine median: called CC=2 SCAR=7.6
153 0 @values = sort { $a <=> $b } @values;
154 8 my $middle = int(@values / 2);
155 8 ▶ return @values % 2
Branch: 1 branch
BranchTrueFalseTotal
@values % 2 ? : 8 0 8
156 ? $values[$middle]
157 : ($values[$middle - 1] + $values[$middle]) / 2;
158 }
159  
160 2 ▶sub run_once ($engine, $stream, $workers, $read_size, $measured) {
Subroutine run_once: called CC=12 SCAR=26.9
161 2 local $Linux::Event::Kernel::Process::_PIPE_DRAIN_ENGINE = $engine;
162 2 my $loop = Linux::Event::Loop->new;
163 2 my $run = {
164 loop => $loop,
165 workers => $workers,
166 completed => 0,
167 stdout_bytes => 0,
168 stderr_bytes => 0,
169 stdout_callbacks => 0,
170 stderr_callbacks => 0,
171 stdout_eof => 0,
172 stderr_eof => 0,
173 heartbeat_callbacks => 0,
174 heartbeat_expirations => 0,
175 heartbeat_max_gap => 0,
176 };
177 2 my @processes;
178 2 for (1 .. $workers) {
179 2 push @processes, $loop->add(BenchPipeProcess->spawn(
180 command => [$^X, '-e', $CHILD, $bytes_per_stream, $stream],
181 stdin => 'pipe',
182 stdout => 'pipe',
183 stderr => 'pipe',
184 read_size => $read_size,
185 max_reads_per_tick => $max_reads_per_tick,
186 data => $run,
187 ));
188 }
189  
190 2 ▶ if ($heartbeat_us) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heartbeat_us) 0 2 2
191 0 my $interval = $heartbeat_us / 1_000_000;
192 0 $run->{heartbeat_last} = now();
193 0 $loop->add(BenchPipeHeartbeat->new(
194 every => $interval, data => $run,
195 ));
196 }
197  
198 2 my $cpu_start = cpu_now();
199 2 $run->{started} = now();
200 2 for my $process (@processes) {
201 2 $process->write_stdin('x');
202 2 $process->close_stdin;
203 }
204 2 $loop->run;
205 2 my $cpu_end = cpu_now();
206 2 my $elapsed = $run->{finished} - $run->{started};
207 2 my $cpu = $cpu_end - $cpu_start;
208 2 ▶ my $expected_stdout = $stream eq 'stderr'
Branch: 1 branch
BranchTrueFalseTotal
$stream eq 'stderr' ? : 0 2 2
209 ? 0 : $bytes_per_stream * $workers;
210 2 ▶ my $expected_stderr = $stream eq 'stdout'
Branch: 1 branch
BranchTrueFalseTotal
$stream eq 'stdout' ? : 0 2 2
211 ? 0 : $bytes_per_stream * $workers;
212 die "stdout mismatch: got $run->{stdout_bytes}, expected $expected_stdout\n"
213 2 ▶ if $run->{stdout_bytes} != $expected_stdout;
Branch: 1 branch
BranchTrueFalseTotal
if $run->{"stdout_bytes"} != $expected_stdout 0 2 2
214 die "stderr mismatch: got $run->{stderr_bytes}, expected $expected_stderr\n"
215 2 ▶ if $run->{stderr_bytes} != $expected_stderr;
Branch: 1 branch
BranchTrueFalseTotal
if $run->{"stderr_bytes"} != $expected_stderr 0 2 2
216 2 ▶ die "stdout EOF mismatch\n" if $run->{stdout_eof} != $workers;
Branch: 1 branch
BranchTrueFalseTotal
if $run->{"stdout_eof"} != $workers 0 2 2
217 2 ▶ die "stderr EOF mismatch\n" if $run->{stderr_eof} != $workers;
Branch: 1 branch
BranchTrueFalseTotal
if $run->{"stderr_eof"} != $workers 0 2 2
218  
219 2 my $bytes = $expected_stdout + $expected_stderr;
220 return {
221 engine => $engine,
222 stream => $stream,
223 workers => $workers,
224 read_size => $read_size,
225 measured => $measured ? JSON::PP::true : JSON::PP::false,
226 bytes => $bytes,
227 elapsed_seconds => 0 + $elapsed,
228 gib_per_second => $bytes / $elapsed / (1024 ** 3),
229 parent_cpu_seconds => 0 + $cpu,
230 parent_cpu_ns_per_kib => $cpu * 1_000_000_000 / ($bytes / 1024),
231 stdout_callbacks => $run->{stdout_callbacks},
232 stderr_callbacks => $run->{stderr_callbacks},
233 callback_bytes => $run->{stdout_callbacks} + $run->{stderr_callbacks}
234 ? $bytes / ($run->{stdout_callbacks} + $run->{stderr_callbacks})
235 : 0,
236 heartbeat_callbacks => $run->{heartbeat_callbacks},
237 heartbeat_expirations => $run->{heartbeat_expirations},
238 heartbeat_max_gap_seconds => $run->{heartbeat_max_gap},
239 2 ▶ };
Branch: 2 branches
BranchTrueFalseTotal
$measured ? : 2 0 2
$run->{'stdout_callbacks'} + $run->{'stderr_callbacks'} ? : 2 0 2
240 }
241  
242 1 my @raw;
243 1 for my $read_size (@read_sizes) {
244 1 for my $stream (@streams) {
245 1 for my $workers (@workers) {
246 1 for my $warmup (1 .. $warmups) {
247 0 run_once($_, $stream, $workers, $read_size, 0) for @engines;
248 }
249 1 for my $repeat (1 .. $repeats) {
250 1 my @order = @engines;
251 1 ▶ push @order, shift @order if @order > 1 && $repeat % 2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @order > 1 and $repeat % 2 == 0 0 1 1
Condition33%1/3
@order > 1 and $repeat % 2 == 0
!ll&&!rl&&r
010
MC/DC0%0/2
@order > 1 and $repeat % 2 == 0
@order > 1 $repeat % 2 == 0
Truth table33%1/3
A and B
A= @order > 1B= $repeat % 2 == 0
ABresult
0X0
100
111
252 1 for my $engine (@order) {
253 2 my $row = run_once(
254 $engine, $stream, $workers, $read_size, 1,
255 );
256 2 $row->{repeat} = $repeat;
257 2 push @raw, $row;
258 }
259 }
260 }
261 }
262 }
263  
264 1 my @summary;
265 1 for my $read_size (@read_sizes) {
266 1 for my $stream (@streams) {
267 1 for my $workers (@workers) {
268 1 for my $engine (@engines) {
269 my @rows = grep {
270 2 $_->{engine} eq $engine
271 && $_->{stream} eq $stream
272 && $_->{workers} == $workers
273 4 ▶ && $_->{read_size} == $read_size
Condition66%6/9
$_->{'engine'} eq $engine && $_->{'stream'} eq $stream && $_->{'workers'} == $workers && $_->{'read_size'} == $read_size
!ll&&!rl&&r
202
$_->{"engine"} eq $engine and $_->{"stream"} eq $stream and $_->{"workers"} == $workers
!ll&&!rl&&r
202
$_->{"engine"} eq $engine and $_->{"stream"} eq $stream
!ll&&!rl&&r
202
MC/DC25%1/4
$_->{'engine'} eq $engine && $_->{'stream'} eq $stream && $_->{'workers'} == $workers && $_->{'read_size'} == $read_size
$_->{"engine"} eq $engine $_->{"stream"} eq $stream $_->{"workers"} == $workers $_->{'read_size'} == $read_size
Truth table40%2/5
A and B and C && D
A= $_->{"engine"} eq $engineB= $_->{"stream"} eq $streamC= $_->{"workers"} == $workersD= $_->{'read_size'} == $read_size
ABCDresult
0XXX0
10XX0
110X0
11100
11111
274 } @raw;
275 push @summary, {
276 engine => $engine,
277 stream => $stream,
278 workers => $workers,
279 read_size => $read_size,
280 median_gib_per_second => median(
281 2 map { $_->{gib_per_second} } @rows,
282 ),
283 median_parent_cpu_ns_per_kib => median(
284 2 map { $_->{parent_cpu_ns_per_kib} } @rows,
285 ),
286 median_callback_bytes => median(
287 2 map { $_->{callback_bytes} } @rows,
288 ),
289 median_heartbeat_max_gap_seconds => median(
290 2 map { $_->{heartbeat_max_gap_seconds} } @rows,
291 ),
292 };
293 }
294 }
295 }
296 }
297  
298 1 printf "Process pipe drain benchmark: bytes/stream=%d max_reads/tick=%d repeats=%d\n",
299 $bytes_per_stream, $max_reads_per_tick, $repeats;
300 1 printf "%8s %7s %7s %10s %12s %17s %15s\n",
301 qw(engine stream workers read_size GiB/s cpu_ns/KiB callback_bytes);
302 1 for my $row (@summary) {
303 printf "%8s %7s %7d %10d %12.3f %17.3f %15.1f\n",
304 $row->{engine}, $row->{stream}, $row->{workers}, $row->{read_size},
305 $row->{median_gib_per_second},
306 $row->{median_parent_cpu_ns_per_kib},
307 2 $row->{median_callback_bytes};
308 }
309  
310 1 ▶if (defined $json_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json_path) 1 0 1
311 1 my $report = {
312 benchmark => 'linux-event-process-pipe-drain-bench',
313 benchmark_contract_version => 1,
314 linux_event_version => $Linux::Event::VERSION,
315 workload => 'pre-spawned gated child pipe output and Process callbacks',
316 configuration => {
317 engines => \@engines,
318 streams => \@streams,
319 workers => \@workers,
320 read_sizes => \@read_sizes,
321 bytes_per_stream => $bytes_per_stream,
322 max_reads_per_tick => $max_reads_per_tick,
323 heartbeat_us => $heartbeat_us,
324 warmups => $warmups,
325 repeats => $repeats,
326 },
327 raw => \@raw,
328 summary => \@summary,
329 };
330 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
331 1 print {$json} JSON::PP->new->canonical->pretty->encode($report);
332 1 ▶ close $json or die "close $json_path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $json or die "close ${json_path}: $!\n" 1 0 1
333 }
334  
335 0 ▶sub usage ($exit) {
Subroutine usage: not called CC=1 SCAR=6.9
336 0 print <<'USAGE';
337 Usage: run-process-pipe-drain-bench.pl [options]
338 --engines=LIST perl,native (default: both)
339 --streams=LIST stdout,stderr,both (default: all)
340 --workers=LIST simultaneous pre-spawned children (default: 1,8,32)
341 --read-sizes=LIST Process read sizes (default: 4096,65536)
342 --bytes-per-stream=N bytes written by each child per stream (default: 16777216)
343 --max-reads-per-tick=N Process fairness limit (default: 64)
344 --heartbeat-us=N recurring fairness probe interval (default: disabled)
345 --warmups=N warmup runs per case and engine (default: 1)
346 --repeats=N measured balanced repeats (default: 7)
347 --json=PATH write a machine-readable report
348 --help show this help
349 USAGE
350 0 exit $exit;
351 }
« blib/lib/Linux/Event/Framer/Fixed.pm ↑ Summary bench/run-framer-send-bench.pl »