bench/run-stream-microbench.pl

filter: Statementstmt 93.7% 179 / 191 Branchbran 58.5% 48 / 82 Conditioncond 33.3% 5 / 15 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 96.0% 24 / 25 Podpod n/a totaltotal 79.5% 257 / 323 CC 48 SCAR 22.4
CC
48
Cov
81%
_client_read
17.1
run_case
16.2
syswrite_all
11.2
CRAP 9.4
« bench/run-first-class-raw-callback-bench.pl ↑ Summary blib/lib/Linux/Event/IO/TTY.pm »
1 #!/usr/bin/env perl
2 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Socket qw(AF_UNIX SOCK_STREAM PF_UNSPEC);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Time::HiRes qw(time clock_gettime CLOCK_PROCESS_CPUTIME_ID);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Linux::Event::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Linux::Event::IO::Sock::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Linux::Event::IO::Sock::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 {
16 package Linux::Event::Bench::RawEchoStream;
17 1 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶ sub on_data ($stream, $bytes) {
Subroutine on_data: called CC=2 SCAR=7.6
19 18 ▶ $stream->write($bytes)
Branch: 1 branch
BranchTrueFalseTotal
$stream->write($bytes) or die "microbenchmark unexpectedly hit Stream backpressure\n" 18 0 18
20 or die "microbenchmark unexpectedly hit Stream backpressure\n";
21 }
22 0 ▶ sub on_error ($stream, $error) { die "Stream error: $error\n" }
Subroutine on_error: not called CC=1 SCAR=6.9
23 }
24  
25 {
26 1 package Linux::Event::Bench::CappedRawEchoStream;
27 1 ▶ use parent -norequire, 'Linux::Event::Bench::RawEchoStream';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶ sub stream_tuning ($class) { return max_pending_bytes => 16 * 1024 * 1024 }
Subroutine stream_tuning: called CC=1 SCAR=0.0
29 }
30  
31 {
32 1 package Linux::Event::Bench::DeadlineRawEchoStream;
33 1 ▶ use parent -norequire, 'Linux::Event::Bench::RawEchoStream';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶ sub stream_tuning ($class) { return idle_timeout => 3_600 }
Subroutine stream_tuning: called CC=1 SCAR=0.0
35 }
36  
37 1 my @clients = (1, 10, 100, 1000);
38 1 my $messages = 100;
39 1 my $warmup = 10;
40 1 my $bytes = 64;
41 1 my $repeats = 6;
42  
43 GetOptions(
44 1 ▶ 'clients=s' => sub { @clients = split /,/, $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 1 ▶ 'messages=i' => \$messages,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("clients=s", sub { @clients = split(/,/u, $_[1], 0); } , "messages=i", \$messages, "warmup=i", \$warmup, "bytes=i", \$bytes, "repeats=i", \$repeats) or die "bad options\n" 1 0 1
46 'warmup=i' => \$warmup,
47 'bytes=i' => \$bytes,
48 'repeats=i' => \$repeats,
49 ) or die "bad options\n";
50  
51 1 ▶die "messages must be > 0\n" if $messages <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $messages <= 0 0 1 1
52 1 ▶die "warmup must be >= 0\n" if $warmup < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $warmup < 0 0 1 1
53 1 ▶die "bytes must be > 0\n" if $bytes <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bytes <= 0 0 1 1
54 1 ▶die "repeats must be > 0\n" if $repeats <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $repeats <= 0 0 1 1
55  
56 1 my @systems = (
57 'raw-reactor',
58 'subclass-stream',
59 'subclass-stream-deadline',
60 'subclass-stream-capped',
61 );
62 1 my @rows;
63  
64 1 for my $count (@clients) {
65 1 ▶ die "client count must be > 0\n" if $count <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count <= 0 0 1 1
66  
67 1 for my $repeat (1 .. $repeats) {
68 # Balanced cyclic rotation. Any multiple of the system count
69 # repeats places each implementation in each execution position equally.
70 1 my $shift = ($repeat - 1) % @systems;
71 1 my @order = (@systems[$shift .. $#systems], @systems[0 .. $shift - 1]);
72 1 for my $system (@order) {
73 4 my $r = run_case($system, $count);
74 4 $r->{repeat} = $repeat;
75 4 push @rows, $r;
76 printf "%s clients=%d repeat=%d %.1f msg/s cpu=%.3f us/msg\n",
77 $system, $count, $repeat,
78 4 $r->{messages_per_second}, $r->{cpu_us_per_message};
79 }
80 }
81 }
82  
83 1 say "\nMedian Stream microbenchmark";
84 1 printf "%-18s %8s %14s %14s\n", 'system', 'clients', 'msg/s', 'cpu us/msg';
85 1 for my $count (@clients) {
86 1 for my $system (@systems) {
87 4 ▶ my @set = grep { $_->{clients} == $count && $_->{system} eq $system } @rows;
Condition66%2/3
$_->{'clients'} == $count && $_->{'system'} eq $system
!ll&&!rl&&r
0124
MC/DC50%1/2
$_->{'clients'} == $count && $_->{'system'} eq $system
$_->{'clients'} == $count $_->{'system'} eq $system
Truth table66%2/3
A && B
A= $_->{'clients'} == $countB= $_->{'system'} eq $system
ABresult
0X0
100
111
88 4 my $rate = median(map { $_->{messages_per_second} } @set);
89 4 my $cpu = median(map { $_->{cpu_us_per_message} } @set);
90 4 printf "%-18s %8d %14.1f %14.3f\n", $system, $count, $rate, $cpu;
91 }
92 }
93  
94 1 say "\nThis is a same-process AF_UNIX development microbenchmark, not the final";
95 1 say "cross-runtime Stream leaderboard. It compares direct raw-reactor echo";
96 1 say "with uncapped, deadline-tracked, and hard-capped subclass Streams.";
97  
98 4 ▶sub run_case ($system, $count) {
Subroutine run_case: called CC=16 SCAR=27.8
99 4 my $loop = Linux::Event::Loop->new;
100 4 my $payload = 'x' x $bytes;
101  
102 4 my @server_fh;
103 my @client_fh;
104 4 my @server_obj;
105 4 my @client_watchers;
106 4 my @client_state;
107  
108 4 ▶ my $bench = {
Branch: 1 branch
BranchTrueFalseTotal
$warmup ? : 4 0 4
109 loop => $loop,
110 payload => $payload,
111 clients => $count,
112 warmup => $warmup,
113 messages => $messages,
114 warmup_done => 0,
115 measured_done => 0,
116 phase => $warmup ? 'warmup' : 'measure',
117 wall_start => undef,
118 cpu_start => undef,
119 wall_end => undef,
120 cpu_end => undef,
121 };
122  
123 4 for my $i (0 .. $count - 1) {
124 4 ▶ socketpair(my $server, my $client, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
Branch: 1 branch
BranchTrueFalseTotal
socketpair my $server, my $client, 1, 1, 0 or die "socketpair: $!" 4 0 4
125 or die "socketpair: $!";
126 4 set_nonblocking($server);
127 4 set_nonblocking($client);
128 4 push @server_fh, $server;
129 4 push @client_fh, $client;
130  
131 4 ▶ if ($system eq 'raw-reactor') {
Branch: 1 branch
BranchTrueFalseTotal
if ($system eq 'raw-reactor') { } 1 3 4
132 1 my $w = $loop->watch(
133 fh => $server,
134 read => \&_raw_server_read,
135 );
136 1 push @server_obj, $w;
137 } else {
138 3 ▶ my $class = $system eq 'subclass-stream-capped'
Branch: 2 branches
BranchTrueFalseTotal
$system eq 'subclass-stream-capped' ? : 1 2 3
$system eq 'subclass-stream-deadline' ? : 1 1 2
139 ? 'Linux::Event::Bench::CappedRawEchoStream'
140 : $system eq 'subclass-stream-deadline'
141 ? 'Linux::Event::Bench::DeadlineRawEchoStream'
142 : 'Linux::Event::Bench::RawEchoStream';
143 3 my $s = $class->new(
144 loop => $loop,
145 fh => $server,
146 );
147 3 push @server_obj, $s;
148 }
149  
150 4 my $state = {
151 bench => $bench,
152 fh => $client,
153 buffer => '',
154 completed => 0,
155 warmup_complete => 0,
156 measured_complete => 0,
157 };
158 4 push @client_state, $state;
159  
160 4 my $cw = $loop->watch(
161 fh => $client,
162 data => $state,
163 read => \&_client_read,
164 );
165 4 push @client_watchers, $cw;
166 }
167  
168 4 $bench->{client_state} = \@client_state;
169  
170 4 ▶ if ($warmup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warmup) { } 4 0 4
171 4 syswrite_all($_->{fh}, $payload) for @client_state;
172 } else {
173 0 start_measurement($bench);
174 }
175  
176 4 $loop->run;
177  
178 4 my $elapsed = $bench->{wall_end} - $bench->{wall_start};
179 4 my $cpu = $bench->{cpu_end} - $bench->{cpu_start};
180 4 my $total = $count * $messages;
181  
182 # Explicit cleanup; Stream owns server fhs, raw watcher cases do not.
183 4 for my $obj (@server_obj) {
184 4 ▶ if ($system eq 'raw-reactor') {
Branch: 1 branch
BranchTrueFalseTotal
if ($system eq 'raw-reactor') { } 1 3 4
185 1 $obj->cancel;
186 } else {
187 3 ▶ $obj->close if !$obj->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->is_closed 3 0 3
188 }
189 }
190 4 $_->cancel for @client_watchers;
191 4 close $_ for @client_fh;
192 4 ▶ if ($system eq 'raw-reactor') {
Branch: 1 branch
BranchTrueFalseTotal
if ($system eq "raw-reactor") 1 3 4
193 1 close $_ for @server_fh;
194 }
195  
196 return {
197 4 system => $system,
198 clients => $count,
199 messages => $total,
200 elapsed_seconds => $elapsed,
201 cpu_seconds => $cpu,
202 messages_per_second => $total / $elapsed,
203 cpu_us_per_message => ($cpu * 1_000_000) / $total,
204 };
205 }
206  
207 6 ▶sub _raw_server_read ($watcher) {
Subroutine _raw_server_read: called CC=6 SCAR=20.5
208 6 my $fh = $watcher->fh;
209 6 while (1) {
210 12 my $buf = '';
211 12 my $n = sysread($fh, $buf, 65_536);
212 12 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) 6 6 12
213 6 ▶ return if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 6 6
214 6 syswrite_all($fh, $buf);
215 6 next;
216 }
217 6 ▶ return if $!{EAGAIN} || $!{EWOULDBLOCK};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EAGAIN"} or $!{"EWOULDBLOCK"} 6 0 6
Condition33%1/3
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
l!l&&r!l&&!r
600
MC/DC0%0/2
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
$!{"EAGAIN"} $!{"EWOULDBLOCK"}
Truth table33%1/3
A or B
A= $!{"EAGAIN"}B= $!{"EWOULDBLOCK"}
ABresult
1X1
011
000
218 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
219 0 die "raw server read: $!";
220 }
221 }
222  
223 24 ▶sub _client_read ($watcher) {
Subroutine _client_read: called CC=14 SCAR=28.4
224 24 my $state = $watcher->data;
225 24 my $bench = $state->{bench};
226 24 my $fh = $state->{fh};
227  
228 24 while (1) {
229 44 my $chunk = '';
230 44 my $n = sysread($fh, $chunk, 65_536);
231 44 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) 24 20 44
232 24 ▶ die "client unexpected EOF\n" if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 24 24
233 24 $state->{buffer} .= $chunk;
234  
235 24 ▶ while (length($state->{buffer}) >= $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $state->{"buffer"} >= $bytes) 24 20 44
236 24 substr($state->{buffer}, 0, $bytes, '');
237 24 $state->{completed}++;
238  
239 24 ▶ if ($bench->{phase} eq 'warmup') {
Branch: 1 branch
BranchTrueFalseTotal
if ($bench->{'phase'} eq 'warmup') { } 4 20 24
240 4 ▶ if ($state->{completed} < $warmup) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state->{'completed'} < $warmup) { } 0 4 4
elsif (not $state->{'warmup_complete'}++) { } 4 0 4
241 0 syswrite_all($fh, $bench->{payload});
242 } elsif (!$state->{warmup_complete}++) {
243 4 $bench->{warmup_done}++;
244 start_measurement($bench)
245 4 ▶ if $bench->{warmup_done} == $bench->{clients};
Branch: 1 branch
BranchTrueFalseTotal
if $bench->{"warmup_done"} == $bench->{"clients"} 4 0 4
246 }
247 } else {
248 20 ▶ if ($state->{completed} < $messages) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state->{'completed'} < $messages) { } 16 4 20
elsif (not $state->{'measured_complete'}++) { } 4 0 4
249 16 syswrite_all($fh, $bench->{payload});
250 } elsif (!$state->{measured_complete}++) {
251 4 $bench->{measured_done}++;
252 4 ▶ if ($bench->{measured_done} == $bench->{clients}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bench->{"measured_done"} == $bench->{"clients"}) 4 0 4
253 4 $bench->{wall_end} = time;
254 4 $bench->{cpu_end} = clock_gettime(CLOCK_PROCESS_CPUTIME_ID);
255 4 $bench->{loop}->stop;
256 4 return;
257 }
258 }
259 }
260 }
261 20 next;
262 }
263  
264 20 ▶ return if $!{EAGAIN} || $!{EWOULDBLOCK};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EAGAIN"} or $!{"EWOULDBLOCK"} 20 0 20
Condition33%1/3
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
l!l&&r!l&&!r
2000
MC/DC0%0/2
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
$!{"EAGAIN"} $!{"EWOULDBLOCK"}
Truth table33%1/3
A or B
A= $!{"EAGAIN"}B= $!{"EWOULDBLOCK"}
ABresult
1X1
011
000
265 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
266 0 die "client read: $!";
267 }
268 }
269  
270 4 ▶sub start_measurement ($bench) {
Subroutine start_measurement: called CC=5 SCAR=16.5
271 4 ▶ return if $bench->{phase} eq 'measure' && defined $bench->{wall_start};
Branch: 1 branch
BranchTrueFalseTotal
if $bench->{"phase"} eq "measure" and defined $bench->{"wall_start"} 0 4 4
Condition33%1/3
$bench->{"phase"} eq "measure" and defined $bench->{"wall_start"}
!ll&&!rl&&r
400
MC/DC0%0/2
$bench->{"phase"} eq "measure" and defined $bench->{"wall_start"}
$bench->{"phase"} eq "measure" defined $bench->{"wall_start"}
Truth table33%1/3
A and B
A= $bench->{"phase"} eq "measure"B= defined $bench->{"wall_start"}
ABresult
0X0
100
111
272 4 $bench->{phase} = 'measure';
273  
274 4 for my $state (@{ $bench->{client_state} }) {
275 4 $state->{completed} = 0;
276 4 $state->{buffer} = '';
277 }
278  
279 4 $bench->{cpu_start} = clock_gettime(CLOCK_PROCESS_CPUTIME_ID);
280 4 $bench->{wall_start} = time;
281 4 syswrite_all($_->{fh}, $bench->{payload}) for @{ $bench->{client_state} };
282 }
283  
284 30 ▶sub syswrite_all ($fh, $bytes) {
Subroutine syswrite_all: called CC=6 SCAR=24.1
285 30 my $off = 0;
286 30 my $len = length($bytes);
287 30 ▶ while ($off < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off < $len) 30 30 60
288 30 my $n = syswrite($fh, $bytes, $len - $off, $off);
289 30 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) 30 0 30
290 30 $off += $n;
291 30 next;
292 }
293 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
294 0 ▶ die "benchmark write would block unexpectedly\n" if $!{EAGAIN} || $!{EWOULDBLOCK};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EAGAIN"} or $!{"EWOULDBLOCK"} 0 0 0
Condition0%0/3
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
$!{"EAGAIN"} $!{"EWOULDBLOCK"}
Truth table0%0/3
A or B
A= $!{"EAGAIN"}B= $!{"EWOULDBLOCK"}
ABresult
1X1
011
000
295 0 die "benchmark write: $!";
296 }
297 }
298  
299 8 ▶sub set_nonblocking ($fh) {
Subroutine set_nonblocking: called CC=3 SCAR=11.7
300 8 my $flags = fcntl($fh, F_GETFL, 0);
301 8 ▶ die "fcntl F_GETFL: $!" if !defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 0 8 8
302 8 ▶ fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
Branch: 1 branch
BranchTrueFalseTotal
fcntl $fh, 4, $flags | 2048 or die "fcntl F_SETFL: $!" 8 0 8
303 or die "fcntl F_SETFL: $!";
304 }
305  
306 8 ▶sub median (@values) {
Subroutine median: called CC=3 SCAR=12.7
307 0 @values = sort { $a <=> $b } @values;
308 8 my $n = @values;
309 8 ▶ return 0 if !$n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 8 8
310 8 ▶ return $values[int($n / 2)] if $n % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $n % 2 8 0 8
311 0 return ($values[$n / 2 - 1] + $values[$n / 2]) / 2;
312 }
« bench/run-first-class-raw-callback-bench.pl ↑ Summary blib/lib/Linux/Event/IO/TTY.pm »