bench/run-framing-microbench.pl

filter: Statementstmt 93.7% 164 / 175 Branchbran 55.4% 41 / 74 Conditioncond 33.3% 4 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 91.3% 21 / 23 Podpod n/a totaltotal 79.1% 231 / 292 CC 42 SCAR 21.3
CC
42
Cov
80%
_client_read
18.9
syswrite_all
11.2
run_case
10.1
CRAP 8.4
« bench/run-stream-microbench.pl ↑ Summary bench/run-callback-batching-fairness.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::RawDelimiterEcho;
16 1 ▶ use parent 'Linux::Event::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶ sub on_data ($stream, $bytes) {
Subroutine on_data: called CC=4 SCAR=14.0
18 6 my $state = $stream->data;
19 6 $state->{buffer} .= $bytes;
20 6 ▶ while ((my $at = index($state->{buffer}, $state->{delimiter})) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $at = index($state->{"buffer"}, $state->{"delimiter"})) >= 0) 6 6 12
21 6 my $message = substr($state->{buffer}, 0, $at, '');
22 6 substr($state->{buffer}, 0, length($state->{delimiter}), '');
23 6 ▶ die "framing payload mismatch\n" if $message ne $state->{payload};
Branch: 1 branch
BranchTrueFalseTotal
if $message ne $state->{"payload"} 0 6 6
24 $stream->write($state->{wire})
25 6 ▶ or die "framing benchmark unexpectedly hit backpressure\n";
Branch: 1 branch
BranchTrueFalseTotal
$stream->write($state->{"wire"}) or die "framing benchmark unexpectedly hit backpressure\n" 6 0 6
26 }
27 }
28 0 ▶ sub on_error ($stream, $error) { die "Stream error: $error\n" }
Subroutine on_error: not called CC=1 SCAR=6.9
29 }
30  
31 {
32 1 package Linux::Event::Bench::NativeDelimiterEcho;
33 1 ▶ use parent 'Linux::Event::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶ use Linux::Event::Framer 'Delimiter', "\x02END\x03";
Subroutine BEGIN: called CC=1 SCAR=0.0
35 6 ▶ sub on_message ($stream, $message) {
Subroutine on_message: called CC=3 SCAR=11.7
36 6 my $state = $stream->data;
37 6 ▶ die "framing payload mismatch\n" if $message ne $state->{payload};
Branch: 1 branch
BranchTrueFalseTotal
if $message ne $state->{"payload"} 0 6 6
38 $stream->write($state->{wire})
39 6 ▶ or die "framing benchmark unexpectedly hit backpressure\n";
Branch: 1 branch
BranchTrueFalseTotal
$stream->write($state->{"wire"}) or die "framing benchmark unexpectedly hit backpressure\n" 6 0 6
40 }
41 0 ▶ sub on_error ($stream, $error) { die "Stream error: $error\n" }
Subroutine on_error: not called CC=1 SCAR=6.9
42 }
43  
44 1 my @clients = (1, 10, 100, 1000);
45 1 my $messages = 100;
46 1 my $warmup = 10;
47 1 my $bytes = 64;
48 1 my $repeats = 6;
49 1 my $delimiter = "\x02END\x03";
50  
51 GetOptions(
52 1 ▶ 'clients=s' => sub { @clients = split /,/, $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
53 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
54 'warmup=i' => \$warmup,
55 'bytes=i' => \$bytes,
56 'repeats=i' => \$repeats,
57 ) or die "bad options\n";
58  
59 1 ▶die "messages must be > 0\n" if $messages <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $messages <= 0 0 1 1
60 1 ▶die "warmup must be >= 0\n" if $warmup < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $warmup < 0 0 1 1
61 1 ▶die "bytes must be > 0\n" if $bytes <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bytes <= 0 0 1 1
62 1 ▶die "repeats must be > 0\n" if $repeats <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $repeats <= 0 0 1 1
63  
64 1 my @systems = ('raw-on-data', 'native-delimiter');
65 1 my @rows;
66  
67 1 for my $count (@clients) {
68 1 ▶ die "client count must be > 0\n" if $count <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count <= 0 0 1 1
69 1 for my $repeat (1 .. $repeats) {
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 2 my $r = run_case($system, $count);
74 2 push @rows, $r;
75 printf "%s clients=%d repeat=%d %.1f msg/s cpu=%.3f us/msg\n",
76 $system, $count, $repeat,
77 2 $r->{messages_per_second}, $r->{cpu_us_per_message};
78 }
79 }
80 }
81  
82 1 say "\nMedian framing 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 2 ▶ my @set = grep { $_->{clients} == $count && $_->{system} eq $system } @rows;
Condition66%2/3
$_->{'clients'} == $count && $_->{'system'} eq $system
!ll&&!rl&&r
022
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 printf "%-18s %8d %14.1f %14.3f\n",
88 $system, $count,
89 2 median(map { $_->{messages_per_second} } @set),
90 2 median(map { $_->{cpu_us_per_message} } @set);
91 }
92 }
93  
94 1 say "\nBoth paths use the same XS read/write transport and wire format.";
95 1 say "The raw row parses in on_data; the native row finds frame boundaries in XS.";
96  
97 2 ▶sub run_case ($system, $count) {
Subroutine run_case: called CC=10 SCAR=23.1
98 2 my $loop = Linux::Event::Loop->new;
99 2 my $payload = 'x' x $bytes;
100 2 my $wire = $payload . $delimiter;
101 2 my $wire_len = length($wire);
102 2 my @server_fh;
103 my @client_fh;
104 2 my @streams;
105 2 my @client_watchers;
106 2 my @states;
107  
108 2 ▶ my $bench = {
Branch: 1 branch
BranchTrueFalseTotal
$warmup ? : 2 0 2
109 loop => $loop,
110 wire => $wire,
111 wire_len => $wire_len,
112 clients => $count,
113 warmup => $warmup,
114 messages => $messages,
115 warmup_done => 0,
116 measured_done => 0,
117 phase => $warmup ? 'warmup' : 'measure',
118 };
119  
120 2 for my $i (0 .. $count - 1) {
121 2 ▶ 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: $!" 2 0 2
122 or die "socketpair: $!";
123 2 set_nonblocking($server);
124 2 set_nonblocking($client);
125 2 push @server_fh, $server;
126 2 push @client_fh, $client;
127  
128 2 my $state_data = {
129 payload => $payload,
130 wire => $wire,
131 delimiter => $delimiter,
132 buffer => '',
133 };
134 2 ▶ my $class = $system eq 'raw-on-data'
Branch: 1 branch
BranchTrueFalseTotal
$system eq 'raw-on-data' ? : 1 1 2
135 ? 'Linux::Event::Bench::RawDelimiterEcho'
136 : 'Linux::Event::Bench::NativeDelimiterEcho';
137 2 push @streams, $class->new(
138 loop => $loop,
139 fh => $server,
140 data => $state_data,
141 );
142  
143 2 my $state = {
144 bench => $bench,
145 fh => $client,
146 buffer => '',
147 completed => 0,
148 warmup_complete => 0,
149 measured_complete => 0,
150 };
151 2 push @states, $state;
152 2 push @client_watchers, $loop->watch(
153 fh => $client,
154 data => $state,
155 read => \&_client_read,
156 );
157 }
158  
159 2 $bench->{states} = \@states;
160 2 ▶ if ($warmup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warmup) { } 2 0 2
161 2 syswrite_all($_->{fh}, $wire) for @states;
162 } else {
163 0 start_measurement($bench);
164 }
165  
166 2 $loop->run;
167 2 my $elapsed = $bench->{wall_end} - $bench->{wall_start};
168 2 my $cpu = $bench->{cpu_end} - $bench->{cpu_start};
169 2 my $total = $count * $messages;
170  
171 2 $_->close for grep { !$_->is_closed } @streams;
172 2 $_->cancel for @client_watchers;
173 2 close $_ for @client_fh;
174  
175 return {
176 2 system => $system,
177 clients => $count,
178 messages_per_second => $total / $elapsed,
179 cpu_us_per_message => ($cpu * 1_000_000) / $total,
180 };
181 }
182  
183 12 ▶sub _client_read ($watcher) {
Subroutine _client_read: called CC=15 SCAR=29.4
184 12 my $state = $watcher->data;
185 12 my $bench = $state->{bench};
186 12 while (1) {
187 22 my $chunk = '';
188 22 my $n = sysread($state->{fh}, $chunk, 65_536);
189 22 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) 12 10 22
190 12 ▶ die "client unexpected EOF\n" if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 12 12
191 12 $state->{buffer} .= $chunk;
192 12 ▶ while (length($state->{buffer}) >= $bench->{wire_len}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $state->{"buffer"} >= $bench->{"wire_len"}) 12 10 22
193 12 my $got = substr($state->{buffer}, 0, $bench->{wire_len}, '');
194 12 ▶ die "client wire mismatch\n" if $got ne $bench->{wire};
Branch: 1 branch
BranchTrueFalseTotal
if $got ne $bench->{"wire"} 0 12 12
195 12 $state->{completed}++;
196 12 ▶ if ($bench->{phase} eq 'warmup') {
Branch: 1 branch
BranchTrueFalseTotal
if ($bench->{'phase'} eq 'warmup') { } 2 10 12
197 2 ▶ if ($state->{completed} < $bench->{warmup}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state->{'completed'} < $bench->{'warmup'}) { } 0 2 2
elsif (not $state->{'warmup_complete'}++) { } 2 0 2
198 0 syswrite_all($state->{fh}, $bench->{wire});
199 } elsif (!$state->{warmup_complete}++) {
200 2 $bench->{warmup_done}++;
201 start_measurement($bench)
202 2 ▶ if $bench->{warmup_done} == $bench->{clients};
Branch: 1 branch
BranchTrueFalseTotal
if $bench->{"warmup_done"} == $bench->{"clients"} 2 0 2
203 }
204 } else {
205 10 ▶ if ($state->{completed} < $bench->{messages}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state->{'completed'} < $bench->{'messages'}) { } 8 2 10
elsif (not $state->{'measured_complete'}++) { } 2 0 2
206 8 syswrite_all($state->{fh}, $bench->{wire});
207 } elsif (!$state->{measured_complete}++) {
208 2 $bench->{measured_done}++;
209 2 ▶ if ($bench->{measured_done} == $bench->{clients}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bench->{"measured_done"} == $bench->{"clients"}) 2 0 2
210 2 $bench->{wall_end} = time;
211 2 $bench->{cpu_end} = clock_gettime(CLOCK_PROCESS_CPUTIME_ID);
212 2 $bench->{loop}->stop;
213 2 return;
214 }
215 }
216 }
217 }
218 10 next;
219 }
220 10 ▶ return if $!{EAGAIN} || $!{EWOULDBLOCK};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EAGAIN"} or $!{"EWOULDBLOCK"} 10 0 10
Condition33%1/3
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
l!l&&r!l&&!r
1000
MC/DC0%0/2
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
$!{"EAGAIN"} $!{"EWOULDBLOCK"}
Truth table33%1/3
A or B
A= $!{"EAGAIN"}B= $!{"EWOULDBLOCK"}
ABresult
1X1
011
000
221 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
222 0 die "client read: $!";
223 }
224 }
225  
226 2 ▶sub start_measurement ($bench) {
Subroutine start_measurement: called CC=5 SCAR=16.5
227 2 ▶ 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 2 2
Condition33%1/3
$bench->{"phase"} eq "measure" and defined $bench->{"wall_start"}
!ll&&!rl&&r
200
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
228 2 $bench->{phase} = 'measure';
229 2 for my $state (@{ $bench->{states} }) {
230 2 $state->{completed} = 0;
231 2 $state->{buffer} = '';
232 }
233 2 $bench->{cpu_start} = clock_gettime(CLOCK_PROCESS_CPUTIME_ID);
234 2 $bench->{wall_start} = time;
235 2 syswrite_all($_->{fh}, $bench->{wire}) for @{ $bench->{states} };
236 }
237  
238 12 ▶sub syswrite_all ($fh, $bytes) {
Subroutine syswrite_all: called CC=6 SCAR=24.1
239 12 my $off = 0;
240 12 my $len = length($bytes);
241 12 ▶ while ($off < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off < $len) 12 12 24
242 12 my $n = syswrite($fh, $bytes, $len - $off, $off);
243 12 ▶ if (defined $n) { $off += $n; next }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) 12 0 12
244 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
245 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
246 0 die "benchmark write: $!";
247 }
248 }
249  
250 4 ▶sub set_nonblocking ($fh) {
Subroutine set_nonblocking: called CC=3 SCAR=11.7
251 4 my $flags = fcntl($fh, F_GETFL, 0);
252 4 ▶ die "fcntl F_GETFL: $!" if !defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 0 4 4
253 4 ▶ fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
Branch: 1 branch
BranchTrueFalseTotal
fcntl $fh, 4, $flags | 2048 or die "fcntl F_SETFL: $!" 4 0 4
254 or die "fcntl F_SETFL: $!";
255 }
256  
257 4 ▶sub median (@values) {
Subroutine median: called CC=3 SCAR=12.7
258 0 @values = sort { $a <=> $b } @values;
259 4 my $n = @values;
260 4 ▶ return 0 if !$n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 4 4
261 4 ▶ return $values[int($n / 2)] if $n % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $n % 2 4 0 4
262 0 return ($values[$n / 2 - 1] + $values[$n / 2]) / 2;
263 }
« bench/run-stream-microbench.pl ↑ Summary bench/run-callback-batching-fairness.pl »