| 1 | #!/usr/bin/env perl | ||||||||||||||||||||
| 2 | 5 | ▶ | use v5.36; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 5 | ▶ | use Getopt::Long qw(GetOptions); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 5 | ▶ | use IO::Socket::INET; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 5 | ▶ | use JSON::PP; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 5 | ▶ | use Socket qw(AF_UNIX SOCK_STREAM PF_UNSPEC); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 5 | ▶ | use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC usleep); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 5 | ▶ | use Linux::Event; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 5 | ▶ | use Linux::Event::Loop; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 5 | ▶ | use Linux::Event::IO::Sock::Stream; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 6 | my $duration = 0.5; | |||||||||||||||||||
| 17 | 6 | my $ping_interval_us = 2_000; | |||||||||||||||||||
| 18 | 6 | my $payload_bytes = 64; | |||||||||||||||||||
| 19 | 6 | my $read_size = 4_096; | |||||||||||||||||||
| 20 | 6 | my @batch_sizes = (0, 4, 16, 64, 256); | |||||||||||||||||||
| 21 | 6 | my @transports = qw(unix tcp); | |||||||||||||||||||
| 22 | 5 | my $warmup = 1; | |||||||||||||||||||
| 23 | 5 | my $repeats = 5; | |||||||||||||||||||
| 24 | 5 | my $json_path; | |||||||||||||||||||
| 25 | my $help; | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | GetOptions( | ||||||||||||||||||||
| 28 | 'duration=f' => \$duration, | ||||||||||||||||||||
| 29 | 'ping-interval-us=i' => \$ping_interval_us, | ||||||||||||||||||||
| 30 | 'bytes=i' => \$payload_bytes, | ||||||||||||||||||||
| 31 | 'read-size=i' => \$read_size, | ||||||||||||||||||||
| 32 | 5 | ▶ | 'batch-sizes=s' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 5 | @batch_sizes = map { 0 + $_ } split /,/, $_[1]; | |||||||||||||||||||
| 34 | }, | ||||||||||||||||||||
| 35 | 5 | ▶ | 'transports=s' => sub { @transports = split /,/, $_[1] }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | 5 | ▶ | 'warmup=i' => \$warmup, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 'repeats=i' => \$repeats, | ||||||||||||||||||||
| 38 | 'json=s' => \$json_path, | ||||||||||||||||||||
| 39 | 'help' => \$help, | ||||||||||||||||||||
| 40 | ) or usage(1); | ||||||||||||||||||||
| 41 | 5 | ▶ | usage(0) if $help; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 5 | ▶ | die "duration must be positive\n" if $duration <= 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 5 | ▶ | die "ping interval must be positive\n" if $ping_interval_us < 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | 5 | ▶ | die "bytes must be positive\n" if $payload_bytes < 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | 5 | ▶ | die "read size must be positive\n" if $read_size < 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 5 | ▶ | die "warmup must be non-negative\n" if $warmup < 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 5 | ▶ | die "repeats must be positive\n" if $repeats < 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | 5 | ▶ | die "batch sizes must be non-negative\n" if grep { $_ < 0 } @batch_sizes; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 5 | my %valid_transport = map { $_ => 1 } qw(unix tcp); | |||||||||||||||||||
| 50 | 5 | ▶ | die "unknown transport\n" if grep { !$valid_transport{$_} } @transports; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | my @cases = map { | ||||||||||||||||||||
| 53 | 5 | my $transport = $_; | |||||||||||||||||||
| 54 | 5 | map { +{ transport => $transport, batch_size => $_ } } @batch_sizes; | |||||||||||||||||||
| 55 | } @transports; | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 5 | for my $case (@cases) { | |||||||||||||||||||
| 58 | 10 | run_case($case) for 1 .. $warmup; | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 5 | my @raw; | |||||||||||||||||||
| 62 | 5 | for my $repeat (1 .. $repeats) { | |||||||||||||||||||
| 63 | 5 | my @ordered = rotate_cases(\@cases, $repeat - 1); | |||||||||||||||||||
| 64 | 5 | for my $case (@ordered) { | |||||||||||||||||||
| 65 | 8 | my $row = run_case($case); | |||||||||||||||||||
| 66 | 4 | $row->{repeat} = $repeat; | |||||||||||||||||||
| 67 | 4 | push @raw, $row; | |||||||||||||||||||
| 68 | printf "%s batch=%d repeat=%d hot=%.1f MiB/s ping p99=%.1f us max=%.1f us n=%d\n", | ||||||||||||||||||||
| 69 | $case->{transport}, $case->{batch_size}, $repeat, | ||||||||||||||||||||
| 70 | $row->{hot_payload_mib_per_second}, $row->{ping_p99_us}, | ||||||||||||||||||||
| 71 | 4 | $row->{ping_max_us}, $row->{ping_count}; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 1 | my @summary; | |||||||||||||||||||
| 76 | 1 | for my $case (@cases) { | |||||||||||||||||||
| 77 | my @rows = grep { | ||||||||||||||||||||
| 78 | 2 | $_->{transport} eq $case->{transport} | |||||||||||||||||||
| 79 | && $_->{batch_size} == $case->{batch_size} | ||||||||||||||||||||
| 80 | 4 | ▶ | } @raw; | ||||||||||||||||||
|
Condition66%2/3
$_->{'transport'} eq $case->{'transport'} && $_->{'batch_size'} == $case->{'batch_size'}
MC/DC50%1/2
$_->{'transport'} eq $case->{'transport'} && $_->{'batch_size'} == $case->{'batch_size'}
$_->{'transport'} eq $case->{'transport'}
$_->{'batch_size'} == $case->{'batch_size'}
Truth table66%2/3
A && B
A= $_->{'transport'} eq $case->{'transport'}B= $_->{'batch_size'} == $case->{'batch_size'}
| |||||||||||||||||||||
| 81 | push @summary, { | ||||||||||||||||||||
| 82 | %$case, | ||||||||||||||||||||
| 83 | median_hot_payload_mib_per_second => median( | ||||||||||||||||||||
| 84 | 2 | map { $_->{hot_payload_mib_per_second} } @rows, | |||||||||||||||||||
| 85 | ), | ||||||||||||||||||||
| 86 | 2 | median_ping_p50_us => median(map { $_->{ping_p50_us} } @rows), | |||||||||||||||||||
| 87 | 2 | median_ping_p99_us => median(map { $_->{ping_p99_us} } @rows), | |||||||||||||||||||
| 88 | 2 | median_ping_max_us => median(map { $_->{ping_max_us} } @rows), | |||||||||||||||||||
| 89 | 2 | median_ping_count => median(map { $_->{ping_count} } @rows), | |||||||||||||||||||
| 90 | median_hot_callback_calls => median( | ||||||||||||||||||||
| 91 | 2 | map { $_->{hot_callback_calls} } @rows, | |||||||||||||||||||
| 92 | ), | ||||||||||||||||||||
| 93 | }; | ||||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 1 | say "\nCallback batching fairness benchmark"; | |||||||||||||||||||
| 97 | 1 | printf "duration=%.3f ping_interval_us=%d payload=%d read_size=%d repeats=%d\n", | |||||||||||||||||||
| 98 | $duration, $ping_interval_us, $payload_bytes, $read_size, $repeats; | ||||||||||||||||||||
| 99 | 1 | printf "%-6s %8s %12s %12s %12s %12s %12s\n", | |||||||||||||||||||
| 100 | 'xport', 'batch', 'hot MiB/s', 'ping p50', 'ping p99', 'ping max', | ||||||||||||||||||||
| 101 | 'hot cb'; | ||||||||||||||||||||
| 102 | 1 | for my $row (@summary) { | |||||||||||||||||||
| 103 | printf "%-6s %8d %12.1f %12.1f %12.1f %12.1f %12.0f\n", | ||||||||||||||||||||
| 104 | $row->{transport}, $row->{batch_size}, | ||||||||||||||||||||
| 105 | $row->{median_hot_payload_mib_per_second}, | ||||||||||||||||||||
| 106 | $row->{median_ping_p50_us}, $row->{median_ping_p99_us}, | ||||||||||||||||||||
| 107 | 2 | $row->{median_ping_max_us}, $row->{median_hot_callback_calls}; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 1 | ▶ | if (defined $json_path) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | 1 | my $report = { | |||||||||||||||||||
| 112 | benchmark => 'linux-event-callback-batching-fairness', | ||||||||||||||||||||
| 113 | benchmark_contract_version => 1, | ||||||||||||||||||||
| 114 | linux_event_version => $Linux::Event::VERSION, | ||||||||||||||||||||
| 115 | configuration => { | ||||||||||||||||||||
| 116 | duration => $duration, | ||||||||||||||||||||
| 117 | ping_interval_us => $ping_interval_us, | ||||||||||||||||||||
| 118 | bytes => $payload_bytes, | ||||||||||||||||||||
| 119 | read_size => $read_size, | ||||||||||||||||||||
| 120 | batch_sizes => \@batch_sizes, | ||||||||||||||||||||
| 121 | transports => \@transports, | ||||||||||||||||||||
| 122 | warmup => $warmup, | ||||||||||||||||||||
| 123 | repeats => $repeats, | ||||||||||||||||||||
| 124 | }, | ||||||||||||||||||||
| 125 | raw => \@raw, | ||||||||||||||||||||
| 126 | summary => \@summary, | ||||||||||||||||||||
| 127 | }; | ||||||||||||||||||||
| 128 | 1 | ▶ | open my $json, '>', $json_path or die "open $json_path: $!\n"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 1 | print {$json} JSON::PP->new->canonical->pretty->encode($report); | |||||||||||||||||||
| 130 | 1 | ▶ | close $json or die "close $json_path: $!\n"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 8 | ▶ | sub run_case ($case) { | ||||||||||||||||||
|
Subroutine
run_case: called CC=19 SCAR=30.2
| |||||||||||||||||||||
| 134 | 8 | my ($hot_receiver, $hot_sender) = connected_pair($case->{transport}); | |||||||||||||||||||
| 135 | 8 | my ($ping_receiver, $ping_sender) = connected_pair($case->{transport}); | |||||||||||||||||||
| 136 | 8 | ▶ | pipe(my $gate_read, my $gate_write) or die "gate pipe: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 8 | my $payload = 'x' x $payload_bytes; | |||||||||||||||||||
| 139 | 8 | my $hot_wire = ($payload . "\n") x 1_000; | |||||||||||||||||||
| 140 | 8 | my $hot_pid = fork(); | |||||||||||||||||||
| 141 | 8 | ▶ | die "hot producer fork: $!" if !defined $hot_pid; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | 8 | ▶ | if ($hot_pid == 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | 2 | close $hot_receiver; | |||||||||||||||||||
| 144 | 2 | close $ping_receiver; | |||||||||||||||||||
| 145 | 2 | close $ping_sender; | |||||||||||||||||||
| 146 | 2 | close $gate_write; | |||||||||||||||||||
| 147 | 2 | my $gate = ''; | |||||||||||||||||||
| 148 | 2 | ▶ | sysread($gate_read, $gate, 1) == 1 or exit 2; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 149 | 2 | close $gate_read; | |||||||||||||||||||
| 150 | 2 | my $deadline = now() + $duration; | |||||||||||||||||||
| 151 | 2 | local $SIG{PIPE} = 'IGNORE'; | |||||||||||||||||||
| 152 | 2 | ▶ | while (now() < $deadline) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 153 | 148 | ▶ | last if !eval { write_all($hot_sender, $hot_wire); 1 }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | 2 | close $hot_sender; | |||||||||||||||||||
| 156 | 2 | exit 0; | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 6 | my $ping_pid = fork(); | |||||||||||||||||||
| 160 | 6 | ▶ | die "ping producer fork: $!" if !defined $ping_pid; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | 6 | ▶ | if ($ping_pid == 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 162 | 2 | close $hot_receiver; | |||||||||||||||||||
| 163 | 2 | close $hot_sender; | |||||||||||||||||||
| 164 | 2 | close $ping_receiver; | |||||||||||||||||||
| 165 | 2 | close $gate_write; | |||||||||||||||||||
| 166 | 2 | my $gate = ''; | |||||||||||||||||||
| 167 | 2 | ▶ | sysread($gate_read, $gate, 1) == 1 or exit 2; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 168 | 2 | close $gate_read; | |||||||||||||||||||
| 169 | 2 | my $deadline = now() + $duration; | |||||||||||||||||||
| 170 | 2 | local $SIG{PIPE} = 'IGNORE'; | |||||||||||||||||||
| 171 | 2 | ▶ | while (now() < $deadline) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 172 | 40 | ▶ | last if !eval { write_all($ping_sender, pack('d', now())); 1 }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 173 | 40 | usleep($ping_interval_us); | |||||||||||||||||||
| 174 | } | ||||||||||||||||||||
| 175 | 2 | close $ping_sender; | |||||||||||||||||||
| 176 | 2 | exit 0; | |||||||||||||||||||
| 177 | } | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 4 | close $hot_sender; | |||||||||||||||||||
| 180 | 4 | close $ping_sender; | |||||||||||||||||||
| 181 | 4 | close $gate_read; | |||||||||||||||||||
| 182 | 4 | my $loop = Linux::Event::Loop->new; | |||||||||||||||||||
| 183 | 4 | my $shared = { loop => $loop, eof => 0, latencies => [] }; | |||||||||||||||||||
| 184 | 4 | my $hot_state = { shared => $shared, messages => 0 }; | |||||||||||||||||||
| 185 | 4 | my $ping_state = { shared => $shared }; | |||||||||||||||||||
| 186 | 4 | my $hot_class = hot_class($case->{batch_size}); | |||||||||||||||||||
| 187 | 4 | my $hot = $hot_class->new( | |||||||||||||||||||
| 188 | loop => $loop, fh => $hot_receiver, data => $hot_state, | ||||||||||||||||||||
| 189 | ); | ||||||||||||||||||||
| 190 | 4 | my $ping = Linux::Event::Bench::BatchFairness::Ping->new( | |||||||||||||||||||
| 191 | loop => $loop, fh => $ping_receiver, data => $ping_state, | ||||||||||||||||||||
| 192 | ); | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 4 | my $started = now(); | |||||||||||||||||||
| 195 | 4 | ▶ | syswrite($gate_write, 'gg') == 2 or die "release producers: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 196 | 4 | close $gate_write; | |||||||||||||||||||
| 197 | 0 | ▶ | local $SIG{ALRM} = sub { die "fairness benchmark timed out\n" }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 198 | 4 | alarm int($duration + 10); | |||||||||||||||||||
| 199 | 4 | my $ok = eval { $loop->run; 1 }; | |||||||||||||||||||
| 200 | 4 | my $error = $@; | |||||||||||||||||||
| 201 | 4 | alarm 0; | |||||||||||||||||||
| 202 | 4 | my $elapsed = now() - $started; | |||||||||||||||||||
| 203 | 4 | ▶ | if (!$ok) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 204 | 0 | kill 'TERM', $hot_pid, $ping_pid; | |||||||||||||||||||
| 205 | 0 | waitpid($hot_pid, 0); | |||||||||||||||||||
| 206 | 0 | waitpid($ping_pid, 0); | |||||||||||||||||||
| 207 | 0 | die $error; | |||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | 4 | waitpid($hot_pid, 0); | |||||||||||||||||||
| 210 | 4 | my $hot_status = $?; | |||||||||||||||||||
| 211 | 4 | waitpid($ping_pid, 0); | |||||||||||||||||||
| 212 | 4 | my $ping_status = $?; | |||||||||||||||||||
| 213 | 4 | ▶ | die "hot producer failed with status $hot_status\n" if $hot_status != 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 214 | 4 | ▶ | die "ping producer failed with status $ping_status\n" if $ping_status != 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | 4 | my @latency = sort { $a <=> $b } @{ $shared->{latencies} }; | |||||||||||||||||||
| 217 | 4 | ▶ | die "fairness benchmark received no pings\n" if !@latency; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 218 | 4 | my $stats = $hot->{xs_state}->stats; | |||||||||||||||||||
| 219 | 4 | my $hot_payload_bytes = $hot_state->{messages} * $payload_bytes; | |||||||||||||||||||
| 220 | my $hot_callbacks = $stats->{message_callback_calls} | ||||||||||||||||||||
| 221 | 4 | + $stats->{message_batch_calls}; | |||||||||||||||||||
| 222 | 4 | ▶ | $hot->close if !$hot->is_closed; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 223 | 4 | ▶ | $ping->close if !$ping->is_closed; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 224 | return { | ||||||||||||||||||||
| 225 | %$case, | ||||||||||||||||||||
| 226 | elapsed_seconds => $elapsed, | ||||||||||||||||||||
| 227 | hot_messages => $hot_state->{messages}, | ||||||||||||||||||||
| 228 | 4 | hot_payload_mib_per_second => | |||||||||||||||||||
| 229 | ($hot_payload_bytes / 1_048_576) / $elapsed, | ||||||||||||||||||||
| 230 | hot_callback_calls => $hot_callbacks, | ||||||||||||||||||||
| 231 | ping_count => scalar @latency, | ||||||||||||||||||||
| 232 | ping_p50_us => percentile(\@latency, 0.50), | ||||||||||||||||||||
| 233 | ping_p99_us => percentile(\@latency, 0.99), | ||||||||||||||||||||
| 234 | ping_max_us => $latency[-1], | ||||||||||||||||||||
| 235 | }; | ||||||||||||||||||||
| 236 | } | ||||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | { | ||||||||||||||||||||
| 239 | package Linux::Event::Bench::BatchFairness::Ping; | ||||||||||||||||||||
| 240 | 5 | ▶ | use parent 'Linux::Event::IO::Sock::Stream'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 241 | 5 | ▶ | use Linux::Event::Framer 'Fixed', size => 8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 242 | 80 | ▶ | sub on_message ($stream, $message) { | ||||||||||||||||||
|
Subroutine
on_message: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 243 | 80 | my $sent = unpack('d', $message); | |||||||||||||||||||
| 244 | 80 | push @{ $stream->data->{shared}{latencies} }, | |||||||||||||||||||
| 245 | (main::now() - $sent) * 1_000_000; | ||||||||||||||||||||
| 246 | } | ||||||||||||||||||||
| 247 | 4 | ▶ | sub on_eof ($stream) { main::note_eof($stream) } | ||||||||||||||||||
|
Subroutine
on_eof: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 248 | } | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | 1 | my %hot_class; | |||||||||||||||||||
| 251 | 4 | ▶ | sub hot_class ($batch_size) { | ||||||||||||||||||
|
Subroutine
hot_class: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 252 | 4 | ▶ | return $hot_class{$batch_size} if $hot_class{$batch_size}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 253 | 4 | my $class = "Linux::Event::Bench::BatchFairness::Hot_$batch_size"; | |||||||||||||||||||
| 254 | 4 | ▶ | eval qq{ | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||
| 255 | package $class; | ||||||||||||||||||||
| 256 | use parent 'Linux::Event::IO::Sock::Stream'; | ||||||||||||||||||||
| 257 | use Linux::Event::Framer 'Delimiter', "\\n"; | ||||||||||||||||||||
| 258 | 1; | ||||||||||||||||||||
| 259 | } or die "define hot Stream class: $@"; | ||||||||||||||||||||
| 260 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 261 | 4 | ▶ | *{"${class}::stream_tuning"} = sub ($class_name) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 262 | 4 | return read_size => $read_size, message_batch_size => $batch_size; | |||||||||||||||||||
| 263 | 4 | }; | |||||||||||||||||||
| 264 | 4 | ▶ | if ($batch_size) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 265 | 1 | ▶ | *{"${class}::on_messages"} = sub ($stream, $messages) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 266 | 23506 | $stream->data->{messages} += scalar @$messages; | |||||||||||||||||||
| 267 | 1 | }; | |||||||||||||||||||
| 268 | } else { | ||||||||||||||||||||
| 269 | 3 | ▶ | *{"${class}::on_message"} = sub ($stream, $message) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 270 | 162000 | $stream->data->{messages}++; | |||||||||||||||||||
| 271 | 3 | }; | |||||||||||||||||||
| 272 | } | ||||||||||||||||||||
| 273 | 4 | ▶ | *{"${class}::on_eof"} = sub ($stream) { main::note_eof($stream) }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 274 | 4 | $hot_class{$batch_size} = $class; | |||||||||||||||||||
| 275 | 4 | return $class; | |||||||||||||||||||
| 276 | } | ||||||||||||||||||||
| 277 | |||||||||||||||||||||
| 278 | 8 | ▶ | sub note_eof ($stream) { | ||||||||||||||||||
|
Subroutine
note_eof: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 279 | 8 | my $shared = $stream->data->{shared}; | |||||||||||||||||||
| 280 | 8 | $shared->{eof}++; | |||||||||||||||||||
| 281 | 8 | ▶ | $shared->{loop}->stop if $shared->{eof} == 2; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 282 | } | ||||||||||||||||||||
| 283 | |||||||||||||||||||||
| 284 | 16 | ▶ | sub connected_pair ($transport) { | ||||||||||||||||||
|
Subroutine
connected_pair: called CC=6 SCAR=29.7
| |||||||||||||||||||||
| 285 | 16 | ▶ | if ($transport eq 'unix') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 286 | 16 | ▶ | socketpair(my $left, my $right, AF_UNIX, SOCK_STREAM, PF_UNSPEC) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 287 | or die "socketpair: $!"; | ||||||||||||||||||||
| 288 | 16 | return ($left, $right); | |||||||||||||||||||
| 289 | } | ||||||||||||||||||||
| 290 | 0 | ▶ | my $listener = IO::Socket::INET->new( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 291 | LocalAddr => '127.0.0.1', LocalPort => 0, | ||||||||||||||||||||
| 292 | Proto => 'tcp', Listen => 1, ReuseAddr => 1, | ||||||||||||||||||||
| 293 | ) or die "TCP listener: $!"; | ||||||||||||||||||||
| 294 | 0 | ▶ | my $sender = IO::Socket::INET->new( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 295 | PeerAddr => '127.0.0.1', PeerPort => $listener->sockport, | ||||||||||||||||||||
| 296 | Proto => 'tcp', | ||||||||||||||||||||
| 297 | ) or die "TCP connect: $!"; | ||||||||||||||||||||
| 298 | 0 | ▶ | my $receiver = $listener->accept or die "TCP accept: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 299 | 0 | close $listener; | |||||||||||||||||||
| 300 | 0 | return ($receiver, $sender); | |||||||||||||||||||
| 301 | } | ||||||||||||||||||||
| 302 | |||||||||||||||||||||
| 303 | 188 | ▶ | sub write_all ($fh, $bytes) { | ||||||||||||||||||
|
Subroutine
write_all: called CC=4 SCAR=15.5
| |||||||||||||||||||||
| 304 | 188 | my $offset = 0; | |||||||||||||||||||
| 305 | 188 | ▶ | while ($offset < length($bytes)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 306 | 188 | my $written = syswrite($fh, $bytes, length($bytes) - $offset, $offset); | |||||||||||||||||||
| 307 | 188 | ▶ | if (defined $written) { $offset += $written; next } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 308 | 0 | ▶ | next if $!{EINTR}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 309 | 0 | die "producer syswrite: $!"; | |||||||||||||||||||
| 310 | } | ||||||||||||||||||||
| 311 | } | ||||||||||||||||||||
| 312 | |||||||||||||||||||||
| 313 | 324 | ▶ | sub now () { clock_gettime(CLOCK_MONOTONIC) } | ||||||||||||||||||
|
Subroutine
now: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 314 | |||||||||||||||||||||
| 315 | 8 | ▶ | sub percentile ($values, $fraction) { | ||||||||||||||||||
|
Subroutine
percentile: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 316 | 8 | my $index = int($fraction * (@$values - 1) + 0.5); | |||||||||||||||||||
| 317 | 8 | return $values->[$index]; | |||||||||||||||||||
| 318 | } | ||||||||||||||||||||
| 319 | |||||||||||||||||||||
| 320 | 12 | ▶ | sub median (@values) { | ||||||||||||||||||
|
Subroutine
median: called CC=2 SCAR=7.6
| |||||||||||||||||||||
| 321 | 0 | @values = sort { $a <=> $b } @values; | |||||||||||||||||||
| 322 | 12 | my $middle = int(@values / 2); | |||||||||||||||||||
| 323 | 12 | ▶ | return @values % 2 | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 324 | ? $values[$middle] | ||||||||||||||||||||
| 325 | : ($values[$middle - 1] + $values[$middle]) / 2; | ||||||||||||||||||||
| 326 | } | ||||||||||||||||||||
| 327 | |||||||||||||||||||||
| 328 | 5 | ▶ | sub rotate_cases ($cases, $offset) { | ||||||||||||||||||
|
Subroutine
rotate_cases: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 329 | 5 | ▶ | return @$cases if !@$cases; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 330 | 5 | $offset %= @$cases; | |||||||||||||||||||
| 331 | 5 | return (@$cases[$offset .. $#$cases], @$cases[0 .. $offset - 1]); | |||||||||||||||||||
| 332 | } | ||||||||||||||||||||
| 333 | |||||||||||||||||||||
| 334 | 0 | ▶ | sub usage ($exit) { | ||||||||||||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 335 | 0 | print <<'USAGE'; | |||||||||||||||||||
| 336 | Usage: run-callback-batching-fairness.pl [options] | ||||||||||||||||||||
| 337 | --duration=SECONDS producer duration per case (default: 0.5) | ||||||||||||||||||||
| 338 | --ping-interval-us=N latency probe interval (default: 2000) | ||||||||||||||||||||
| 339 | --bytes=N hot payload bytes (default: 64) | ||||||||||||||||||||
| 340 | --read-size=N native syscall read size (default: 4096) | ||||||||||||||||||||
| 341 | --batch-sizes=LIST zero or frame limits (default: 0,4,16,64,256) | ||||||||||||||||||||
| 342 | --transports=LIST unix,tcp (default: unix,tcp) | ||||||||||||||||||||
| 343 | --warmup=N untimed case sweeps (default: 1) | ||||||||||||||||||||
| 344 | --repeats=N measured sweeps (default: 5) | ||||||||||||||||||||
| 345 | --json=PATH write machine-readable report | ||||||||||||||||||||
| 346 | --help show this help | ||||||||||||||||||||
| 347 | USAGE | ||||||||||||||||||||
| 348 | 0 | exit $exit; | |||||||||||||||||||
| 349 | } | ||||||||||||||||||||