« 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
| |||||||||||||||||||||||||||||
| 20 | $stream->write($state->{wire}) | ||||||||||||||||||||||||||||
| 21 | 72 | ▶ | or die "benchmark unexpectedly hit backpressure\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | die "messages must be > 0\n" if $messages <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | die "warmup must be >= 0\n" if $warmup < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | ▶ | die "bytes must be > 0\n" if $bytes <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | die "repeats must be > 0\n" if $repeats <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | ▶ | eval qq{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 136 | 0 | return $payload . "\x02END\x03"; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 12 | ▶ | if ($name eq 'fixed') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 2 | return $payload; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 10 | ▶ | if ($name eq 'length') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 2 | return pack('N', length($payload)) . $payload; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 8 | ▶ | if ($name eq 'u32be') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 2 | return pack('N', length($payload)) . $payload; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 6 | ▶ | if ($name eq 'netstring') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 2 | return length($payload) . ':' . $payload . ','; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 4 | ▶ | if ($name eq 'varint') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 2 | my $value = length($payload); | |||||||||||||||||||||||||||
| 152 | 2 | my @prefix; | |||||||||||||||||||||||||||
| 153 | 2 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 2 | my $byte = $value % 128; | |||||||||||||||||||||||||||
| 155 | 2 | $value = int($value / 128); | |||||||||||||||||||||||||||
| 156 | 2 | ▶ | $byte |= 0x80 if $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 251 | 72 | ▶ | die "client unexpected EOF\n" if $n == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 72 | $state->{buffer} .= $chunk; | |||||||||||||||||||||||||||
| 253 | 72 | ▶ | while (length($state->{buffer}) >= $bench->{wire_len}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 256 | 72 | $state->{completed}++; | |||||||||||||||||||||||||||
| 257 | 72 | ▶ | if ($bench->{phase} eq 'warmup') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 12 | ▶ | if ($state->{completed} < $bench->{warmup}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | } else { | ||||||||||||||||||||||||||||
| 266 | 60 | ▶ | if ($state->{completed} < $bench->{messages}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
MC/DC0%0/2
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
$!{"EAGAIN"}
$!{"EWOULDBLOCK"}
Truth table33%1/3
A or B
A= $!{"EAGAIN"}B= $!{"EWOULDBLOCK"}
| |||||||||||||||||||||||||||||
| 282 | 0 | ▶ | next if $!{EINTR}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$bench->{"phase"} eq "measure" and defined $bench->{"wall_start"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 303 | 72 | my $n = syswrite($fh, $bytes, $len - $off, $off); | |||||||||||||||||||||||||||
| 304 | 72 | ▶ | if (defined $n) { $off += $n; next } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | 0 | ▶ | next if $!{EINTR}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 0 | ▶ | die "benchmark write would block unexpectedly\n" if $!{EAGAIN} || $!{EWOULDBLOCK}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
MC/DC0%0/2
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
$!{"EAGAIN"}
$!{"EWOULDBLOCK"}
Truth table0%0/3
A or B
A= $!{"EAGAIN"}B= $!{"EWOULDBLOCK"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 314 | 24 | ▶ | fcntl($fh, F_SETFL, $flags | O_NONBLOCK) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 322 | 12 | ▶ | return $values[int($n / 2)] if $n % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 12 | return ($values[$n / 2 - 1] + $values[$n / 2]) / 2; | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||