bench/run-framer-send-bench.pl

filter: Statementstmt 94.6% 159 / 168 Branchbran 51.6% 32 / 62 Conditioncond 33.3% 3 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 92.3% 24 / 26 Podpod n/a totaltotal 80.8% 219 / 271 CC 21 SCAR 16.7
CC
21
Cov
81%
__ANON__
15.3
run_once
8.9
median
3.8
CRAP 5.3
« bench/run-process-pipe-drain-bench.pl ↑ Summary blib/lib/Linux/Event/Framer/Netstring.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 Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Socket qw(AF_UNIX SOCK_STREAM PF_UNSPEC SOL_SOCKET SO_RCVBUF SO_SNDBUF);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Sys::Hostname qw(hostname);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Time::HiRes qw(time clock_gettime CLOCK_PROCESS_CPUTIME_ID);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Linux::Event::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Linux::Event::IO::Sock::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 {
18 package Linux::Event::Bench::SendLength;
19 1 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ use Linux::Event::Framer 'LengthPrefix', bytes => 4, endian => 'big';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 sub on_message ($stream, $message) { }
22 0 ▶ sub on_error ($stream, $error) { die "Stream error: $error\n" }
Subroutine on_error: not called CC=1 SCAR=6.9
23 }
24  
25 {
26 1 package Linux::Event::Bench::SendVarint;
27 1 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶ use Linux::Event::Framer 'Varint';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 sub on_message ($stream, $message) { }
30 0 ▶ sub on_error ($stream, $error) { die "Stream error: $error\n" }
Subroutine on_error: not called CC=1 SCAR=6.9
31 }
32  
33 1 my @original_argv = @ARGV;
34 1 my @sizes = (64, 256, 1_024, 4_096, 16_384, 32_768,
35 65_536, 131_072, 200_000);
36 1 my @framers = qw(length varint);
37 1 my $repeats = 5;
38 1 my $warmup = 1;
39 1 my $target_bytes = 16 * 1024 * 1024;
40 1 my $min_messages = 128;
41 1 my $max_messages = 200_000;
42 1 my $variant = 'unspecified';
43 1 my $commit = 'unspecified';
44 1 my $output;
45  
46 GetOptions(
47 1 ▶ 'sizes=s' => sub { @sizes = split /,/, $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 1 ▶ 'framers=s' => sub { @framers = split /,/, $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 1 ▶ 'repeats=i' => \$repeats,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("sizes=s", sub { @sizes = split(/,/u, $_[1], 0); } , "framers=s", sub { @framers = split(/,/u, $_[1], 0); } , "repeats=i", \$repeats, "warmup=i", \$warmup, "target-bytes=i", \$target_bytes, "min-messages=i", \$min_messages, "max-messages=i", \$max_messages, "variant=s", \$variant, "commit=s", \$commit, "output=s", \$output) or die "bad options\n" 1 0 1
50 'warmup=i' => \$warmup,
51 'target-bytes=i' => \$target_bytes,
52 'min-messages=i' => \$min_messages,
53 'max-messages=i' => \$max_messages,
54 'variant=s' => \$variant,
55 'commit=s' => \$commit,
56 'output=s' => \$output,
57 ) or die "bad options\n";
58  
59 1 my %valid_framer = map { $_ => 1 } qw(length varint);
60 1 ▶die "sizes must be positive\n" if grep { $_ <= 0 } @sizes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ <= 0;} @sizes 0 1 1
61 1 ▶die "unknown framer\n" if grep { !$valid_framer{$_} } @framers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $valid_framer{$_};} @framers 0 1 1
62 1 ▶die "repeats must be positive\n" if $repeats <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $repeats <= 0 0 1 1
63 1 ▶die "warmup must be non-negative\n" if $warmup < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $warmup < 0 0 1 1
64 1 ▶die "target-bytes must be positive\n" if $target_bytes <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $target_bytes <= 0 0 1 1
65 1 ▶die "message bounds are invalid\n"
Branch: 1 branch
BranchTrueFalseTotal
if $min_messages <= 0 or $max_messages < $min_messages 0 1 1
Condition33%1/3
$min_messages <= 0 or $max_messages < $min_messages
l!l&&r!l&&!r
001
MC/DC0%0/2
$min_messages <= 0 or $max_messages < $min_messages
$min_messages <= 0 $max_messages < $min_messages
Truth table33%1/3
A or B
A= $min_messages <= 0B= $max_messages < $min_messages
ABresult
1X1
011
000
66 if $min_messages <= 0 || $max_messages < $min_messages;
67  
68 1 my %class_for = (
69 length => 'Linux::Event::Bench::SendLength',
70 varint => 'Linux::Event::Bench::SendVarint',
71 );
72 1 my @samples;
73 my %effective_by_case;
74  
75 1 for my $framer (@framers) {
76 2 for my $size (@sizes) {
77 2 my $messages = int($target_bytes / $size);
78 2 ▶ $messages = $min_messages if $messages < $min_messages;
Branch: 1 branch
BranchTrueFalseTotal
if $messages < $min_messages 0 2 2
79 2 ▶ $messages = $max_messages if $messages > $max_messages;
Branch: 1 branch
BranchTrueFalseTotal
if $messages > $max_messages 0 2 2
80 2 run_once($framer, $size, $messages) for 1 .. $warmup;
81 2 for my $repeat (1 .. $repeats) {
82 2 my ($sample, $effective) = run_once($framer, $size, $messages);
83 2 $sample->{repeat} = $repeat;
84 2 push @samples, $sample;
85 2 $effective_by_case{"$framer/$size"} = $effective;
86 printf "%s %7d B repeat=%d %10.1f msg/s %8.3f ns/B\n",
87 $framer, $size, $repeat, $sample->{messages_per_second},
88 2 $sample->{cpu_ns_per_payload_byte};
89 }
90 }
91 }
92  
93 1 my @summary;
94 1 for my $framer (@framers) {
95 2 for my $size (@sizes) {
96 my @case = grep {
97 2 ▶ $_->{framer} eq $framer && $_->{payload_bytes} == $size
Condition66%2/3
$_->{'framer'} eq $framer && $_->{'payload_bytes'} == $size
!ll&&!rl&&r
202
MC/DC50%1/2
$_->{'framer'} eq $framer && $_->{'payload_bytes'} == $size
$_->{'framer'} eq $framer $_->{'payload_bytes'} == $size
Truth table66%2/3
A && B
A= $_->{'framer'} eq $framerB= $_->{'payload_bytes'} == $size
ABresult
0X0
100
111
98 } @samples;
99 push @summary, {
100 framer => $framer,
101 payload_bytes => 0 + $size,
102 messages => $case[0]{messages},
103 median_messages_per_second => median(
104 2 map { $_->{messages_per_second} } @case),
105 median_payload_mib_per_second => median(
106 2 map { $_->{payload_mib_per_second} } @case),
107 median_cpu_ns_per_payload_byte => median(
108 2 map { $_->{cpu_ns_per_payload_byte} } @case),
109 };
110 }
111 }
112  
113 my $report = {
114 benchmark => 'framer-send',
115 benchmark_contract_version => 1,
116 variant => $variant,
117 commit => $commit,
118 command => join(' ', $^X, $0, @original_argv),
119 generated_at_epoch => time,
120 runtime => {
121 hostname => hostname(),
122 perl => "$^V",
123 os => $^O,
124 architecture => $Config::Config{archname},
125 compiler => $Config::Config{cc},
126 compiler_flags => $Config::Config{ccflags},
127 },
128 requested_config => {
129 1 sizes => [map { 0 + $_ } @sizes],
130 framers => [@framers],
131 repeats => $repeats,
132 warmup => $warmup,
133 target_payload_bytes_per_sample => $target_bytes,
134 min_messages => $min_messages,
135 max_messages => $max_messages,
136 },
137 effective_config_by_case => \%effective_by_case,
138 samples => \@samples,
139 summary => \@summary,
140 };
141  
142 1 my $json = JSON::PP->new->canonical->pretty->encode($report);
143 1 ▶if (defined $output) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output) { } 1 0 1
144 1 ▶ open my $fh, '>:raw', $output or die "open $output: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $output or die "open ${output}: $!\n" 1 0 1
145 1 ▶ print {$fh} $json or die "write $output: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $json or die "write ${output}: $!\n" 1 0 1
146 1 ▶ close $fh or die "close $output: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "close ${output}: $!\n" 1 0 1
147 } else {
148 0 print $json;
149 }
150  
151 2 ▶sub run_once ($framer, $payload_size, $messages) {
Subroutine run_once: called CC=8 SCAR=21.9
152 2 ▶ socketpair(my $producer, my $peer, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
Branch: 1 branch
BranchTrueFalseTotal
socketpair my $producer, my $peer, 1, 1, 0 or die "socketpair: $!" 2 0 2
153 or die "socketpair: $!";
154 2 set_nonblocking($peer);
155 2 my $loop = Linux::Event::Loop->new;
156 2 my $class = $class_for{$framer};
157 2 my $stream = $class->new(loop => $loop, fh => $producer);
158 2 my $payload = 'x' x $payload_size;
159 2 ▶ my $prefix_bytes = $framer eq 'length' ? 4 : varint_width($payload_size);
Branch: 1 branch
BranchTrueFalseTotal
$framer eq 'length' ? : 1 1 2
160 2 my $wire_bytes = $messages * ($payload_size + $prefix_bytes);
161 2 my $received = 0;
162 2 my ($wall_end, $cpu_end);
163 my $watcher = $loop->watch(
164 fh => $peer,
165 2 ▶ read => sub ($watcher) {
Subroutine __ANON__: called CC=8 SCAR=27.2
166 2 while (1) {
167 2 my $chunk = '';
168 2 my $count = sysread($peer, $chunk, 262_144);
169 2 ▶ if (defined $count) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $count) 2 0 2
170 2 ▶ die "unexpected benchmark EOF\n" if $count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 0 2 2
171 2 $received += $count;
172 2 ▶ die "received too many framed bytes\n"
Branch: 1 branch
BranchTrueFalseTotal
if $received > $wire_bytes 0 2 2
173 if $received > $wire_bytes;
174 2 ▶ if ($received == $wire_bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($received == $wire_bytes) 2 0 2
175 2 $wall_end = time;
176 2 $cpu_end = clock_gettime(CLOCK_PROCESS_CPUTIME_ID);
177 2 $loop->stop;
178 2 return;
179 }
180 0 next;
181 }
182 0 ▶ return 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
183 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
184 0 die "peer read: $!\n";
185 }
186 },
187 2 );
188  
189 2 my $send_buffer = socket_buffer($producer, SO_SNDBUF);
190 2 my $receive_buffer = socket_buffer($peer, SO_RCVBUF);
191 2 my $cpu_start = clock_gettime(CLOCK_PROCESS_CPUTIME_ID);
192 2 my $wall_start = time;
193 2 $stream->send($payload) for 1 .. $messages;
194 2 ▶ $loop->run if $received < $wire_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if $received < $wire_bytes 2 0 2
195 2 ▶ die "benchmark did not receive every byte\n" if $received != $wire_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if $received != $wire_bytes 0 2 2
196  
197 2 my $elapsed = $wall_end - $wall_start;
198 2 my $cpu = $cpu_end - $cpu_start;
199 2 my $payload_bytes = $messages * $payload_size;
200 2 my $stats = $stream->{xs_state}->stats;
201 2 my $options = $stream->{descriptor}{options};
202 2 my $native = $stream->{descriptor}{framer}{native};
203 my $sample = {
204 framer => $framer,
205 payload_bytes => $payload_size,
206 prefix_bytes => $prefix_bytes,
207 messages => $messages,
208 payload_total_bytes => $payload_bytes,
209 wire_total_bytes => $wire_bytes,
210 elapsed_seconds => $elapsed,
211 cpu_seconds => $cpu,
212 messages_per_second => $messages / $elapsed,
213 payload_mib_per_second => ($payload_bytes / 1_048_576) / $elapsed,
214 cpu_ns_per_payload_byte => $cpu * 1_000_000_000 / $payload_bytes,
215 stream_stats => {
216 2 map { $_ => $stats->{$_} } qw(
217 write_submit_calls write_calls writev_calls bytes_written
218 write_eagain_count write_eintr_count queued_segments
219 queue_peak_bytes pending_bytes
220 )
221 },
222 };
223 my $effective = {
224 delivery => 'write-only framed Stream send()',
225 framer_class => $framer eq 'length'
226 ? 'Linux::Event::Framer::LengthPrefix'
227 : 'Linux::Event::Framer::Varint',
228 framer_parameters => $framer eq 'length'
229 ? { bytes => 4, endian => 'big', include_prefix => 0,
230 max_frame => undef }
231 : { include_prefix => 0, max_frame => undef },
232 transport => 'AF_UNIX SOCK_STREAM socketpair',
233 concurrency => 1,
234 producer_topology => 'one Stream producer, one Loop peer drain',
235 readiness => 'level-triggered',
236 peer_read_size => 262_144,
237 read_size => 0 + $options->{read_size},
238 read_budget_bytes => 0 + $options->{read_budget_bytes},
239 read_batch_bytes => 0 + $options->{read_batch_bytes},
240 message_batch_size => 0 + $options->{message_batch_size},
241 max_buffer => 0 + $options->{max_buffer},
242 high_watermark => 0 + $options->{high_watermark},
243 low_watermark => 0 + $options->{low_watermark},
244 max_pending_bytes => 0 + $options->{max_pending_bytes},
245 2 ▶ write_queue => 'native segmented queue with writev drain',
Branch: 2 branches
BranchTrueFalseTotal
$framer eq 'length' ? : 1 1 2
$framer eq 'length' ? : 1 1 2
246 tls => JSON::PP::false,
247 socket_nonblocking => JSON::PP::true,
248 socket_send_buffer => $send_buffer,
249 socket_receive_buffer => $receive_buffer,
250 socket_options => 'OS defaults',
251 native_descriptor => { %$native },
252 };
253  
254 2 $stream->close;
255 2 $watcher->cancel;
256 2 close $peer;
257 2 return ($sample, $effective);
258 }
259  
260 1 ▶sub varint_width ($value) {
Subroutine varint_width: called CC=2 SCAR=7.1
261 1 my $width = 1;
262 1 ▶ $width++, $value >>= 7 while $value >= 128;
Branch: 1 branch
BranchTrueFalseTotal
$value >= 128 and do { ++$width, $value >>= 7 } 0 1 1
263 1 return $width;
264 }
265  
266 4 ▶sub socket_buffer ($fh, $name) {
Subroutine socket_buffer: called CC=2 SCAR=7.1
267 4 my $packed = getsockopt($fh, SOL_SOCKET, $name);
268 4 ▶ return undef if !defined $packed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $packed 0 4 4
269 4 return unpack('i', $packed);
270 }
271  
272 2 ▶sub set_nonblocking ($fh) {
Subroutine set_nonblocking: called CC=3 SCAR=11.7
273 2 my $flags = fcntl($fh, F_GETFL, 0);
274 2 ▶ die "fcntl F_GETFL: $!\n" if !defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 0 2 2
275 2 ▶ fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
Branch: 1 branch
BranchTrueFalseTotal
fcntl $fh, 4, $flags | 2048 or die "fcntl F_SETFL: $!\n" 2 0 2
276 or die "fcntl F_SETFL: $!\n";
277 }
278  
279 6 ▶sub median (@values) {
Subroutine median: called CC=3 SCAR=13.3
280 0 @values = sort { $a <=> $b } @values;
281 6 ▶ return 0 if !@values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 6 6
282 6 ▶ return $values[int(@values / 2)] if @values % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @values % 2 6 0 6
283 0 return ($values[@values / 2 - 1] + $values[@values / 2]) / 2;
284 }
« bench/run-process-pipe-drain-bench.pl ↑ Summary blib/lib/Linux/Event/Framer/Netstring.pm »