Acme/CPANModulesUtil/Bencher.pm

filter: Statementstmt 29.2% 19 / 65 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 23.0% 24 / 104 SCAR 55.1
CC
21
Cov
20%
gen_bencher_scenario
462.0
BEGIN
1.0
BEGIN
1.0
CRAP 246.8
↑ Summary Bencher/Util/CPANModules.pm »
1 package Acme::CPANModulesUtil::Bencher;
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  
7 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 1 our $DATE = '2022-11-30'; # DATE
11 1 our $DIST = 'Acme-CPANModulesUtil-Bencher'; # DIST
12 1 our $VERSION = '0.005'; # VERSION
13  
14 1 our %SPEC;
15  
16 1 our @EXPORT_OK = qw(gen_bencher_scenario);
17  
18 $SPEC{gen_bencher_scenario} = {
19 1 v => 1.1,
20 summary => 'Generate/extract Bencher scenario from information in an Acme::CPANModules::* list',
21 description => <<'_',
22  
23 An <pm:Acme::CPANModules>::* module can contain benchmark information, for
24 example in <pm:Acme::CPANModules::TextTable>, each entry has the following
25 property:
26  
27 entries => [
28 ...
29 {
30 module => 'Text::ANSITable',
31 ...
32 bench_code => sub {
33 my ($table) = @_;
34 my $t = Text::ANSITable->new(
35 use_utf8 => 0,
36 use_box_chars => 0,
37 use_color => 0,
38 columns => $table->[0],
39 border_style => 'Default::single_ascii',
40 );
41 $t->add_row($table->[$_]) for 1..@$table-1;
42 $t->draw;
43 },
44  
45 # per-function participant
46 functions => {
47 'func1' => {
48 bench_code_template => 'Text::ANSITable::func1([])',
49 },
50 ...
51 },
52  
53 The list also contains information about the benchmark datasets:
54  
55 bench_datasets => [
56 {name=>'tiny (1x1)' , argv => [_make_table( 1, 1)],},
57 {name=>'small (3x5)' , argv => [_make_table( 3, 5)],},
58 {name=>'wide (30x5)' , argv => [_make_table(30, 5)],},
59 {name=>'long (3x300)' , argv => [_make_table( 3, 300)],},
60 {name=>'large (30x300)', argv => [_make_table(30, 300)],},
61 ],
62  
63 This routine extract those information and return a <pm:Bencher> scenario
64 structure.
65  
66 _
67 args => {
68 cpanmodule => {
69 summary => 'Name of Acme::CPANModules::* module, without the prefix',
70 schema => 'perl::modname*',
71 req => 1,
72 pos => 0,
73 'x.completion' => ['perl_modname' => {ns_prefix=>'Acme::CPANModules'}],
74 },
75 },
76 };
77 sub gen_bencher_scenario {
78 0 ▶ my %args = @_;
Subroutine gen_bencher_scenario: not called CC=21 SCAR=61.4
Pod documented
79  
80 0 my $list;
81 my $mod;
82  
83 0 ▶ if ($args{_list}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'_list'}) { } 0 0 0
84 0 $list = $args{_list};
85 } else {
86 0 ▶ $mod = $args{cpanmodule} or return [400, "Please specify cpanmodule"];
Branch: 1 branch
BranchTrueFalseTotal
$mod = $args{"cpanmodule"} or return [400, "Please specify cpanmodule"] 0 0 0
87 0 ▶ $mod = "Acme::CPANModules::$mod" unless $mod =~ /\AAcme::CPANModules::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $mod =~ /\AAcme::CPANModules::/ 0 0 0
88 0 (my $mod_pm = "$mod.pm") =~ s!::!/!g;
89 0 require $mod_pm;
90  
91 0 $list = ${"$mod\::LIST"};
92 }
93  
94 my $scenario = {
95 summary => $list->{summary},
96 0 participants => [],
97 };
98  
99 0 ▶ $scenario->{description} = "This scenario is generated from ".
Branch: 1 branch
BranchTrueFalseTotal
$mod ? : 0 0 0
100 ($mod ? "<pm:$mod>" : "an <pm:Acme::CPANModules> list").".";
101  
102 0 for (qw/datasets/) {
103 0 ▶ if ($list->{"bench_$_"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$list{"bench_$_"}) 0 0 0
104 0 $scenario->{$_} = $list->{"bench_$_"};
105 }
106 }
107  
108 0 for my $e (@{ $list->{entries} }) {
109 0 my @per_function_participants;
110  
111 # we currently don't handle entries with 'modules'
112 0 ▶ next unless $e->{module};
Branch: 1 branch
BranchTrueFalseTotal
unless $e->{"module"} 0 0 0
113  
114 # per-function participant
115 0 ▶ if ($e->{functions}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"functions"}) 0 0 0
116 0 for my $fname (sort keys %{ $e->{functions} }) {
117 0 my $fspec = $e->{functions}{$fname};
118 my $p = {
119 module => $e->{module},
120 0 function => $fname,
121 };
122 0 my $has_bench_code;
123 0 for (qw/code code_template fcall_template/) {
124 0 ▶ if (defined $fspec->{"bench_$_"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$fspec{"bench_$_"}) 0 0 0
125 0 $p->{$_} = $fspec->{"bench_$_"};
126 0 $has_bench_code++;
127 }
128 }
129 0 ▶ next unless $has_bench_code;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_bench_code 0 0 0
130 0 push @per_function_participants, $p;
131 }
132 }
133  
134 my $p = {
135 module => $e->{module},
136 0 };
137 0 my $has_bench_code;
138 0 for (qw/code code_template fcall_template/) {
139 0 ▶ if ($e->{"bench_$_"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$e{"bench_$_"}) 0 0 0
140 0 $has_bench_code++;
141 0 $p->{$_} = $e->{"bench_$_"};
142 }
143 }
144 0 for (qw/include_by_default/) {
145 0 ▶ if (exists $e->{"bench_$_"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$e{"bench_$_"}) 0 0 0
146 0 $p->{$_} = $e->{"bench_$_"};
147 }
148 }
149 0 ▶ if ($has_bench_code || (!@per_function_participants && !$scenario->{datasets})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_bench_code or not @per_function_participants || $scenario->{"datasets"}) 0 0 0
Condition0%0/6
$has_bench_code or not @per_function_participants || $scenario->{"datasets"}
l!l&&r!l&&!r
000
@per_function_participants || $scenario->{'datasets'}
l!l&&r!l&&!r
000
MC/DC0%0/3
$has_bench_code or not @per_function_participants || $scenario->{"datasets"}
$has_bench_code @per_function_participants $scenario->{'datasets'}
Truth table0%0/4
A or not(B || C)
A= $has_bench_codeB= @per_function_participantsC= $scenario->{'datasets'}
ABCresult
1XX1
0001
01X0
0010
150 0 push @{ $scenario->{participants} }, $p;
151 }
152 0 push @{ $scenario->{participants} }, @per_function_participants;
153 }
154  
155 0 [200, "OK", $scenario];
156 }
157  
158 1 1;
159 # ABSTRACT: Generate/extract Bencher scenario from information in an Acme::CPANModules::* list
160  
161 __END__
↑ Summary Bencher/Util/CPANModules.pm »