bench/run-framing-microbench.pl

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