bench/run-native-framers-microbench.pl

filter: Statementstmt 95.6% 221 / 231 Branchbran 57.9% 51 / 88 Conditioncond 33.3% 4 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 97.1% 34 / 35 Podpod n/a totaltotal 83.1% 311 / 374 CC 47 SCAR 20.3
CC
47
Cov
83%
_client_read
18.9
syswrite_all
11.2
wire_for
10.3
CRAP 7.6
« bench/run-callback-batching-microbench.pl ↑ Summary blib/lib/Linux/Event/Framer/DecimalLength.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::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 {
15 package Linux::Event::Bench::NativeFramerBase;
16 1 ▶ use parent 'Linux::Event::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 72 ▶ sub on_message ($stream, $message) {
Subroutine on_message: called CC=3 SCAR=11.7
18 72 my $state = $stream->data;
19 72 ▶ die "framing payload mismatch\n" if $message ne $state->{payload};
Branch: 1 branch
BranchTrueFalseTotal
if $message ne $state->{"payload"} 0 72 72
20 $stream->write($state->{wire})
21 72 ▶ or die "benchmark unexpectedly hit backpressure\n";
Branch: 1 branch
BranchTrueFalseTotal
$stream->write($state->{"wire"}) or die "benchmark unexpectedly hit backpressure\n" 72 0 72
22 }
23 0 ▶ sub on_error ($stream, $error) { die "Stream error: $error\n" }
Subroutine on_error: not called CC=1 SCAR=6.9
24 }
25  
26 {
27 1 package Linux::Event::Bench::NativeDelimiter;
28 1 ▶ use parent -norequire, 'Linux::Event::Bench::NativeFramerBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶ use Linux::Event::Framer 'Delimiter', "\x02END\x03";
Subroutine BEGIN: called CC=1 SCAR=0.0
30 }
31  
32 {
33 1 package Linux::Event::Bench::NativeLength;
34 1 ▶ use parent -norequire, 'Linux::Event::Bench::NativeFramerBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶ use Linux::Event::Framer 'LengthPrefix', bytes => 4, endian => 'big';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 }
37  
38 {
39 1 package Linux::Event::Bench::NativeU32BE;
40 1 ▶ use parent -norequire, 'Linux::Event::Bench::NativeFramerBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶ use Linux::Event::Framer 'U32BE';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 }
43  
44 {
45 1 package Linux::Event::Bench::NativeNetstring;
46 1 ▶ use parent -norequire, 'Linux::Event::Bench::NativeFramerBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶ use Linux::Event::Framer 'Netstring';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 }
49  
50 {
51 1 package Linux::Event::Bench::NativeVarint;
52 1 ▶ use parent -norequire, 'Linux::Event::Bench::NativeFramerBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶ use Linux::Event::Framer 'Varint';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 }
55  
56 {
57 1 package Linux::Event::Bench::NativeDecimal;
58 1 ▶ use parent -norequire, 'Linux::Event::Bench::NativeFramerBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶ use Linux::Event::Framer 'DecimalLength';
Subroutine BEGIN: called CC=1 SCAR=0.0
60 }
61  
62 1 my @clients = (1, 10, 100, 1000);
63 1 my $messages = 100;
64 1 my $warmup = 10;
65 1 my $bytes = 64;
66 1 my $repeats = 6;
67 1 my @framers = qw(delimiter fixed length u32be netstring varint decimal);
68  
69 GetOptions(
70 1 ▶ 'clients=s' => sub { @clients = split /,/, $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
71 'messages=i' => \$messages,
72 'warmup=i' => \$warmup,
73 'bytes=i' => \$bytes,
74 'repeats=i' => \$repeats,
75 1 ▶ 'framers=s' => sub { @framers = split /,/, $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
76 1 ▶) or die "bad options\n";
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, "framers=s", sub { @framers = split(/,/u, $_[1], 0); } ) or die "bad options\n" 1 0 1
77  
78 1 ▶die "messages must be > 0\n" if $messages <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $messages <= 0 0 1 1
79 1 ▶die "warmup must be >= 0\n" if $warmup < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $warmup < 0 0 1 1
80 1 ▶die "bytes must be > 0\n" if $bytes <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bytes <= 0 0 1 1
81 1 ▶die "repeats must be > 0\n" if $repeats <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $repeats <= 0 0 1 1
82  
83 1 my %valid = map { $_ => 1 } qw(delimiter fixed length u32be netstring varint decimal);
84 1 ▶die "unknown framer in --framers\n" if grep { !$valid{$_} } @framers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $valid{$_};} @framers 0 1 1
85  
86 1 ▶eval qq{
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "\n package Linux::Event::Bench::NativeFixed;\n use parent -norequire, 'Linux::Event::Bench::NativeFramerBase';\n use Linux::Event::Framer 'Fixed', size => $bytes;\n 1;\n" or die "define fixed-size benchmark Stream: $@" 1 0 1
87 package Linux::Event::Bench::NativeFixed;
88 use parent -norequire, 'Linux::Event::Bench::NativeFramerBase';
89 use Linux::Event::Framer 'Fixed', size => $bytes;
90 1;
91 } or die "define fixed-size benchmark Stream: $@";
92  
93 1 my %stream_class = (
94 delimiter => 'Linux::Event::Bench::NativeDelimiter',
95 fixed => 'Linux::Event::Bench::NativeFixed',
96 length => 'Linux::Event::Bench::NativeLength',
97 u32be => 'Linux::Event::Bench::NativeU32BE',
98 netstring => 'Linux::Event::Bench::NativeNetstring',
99 varint => 'Linux::Event::Bench::NativeVarint',
100 decimal => 'Linux::Event::Bench::NativeDecimal',
101 );
102 1 my @rows;
103  
104 1 for my $framer_name (@framers) {
105 6 for my $count (@clients) {
106 6 ▶ die "client count must be > 0\n" if $count <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count <= 0 0 6 6
107 6 for my $repeat (1 .. $repeats) {
108 12 my $r = run_case($framer_name, $count);
109 12 push @rows, $r;
110 printf "%s/native clients=%d repeat=%d %.1f msg/s cpu=%.3f us/msg\n",
111 $framer_name, $count, $repeat,
112 12 $r->{messages_per_second}, $r->{cpu_us_per_message};
113 }
114 }
115 }
116  
117 1 say "\nMedian native built-in framing microbenchmark";
118 1 printf "%-10s %8s %14s %14s\n", 'framer', 'clients', 'msg/s', 'cpu us/msg';
119 1 for my $framer_name (@framers) {
120 6 for my $count (@clients) {
121 my @set = grep {
122 6 ▶ $_->{framer} eq $framer_name && $_->{clients} == $count
Condition66%2/3
$_->{'framer'} eq $framer_name && $_->{'clients'} == $count
!ll&&!rl&&r
60012
MC/DC50%1/2
$_->{'framer'} eq $framer_name && $_->{'clients'} == $count
$_->{'framer'} eq $framer_name $_->{'clients'} == $count
Truth table66%2/3
A && B
A= $_->{'framer'} eq $framer_nameB= $_->{'clients'} == $count
ABresult
0X0
100
111
123 } @rows;
124 printf "%-10s %8d %14.1f %14.3f\n",
125 $framer_name, $count,
126 12 median(map { $_->{messages_per_second} } @set),
127 6 median(map { $_->{cpu_us_per_message} } @set);
128 }
129 }
130  
131 1 say "\nEach row uses a canonical Stream subclass and its native built-in parser.";
132 1 say "Compare framers only within the same payload, client, and host settings.";
133  
134 12 ▶sub wire_for ($name, $payload) {
Subroutine wire_for: called CC=10 SCAR=23.4
135 12 ▶ if ($name eq 'delimiter') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "delimiter") 0 12 12
136 0 return $payload . "\x02END\x03";
137 }
138 12 ▶ if ($name eq 'fixed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "fixed") 2 10 12
139 2 return $payload;
140 }
141 10 ▶ if ($name eq 'length') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "length") 2 8 10
142 2 return pack('N', length($payload)) . $payload;
143 }
144 8 ▶ if ($name eq 'u32be') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "u32be") 2 6 8
145 2 return pack('N', length($payload)) . $payload;
146 }
147 6 ▶ if ($name eq 'netstring') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "netstring") 2 4 6
148 2 return length($payload) . ':' . $payload . ',';
149 }
150 4 ▶ if ($name eq 'varint') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "varint") 2 2 4
151 2 my $value = length($payload);
152 2 my @prefix;
153 2 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$value and do { do { my $byte = $value % 128; $value = int $value / 128; $byte |= 128 if $value; push @prefix, $byte } } 0 2 2
154 2 my $byte = $value % 128;
155 2 $value = int($value / 128);
156 2 ▶ $byte |= 0x80 if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 0 2 2
157 2 push @prefix, $byte;
158 } while ($value);
159 2 return pack('C*', @prefix) . $payload;
160 }
161 2 ▶ if ($name eq 'decimal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "decimal") 2 0 2
162 2 return length($payload) . ' ' . $payload;
163 }
164 0 die "unknown framer $name\n";
165 }
166  
167 12 ▶sub run_case ($framer_name, $count) {
Subroutine run_case: called CC=9 SCAR=22.1
168 12 my $loop = Linux::Event::Loop->new;
169 12 my $payload = 'x' x $bytes;
170 12 my $wire = wire_for($framer_name, $payload);
171 12 my $wire_len = length($wire);
172 12 my @client_fh;
173 my @streams;
174 12 my @client_watchers;
175 12 my @states;
176  
177 12 ▶ my $bench = {
Branch: 1 branch
BranchTrueFalseTotal
$warmup ? : 12 0 12
178 loop => $loop,
179 wire => $wire,
180 wire_len => $wire_len,
181 clients => $count,
182 warmup => $warmup,
183 messages => $messages,
184 warmup_done => 0,
185 measured_done => 0,
186 phase => $warmup ? 'warmup' : 'measure',
187 };
188  
189 12 for my $i (0 .. $count - 1) {
190 12 ▶ 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: $!" 12 0 12
191 or die "socketpair: $!";
192 12 set_nonblocking($server);
193 12 set_nonblocking($client);
194 12 push @client_fh, $client;
195  
196 12 my $class = $stream_class{$framer_name};
197 12 push @streams, $class->new(
198 loop => $loop,
199 fh => $server,
200 data => { payload => $payload, wire => $wire },
201 );
202  
203 12 my $state = {
204 bench => $bench,
205 fh => $client,
206 buffer => '',
207 completed => 0,
208 warmup_complete => 0,
209 measured_complete => 0,
210 };
211 12 push @states, $state;
212 12 push @client_watchers, $loop->watch(
213 fh => $client,
214 data => $state,
215 read => \&_client_read,
216 );
217 }
218  
219 12 $bench->{states} = \@states;
220 12 ▶ if ($warmup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warmup) { } 12 0 12
221 12 syswrite_all($_->{fh}, $wire) for @states;
222 } else {
223 0 start_measurement($bench);
224 }
225  
226 12 $loop->run;
227 12 my $elapsed = $bench->{wall_end} - $bench->{wall_start};
228 12 my $cpu = $bench->{cpu_end} - $bench->{cpu_start};
229 12 my $total = $count * $messages;
230  
231 12 $_->close for grep { !$_->is_closed } @streams;
232 12 $_->cancel for @client_watchers;
233 12 close $_ for @client_fh;
234  
235 return {
236 12 framer => $framer_name,
237 mode => 'native',
238 clients => $count,
239 messages_per_second => $total / $elapsed,
240 cpu_us_per_message => ($cpu * 1_000_000) / $total,
241 };
242 }
243  
244 72 ▶sub _client_read ($watcher) {
Subroutine _client_read: called CC=15 SCAR=29.4
245 72 my $state = $watcher->data;
246 72 my $bench = $state->{bench};
247 72 while (1) {
248 132 my $chunk = '';
249 132 my $n = sysread($state->{fh}, $chunk, 65_536);
250 132 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) 72 60 132
251 72 ▶ die "client unexpected EOF\n" if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 72 72
252 72 $state->{buffer} .= $chunk;
253 72 ▶ while (length($state->{buffer}) >= $bench->{wire_len}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $state->{"buffer"} >= $bench->{"wire_len"}) 72 60 132
254 72 my $got = substr($state->{buffer}, 0, $bench->{wire_len}, '');
255 72 ▶ die "client wire mismatch\n" if $got ne $bench->{wire};
Branch: 1 branch
BranchTrueFalseTotal
if $got ne $bench->{"wire"} 0 72 72
256 72 $state->{completed}++;
257 72 ▶ if ($bench->{phase} eq 'warmup') {
Branch: 1 branch
BranchTrueFalseTotal
if ($bench->{'phase'} eq 'warmup') { } 12 60 72
258 12 ▶ if ($state->{completed} < $bench->{warmup}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state->{'completed'} < $bench->{'warmup'}) { } 0 12 12
elsif (not $state->{'warmup_complete'}++) { } 12 0 12
259 0 syswrite_all($state->{fh}, $bench->{wire});
260 } elsif (!$state->{warmup_complete}++) {
261 12 $bench->{warmup_done}++;
262 start_measurement($bench)
263 12 ▶ if $bench->{warmup_done} == $bench->{clients};
Branch: 1 branch
BranchTrueFalseTotal
if $bench->{"warmup_done"} == $bench->{"clients"} 12 0 12
264 }
265 } else {
266 60 ▶ if ($state->{completed} < $bench->{messages}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state->{'completed'} < $bench->{'messages'}) { } 48 12 60
elsif (not $state->{'measured_complete'}++) { } 12 0 12
267 48 syswrite_all($state->{fh}, $bench->{wire});
268 } elsif (!$state->{measured_complete}++) {
269 12 $bench->{measured_done}++;
270 12 ▶ if ($bench->{measured_done} == $bench->{clients}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bench->{"measured_done"} == $bench->{"clients"}) 12 0 12
271 12 $bench->{wall_end} = time;
272 12 $bench->{cpu_end} = clock_gettime(CLOCK_PROCESS_CPUTIME_ID);
273 12 $bench->{loop}->stop;
274 12 return;
275 }
276 }
277 }
278 }
279 60 next;
280 }
281 60 ▶ return if $!{EAGAIN} || $!{EWOULDBLOCK};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EAGAIN"} or $!{"EWOULDBLOCK"} 60 0 60
Condition33%1/3
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
l!l&&r!l&&!r
6000
MC/DC0%0/2
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
$!{"EAGAIN"} $!{"EWOULDBLOCK"}
Truth table33%1/3
A or B
A= $!{"EAGAIN"}B= $!{"EWOULDBLOCK"}
ABresult
1X1
011
000
282 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
283 0 die "client read: $!";
284 }
285 }
286  
287 12 ▶sub start_measurement ($bench) {
Subroutine start_measurement: called CC=5 SCAR=16.5
288 12 ▶ 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 12 12
Condition33%1/3
$bench->{"phase"} eq "measure" and defined $bench->{"wall_start"}
!ll&&!rl&&r
1200
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
289 12 $bench->{phase} = 'measure';
290 12 for my $state (@{ $bench->{states} }) {
291 12 $state->{completed} = 0;
292 12 $state->{buffer} = '';
293 }
294 12 $bench->{cpu_start} = clock_gettime(CLOCK_PROCESS_CPUTIME_ID);
295 12 $bench->{wall_start} = time;
296 12 syswrite_all($_->{fh}, $bench->{wire}) for @{ $bench->{states} };
297 }
298  
299 72 ▶sub syswrite_all ($fh, $bytes) {
Subroutine syswrite_all: called CC=6 SCAR=24.1
300 72 my $off = 0;
301 72 my $len = length($bytes);
302 72 ▶ while ($off < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off < $len) 72 72 144
303 72 my $n = syswrite($fh, $bytes, $len - $off, $off);
304 72 ▶ if (defined $n) { $off += $n; next }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) 72 0 72
305 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
306 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
307 0 die "benchmark write: $!";
308 }
309 }
310  
311 24 ▶sub set_nonblocking ($fh) {
Subroutine set_nonblocking: called CC=3 SCAR=11.7
312 24 my $flags = fcntl($fh, F_GETFL, 0);
313 24 ▶ die "fcntl F_GETFL: $!" if !defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 0 24 24
314 24 ▶ fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
Branch: 1 branch
BranchTrueFalseTotal
fcntl $fh, 4, $flags | 2048 or die "fcntl F_SETFL: $!" 24 0 24
315 or die "fcntl F_SETFL: $!";
316 }
317  
318 12 ▶sub median (@values) {
Subroutine median: called CC=3 SCAR=11.2
319 12 @values = sort { $a <=> $b } @values;
320 12 my $n = @values;
321 12 ▶ return 0 if !$n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 12 12
322 12 ▶ return $values[int($n / 2)] if $n % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $n % 2 0 12 12
323 12 return ($values[$n / 2 - 1] + $values[$n / 2]) / 2;
324 }
« bench/run-callback-batching-microbench.pl ↑ Summary blib/lib/Linux/Event/Framer/DecimalLength.pm »