« blib/lib/Linux/Event/Framer/U32BE.pm
↑ Summary
bench/run-first-class-callback-construction-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 Getopt::Long qw(GetOptions); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Scalar::Util qw(refaddr); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Linux::Event; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Linux::Event::Loop; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Linux::Event::Kernel::Process; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | my $processes = 1_000; | |||||||||||||||||||||||||||
| 16 | 1 | my @concurrency = (1, 8, 32); | |||||||||||||||||||||||||||
| 17 | 1 | my $repeats = 5; | |||||||||||||||||||||||||||
| 18 | 1 | my $program = '/bin/true'; | |||||||||||||||||||||||||||
| 19 | 1 | my $json_path; | |||||||||||||||||||||||||||
| 20 | my $help; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | GetOptions( | ||||||||||||||||||||||||||||
| 23 | 'processes=i' => \$processes, | ||||||||||||||||||||||||||||
| 24 | 1 | ▶ | 'concurrency=s' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | @concurrency = map { 0 + $_ } split /,/, $_[1]; | |||||||||||||||||||||||||||
| 26 | }, | ||||||||||||||||||||||||||||
| 27 | 1 | ▶ | 'repeats=i' => \$repeats, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 'program=s' => \$program, | ||||||||||||||||||||||||||||
| 29 | 'json=s' => \$json_path, | ||||||||||||||||||||||||||||
| 30 | 'help' => \$help, | ||||||||||||||||||||||||||||
| 31 | ) or usage(1); | ||||||||||||||||||||||||||||
| 32 | 1 | ▶ | usage(0) if $help; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | die "processes must be positive\n" if $processes < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | die "concurrency values must be positive\n" if grep { $_ < 1 } @concurrency; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | die "repeats must be positive\n" if $repeats < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | die "program must be executable\n" if !-x $program; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | { | ||||||||||||||||||||||||||||
| 39 | package BenchProcess; | ||||||||||||||||||||||||||||
| 40 | 1 | ▶ | use parent 'Linux::Event::Kernel::Process'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 12 | ▶ | sub on_exit ($process) { | ||||||||||||||||||||||||||
|
Subroutine
on_exit: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 43 | 12 | my $run = $process->data; | |||||||||||||||||||||||||||
| 44 | 12 | ▶ | die "benchmark child did not exit successfully\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $process->exit_code or $process->exit_code != 0
MC/DC0%0/2
not defined $process->exit_code or $process->exit_code != 0
not defined $process->exit_code
$process->exit_code != 0
Truth table33%1/3
not(A) or B
A= not defined $process->exit_codeB= $process->exit_code != 0
| |||||||||||||||||||||||||||||
| 45 | if !defined($process->exit_code) || $process->exit_code != 0; | ||||||||||||||||||||||||||||
| 46 | 12 | delete $run->{active}{ Scalar::Util::refaddr($process) }; | |||||||||||||||||||||||||||
| 47 | 12 | $run->{completed}++; | |||||||||||||||||||||||||||
| 48 | 12 | ▶ | if ($run->{completed} == $run->{processes}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 2 | $run->{finished} = main::now(); | |||||||||||||||||||||||||||
| 50 | 2 | $run->{loop}->stop; | |||||||||||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||||||||||
| 52 | 10 | main::launch_available($run); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub on_error ($process, $error) { die "$error\n" } | ||||||||||||||||||||||||||
|
Subroutine
on_error: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | sub now () { clock_gettime(CLOCK_MONOTONIC) } | ||||||||||||||||||||||||||
|
Subroutine
now: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 4 | ▶ | sub median (@value) { | ||||||||||||||||||||||||||
|
Subroutine
median: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 62 | 0 | @value = sort { $a <=> $b } @value; | |||||||||||||||||||||||||||
| 63 | 4 | my $middle = int(@value / 2); | |||||||||||||||||||||||||||
| 64 | 4 | ▶ | return @value % 2 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | ? $value[$middle] | ||||||||||||||||||||||||||||
| 66 | : ($value[$middle - 1] + $value[$middle]) / 2; | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 12 | ▶ | sub launch_available ($run) { | ||||||||||||||||||||||||||
|
Subroutine
launch_available: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 70 | 12 | ▶ | while ($run->{launched} < $run->{processes} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$run->{"launched"} < $run->{"processes"} and keys %{$$run{"active"};} < $run->{"concurrency"}
MC/DC100%2/2
$run->{"launched"} < $run->{"processes"} and keys %{$$run{"active"};} < $run->{"concurrency"}
$run->{"launched"} < $run->{"processes"}
keys %{$$run{"active"};} < $run->{"concurrency"}
Truth table100%3/3
A and B
A= $run->{"launched"} < $run->{"processes"}B= keys %{$$run{"active"};} < $run->{"concurrency"}
| |||||||||||||||||||||||||||||
| 71 | 21 | && keys(%{ $run->{active} }) < $run->{concurrency}) { | |||||||||||||||||||||||||||
| 72 | my $process = BenchProcess->spawn( | ||||||||||||||||||||||||||||
| 73 | 12 | command => [$run->{program}], # required | |||||||||||||||||||||||||||
| 74 | stdin => 'null', # optional | ||||||||||||||||||||||||||||
| 75 | stdout => 'null', # optional | ||||||||||||||||||||||||||||
| 76 | stderr => 'null', # optional | ||||||||||||||||||||||||||||
| 77 | data => $run, # optional | ||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||
| 79 | 12 | $run->{active}{ refaddr($process) } = $process; | |||||||||||||||||||||||||||
| 80 | 12 | $run->{launched}++; | |||||||||||||||||||||||||||
| 81 | 12 | $run->{loop}->add($process); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 12 | return; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | my @raw; | |||||||||||||||||||||||||||
| 87 | 1 | for my $concurrency (@concurrency) { | |||||||||||||||||||||||||||
| 88 | 2 | for my $repeat (1 .. $repeats) { | |||||||||||||||||||||||||||
| 89 | 2 | my $loop = Linux::Event::Loop->new; | |||||||||||||||||||||||||||
| 90 | 2 | my $run = { | |||||||||||||||||||||||||||
| 91 | loop => $loop, | ||||||||||||||||||||||||||||
| 92 | program => $program, | ||||||||||||||||||||||||||||
| 93 | processes => $processes, | ||||||||||||||||||||||||||||
| 94 | concurrency => $concurrency, | ||||||||||||||||||||||||||||
| 95 | launched => 0, | ||||||||||||||||||||||||||||
| 96 | completed => 0, | ||||||||||||||||||||||||||||
| 97 | active => {}, | ||||||||||||||||||||||||||||
| 98 | }; | ||||||||||||||||||||||||||||
| 99 | 2 | my ($user_start, $system_start) = (times)[0, 1]; | |||||||||||||||||||||||||||
| 100 | 2 | $run->{started} = now(); | |||||||||||||||||||||||||||
| 101 | 2 | launch_available($run); | |||||||||||||||||||||||||||
| 102 | 2 | $loop->run; | |||||||||||||||||||||||||||
| 103 | 2 | my ($user_end, $system_end) = (times)[0, 1]; | |||||||||||||||||||||||||||
| 104 | 2 | my $elapsed = $run->{finished} - $run->{started}; | |||||||||||||||||||||||||||
| 105 | 2 | my $cpu = ($user_end - $user_start) + ($system_end - $system_start); | |||||||||||||||||||||||||||
| 106 | die "Process benchmark completed $run->{completed} of $processes children\n" | ||||||||||||||||||||||||||||
| 107 | 2 | ▶ | if $run->{completed} != $processes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 2 | push @raw, { | |||||||||||||||||||||||||||
| 109 | concurrency => $concurrency, | ||||||||||||||||||||||||||||
| 110 | repeat => $repeat, | ||||||||||||||||||||||||||||
| 111 | processes => $processes, | ||||||||||||||||||||||||||||
| 112 | elapsed_seconds => 0 + $elapsed, | ||||||||||||||||||||||||||||
| 113 | processes_per_second => $processes / $elapsed, | ||||||||||||||||||||||||||||
| 114 | parent_cpu_us_per_process => $cpu * 1_000_000 / $processes, | ||||||||||||||||||||||||||||
| 115 | }; | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | my @summary; | |||||||||||||||||||||||||||
| 120 | 1 | for my $concurrency (@concurrency) { | |||||||||||||||||||||||||||
| 121 | 2 | my @row = grep { $_->{concurrency} == $concurrency } @raw; | |||||||||||||||||||||||||||
| 122 | push @summary, { | ||||||||||||||||||||||||||||
| 123 | concurrency => $concurrency, | ||||||||||||||||||||||||||||
| 124 | median_processes_per_second => median( | ||||||||||||||||||||||||||||
| 125 | 2 | map { $_->{processes_per_second} } @row, | |||||||||||||||||||||||||||
| 126 | ), | ||||||||||||||||||||||||||||
| 127 | median_parent_cpu_us_per_process => median( | ||||||||||||||||||||||||||||
| 128 | 2 | map { $_->{parent_cpu_us_per_process} } @row, | |||||||||||||||||||||||||||
| 129 | ), | ||||||||||||||||||||||||||||
| 130 | }; | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 1 | printf "Process pidfd spawn benchmark: processes=%d repeats=%d program=%s\n", | |||||||||||||||||||||||||||
| 134 | $processes, $repeats, $program; | ||||||||||||||||||||||||||||
| 135 | 1 | printf "%11s %18s %22s\n", | |||||||||||||||||||||||||||
| 136 | 'concurrency', 'processes/s', 'parent cpu us/process'; | ||||||||||||||||||||||||||||
| 137 | 1 | for my $row (@summary) { | |||||||||||||||||||||||||||
| 138 | printf "%11d %18.1f %22.3f\n", | ||||||||||||||||||||||||||||
| 139 | $row->{concurrency}, | ||||||||||||||||||||||||||||
| 140 | $row->{median_processes_per_second}, | ||||||||||||||||||||||||||||
| 141 | 2 | $row->{median_parent_cpu_us_per_process}; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 1 | ▶ | if (defined $json_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 1 | my $report = { | |||||||||||||||||||||||||||
| 146 | benchmark => 'linux-event-process-microbench', | ||||||||||||||||||||||||||||
| 147 | benchmark_contract_version => 1, | ||||||||||||||||||||||||||||
| 148 | linux_event_version => $Linux::Event::VERSION, | ||||||||||||||||||||||||||||
| 149 | workload => 'posix_spawnp plus pidfd exit notification', | ||||||||||||||||||||||||||||
| 150 | configuration => { | ||||||||||||||||||||||||||||
| 151 | processes => $processes, | ||||||||||||||||||||||||||||
| 152 | concurrency => \@concurrency, | ||||||||||||||||||||||||||||
| 153 | repeats => $repeats, | ||||||||||||||||||||||||||||
| 154 | program => $program, | ||||||||||||||||||||||||||||
| 155 | }, | ||||||||||||||||||||||||||||
| 156 | raw => \@raw, | ||||||||||||||||||||||||||||
| 157 | summary => \@summary, | ||||||||||||||||||||||||||||
| 158 | }; | ||||||||||||||||||||||||||||
| 159 | 1 | ▶ | open my $json, '>', $json_path or die "open $json_path: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 1 | print {$json} JSON::PP->new->canonical->pretty->encode($report); | |||||||||||||||||||||||||||
| 161 | 1 | ▶ | close $json or die "close $json_path: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | sub usage ($exit) { | ||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 165 | 0 | print <<'USAGE'; | |||||||||||||||||||||||||||
| 166 | Usage: run-process-microbench.pl [options] | ||||||||||||||||||||||||||||
| 167 | --processes=N children per repeat (default: 1000) | ||||||||||||||||||||||||||||
| 168 | --concurrency=LIST maximum live children (default: 1,8,32) | ||||||||||||||||||||||||||||
| 169 | --repeats=N measured repeats (default: 5) | ||||||||||||||||||||||||||||
| 170 | --program=PATH no-argument executable (default: /bin/true) | ||||||||||||||||||||||||||||
| 171 | --json=PATH write a machine-readable report | ||||||||||||||||||||||||||||
| 172 | --help show this help | ||||||||||||||||||||||||||||
| 173 | USAGE | ||||||||||||||||||||||||||||
| 174 | 0 | exit $exit; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||