bench/run-stream-microbench.pl

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