blib/lib/BenchmarkAnything/Storage/Frontend/Lib.pm

filter: Statementstmt 23.8% 81 / 340 Branchbran 8.2% 15 / 182 Conditioncond 3.7% 1 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 42.1% 16 / 38 Podpod 100% 15 / 15 totaltotal 20.6% 128 / 619 CC 105 SCAR 43.5
CC
105
Cov
18%
_format_flat_outer
206.9
add
192.9
init_workdir
182.0
CRAP 77.3
↑ Summary
1 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package BenchmarkAnything::Storage::Frontend::Lib;
5 # git description: v0.022-4-g0771090
6  
7 3 our $AUTHORITY = 'cpan:SCHWIGON';
8 # ABSTRACT: Basic functions to access a BenchmarkAnything store
9 3 $BenchmarkAnything::Storage::Frontend::Lib::VERSION = '0.023';
10 3 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 sub new
14 4 ▶{
Subroutine new: called CC=3 SCAR=11.2
Pod documented
15 4 my $class = shift;
16 4 my $self = bless { @_ }, $class;
17 4 require BenchmarkAnything::Config;
18 4 ▶ $self->{config} = BenchmarkAnything::Config->new(cfgfile => $self->{cfgfile}) unless $self->{noconfig};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"noconfig"} 4 0 4
19 4 ▶ $self->connect unless $self->{noconnect};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"noconnect"} 4 0 4
20 4 return $self;
21 }
22  
23 sub _format_flat_inner_scalar
24 0 ▶{
Subroutine _format_flat_inner_scalar: not called CC=1 SCAR=0.6
25 0 my ($self, $result, $opt) = @_;
26  
27 3 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 0 return "$result";
30 }
31  
32 sub _format_flat_inner_array
33 0 ▶{
Subroutine _format_flat_inner_array: not called CC=2 SCAR=11.6
34 0 my ($self, $result, $opt) = @_;
35  
36 3 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 return
39 join($opt->{separator},
40 map {
41 # only SCALARS allowed (where reftype returns undef)
42 0 ▶ die "benchmarkanything: unsupported innermost nesting (".reftype($_).") for 'flat' output.\n" if defined reftype($_);
Branch: 1 branch
BranchTrueFalseTotal
if defined builtin::reftype($_) 0 0 0
43 0 "".$_
44 } @$result);
45 }
46  
47 sub _format_flat_inner_hash
48 0 ▶{
Subroutine _format_flat_inner_hash: not called CC=2 SCAR=12.2
49 0 my ($self, $result, $opt) = @_;
50  
51 3 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 return
54 join($opt->{separator},
55 0 map { my $v = $result->{$_};
56 # only SCALARS allowed (where reftype returns undef)
57 0 ▶ die "benchmarkanything: unsupported innermost nesting (".reftype($v).") for 'flat' output.\n" if defined reftype($v);
Branch: 1 branch
BranchTrueFalseTotal
if defined builtin::reftype($v) 0 0 0
58 0 "$_=".$v
59 } keys %$result);
60 }
61  
62 sub _format_flat_outer
63 0 ▶{
Subroutine _format_flat_outer: not called CC=15 SCAR=53.3
64 0 my ($self, $result, $opt) = @_;
65  
66 3 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 0 my $output = "";
69 0 ▶ die "benchmarkanything: can not flatten data structure (undef) - try other output format.\n" unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 0 0
70  
71 0 ▶ my $A = ""; my $B = ""; if ($opt->{fb}) { $A = "["; $B = "]" }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"fb"}) 0 0 0
72 0 my $fi = $opt->{fi};
73  
74 0 ▶ if (!defined reftype $result) { # SCALAR
Branch: 3 branches
BranchTrueFalseTotal
if (not defined builtin::reftype($result)) { } 0 0 0
elsif (builtin::reftype($result) eq 'ARRAY') { } 0 0 0
elsif (builtin::reftype($result) eq 'HASH') { } 0 0 0
75 0 $output .= $result."\n"; # stringify
76 }
77 elsif (reftype $result eq 'ARRAY') {
78 0 ▶ for (my $i=0; $i<@$result; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$result) 0 0 0
79 0 my $entry = $result->[$i];
80 0 ▶ my $prefix = $fi ? "$i:" : "";
Branch: 1 branch
BranchTrueFalseTotal
$fi ? : 0 0 0
81 0 ▶ if (!defined reftype $entry) { # SCALAR
Branch: 3 branches
BranchTrueFalseTotal
if (not defined builtin::reftype($entry)) { } 0 0 0
elsif (builtin::reftype($entry) eq 'ARRAY') { } 0 0 0
elsif (builtin::reftype($entry) eq 'HASH') { } 0 0 0
82 0 $output .= $prefix.$A.$self->_format_flat_inner_scalar($entry, $opt)."$B\n";
83 }
84 elsif (reftype $entry eq 'ARRAY') {
85 0 $output .= $prefix.$A.$self->_format_flat_inner_array($entry, $opt)."$B\n";
86 }
87 elsif (reftype $entry eq 'HASH') {
88 0 $output .= $prefix.$A.$self->_format_flat_inner_hash($entry, $opt)."$B\n";
89 }
90 else {
91 0 die "benchmarkanything: can not flatten data structure (".reftype($entry).").\n";
92 }
93 }
94 }
95 elsif (reftype $result eq 'HASH') {
96 0 my @keys = keys %$result;
97 0 foreach my $key (@keys) {
98 0 my $entry = $result->{$key};
99 0 ▶ if (!defined reftype $entry) { # SCALAR
Branch: 3 branches
BranchTrueFalseTotal
if (not defined builtin::reftype($entry)) { } 0 0 0
elsif (builtin::reftype($entry) eq 'ARRAY') { } 0 0 0
elsif (builtin::reftype($entry) eq 'HASH') { } 0 0 0
100 0 $output .= "$key:".$self->_format_flat_inner_scalar($entry, $opt)."\n";
101 }
102 elsif (reftype $entry eq 'ARRAY') {
103 0 $output .= "$key:".$self->_format_flat_inner_array($entry, $opt)."\n";
104 }
105 elsif (reftype $entry eq 'HASH') {
106 0 $output .= "$key:".$self->_format_flat_inner_hash($entry, $opt)."\n";
107 }
108 else {
109 0 die "benchmarkanything: can not flatten data structure (".reftype($entry).").\n";
110 }
111 }
112 }
113 else {
114 0 die "benchmarkanything: can not flatten data structure (".reftype($result).") - try other output format.\n";
115 }
116  
117 0 return $output;
118 }
119  
120 sub _format_flat
121 0 ▶{
Subroutine _format_flat: not called CC=3 SCAR=24.8
122 0 my ($self, $result, $opt) = @_;
123  
124 # ensure array container
125 # for consistent output in 'getpoint' and 'search'
126 0 ▶ my $resultlist = reftype($result) eq 'ARRAY' ? $result : [$result];
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($result) eq 'ARRAY' ? : 0 0 0
127  
128 0 my $output = "";
129 0 ▶ $opt->{separator} = ";" unless defined $opt->{separator};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"separator"} 0 0 0
130 0 $output .= $self->_format_flat_outer($resultlist, $opt);
131 0 return $output;
132 }
133  
134  
135 sub _output_format
136 0 ▶{
Subroutine _output_format: not called CC=8 SCAR=42.8
137 0 my ($self, $data, $opt) = @_;
138  
139 0 my $output = "";
140 0 ▶ my $outtype = $opt->{outtype} || 'json';
Condition0%0/2
$opt->{'outtype'} || 'json'
l!l
00
MC/DC0%0/1
$opt->{'outtype'} || 'json'
$opt->{'outtype'}
Truth table0%0/2
A
A= $opt->{'outtype'}
Aresult
11
00
141  
142 0 ▶ if ($outtype eq "yaml")
Branch: 6 branches
BranchTrueFalseTotal
if ($outtype eq 'yaml') { } 0 0 0
elsif ($outtype eq 'json') { } 0 0 0
elsif ($outtype eq 'ini') { } 0 0 0
elsif ($outtype eq 'dumper') { } 0 0 0
elsif ($outtype eq 'xml') { } 0 0 0
elsif ($outtype eq 'flat') { } 0 0 0
143 {
144 0 require YAML::Any;
145 0 $output .= YAML::Any::Dump($data);
146 }
147 elsif ($outtype eq "json")
148 {
149 0 eval "use JSON -convert_blessed_universally";
150 0 my $json = JSON->new->allow_nonref->pretty->allow_blessed->convert_blessed;
151 0 $output .= $json->encode($data);
152 }
153 elsif ($outtype eq "ini") {
154 0 require Config::INI::Serializer;
155 0 my $ini = Config::INI::Serializer->new;
156 0 $output .= $ini->serialize($data);
157 }
158 elsif ($outtype eq "dumper")
159 {
160 0 require Data::Dumper;
161 0 $output .= Data::Dumper::Dumper($data);
162 }
163 elsif ($outtype eq "xml")
164 {
165 0 require XML::Simple;
166 0 my $xs = new XML::Simple;
167 0 $output .= $xs->XMLout($data, AttrIndent => 1, KeepRoot => 1);
168 }
169 elsif ($outtype eq "flat") {
170 0 $output .= $self->_format_flat( $data, $opt );
171 }
172 else
173 {
174 0 die "benchmarkanything-storage: unrecognized output format: $outtype.";
175 }
176 0 return $output;
177 }
178  
179  
180 sub connect
181 5 ▶{
Subroutine connect: called CC=9 SCAR=26.7
Pod documented
182 5 my ($self) = @_;
183  
184 5 my $backend = $self->{config}{benchmarkanything}{backend};
185 5 ▶ if ($backend eq 'local')
Branch: 2 branches
BranchTrueFalseTotal
if ($backend eq 'local') { } 5 0 5
elsif ($backend eq 'http') { } 0 0 0
186 {
187 5 require DBI;
188 5 require BenchmarkAnything::Storage::Backend::SQL;
189 3 ▶ no warnings 'once'; # avoid 'Name "DBI::errstr" used only once'
Subroutine BEGIN: called CC=1 SCAR=0.0
190  
191 # connect
192 5 ▶ print "Connect db...\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 5 5
193 5 my $dsn = $self->{config}{benchmarkanything}{storage}{backend}{sql}{dsn};
194 5 my $user = $self->{config}{benchmarkanything}{storage}{backend}{sql}{user};
195 5 my $password = $self->{config}{benchmarkanything}{storage}{backend}{sql}{password};
196 5 ▶ my $dbh = DBI->connect($dsn, $user, $password, {'RaiseError' => 1, 'mysql_auto_reconnect' => 1})
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect($dsn, $user, $password, {"RaiseError", 1, "mysql_auto_reconnect", 1}) or die 'benchmarkanything: can not connect: ' . $DBI::errstr 5 0 5
197 or die "benchmarkanything: can not connect: ".$DBI::errstr;
198  
199 # external search engine
200 5 ▶ my $searchengine = $self->{config}{benchmarkanything}{searchengine} || {};
Condition50%1/2
$self->{'config'}{'benchmarkanything'}{'searchengine'} || {}
l!l
05
MC/DC0%0/1
$self->{'config'}{'benchmarkanything'}{'searchengine'} || {}
$self->{'config'}{'benchmarkanything'}{'searchengine'}
Truth table50%1/2
A
A= $self->{'config'}{'benchmarkanything'}{'searchengine'}
Aresult
11
00
201  
202 # remember
203 5 $self->{dbh} = $dbh;
204 $self->{backend} = BenchmarkAnything::Storage::Backend::SQL->new({dbh => $dbh,
205 dbh_config => $self->{config}{benchmarkanything}{storage}{backend}{sql},
206 debug => $self->{config}{benchmarkanything}{debug},
207 force => $self->{force},
208 verbose => $self->{config}{benchmarkanything}{verbose},
209 5 ▶ (keys %$searchengine ? (searchengine => $searchengine) : ()),
Branch: 1 branch
BranchTrueFalseTotal
keys %$searchengine ? : 0 5 5
210 });
211 }
212 elsif ($backend eq 'http')
213 {
214 0 my $ua = $self->_get_user_agent;
215 0 my $url = $self->_get_base_url."/api/v1/hello";
216 0 ▶ die "benchmarkanything: can't connect to result storage ($url)\n" if (!$ua->get($url)->res->code or $ua->get($url)->res->code != 200);
Branch: 1 branch
BranchTrueFalseTotal
if not $ua->get($url)->res->code or $ua->get($url)->res->code != 200 0 0 0
Condition0%0/3
not $ua->get($url)->res->code or $ua->get($url)->res->code != 200
l!l&&r!l&&!r
000
MC/DC0%0/2
not $ua->get($url)->res->code or $ua->get($url)->res->code != 200
not $ua->get($url)->res->code $ua->get($url)->res->code != 200
Truth table0%0/3
not(A) or B
A= not $ua->get($url)->res->codeB= $ua->get($url)->res->code != 200
ABresult
1X1
011
000
217 }
218  
219 5 return $self;
220 }
221  
222  
223 sub disconnect
224 0 ▶{
Subroutine disconnect: not called CC=4 SCAR=30.0
Pod documented
225 0 my ($self) = @_;
226  
227 0 my $backend = $self->{config}{benchmarkanything}{backend};
228 0 ▶ if ($backend eq 'local')
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq "local") 0 0 0
229 {
230 0 ▶ if ($self->{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dbh"}) 0 0 0
231 0 ▶ $self->{dbh}->commit unless $self->{dbh}{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"}{"AutoCommit"} 0 0 0
232 0 undef $self->{dbh}; # setting dbh to undef does better cleanup than disconnect();
233 }
234 }
235 0 return $self;
236 }
237  
238  
239 sub _are_you_sure
240 1 ▶{
Subroutine _are_you_sure: called CC=5 SCAR=24.2
241 1 my ($self) = @_;
242  
243 # DSN
244 1 my $dsn = $self->{config}{benchmarkanything}{storage}{backend}{sql}{dsn};
245  
246 # option --really
247 1 ▶ if ($self->{really})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"really"}) 1 0 1
248 {
249 1 ▶ if ($self->{really} eq $dsn)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'really'} eq $dsn) { } 1 0 1
250 {
251 1 return 1;
252 }
253 else
254 {
255 0 print STDERR "DSN does not match - asking interactive.\n";
256 }
257 }
258  
259 # ask on stdin
260 0 print "REALLY DROP AND RE-CREATE DATABASE TABLES [$dsn] (y/N): ";
261 0 read STDIN, my $answer, 1;
262 0 ▶ return 1 if $answer && $answer =~ /^y(es)?$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $answer and $answer =~ /^y(es)?$/i 0 0 0
Condition0%0/3
$answer and $answer =~ /^y(es)?$/i
!ll&&!rl&&r
000
MC/DC0%0/2
$answer and $answer =~ /^y(es)?$/i
$answer $answer =~ /^y(es)?$/i
Truth table0%0/3
A and B
A= $answerB= $answer =~ /^y(es)?$/i
ABresult
0X0
100
111
263  
264 # default: NO
265 0 return 0;
266 }
267  
268  
269 sub createdb
270 1 ▶{
Subroutine createdb: called CC=5 SCAR=16.3
Pod documented
271 1 my ($self) = @_;
272  
273 1 ▶ if ($self->_are_you_sure)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_are_you_sure) 1 0 1
274 {
275 3 ▶ no warnings 'once'; # avoid 'Name "DBI::errstr" used only once'
Subroutine BEGIN: called CC=1 SCAR=0.0
276  
277 1 require DBI;
278 1 require File::Slurper;
279 1 require File::ShareDir;
280 3 ▶ use DBIx::MultiStatementDo;
Subroutine BEGIN: called CC=1 SCAR=0.0
281  
282 1 my $batch = DBIx::MultiStatementDo->new(dbh => $self->{dbh});
283  
284 # get schema SQL according to driver
285 1 my $dsn = $self->{config}{benchmarkanything}{storage}{backend}{sql}{dsn};
286 1 ▶ my ($scheme, $driver, $attr_string, $attr_hash, $driver_dsn) = DBI->parse_dsn($dsn)
Branch: 1 branch
BranchTrueFalseTotal
() = "DBI"->parse_dsn($dsn) or die "benchmarkanything: can not parse DBI DSN '${dsn}'" 1 0 1
287 or die "benchmarkanything: can not parse DBI DSN '$dsn'";
288 1 my ($dbname) = $driver_dsn =~ m/database=(\w+)/g;
289 1 my $sql_file = File::ShareDir::dist_file('BenchmarkAnything-Storage-Backend-SQL', "create-schema.$driver");
290 1 my $sql = File::Slurper::read_text($sql_file);
291 1 ▶ $sql =~ s/^use `testrundb`;/use `$dbname`;/m if $dbname; # replace BenchmarkAnything::Storage::Backend::SQL's default
Branch: 1 branch
BranchTrueFalseTotal
if $dbname 0 1 1
292  
293 # execute schema SQL
294 1 my @results = $batch->do($sql);
295 1 ▶ if (not @results)
Branch: 1 branch
BranchTrueFalseTotal
unless (@results) 0 1 1
296 {
297 0 die "benchmarkanything: error while creating BenchmarkAnything DB: ".$batch->dbh->errstr;
298 }
299  
300 }
301  
302 1 return;
303 }
304  
305  
306 sub _default_additional_keys
307 0 ▶{
Subroutine _default_additional_keys: not called CC=2 SCAR=17.9
308 0 my ($self) = @_;
309  
310 0 my $backend = $self->{config}{benchmarkanything}{backend};
311 0 ▶ if ($backend eq 'local')
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq 'local') { } 0 0 0
312 {
313 0 return { $self->{backend}->default_columns };
314 }
315 else
316 {
317 # Hardcoded from BenchmarkAnything::Storage::Backend::SQL::Query::common,
318 # as it is a backend-special and internal thing anyway.
319 return {
320 0 'NAME' => 'b.bench',
321 'UNIT' => 'bu.bench_unit',
322 'VALUE' => 'bv.bench_value',
323 'VALUE_ID' => 'bv.bench_value_id',
324 'CREATED' => 'bv.created_at',
325 };
326 }
327 }
328  
329  
330  
331 sub _get_benchmark_operators
332 0 ▶{
Subroutine _get_benchmark_operators: not called CC=2 SCAR=17.9
333 0 my ($self) = @_;
334  
335 0 my $backend = $self->{config}{benchmarkanything}{backend};
336 0 ▶ if ($backend eq 'local')
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq 'local') { } 0 0 0
337 {
338 0 return [ $self->{backend}->benchmark_operators ];
339 }
340 else
341 {
342 # Hardcoded from BenchmarkAnything::Storage::Backend::SQL::Query::common,
343 # as it is a backend-special and internal thing anyway.
344 0 return [ '=', '!=', 'like', 'not_like', 'is_empty', '<', '>', '<=', '>=' ];
345 }
346 }
347  
348  
349  
350 sub _get_additional_key_id
351 0 ▶{
Subroutine _get_additional_key_id: not called CC=2 SCAR=17.9
352 0 my ($self, $key_name) = @_;
353  
354 0 my $backend = $self->{config}{benchmarkanything}{backend};
355 0 ▶ if ($backend eq 'local')
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq 'local') { } 0 0 0
356 {
357 0 return $self->{backend}->_get_additional_key_id($key_name);
358 }
359 else
360 {
361 0 die "benchmarkanything: no backend '$backend' allowed here, available backends are: 'local'.\n";
362 }
363 }
364  
365  
366  
367 sub init_workdir
368 0 ▶{
Subroutine init_workdir: not called CC=13 SCAR=52.0
Pod documented
369 0 my ($self) = @_;
370  
371 0 require File::Basename;
372 0 require File::ShareDir;
373 0 require File::HomeDir;
374 0 require File::Slurper;
375  
376 0 my $home_ba = File::HomeDir->my_home."/.benchmarkanything";
377 0 my $command = File::Basename::basename($0);
378  
379 0 ▶ if (-d $home_ba)
Branch: 1 branch
BranchTrueFalseTotal
if (-d $home_ba) { } 0 0 0
380 {
381 0 ▶ print "Workdir '$home_ba' already exists - skipping.\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
382 }
383 else
384 {
385 0 require File::Path;
386 0 File::Path::make_path($home_ba);
387 }
388  
389 0 foreach my $basename (qw(client.cfg server.cfg default.cfg README))
390 {
391 0 my $source_file = File::ShareDir::dist_file('BenchmarkAnything-Storage-Frontend-Lib', "config/$basename");
392 0 my $dest_file = "$home_ba/$basename";
393  
394 0 ▶ if (! -e $dest_file)
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $dest_file) { } 0 0 0
395 {
396 0 my $content = File::Slurper::read_text($source_file);
397  
398 # poor man's templating
399 0 $content =~ s{\[%\s*CLIENTCFG\s*%\]}{$home_ba/client.cfg}g;
400 0 $content =~ s{\[%\s*SERVERCFG\s*%\]}{$home_ba/server.cfg}g;
401 0 $content =~ s{\[%\s*LOCALCFG\s*%\]}{$home_ba/default.cfg}g;
402 0 $content =~ s{\[%\s*CFG\s*%\]}{$dest_file}g;
403 0 $content =~ s{\[%\s*HOME\s*%\]}{$home_ba}g;
404  
405 0 ▶ print "Create configfile: $dest_file...\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
406 0 ▶ open my $CFGFILE, ">", $dest_file or die "Could not create $dest_file.\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $CFGFILE, ">", $dest_file or die "Could not create $dest_file.\n" 0 0 0
407 0 print $CFGFILE $content;
408 0 close $CFGFILE;
409 }
410 else
411 {
412 0 ▶ print "Config '$dest_file' already exists - skipping.\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
413 }
414 }
415  
416 0 my $dbfile = "$home_ba/benchmarkanything.sqlite";
417 0 my $we_created_db = 0;
418 0 ▶ if (! -e $dbfile)
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $dbfile) { } 0 0 0
419 {
420 0 ▶ print "Create storage: $dbfile...\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
421 0 __PACKAGE__->new(cfgfile => "$home_ba/default.cfg",
422 really => "dbi:SQLite:$dbfile",
423 )->createdb;
424 0 $we_created_db = 1;
425 }
426 else
427 {
428 0 ▶ print "Storage '$dbfile' already exists - skipping.\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
429 }
430  
431 0 ▶ if ($self->{verbose})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 0 0
432 {
433 0 print "\n";
434 0 print "By default it will use this config: $home_ba/default.cfg\n";
435 0 print "If you want another one, set it in your ~/.bash_profile:\n";
436 0 print " export BENCHMARKANYTHING_CONFIGFILE=$home_ba/client.cfg\n";
437  
438 0 ▶ unless ($we_created_db)
Branch: 1 branch
BranchTrueFalseTotal
if ($we_created_db) { } 0 0 0
439 {
440 0 print "\n";
441 0 print "Initialize a new database (it asks for confirmation) with:\n";
442 0 print " $command createdb\n";
443 0 print "\nReady.\n";
444 }
445 else
446 {
447 0 print "\n";
448 0 print "Create sample values like this:\n";
449 0 print qq( echo '{"BenchmarkAnythingData":[{"NAME":"benchmarkanything.hello.world", "VALUE":17.2}]}' | $command add\n);
450 0 print "\n";
451 0 print "List metric names:\n";
452 0 print qq( $command listnames\n);
453 0 print "\n";
454 0 print "Query sample values:\n";
455 0 print qq( echo '{"select":["NAME","VALUE"],"where":[["=","NAME","benchmarkanything.hello.world"]]}' | $command search\n);
456 0 print "\n";
457 }
458 }
459  
460 0 return;
461 }
462  
463  
464 sub add
465 1 ▶{
Subroutine add: called CC=17 SCAR=52.6
Pod documented
466 1 my ($self, $data) = @_;
467  
468 # --- validate ---
469 1 ▶ if (not $data)
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 1 1
470 {
471 0 die "benchmarkanything: no input data provided.\n";
472 }
473  
474 1 ▶ if (not $self->{skipvalidation}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"skipvalidation"}) 1 0 1
475 1 require BenchmarkAnything::Schema;
476 1 ▶ print "Verify schema...\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 1 1
477 1 ▶ if (not my $result = BenchmarkAnything::Schema::valid_json_schema($data))
Branch: 1 branch
BranchTrueFalseTotal
unless (my $result = BenchmarkAnything::Schema::valid_json_schema($data)) 0 0 0
478 {
479 0 die "benchmarkanything: add: invalid input: ".join("; ", $result->errors)."\n";
480 }
481 }
482  
483 # --- add to storage ---
484  
485 0 my $backend = $self->{config}{benchmarkanything}{backend};
486 0 ▶ if ($backend eq 'local')
Branch: 2 branches
BranchTrueFalseTotal
if ($backend eq 'local') { } 0 0 0
elsif ($backend eq 'http') { } 0 0 0
487 {
488 0 my $success;
489 0 ▶ if ($self->{queuemode})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'queuemode'}) { } 0 0 0
490 {
491 # only queue for later processing
492 0 ▶ print "Enqueue data [backend:local]...\n" if $self->{verbose} or $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} or $self->{"debug"} 0 0 0
Condition0%0/3
$self->{"verbose"} or $self->{"debug"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"verbose"} or $self->{"debug"}
$self->{"verbose"} $self->{"debug"}
Truth table0%0/3
A or B
A= $self->{"verbose"}B= $self->{"debug"}
ABresult
1X1
011
000
493 0 $success = $self->{backend}->enqueue_multi_benchmark($data->{BenchmarkAnythingData});
494 }
495 else
496 {
497 0 ▶ print "Add data [backend:local]...\n" if $self->{verbose} or $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} or $self->{"debug"} 0 0 0
Condition0%0/3
$self->{"verbose"} or $self->{"debug"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"verbose"} or $self->{"debug"}
$self->{"verbose"} $self->{"debug"}
Truth table0%0/3
A or B
A= $self->{"verbose"}B= $self->{"debug"}
ABresult
1X1
011
000
498 # preserve order, otherwise add_multi_benchmark() would reorder to optimize insert
499 0 foreach my $chunk (@{$data->{BenchmarkAnythingData}})
500 {
501 0 ▶ print "." if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
502 0 $success = $self->{backend}->add_multi_benchmark([$chunk]);
503 }
504 }
505 0 ▶ if (not $success)
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
506 {
507 0 die "benchmarkanything: error while adding data: ".$@;
508 }
509 0 ▶ print "Done.\n" if $self->{verbose} or $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} or $self->{"debug"} 0 0 0
Condition0%0/3
$self->{"verbose"} or $self->{"debug"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"verbose"} or $self->{"debug"}
$self->{"verbose"} $self->{"debug"}
Truth table0%0/3
A or B
A= $self->{"verbose"}B= $self->{"debug"}
ABresult
1X1
011
000
510 }
511 elsif ($backend eq 'http')
512 {
513 0 require BenchmarkAnything::Reporter;
514 $self->{config} = BenchmarkAnything::Reporter->new(config => $self->{config},
515 verbose => $self->{verbose},
516 debug => $self->{debug},
517 0 );
518 }
519 else
520 {
521 0 die "benchmarkanything: no backend '$backend', available backends are: 'http', 'local'.\n";
522 }
523  
524 0 return $self;
525 }
526  
527 sub _get_user_agent
528 0 ▶{
Subroutine _get_user_agent: not called CC=1 SCAR=6.9
529 0 require Mojo::UserAgent;
530 0 return Mojo::UserAgent->new;
531 }
532  
533 sub _get_base_url
534 0 ▶{
Subroutine _get_base_url: not called CC=1 SCAR=6.9
535 0 shift->{config}{benchmarkanything}{backends}{http}{base_url};
536 }
537  
538  
539 sub search
540 0 ▶{
Subroutine search: not called CC=8 SCAR=42.8
Pod documented
541 0 my ($self, $query, $value_id) = @_;
542  
543 # --- validate ---
544 0 ▶ if (not $query and not $value_id)
Branch: 1 branch
BranchTrueFalseTotal
unless ($query or $value_id) 0 0 0
Condition0%0/3
$query or $value_id
l!l&&r!l&&!r
000
MC/DC0%0/2
$query or $value_id
$query $value_id
Truth table0%0/3
A or B
A= $queryB= $value_id
ABresult
1X1
011
000
545 {
546 0 die "benchmarkanything: no query or value_id provided.\n";
547 }
548  
549 0 my $backend = $self->{config}{benchmarkanything}{backend};
550 0 ▶ if ($backend eq 'local')
Branch: 2 branches
BranchTrueFalseTotal
if ($backend eq 'local') { } 0 0 0
elsif ($backend eq 'http') { } 0 0 0
551 {
552 # single values
553 0 ▶ return $self->{backend}->get_single_benchmark_point($value_id) if $value_id;
Branch: 1 branch
BranchTrueFalseTotal
if $value_id 0 0 0
554 0 return $self->{backend}->search_array($query);
555 }
556 elsif ($backend eq 'http')
557 {
558 0 my $ua = $self->_get_user_agent;
559 0 my $url = $self->_get_base_url."/api/v1/search";
560 0 my $res;
561 0 ▶ if ($value_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value_id) { } 0 0 0
562 0 $url .= "/$value_id";
563 0 $res = $ua->get($url)->res;
564 } else {
565 0 $res = $ua->post($url => json => $query)->res;
566 }
567  
568 0 ▶ die "benchmarkanything: ".$res->error->{message}." ($url)\n" if $res->error;
Branch: 1 branch
BranchTrueFalseTotal
if $res->error 0 0 0
569  
570 0 return $res->json;
571 }
572 else
573 {
574 0 die "benchmarkanything: no backend '$backend', available backends are: 'http', 'local'.\n";
575 }
576 }
577  
578  
579 sub listnames
580 0 ▶{
Subroutine listnames: not called CC=5 SCAR=34.0
Pod documented
581 0 my ($self, $pattern) = @_;
582  
583 0 my $backend = $self->{config}{benchmarkanything}{backend};
584 0 ▶ if ($backend eq 'local')
Branch: 2 branches
BranchTrueFalseTotal
if ($backend eq 'local') { } 0 0 0
elsif ($backend eq 'http') { } 0 0 0
585 {
586 0 ▶ return $self->{backend}->list_benchmark_names(defined($pattern) ? ($pattern) : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $pattern ? : 0 0 0
587 }
588 elsif ($backend eq 'http')
589 {
590 0 my $ua = $self->_get_user_agent;
591 0 my $url = $self->_get_base_url."/api/v1/listnames";
592  
593 0 my $res = $ua->get($url)->res;
594 0 ▶ die "benchmarkanything: ".$res->error->{message}." ($url)\n" if $res->error;
Branch: 1 branch
BranchTrueFalseTotal
if $res->error 0 0 0
595  
596 0 my $result = $res->json;
597  
598 # output
599 0 return $result;
600 }
601 else
602 {
603 0 die "benchmarkanything: no backend '$backend', available backends are: 'http', 'local'.\n";
604 }
605 }
606  
607  
608 sub listkeys
609 0 ▶{
Subroutine listkeys: not called CC=5 SCAR=34.0
Pod documented
610 0 my ($self, $pattern) = @_;
611  
612 0 my $backend = $self->{config}{benchmarkanything}{backend};
613 0 ▶ if ($backend eq 'local')
Branch: 2 branches
BranchTrueFalseTotal
if ($backend eq 'local') { } 0 0 0
elsif ($backend eq 'http') { } 0 0 0
614 {
615 0 ▶ return $self->{backend}->list_additional_keys(defined($pattern) ? ($pattern) : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $pattern ? : 0 0 0
616 }
617 elsif ($backend eq 'http')
618 {
619 0 my $ua = $self->_get_user_agent;
620 0 my $url = $self->_get_base_url."/api/v1/listkeys";
621  
622 0 my $res = $ua->get($url)->res;
623 0 ▶ die "benchmarkanything: ".$res->error->{message}." ($url)\n" if $res->error;
Branch: 1 branch
BranchTrueFalseTotal
if $res->error 0 0 0
624  
625 0 my $result = $res->json;
626  
627 # output
628 0 return $result;
629 }
630 else
631 {
632 0 die "benchmarkanything: no backend '$backend', available backends are: 'http', 'local'.\n";
633 }
634 }
635  
636  
637 sub stats
638 0 ▶{
Subroutine stats: not called CC=4 SCAR=30.0
Pod documented
639 0 my ($self) = @_;
640  
641 0 my $backend = $self->{config}{benchmarkanything}{backend};
642 0 ▶ if ($backend eq 'local')
Branch: 2 branches
BranchTrueFalseTotal
if ($backend eq 'local') { } 0 0 0
elsif ($backend eq 'http') { } 0 0 0
643 {
644 0 return $self->{backend}->get_stats;
645 }
646 elsif ($backend eq 'http')
647 {
648 0 my $ua = $self->_get_user_agent;
649 0 my $url = $self->_get_base_url."/api/v1/stats";
650  
651 0 my $res = $ua->get($url)->res;
652 0 ▶ die "benchmarkanything: ".$res->error->{message}." ($url)\n" if $res->error;
Branch: 1 branch
BranchTrueFalseTotal
if $res->error 0 0 0
653  
654 0 my $result = $res->json;
655  
656 # output
657 0 return $result;
658 }
659 else
660 {
661 0 die "benchmarkanything: no backend '$backend', available backends are: 'http', 'local'.\n";
662 }
663 }
664  
665  
666 sub gc
667 0 ▶{
Subroutine gc: not called CC=2 SCAR=17.9
Pod documented
668 0 my ($self) = @_;
669  
670 0 my $backend = $self->{config}{benchmarkanything}{backend};
671 0 ▶ if ($backend eq 'local')
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq "local") 0 0 0
672 {
673 0 $self->{backend}->gc;
674 }
675 }
676  
677  
678 sub process_raw_result_queue
679 0 ▶{
Subroutine process_raw_result_queue: not called CC=6 SCAR=37.4
Pod documented
680 0 my ($self, $count) = @_;
681  
682 0 require LockFile::Simple;
683  
684 0 my $lock;
685 0 my $lockmgr = LockFile::Simple->make(-stale => 1, -autoclean => 1);
686  
687 0 ▶ return unless $lock = $lockmgr->trylock('/tmp/process_raw_result_queue');
Branch: 1 branch
BranchTrueFalseTotal
unless $lock = $lockmgr->trylock("/tmp/process_raw_result_queue") 0 0 0
688  
689 0 ▶ $count ||= 10;
Condition0%0/2
$count ||= 10
l!l
00
MC/DC0%0/1
$count ||= 10
$count
Truth table0%0/2
A
A= $count
Aresult
11
00
690  
691 0 my $backend = $self->{config}{benchmarkanything}{backend};
692 0 ▶ if ($backend eq 'local')
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq 'local') { } 0 0 0
693 {
694 0 my @dequeued_raw_bench_bundle_ids;
695 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
($count < 1 or not @dequeued_raw_bench_bundle_ids) or do { do { @dequeued_raw_bench_bundle_ids = $self->{"backend"}->process_queued_multi_benchmark($count); $count = $count - @dequeued_raw_bench_bundle_ids } } 0 0 0
Condition0%0/3
$count < 1 or not @dequeued_raw_bench_bundle_ids
l!l&&r!l&&!r
000
MC/DC0%0/2
$count < 1 or not @dequeued_raw_bench_bundle_ids
$count < 1 not @dequeued_raw_bench_bundle_ids
Truth table0%0/3
A or not(B)
A= $count < 1B= not @dequeued_raw_bench_bundle_ids
ABresult
1X1
011
000
696 0 @dequeued_raw_bench_bundle_ids = $self->{backend}->process_queued_multi_benchmark($count);
697 #print STDERR "Processed bench_bundles: ".join(",", @dequeued_raw_bench_bundle_ids)."\n" if $self->{verbose};
698 0 $count = $count - @dequeued_raw_bench_bundle_ids;
699 } until ($count < 1 or not @dequeued_raw_bench_bundle_ids);
700 }
701 else
702 {
703 0 $lock->release;
704 0 die "benchmarkanything: only backend 'local' allowed in 'process_raw_result_queue'.\n";
705 }
706  
707 0 $lock->release;
708 0 return;
709 }
710  
711  
712 sub init_search_engine
713 0 ▶{
Subroutine init_search_engine: not called CC=2 SCAR=17.9
Pod documented
714 0 my ($self, $force) = @_;
715  
716 0 my $backend = $self->{config}{benchmarkanything}{backend};
717 0 ▶ if ($backend eq 'local')
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq 'local') { } 0 0 0
718 {
719 0 $self->{backend}->init_search_engine($force);
720 }
721 else
722 {
723 0 die "benchmarkanything: only backend 'local' allowed in 'init_search_engine'.\n";
724 }
725 0 return;
726 }
727  
728  
729 sub sync_search_engine
730 0 ▶{
Subroutine sync_search_engine: not called CC=2 SCAR=17.9
Pod documented
731 0 my ($self, $force, $start, $count) = @_;
732  
733 0 my $backend = $self->{config}{benchmarkanything}{backend};
734 0 ▶ if ($backend eq 'local')
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq 'local') { } 0 0 0
735 {
736 0 $self->{backend}->sync_search_engine($force, $start, $count);
737 }
738 else
739 {
740 0 die "benchmarkanything: only backend 'local' allowed in 'sync_search_engine'.\n";
741 }
742 0 return;
743 }
744  
745  
746  
747 sub getpoint
748 0 ▶{
Subroutine getpoint: not called CC=2 SCAR=17.9
Pod documented
749 0 my ($self, $value_id) = @_;
750  
751 0 return $self->search(undef, $value_id);
752 0 ▶ die "benchmarkanything: please provide a benchmark value_id'\n" unless $value_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $value_id 0 0 0
753 }
754  
755 3 1;
756  
757 __END__
↑ Summary