blib/lib/App/BencherUtils.pm

filter: Statementstmt 19.7% 53 / 268 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 46.1% 12 / 26 Podpod 100% 8 / 8 totaltotal 15.5% 73 / 469 CC 80 SCAR 61.5
CC
80
Cov
13%
list_bencher_results
1260.0
cleanup_old_bencher_results
156.0
list_bencher_scenario_modules
132.0
CRAP 466.5
↑ Summary
1 package App::BencherUtils;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict 'subs', 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Data::Clean::ForJSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Function::Fallback::CoreOrPP qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Perinci::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Perinci::Sub::Util qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
16 1 our $DATE = '2022-08-24'; # DATE
17 1 our $DIST = 'App-BencherUtils'; # DIST
18 1 our $VERSION = '0.245'; # VERSION
19  
20 1 our %SPEC;
21  
22 1 $SPEC{':package'} = {
23 v => 1.1,
24 summary => 'Utilities related to bencher',
25 };
26  
27 1 my %args_common = (
28 result_dir => {
29 summary => 'Directory to store results files in',
30 schema => 'str*',
31 req => 1,
32 },
33 );
34  
35 1 my %args_common_query = (
36 query => {
37 schema => ['array*', of=>'str*'],
38 pos => 0,
39 greedy => 1,
40 },
41 detail => {
42 schema => 'bool',
43 cmdline_aliases => {l=>{}},
44 },
45 );
46  
47 0 ▶sub _clean {
Subroutine _clean: not called CC=1 SCAR=6.9
48 0 state $cleanser = Data::Clean::ForJSON->get_cleanser;
49 0 $cleanser->clone_and_clean($_[0]);
50 }
51  
52 0 ▶sub _json {
Subroutine _json: not called CC=1 SCAR=6.9
53 0 state $json = do {
54 0 require JSON::MaybeXS;
55 0 my $json = JSON::MaybeXS->new;
56 0 $json->convert_blessed(1);
57 0 $json->allow_nonref(1);
58 0 $json->canonical(1);
59 };
60 0 $json;
61 }
62  
63 0 ▶sub _encode_json {
Subroutine _encode_json: not called CC=1 SCAR=0.4
64 1 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
66 0 ▶ local *version::TO_JSON = sub { "$_[0]" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 _json->encode($_[0]);
68 }
69  
70 0 ▶sub _complete_scenario_module {
Subroutine _complete_scenario_module: not called CC=1 SCAR=6.9
71 0 require Complete::Module;
72 0 my %args = @_;
73 Complete::Module::complete_module(
74 0 word=>$args{word}, ns_prefix=>'Bencher::Scenario');
75 }
76  
77 1 my $re_filename = qr/\A
78 (\w+(?:-(?:\w+))*)
79 (\.module_startup)?
80 \.(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d)-(\d\d)-(\d\d)
81 \.json
82 \z/x;
83  
84 0 ▶sub _complete_scenario_in_result_dir {
Subroutine _complete_scenario_in_result_dir: not called CC=7 SCAR=40.3
85 0 require Complete::Util;
86  
87 0 my %args = @_;
88 0 my $word = $args{word};
89 0 my $cmdline = $args{cmdline};
90 0 my $r = $args{r};
91  
92 0 ▶ return unless $cmdline;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmdline 0 0 0
93  
94 0 $r->{read_config} = 1;
95  
96 0 my $res = $cmdline->parse_argv($r);
97 0 ▶ return unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
98  
99 # combine from command-line and from config/env
100 0 my $final_args = { %{$res->[2]}, %{$args{args}} };
101 #log_trace("final args=%s", $final_args);
102  
103 0 ▶ return [] unless $final_args->{result_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $final_args->{"result_dir"} 0 0 0
104  
105 0 my %scenarios;
106  
107 0 ▶ opendir my($dh), $final_args->{result_dir} or return;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $final_args->{"result_dir"} or return 0 0 0
108 0 for my $filename (readdir $dh) {
109 0 ▶ $filename =~ $re_filename or next;
Branch: 1 branch
BranchTrueFalseTotal
$filename =~ /$re_filename/ or next 0 0 0
110 0 my $sc = $1; $sc =~ s/-/::/g;
111 0 $scenarios{$sc}++;
112 }
113  
114 0 Complete::Util::complete_hash_key(hash=>\%scenarios, word=>$word);
115 }
116  
117 $SPEC{list_bencher_results} = {
118 1 v => 1.1,
119 summary => "List results in results directory",
120 args => {
121 %args_common,
122 %args_common_query,
123  
124 include_scenarios => {
125 'x.name.is_plural' => 1,
126 schema => ['array*', of=>'str*'],
127 tags => ['category:filtering'],
128 element_completion => \&_complete_scenario_in_result_dir,
129 },
130 exclude_scenarios => {
131 'x.name.is_plural' => 1,
132 schema => ['array*', of=>'str*'],
133 tags => ['category:filtering'],
134 element_completion => \&_complete_scenario_in_result_dir,
135 },
136 module_startup => {
137 schema => 'bool*',
138 tags => ['category:filtering'],
139 },
140 latest => {
141 'summary.alt.bool.yes' => 'Only list the latest result for every scenario+CPU',
142 'summary.alt.bool.no' => 'Do not list the latest result for every scenario+CPU',
143 schema => ['bool*'],
144 tags => ['category:filtering'],
145 },
146  
147 fmt => {
148 summary => 'Display each result with bencher-fmt',
149 schema => 'bool*',
150 tags => ['category:display'],
151 },
152 },
153 examples => [
154 {
155 summary => 'List all results',
156 src => 'list-bencher-results',
157 src_plang => 'bash',
158 test => 0,
159 'x.doc.show_result' => 0,
160 },
161 {
162 summary => 'List all results, show detail information',
163 src => 'list-bencher-results -l',
164 src_plang => 'bash',
165 test => 0,
166 'x.doc.show_result' => 0,
167 },
168 {
169 summary => 'List matching results only',
170 src => 'list-bencher-results --exclude-scenario Perl::Startup Startup',
171 src_plang => 'bash',
172 test => 0,
173 'x.doc.show_result' => 0,
174 },
175 {
176 summary => 'List matching results, format all using bencher-fmt',
177 src => 'list-bencher-results --exclude-scenario Perl::Startup Startup --fmt',
178 src_plang => 'bash',
179 test => 0,
180 'x.doc.show_result' => 0,
181 },
182 {
183 summary => 'List latest result for each scenario+CPU',
184 src => 'list-bencher-results QUERY --latest',
185 src_plang => 'bash',
186 test => 0,
187 'x.doc.show_result' => 0,
188 },
189 {
190 summary => 'Delete old results',
191 src => 'cd $RESULT_DIR; list-bencher-results --no-latest | xargs rm',
192 src_plang => 'bash',
193 test => 0,
194 'x.doc.show_result' => 0,
195 },
196 ],
197 };
198 0 ▶sub list_bencher_results {
Subroutine list_bencher_results: not called CC=35 SCAR=71.4
Pod documented
199 0 require File::Slurper;
200  
201 0 my %args = @_;
202  
203 0 my $dir = $args{result_dir};
204  
205 0 ▶ opendir my($dh), $dir or return [500, "Can't read result_dir `$dir`: $!"];
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or return [500, "Can't read result_dir `$dir`: $!"] 0 0 0
206  
207 # normalize
208 ## no critic (ControlStructures::ProhibitMutatingListFunctions)
209 my $include_scenarios = [
210 0 ▶ map {s!/!::!g; $_} @{ $args{include_scenarios} // [] }
Condition0%0/2
$args{"include_scenarios"} // []
l!l
00
MC/DC0%0/1
$args{"include_scenarios"} // []
$args{"include_scenarios"}
Truth table0%0/2
A
A= $args{"include_scenarios"}
Aresult
11
00
211 ];
212 my $exclude_scenarios = [
213 0 ▶ map {s!/!::!g; $_} @{ $args{exclude_scenarios} // [] }
Condition0%0/2
$args{"exclude_scenarios"} // []
l!l
00
MC/DC0%0/1
$args{"exclude_scenarios"} // []
$args{"exclude_scenarios"}
Truth table0%0/2
A
A= $args{"exclude_scenarios"}
Aresult
11
00
214 ];
215 ## use critic
216  
217 0 my %latest; # key = module+(module_startup)+cpu, value = latest row
218 my @rows;
219 FILE:
220 0 for my $filename (sort readdir $dh) {
221 0 ▶ next unless $filename =~ $re_filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename =~ /$re_filename/ 0 0 0
222 0 ▶ my $row = {
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 0 0 0
223 filename => $filename,
224 scenario => $1,
225 module_startup => $2 ? 1:0,
226 time => "$3-$4-$5T$6:$7:$8",
227 };
228 0 $row->{scenario} =~ s/-/::/g;
229 0 ▶ if (@$include_scenarios) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$include_scenarios) 0 0 0
230 0 ▶ next FILE unless grep {$row->{scenario} eq $_} @$include_scenarios;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$row->{"scenario"} eq $_;} @$include_scenarios 0 0 0
231 }
232 0 ▶ if (@$exclude_scenarios) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$exclude_scenarios) 0 0 0
233 0 ▶ next FILE if grep {$row->{scenario} eq $_} @$exclude_scenarios;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$row->{"scenario"} eq $_;} @$exclude_scenarios 0 0 0
234 }
235  
236 0 my $benchres = _json->decode(File::Slurper::read_text("$dir/$filename"));
237 0 $row->{res} = $benchres;
238 0 $row->{cpu} = $benchres->[3]{'func.cpu_info'}[0]{name};
239  
240 0 ▶ $row->{module_startup} = 1 if $benchres->[3]{'func.module_startup'};
Branch: 1 branch
BranchTrueFalseTotal
if $benchres->[3]{"func.module_startup"} 0 0 0
241  
242 0 ▶ if (defined $args{module_startup}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"module_startup"}) 0 0 0
243 0 ▶ next FILE if $row->{module_startup} xor $args{module_startup};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"module_startup"} xor $args{"module_startup"} 0 0 0
Condition0%0/4
$row->{"module_startup"} xor $args{"module_startup"}
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$row->{"module_startup"} xor $args{"module_startup"}
$row->{"module_startup"} $args{"module_startup"}
Truth table0%0/4
A xor B
A= $row->{"module_startup"}B= $args{"module_startup"}
ABresult
110
101
011
000
244 }
245  
246 my $key = sprintf(
247 "%s.%s.%s",
248 $row->{scenario},
249 $row->{module_startup} ? 0:1,
250 0 ▶ $row->{cpu} // '', # when bencher is run --no-return-meta, there's no cpu information
Branch: 1 branch
BranchTrueFalseTotal
$row->{'module_startup'} ? : 0 0 0
Condition0%0/2
$row->{"cpu"} // ""
l!l
00
MC/DC0%0/1
$row->{"cpu"} // ""
$row->{"cpu"}
Truth table0%0/2
A
A= $row->{"cpu"}
Aresult
11
00
251 );
252  
253 0 ▶ if ($args{query} && @{ $args{query} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"query"} and @{$args{"query"};}) 0 0 0
Condition0%0/3
$args{"query"} and @{$args{"query"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"query"} and @{$args{"query"};}
$args{"query"} @{$args{"query"};}
Truth table0%0/3
A and B
A= $args{"query"}B= @{$args{"query"};}
ABresult
0X0
100
111
254 0 my $matches = 1;
255 QUERY_WORD:
256 0 for my $q (@{ $args{query} }) {
257 0 my $lq = lc($q);
258 0 ▶ if (index(lc($row->{cpu} // ''), $lq) == -1 &&
Branch: 1 branch
BranchTrueFalseTotal
if (index(lc($row->{"cpu"} // ""), $lq) == -1 and index(lc $row->{"filename"}, $lq) == -1 and index(lc $row->{"scenario"}, $lq) == -1) 0 0 0
Condition0%0/8
index(lc($row->{"cpu"} // ""), $lq) == -1 and index(lc $row->{"filename"}, $lq) == -1 and index(lc $row->{"scenario"}, $lq) == -1
!ll&&!rl&&r
000
index(lc($row->{"cpu"} // ""), $lq) == -1 and index(lc $row->{"filename"}, $lq) == -1
!ll&&!rl&&r
000
$row->{"cpu"} // ""
l!l
00
MC/DC0%0/3
index(lc($row->{"cpu"} // ""), $lq) == -1 and index(lc $row->{"filename"}, $lq) == -1 and index(lc $row->{"scenario"}, $lq) == -1
index(lc($row->{"cpu"} // ""), $lq) == -1 index(lc $row->{"filename"}, $lq) == -1 index(lc $row->{"scenario"}, $lq) == -1
MC/DC0%0/1
$row->{"cpu"} // ""
$row->{"cpu"}
Truth table0%0/4
A and B and C
A= index(lc($row->{"cpu"} // ""), $lq) == -1B= index(lc $row->{"filename"}, $lq) == -1C= index(lc $row->{"scenario"}, $lq) == -1
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/2
A
A= $row->{"cpu"}
Aresult
11
00
259 index(lc($row->{filename}), $lq) == -1 &&
260 index(lc($row->{scenario}), $lq) == -1
261 ) {
262 0 $matches = 0;
263 0 last;
264 }
265 }
266 0 ▶ next unless $matches;
Branch: 1 branch
BranchTrueFalseTotal
unless $matches 0 0 0
267 }
268  
269 0 ▶ if (!$latest{$key} || $latest{$key}{time} lt $row->{time}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $latest{$key} or $latest{$key}{"time"} lt $row->{"time"}) 0 0 0
Condition0%0/3
not $latest{$key} or $latest{$key}{"time"} lt $row->{"time"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $latest{$key} or $latest{$key}{"time"} lt $row->{"time"}
not $latest{$key} $latest{$key}{"time"} lt $row->{"time"}
Truth table0%0/3
not(A) or B
A= not $latest{$key}B= $latest{$key}{"time"} lt $row->{"time"}
ABresult
1X1
011
000
270 0 $latest{$key} = $row;
271 }
272 0 $row->{_key} = $key;
273  
274 0 push @rows, $row;
275 }
276  
277 # we do the 'latest' filter here after we get all the rows
278 0 ▶ if (defined $args{latest}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"latest"}) 0 0 0
279 0 my @rows0 = @rows;
280 @rows = grep {
281 0 my $latest_time = $latest{ $_->{_key} }{time};
282 $args{latest} ?
283 $_->{time} eq $latest_time :
284 0 ▶ $_->{time} ne $latest_time;
Branch: 1 branch
BranchTrueFalseTotal
$args{'latest'} ? : 0 0 0
285 } @rows;
286 }
287  
288 0 for (@rows) { delete $_->{_key} }
289  
290 0 my $resmeta = {};
291 0 ▶ if ($args{fmt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'fmt'}) { } 0 0 0
292 0 require Bencher::Backend;
293  
294 0 $resmeta->{'cmdline.skip_format'} = 1;
295 0 my @content;
296 0 for my $row (@rows) {
297 0 ▶ push @content, "$row->{filename} (cpu: ", ($row->{cpu}//''), "):\n";
Condition0%0/2
$row->{"cpu"} // ""
l!l
00
MC/DC0%0/1
$row->{"cpu"} // ""
$row->{"cpu"}
Truth table0%0/2
A
A= $row->{"cpu"}
Aresult
11
00
298 0 push @content, Bencher::Backend::format_result($row->{res});
299 0 push @content, "\n";
300 }
301 0 return [200, "OK", join("", @content), $resmeta];
302 } else {
303 0 delete($_->{res}) for @rows;
304 0 ▶ if ($args{detail}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'detail'}) { } 0 0 0
305 0 $resmeta->{'table.fields'} = [qw(scenario module_startup time cpu filename)];
306 } else {
307 0 @rows = map {$_->{filename}} @rows;
308 }
309 0 return [200, "OK", \@rows, $resmeta];
310 }
311 }
312  
313 $SPEC{cleanup_old_bencher_results} = {
314 1 v => 1.1,
315 summary => 'Delete old results',
316 description => <<'_',
317  
318 By default it will only keep 1 latest result for each scenario for the same CPU
319 and the same module versions.
320  
321 You can use `--dry-run` first to see which files would be deleted without
322 actually deleting them.
323  
324 _
325 args => {
326 %args_common,
327 %args_common_query,
328 num_keep => {
329 summary => 'Number of old results to keep',
330 schema => ['int*', min=>0],
331 default => 0,
332 },
333 },
334 features => {
335 dry_run => 1,
336 },
337 };
338 0 ▶sub cleanup_old_bencher_results {
Subroutine cleanup_old_bencher_results: not called CC=12 SCAR=50.5
Pod documented
339 0 require File::Slurper;
340  
341 0 my %args = @_;
342 0 ▶ my $num_keep = $args{num_keep} // 0;
Condition0%0/2
$args{"num_keep"} // 0
l!l
00
MC/DC0%0/1
$args{"num_keep"} // 0
$args{"num_keep"}
Truth table0%0/2
A
A= $args{"num_keep"}
Aresult
11
00
343  
344 my $res = list_bencher_results(
345 detail => 1,
346 maybe result_dir => $args{result_dir},
347 maybe query => $args{query},
348 0 );
349 0 ▶ return $res if $res->[0] != 200;
Branch: 1 branch
BranchTrueFalseTotal
if $res->[0] != 200 0 0 0
350  
351 0 my @scenarios;
352 0 for my $scenario (@{ $res->[2] }) {
353 0 $scenario->{result} = _json->decode(File::Slurper::read_text(
354 "$args{result_dir}/$scenario->{filename}"));
355 0 push @scenarios, $scenario;
356 }
357  
358 0 my %filenames; # key = scenario|cpu|module_startup(0|1)|json(module_versions), value = [filename, ...]
359 0 for my $scenario (@scenarios) {
360 my $key = join(
361 "|",
362 $scenario->{scenario},
363 $scenario->{cpu} // '',
364 $scenario->{module_startup} ? 1:0,
365 0 ▶ _encode_json($scenario->{result}[3]{'func.module_versions'}),
Branch: 1 branch
BranchTrueFalseTotal
$scenario->{'module_startup'} ? : 0 0 0
Condition0%0/2
$scenario->{"cpu"} // ""
l!l
00
MC/DC0%0/1
$scenario->{"cpu"} // ""
$scenario->{"cpu"}
Truth table0%0/2
A
A= $scenario->{"cpu"}
Aresult
11
00
366 );
367 #log_trace("key = %s", $key);
368 0 push @{$filenames{$key}}, $scenario->{filename};
369 }
370  
371 0 $res = envresmulti();
372 0 for my $key (sort keys %filenames) {
373 0 my $val = $filenames{$key};
374 0 ▶ next unless @$val > $num_keep+1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$val > $num_keep + 1 0 0 0
375 0 $val = [sort @$val];
376 0 for my $f (@{$val}[0..$#{$val}-$num_keep-1]) {
377 0 ▶ if ($args{-dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'-dry_run'}) { } 0 0 0
378 0 log_warn("[DRY-RUN] Deleting %s ...", $f);
379 0 $res->add_result(200, "OK (dry-run)", {item_id=>$f});
380 } else {
381 0 log_warn("Deleting %s ...", $f);
382 0 ▶ if (unlink "$args{result_dir}/$f") {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink "$args{'result_dir'}/$f") { } 0 0 0
383 0 $res->add_result(200, "OK", {item_id=>$f});
384 } else {
385 0 log_warn("Can't unlink '%s': %s", $f, $!);
386 0 $res->add_result(500, "Can't unlink: $!", {item_id=>$f});
387 }
388 }
389 }
390 }
391 0 return $res->as_struct;
392 }
393  
394 $SPEC{list_bencher_scenario_modules} = {
395 1 v => 1.1,
396 summary => 'List Bencher scenario modules',
397 args => {
398 query => {
399 #schema => ['array*', of=>'str*'],
400 schema => 'str*',
401 pos => 0,
402 },
403 detail => {
404 schema => 'bool*',
405 cmdline_aliases => {l=>{}},
406 },
407 },
408 };
409 0 ▶sub list_bencher_scenario_modules {
Subroutine list_bencher_scenario_modules: not called CC=11 SCAR=48.8
Pod documented
410 0 require Module::List::Tiny;
411  
412 0 my %args = @_;
413 0 ▶ my $q = lc($args{query} // '');
Condition0%0/2
$args{"query"} // ""
l!l
00
MC/DC0%0/1
$args{"query"} // ""
$args{"query"}
Truth table0%0/2
A
A= $args{"query"}
Aresult
11
00
414 0 my $detail = $args{detail};
415  
416 0 my $res = Module::List::Tiny::list_modules(
417 "Bencher::Scenario::", {list_modules=>1, recurse=>1});
418 0 my @res0 = sort keys %$res;
419  
420 0 my @res;
421 0 my $resmeta = {};
422 0 for my $mod (@res0) {
423 0 (my $scenario_name = $mod) =~ s/^Bencher::Scenario:://;
424 0 ▶ next if length($q) && index(lc($scenario_name), $q) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $q and index(lc $scenario_name, $q) == -1 0 0 0
Condition0%0/3
length $q and index(lc $scenario_name, $q) == -1
!ll&&!rl&&r
000
MC/DC0%0/2
length $q and index(lc $scenario_name, $q) == -1
length $q index(lc $scenario_name, $q) == -1
Truth table0%0/3
A and B
A= length $qB= index(lc $scenario_name, $q) == -1
ABresult
0X0
100
111
425 0 ▶ if ($detail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($detail) { } 0 0 0
426 0 (my $mod_pm = "$mod.pm") =~ s!::!/!g;
427 0 require $mod_pm;
428 0 my $scenario = ${"$mod\::scenario"};
429 0 my %participant_types;
430 0 for my $p (@{ $scenario->{participants} }) {
431 0 for my $t (qw/code code_template fcall_template
432 cmdline cmdline_template
433 perl_cmdline perl_cmdline_template
434 /) {
435 0 ▶ if ($p->{$t}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{$t}) 0 0 0
436 0 $participant_types{$t}++;
437 0 last;
438 }
439 }
440 }
441 push @res, {
442 name => $scenario_name,
443 summary => $scenario->{summary},
444 0 ▶ num_participants => scalar(@{$scenario->{participants}}),
Branch: 1 branch
BranchTrueFalseTotal
$scenario->{'datasets'} ? : 0 0 0
445 num_datasets => $scenario->{datasets} ?
446 0 scalar(@{$scenario->{datasets}}) : '-',
447 participant_types => join(", ", sort keys %participant_types),
448 };
449 } else {
450 0 push @res, $scenario_name;
451 }
452 }
453 0 ▶ $resmeta = {'table.fields' => [qw/name summary num_participants num_datasets
Branch: 1 branch
BranchTrueFalseTotal
if $detail 0 0 0
454 participant_types
455 /]}
456 if $detail;
457  
458 0 [200, "OK", \@res, $resmeta];
459 }
460  
461 $SPEC{format_bencher_result} = {
462 1 v => 1.1,
463 summary => 'Format bencher raw/JSON result',
464 args => {
465 json => {
466 summary => 'JSON data',
467 schema => 'str*', # XXX filename
468 req => 1,
469 pos => 0,
470 cmdline_src => 'stdin_or_file',
471 },
472 as => {
473 schema => ['str*', in=>[qw/bencher_table benchmark_pm_table/]],
474 default => 'bencher_table',
475 },
476 },
477 };
478 0 ▶sub format_bencher_result {
Subroutine format_bencher_result: not called CC=3 SCAR=24.8
Pod documented
479 0 require Bencher::Backend;
480  
481 0 my %args = @_;
482 0 my $res = _json->decode($args{json});
483 [200, "OK", Bencher::Backend::format_result($res, undef, {
484 ($args{as} eq 'bencher_table' ? (render_as_text_table=>1) : ()),
485 0 ▶ ($args{as} eq 'benchmark_pm_table' ? (render_as_benchmark_pm=>1) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$args{'as'} eq 'bencher_table' ? : 0 0 0
$args{'as'} eq 'benchmark_pm_table' ? : 0 0 0
486 }),
487 {'cmdline.skip_format'=>1}];
488 }
489  
490 $SPEC{chart_bencher_result} = {
491 1 v => 1.1,
492 summary => 'Generate chart of bencher result and display it',
493 args => {
494 json => {
495 summary => 'JSON data',
496 schema => 'str*', # XXX filename
497 req => 1,
498 pos => 0,
499 cmdline_src => 'stdin_or_file',
500 },
501 },
502 };
503 0 ▶sub chart_bencher_result {
Subroutine chart_bencher_result: not called CC=3 SCAR=24.8
Pod documented
504 0 require Bencher::Backend;
505 0 require Browser::Open;
506 0 require File::Temp;
507  
508 0 my %args = @_;
509  
510 0 my $envres = _json->decode($args{json});
511  
512 0 my ($temp_fh, $temp_fname) = File::Temp::tempfile();
513  
514 0 $temp_fname .= ".png";
515  
516 0 my $chart_res = Bencher::Backend::chart_result(
517 envres => $envres, output_file => $temp_fname, overwrite=>1);
518  
519 0 ▶ return $chart_res if $chart_res->[0] != 200;
Branch: 1 branch
BranchTrueFalseTotal
if $chart_res->[0] != 200 0 0 0
520  
521 0 my $view_res = Browser::Open::open_browser("file:$temp_fname");
522  
523 0 ▶ $view_res ? [500, "Failed"] : [200, "OK"];
Branch: 1 branch
BranchTrueFalseTotal
$view_res ? : 0 0 0
524 }
525  
526 $SPEC{bencher_module_startup_overhead} = {
527 1 v => 1.1,
528 summary => 'Accept a list of module names and '.
529 'perform startup overhead benchmark',
530 description => <<'_',
531  
532 % bencher-module-startup-overhead Mod1 Mod2 Mod3
533  
534 is basically a shortcut for creating a scenario like this:
535  
536 {
537 module_startup => 1,
538 participants => [
539 {module=>"Mod1"},
540 {module=>"Mod2"},
541 {module=>"Mod3"},
542 ],
543 }
544  
545 and running that scenario with `bencher`.
546  
547 To specify import arguments, you can use:
548  
549 % bencher-module-startup-overhead Mod1 Mod2=arg1,arg2
550  
551 which will translate to this Bencher scenario:
552  
553 {
554 module_startup => 1,
555 participants => [
556 {module=>"Mod1"},
557 {module=>"Mod2", import_args=>'arg1,arg2'},
558 ],
559 }
560  
561  
562 _
563 args => {
564 modules => {
565 'x.name.is_plural' => 1,
566 'x.name.singular' => 'module',
567 schema => ['array*', of=>'perl::modargs*'],
568 req => 1,
569 pos => 0,
570 greedy => 1,
571 cmdline_src => 'stdin_or_args',
572 },
573 with_process_size => {
574 schema => 'bool*',
575 },
576 },
577  
578 };
579 0 ▶sub bencher_module_startup_overhead {
Subroutine bencher_module_startup_overhead: not called CC=9 SCAR=45.0
Pod documented
580 0 my %args = @_;
581  
582 0 my $mods = $args{modules};
583  
584 my $with_process_size = $args{with_process_size} //
585 0 ▶ $^O =~ /linux/ ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'with_process_size'} // $^O =~ /linux/ ? : 0 0 0
Condition0%0/3
$args{"with_process_size"} // $^O =~ /linux/
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"with_process_size"} // $^O =~ /linux/
$args{"with_process_size"} $^O =~ /linux/
Truth table0%0/3
A // B
A= $args{"with_process_size"}B= $^O =~ /linux/
ABresult
1X1
011
000
586  
587 0 my $scenario = {
588 module_startup => 1,
589 participants => [],
590 with_process_size => $with_process_size,
591 };
592 0 for my $mod (@$mods) {
593 0 my $import_args;
594 0 ▶ if ($mod =~ s/=(.*)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ s/=(.*)//) 0 0 0
595 0 $import_args = $1;
596 }
597 0 push @{$scenario->{participants}}, {
598 module => $mod,
599 (import_args => $import_args) x !!defined($import_args),
600 };
601 }
602  
603 0 require Bencher::Backend;
604 0 my $res = Bencher::Backend::bencher(
605 action => 'bench',
606 scenario => $scenario,
607 );
608 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
609  
610 0 my $r = $args{-cmdline_r};
611 0 ▶ return $res if !$r || $r->{format} && $r->{format} !~ /text/;
Branch: 1 branch
BranchTrueFalseTotal
if not $r or $r->{"format"} and not $r->{"format"} =~ /text/ 0 0 0
Condition0%0/6
not $r or $r->{"format"} and not $r->{"format"} =~ /text/
l!l&&r!l&&!r
000
$r->{"format"} and not $r->{"format"} =~ /text/
!ll&&!rl&&r
000
MC/DC0%0/3
not $r or $r->{"format"} and not $r->{"format"} =~ /text/
not $r $r->{"format"} not $r->{"format"} =~ /text/
Truth table0%0/4
not(A) or B and not(C)
A= not $rB= $r->{"format"}C= not $r->{"format"} =~ /text/
ABCresult
1XX1
0111
00X0
0100
612  
613 0 [200, "OK", Bencher::Backend::format_result($res),
614 {'cmdline.skip_format'=>1}];
615 }
616  
617 $SPEC{bencher_code} = {
618 1 v => 1.1,
619 summary => 'Accept a list of codes and '.
620 'perform benchmark',
621 description => <<'_',
622  
623 % bencher-code 'code1' 'code2'
624  
625 is basically a shortcut for creating a scenario like this:
626  
627 {
628 participants => [
629 {code_template=>'code1'},
630 {code_template=>'code2'},
631 ],
632 }
633  
634 and running that scenario with `bencher`.
635  
636 _
637 args => {
638 startup => {
639 summary => 'Use code_startup mode instead of normal benchmark',
640 schema => 'bool*',
641 default => 0,
642 },
643 codes => {
644 'x.name.is_plural' => 1,
645 'x.name.singular' => 'code',
646 schema => ['array*', of=>'str*'],
647 req => 1,
648 pos => 0,
649 greedy => 1,
650 cmdline_src => 'stdin_or_args',
651 },
652 with_process_size => {
653 schema => 'bool*',
654 },
655 precision => {
656 schema => 'float*',
657 },
658 },
659  
660 };
661 0 ▶sub bencher_code {
Subroutine bencher_code: not called CC=6 SCAR=37.4
Pod documented
662 0 my %args = @_;
663  
664 0 my $codes = $args{codes};
665  
666 0 my $scenario = {
667 participants => [],
668 };
669 0 for my $code (@$codes) {
670 0 push @{$scenario->{participants}}, {
671 code_template => $code,
672 };
673 }
674  
675 0 require Bencher::Backend;
676 my $res = Bencher::Backend::bencher(
677 action => 'bench',
678 scenario => $scenario,
679 code_startup => $args{startup},
680 with_process_size => $args{with_process_size},
681 0 (precision => $args{precision}) x !!(defined $args{precision}),
682 );
683 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
684  
685 0 my $r = $args{-cmdline_r};
686 0 ▶ return $res if !$r || $r->{format} && $r->{format} !~ /text/;
Branch: 1 branch
BranchTrueFalseTotal
if not $r or $r->{"format"} and not $r->{"format"} =~ /text/ 0 0 0
Condition0%0/6
not $r or $r->{"format"} and not $r->{"format"} =~ /text/
l!l&&r!l&&!r
000
$r->{"format"} and not $r->{"format"} =~ /text/
!ll&&!rl&&r
000
MC/DC0%0/3
not $r or $r->{"format"} and not $r->{"format"} =~ /text/
not $r $r->{"format"} not $r->{"format"} =~ /text/
Truth table0%0/4
not(A) or B and not(C)
A= not $rB= $r->{"format"}C= not $r->{"format"} =~ /text/
ABCresult
1XX1
0111
00X0
0100
687  
688 0 [200, "OK", Bencher::Backend::format_result($res),
689 {'cmdline.skip_format'=>1}];
690 }
691  
692 $SPEC{bencher_for} = {
693 1 v => 1.1,
694 summary => 'List distributions that benchmarks specified modules',
695 description => <<'_',
696  
697 This utility consults <prog:lcpan> (local indexed CPAN mirror) to check if there
698 are distributions that benchmarks a specified module. This is done by checking
699 the presence of a dependency with the relationship `x_benchmarks`.
700  
701 _
702 args => {
703 modules => {
704 schema => ['array*', of=>'perl::modname*'],
705 req => 1,
706 pos => 0,
707 greedy => 1,
708 },
709 },
710  
711 };
712 0 ▶sub bencher_for {
Subroutine bencher_for: not called CC=2 SCAR=17.9
Pod documented
713 0 require App::lcpan::Call;
714  
715 0 my %args = @_;
716  
717 my $res = App::lcpan::Call::call_lcpan_script(
718 0 argv => ["rdeps", "--phase", "x_benchmarks", @{ $args{modules} }],
719 );
720  
721 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
722  
723 0 return [200, "OK", [map {$_->{dist}} @{ $res->[2] }]];
724 }
725  
726 1 1;
727 # ABSTRACT: Utilities related to bencher
728  
729 __END__
↑ Summary