bench/run-native-framers-microbench.pl

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