bench/run-stream-transition-bench.pl

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