bench/run-stream-transition-bench.pl

filter: Statementstmt 89.3% 143 / 160 Branchbran 45.0% 18 / 40 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 81.2% 26 / 32 Podpod n/a totaltotal 77.6% 188 / 242 CC 15 SCAR 15.1
CC
15
Cov
79%
git_commit
21.7
run_case
6.0
usage
6.0
CRAP 4.5
« blib/lib/Linux/Event/Address.pm ↑ Summary blib/lib/Linux/Event/IO/Pipe.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 File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use FindBin qw($Bin);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use POSIX qw(strftime uname);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Socket qw(AF_UNIX SOCK_STREAM PF_UNSPEC);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Time::HiRes qw(time clock_gettime CLOCK_PROCESS_CPUTIME_ID);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Linux::Event::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Linux::Event::IO::Sock::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Linux::Event::IO::Sock::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 {
20 package Linux::Event::Bench::Transition::RawA;
21 1 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 0 ▶ sub on_data ($stream, $bytes) { return }
Subroutine on_data: not called CC=1 SCAR=6.9
23 }
24  
25 {
26 1 package Linux::Event::Bench::Transition::RawB;
27 1 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 0 ▶ sub on_data ($stream, $bytes) { return }
Subroutine on_data: not called CC=1 SCAR=6.9
29 }
30  
31 {
32 1 package Linux::Event::Bench::Transition::Delimiter;
33 1 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶ use Linux::Event::Framer 'Delimiter', "\n";
Subroutine BEGIN: called CC=1 SCAR=0.0
35 0 ▶ sub on_message ($stream, $message) { return }
Subroutine on_message: not called CC=1 SCAR=6.9
36 }
37  
38 {
39 1 package Linux::Event::Bench::Transition::Fixed;
40 1 ▶ use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶ use Linux::Event::Framer 'Fixed', size => 64;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 0 ▶ sub on_message ($stream, $message) { return }
Subroutine on_message: not called CC=1 SCAR=6.9
43 }
44  
45 1 my %case_classes = (
46 'raw-raw' => [
47 'Linux::Event::Bench::Transition::RawA',
48 'Linux::Event::Bench::Transition::RawB',
49 ],
50 'framed-framed' => [
51 'Linux::Event::Bench::Transition::Delimiter',
52 'Linux::Event::Bench::Transition::Fixed',
53 ],
54 'raw-framed' => [
55 'Linux::Event::Bench::Transition::RawA',
56 'Linux::Event::Bench::Transition::Delimiter',
57 ],
58 );
59  
60 1 my $contract_version = 1;
61 1 my $iterations = 1_000_000;
62 1 my $pool_size = 256;
63 1 my $warmup = 10_000;
64 1 my $repeats = 7;
65 1 my @cases = qw(raw-raw framed-framed raw-framed);
66 1 my $json_path;
67 my $help;
68  
69 GetOptions(
70 'iterations=i' => \$iterations,
71 'pool=i' => \$pool_size,
72 'warmup=i' => \$warmup,
73 'repeats=i' => \$repeats,
74 0 ▶ 'cases=s' => sub { @cases = split /,/, $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 1 ▶ 'json=s' => \$json_path,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("iterations=i", \$iterations, "pool=i", \$pool_size, "warmup=i", \$warmup, "repeats=i", \$repeats, "cases=s", sub { @cases = split(/,/u, $_[1], 0); } , "json=s", \$json_path, "help", \$help) or usage(2) 1 0 1
76 'help' => \$help,
77 ) or usage(2);
78  
79 1 ▶usage(0) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 1 1
80 1 ▶die "iterations must be > 0\n" if $iterations <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $iterations <= 0 0 1 1
81 1 ▶die "pool must be > 0\n" if $pool_size <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pool_size <= 0 0 1 1
82 1 ▶die "warmup must be >= 0\n" if $warmup < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $warmup < 0 0 1 1
83 1 ▶die "repeats must be > 0\n" if $repeats <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $repeats <= 0 0 1 1
84 1 ▶die "at least one case is required\n" if !@cases;
Branch: 1 branch
BranchTrueFalseTotal
unless @cases 0 1 1
85 1 die "unknown case: $_\n" for grep { !exists $case_classes{$_} } @cases;
86  
87 1 say 'Linux::Event Stream protocol-transition benchmark';
88 1 say "version=$Linux::Event::IO::Sock::Stream::VERSION perl=$^V pid=$$";
89 1 say "contract=$contract_version cases=" . join(',', @cases);
90  
91 1 my @records;
92 1 for my $repeat (1 .. $repeats) {
93 1 for my $case (rotated_cases($repeat)) {
94 3 my $row = run_case($case);
95 3 $row->{repeat} = $repeat;
96 3 push @records, $row;
97 printf "%s repeat=%d %.1f transitions/s cpu=%.3f us/transition\n",
98 $case, $repeat, $row->{transitions_per_second},
99 3 $row->{cpu_us_per_transition};
100 }
101 }
102  
103 1 my @summary;
104 1 say "\nMedian protocol-transition summary";
105 1 printf "%-18s %20s %20s\n",
106 'case', 'transitions/s', 'cpu us/transition';
107 1 for my $case (@cases) {
108 3 my @set = grep { $_->{case} eq $case } @records;
109 my $row = {
110 benchmark_contract_version => $contract_version,
111 case => $case,
112 3 transitions_per_second => median(map { $_->{transitions_per_second} } @set),
113 3 cpu_us_per_transition => median(map { $_->{cpu_us_per_transition} } @set),
114 };
115 3 push @summary, $row;
116 printf "%-18s %20.1f %20.3f\n", $case,
117 3 $row->{transitions_per_second}, $row->{cpu_us_per_transition};
118 }
119  
120 1 ▶if (defined $json_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json_path) 1 0 1
121 1 my $report = {
122 benchmark => 'linux-event-stream-transition',
123 benchmark_contract_version => $contract_version,
124 generated_at => strftime('%Y-%m-%dT%H:%M:%SZ', gmtime),
125 environment => environment_info(),
126 configuration => {
127 iterations => $iterations,
128 pool => $pool_size,
129 warmup => $warmup,
130 repeats => $repeats,
131 cases => \@cases,
132 },
133 records => \@records,
134 summary => \@summary,
135 notes => [
136 'Every timed operation changes one live Stream to the other class.',
137 'The fd, watcher, XSState, output queue, lifecycle, and data are retained.',
138 'No input is supplied, so results isolate descriptor and read-buffer changes.',
139 'raw-raw replaces raw scratch storage; framed-framed needs no raw scratch storage.',
140 'raw-framed alternates raw scratch allocation and release.',
141 ],
142 };
143  
144 1 my $dir = dirname($json_path);
145 1 ▶ make_path($dir) if $dir ne '.' && !-d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir ne "." and not -d $dir 0 1 1
Condition33%1/3
$dir ne "." and not -d $dir
!ll&&!rl&&r
010
MC/DC0%0/2
$dir ne "." and not -d $dir
$dir ne "." not -d $dir
Truth table33%1/3
A and not(B)
A= $dir ne "."B= not -d $dir
ABresult
0X0
100
111
146 1 ▶ open my $out, '>', $json_path or die "open $json_path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", $json_path or die "open ${json_path}: $!\n" 1 0 1
147 1 print {$out} JSON::PP->new->canonical->pretty->encode($report);
148 1 ▶ close $out or die "close $json_path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $out or die "close ${json_path}: $!\n" 1 0 1
149 1 say "\nWrote $json_path";
150 }
151  
152 3 ▶sub run_case ($case) {
Subroutine run_case: called CC=6 SCAR=18.0
153 3 my ($class_a, $class_b) = @{ $case_classes{$case} };
154 3 my $loop = Linux::Event::Loop->new;
155 3 my (@streams, @peers);
156  
157 3 for my $i (0 .. $pool_size - 1) {
158 12 ▶ socketpair(my $stream_fh, my $peer_fh,
Branch: 1 branch
BranchTrueFalseTotal
socketpair my $stream_fh, my $peer_fh, 1, 1, 0 or die "socketpair ${i}: $!" 12 0 12
159 AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die "socketpair $i: $!";
160 12 push @streams, $class_a->new(loop => $loop, fh => $stream_fh);
161 12 push @peers, $peer_fh;
162 }
163  
164 3 transition_many(\@streams, $class_a, $class_b, $warmup);
165  
166 3 my $wall_start = time;
167 3 my $cpu_start = clock_gettime(CLOCK_PROCESS_CPUTIME_ID);
168 3 transition_many(\@streams, $class_a, $class_b, $iterations);
169 3 my $cpu_seconds = clock_gettime(CLOCK_PROCESS_CPUTIME_ID) - $cpu_start;
170 3 my $elapsed_seconds = time - $wall_start;
171  
172 3 for my $stream (@streams) {
173 12 my $fh = $stream->detach;
174 12 close $fh;
175 }
176 3 close $_ for @peers;
177  
178 3 ▶ die "timer produced a non-positive transition interval\n"
Branch: 1 branch
BranchTrueFalseTotal
if $elapsed_seconds <= 0 0 3 3
179 if $elapsed_seconds <= 0;
180 return {
181 3 benchmark_contract_version => $contract_version,
182 case => $case,
183 transitions => $iterations,
184 elapsed_seconds => $elapsed_seconds,
185 cpu_seconds => $cpu_seconds,
186 transitions_per_second => $iterations / $elapsed_seconds,
187 cpu_us_per_transition => ($cpu_seconds * 1_000_000) / $iterations,
188 };
189 }
190  
191 6 ▶sub transition_many ($streams, $class_a, $class_b, $count) {
Subroutine transition_many: called CC=3 SCAR=11.0
192 6 for my $i (0 .. $count - 1) {
193 108 my $stream = $streams->[$i % @$streams];
194 108 ▶ my $target = ref($stream) eq $class_a ? $class_b : $class_a;
Branch: 1 branch
BranchTrueFalseTotal
ref $stream eq $class_a ? : 54 54 108
195 108 $stream->transition_to($target);
196 }
197 }
198  
199 1 ▶sub rotated_cases ($repeat) {
Subroutine rotated_cases: called CC=1 SCAR=0.0
200 1 my $shift = ($repeat - 1) % @cases;
201 1 return (@cases[$shift .. $#cases], @cases[0 .. $shift - 1]);
202 }
203  
204 6 ▶sub median (@values) {
Subroutine median: called CC=3 SCAR=12.7
205 6 ▶ die "median requires at least one value\n" if !@values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 6 6
206 0 @values = sort { $a <=> $b } @values;
207 6 my $middle = int(@values / 2);
208 6 ▶ return $values[$middle] if @values % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @values % 2 6 0 6
209 0 return ($values[$middle - 1] + $values[$middle]) / 2;
210 }
211  
212 1 ▶sub environment_info () {
Subroutine environment_info: called CC=1 SCAR=0.0
213 1 my ($sysname, $nodename, $release, $version, $machine) = uname();
214 return {
215 1 linux_event_version => $Linux::Event::IO::Sock::Stream::VERSION,
216 perl_version => "$^V",
217 perl_executable => $^X,
218 os => $^O,
219 uname => {
220 sysname => $sysname,
221 release => $release,
222 version => $version,
223 machine => $machine,
224 },
225 git_commit => git_commit(),
226 };
227 }
228  
229 1 ▶sub git_commit () {
Subroutine git_commit: called CC=5 SCAR=30.8
230 1 ▶ return undef if !-e "$Bin/../.git";
Branch: 1 branch
BranchTrueFalseTotal
unless -e "$main::Bin/../.git" 1 0 1
231 0 ▶ open my $git, '-|', 'git', '-C', "$Bin/..", 'rev-parse', 'HEAD'
Branch: 1 branch
BranchTrueFalseTotal
open my $git, "-|", "git", "-C", "$main::Bin/..", "rev-parse", "HEAD" or return undef 0 0 0
232 or return undef;
233 0 my $commit = <$git>;
234 0 my $ok = close $git;
235 0 ▶ return undef if !$ok || !defined $commit;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok and defined $commit 0 0 0
Condition0%0/3
$ok and defined $commit
!ll&&!rl&&r
000
MC/DC0%0/2
$ok and defined $commit
$ok defined $commit
Truth table0%0/3
A and B
A= $okB= defined $commit
ABresult
0X0
100
111
236 0 chomp $commit;
237 0 return $commit;
238 }
239  
240 0 ▶sub usage ($status) {
Subroutine usage: not called CC=2 SCAR=17.9
241 0 ▶ my $fh = $status ? *STDERR : *STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 0 0 0
242 0 print {$fh} <<'USAGE';
243 Usage: perl bench/run-stream-transition-bench.pl [options]
244  
245 --iterations=N transitions per case/repeat (default 1000000)
246 --pool=N live Streams cycled by the benchmark (default 256)
247 --warmup=N untimed transitions per case (default 10000)
248 --repeats=N repeats per case (default 7)
249 --cases=A,B raw-raw,framed-framed,raw-framed
250 --json=PATH write raw records, summaries, and environment as JSON
251 --help show this help
252 USAGE
253 0 exit $status;
254 }
« blib/lib/Linux/Event/Address.pm ↑ Summary blib/lib/Linux/Event/IO/Pipe.pm »