bin/benchmarkanything-storage

filter: Statementstmt 61.0% 108 / 177 Branchbran 45.8% 11 / 24 Conditioncond 33.3% 9 / 27 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 63.0% 29 / 46 Podpod n/a totaltotal 54.8% 158 / 288 CC 25 SCAR 17.2
CC
25
Cov
56%
_sync_search_engine
20.0
_read_in
17.9
_set_defaults
6.6
CRAP 5.6
↑ Summary blib/lib/BenchmarkAnything/Storage/Frontend/Tools.pm »
1 #! /usr/bin/perl
2 # PODNAME: benchmarkanything-storage
3 # ABSTRACT: BenchmarkAnything storage cmdline tool
4  
5 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use App::Rad;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 ######################################################################
12 #
13 # App::Rad interface
14 #
15 ######################################################################
16  
17 4 App::Rad->run();
18  
19 sub setup
20 4 ▶{
Subroutine setup: called CC=1 SCAR=0.0
21 4 my $c = shift;
22 4 $c->unregister_command("help");
23 4 $c->register_commands("help", "search", "add", "createdb", "listnames", "listkeys", "stats", "processqueue", "initsearch", "syncsearch", "gc", "init");
24 4 $c->register('search-sync' => \&syncsearch);
25 4 $c->register('search-init' => \&initsearch);
26 }
27  
28 sub teardown
29 2 ▶{
Subroutine teardown: called CC=2 SCAR=7.2
30 2 my $c = shift;
31  
32 2 ▶ _disconnect($c) unless $c->cmd =~ /^init|help$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->cmd =~ /^init|help$/ 2 0 2
33 }
34  
35 sub help
36 0 ▶{
Subroutine help: not called CC=1 SCAR=6.9
37 0 my ($c) = @_;
38  
39 0 return qq{benchmarkanything-storage
40 [-i|--intype <TYPE>]
41 [-o|--outtype <TYPE>]
42 [-s|--separator <SEPARATOR>]
43 [--fb]
44 [--fi]
45 [-c|--cfgfile <FILE>]
46 [--debug]
47 [-d|--data]
48 [-f|--force]
49 [-v|--verbose]
50 [--skipvalidation]
51 [--queuemode]
52 [--startid <VALUE_ID>]
53 [--bulkcount <COUNT>]
54 [-p|--pattern <PATTERN>]
55 [--id <VALUE_ID>]
56 [--really <DSN>]
57 [init|search|add|createdb|listnames|listkeys|stats|processqueue|search-init|search-sync|gc]
58 <DATAFILE>
59  
60 Sub commands:
61  
62 init - Initialize a local BenchmarkAnything setup.
63  
64 search - Search BenchmarkAnything data.
65  
66 add - Add one or more BenchmarkAnything entries.
67  
68 createdb - Drop and create tables in the backend store.
69  
70 listnames - List existing metric names.
71  
72 listkeys - List existing additional key names.
73  
74 stats - Show counts of data points, metrics, keys, etc.
75  
76 processqueue - Works on the result queue created by add --queuemode.
77  
78 search-init - Initializes the configured search engine (Elasticsearch).
79  
80 search-sync - Syncs the DB with the configured search engine (Elasticsearch).
81  
82 gc - Garbage collects artefacts in the backend store.
83  
84  
85 Options:
86  
87 -i
88 --intype - input format
89 [json(default), yaml, dumper]
90 -o
91 --outtype - output format
92 [json(default), yaml, dumper]
93  
94 -s
95 --separator - sub entry separator for output format 'flat'
96 (default=;)
97  
98 --fb - on output format 'flat' use [brackets] around
99 outer arrays
100  
101 --fi - on output format 'flat' prefix outer array lines
102 with index
103  
104 -c
105 --cfgfile - config file for storage backend
106  
107 -p
108 --pattern - pattern for 'listnames'/'listkeys' commands (using LIKE)
109  
110 --id - search id for 'search' command. When given,
111 other queries are ignored and the result point
112 is returned with all additional key/value fields.
113  
114 --queuemode - used for 'add' command; values are just enqueued
115 for later actual processing. This lets
116 'add' return quicker, gives higher throughput.
117  
118 -b
119 --backend - backend ['sql' (default)]
120  
121 --debug - Pass through 'debug' option to used modules
122  
123 --verbose - print what's going on
124  
125 --force - Force the respective command
126 + for 'search-init' it means to delete and re-create the index
127 + for 'search-sync' it means to (re-)index all
128  
129 --startid - Starting VALUE_ID for 'search-sync' (default 1).
130  
131 --bulkcount - Chunk size for 'search-sync' (default 10000)
132 or for 'processqueue' (default 100).
133  
134 --skipvalidation - Disables schema validation checking,
135 usually for 'add' command.
136  
137 --really - used for 'createdb' command.
138 Avoids the 'Are you sure?' question. You need to
139 provide the DSN from config that createdb would use,
140 to avoid painful mistakes.
141  
142 -d
143 --data - Data [instead of DATAFILE].
144  
145 <DATAFILE> - input data file ("-" for STDIN) unless --data is given
146 Content depends on the sub command:
147 + for 'search' it is a search query
148 + for 'add' it is BenchmarkAnything data
149 + for 'createdb' no input data is used
150 };
151 }
152  
153 sub _connect
154 4 ▶{
Subroutine _connect: called CC=2 SCAR=7.0
155 4 my ($c) = @_;
156  
157 4 require BenchmarkAnything::Storage::Frontend::Lib;
158  
159 4 my $opt = $c->options;
160 $c->{_balib} = BenchmarkAnything::Storage::Frontend::Lib->new
161 (cfgfile => $opt->{cfgfile},
162 really => $opt->{really},
163 debug => $opt->{debug},
164 verbose => $opt->{verbose},
165 skipvalidation => $opt->{skipvalidation},
166 queuemode => $opt->{queuemode},
167 4 ▶ ($c->cmd =~ /^(init|help)$/ ?
Branch: 1 branch
BranchTrueFalseTotal
$c->cmd =~ /^(init|help)$/ ? : 0 4 4
168 ( noconnect => 1,
169 noconfig => 1,
170 ) : ()),
171 );
172 }
173  
174 sub _disconnect
175 2 ▶{
Subroutine _disconnect: called CC=2 SCAR=7.2
176 2 my ($c) = @_;
177  
178 2 ▶ $c->{_balib}->disconnect if $c->{_balib}; # balib is undef on getopt errors
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"_balib"} 2 0 2
179 }
180  
181 sub _getopt
182 8 ▶{
Subroutine _getopt: called CC=3 SCAR=12.7
183 8 my ($c) = @_;
184  
185 8 ▶ $c->getopt( "cfgfile|c=s",
Branch: 1 branch
BranchTrueFalseTotal
$c->getopt("cfgfile|c=s", "backend|b=s", "intype|i=s", "outtype|o=s", "separator|s=s", "fb", "fi", "pattern|p=s", "id=s", "debug", "data|d=s", "force|f", "verbose|v", "startid=i", "bulkcount=i", "skipvalidation", "queuemode", "really=s") or help() and return undef 4 0 4
Condition25%1/4
$c->getopt('cfgfile|c=s', 'backend|b=s', 'intype|i=s', 'outtype|o=s', 'separator|s=s', 'fb', 'fi', 'pattern|p=s', 'id=s', 'debug', 'data|d=s', 'force|f', 'verbose|v', 'startid=i', 'bulkcount=i', 'skipvalidation', 'queuemode', 'really=s') || help() && (return undef)
l!l
40
help() and return undef
!ll
00
MC/DC0%0/2
$c->getopt('cfgfile|c=s', 'backend|b=s', 'intype|i=s', 'outtype|o=s', 'separator|s=s', 'fb', 'fi', 'pattern|p=s', 'id=s', 'debug', 'data|d=s', 'force|f', 'verbose|v', 'startid=i', 'bulkcount=i', 'skipvalidation', 'queuemode', 'really=s') || help() && (return undef)
$c->getopt('cfgfile|c=s', 'backend|b=s', 'intype|i=s', 'outtype|o=s', 'separator|s=s', 'fb', 'fi', 'pattern|p=s', 'id=s', 'debug', 'data|d=s', 'force|f', 'verbose|v', 'startid=i', 'bulkcount=i', 'skipvalidation', 'queuemode', 'really=s') help()
Truth table33%1/3
A || B
A= $c->getopt('cfgfile|c=s', 'backend|b=s', 'intype|i=s', 'outtype|o=s', 'separator|s=s', 'fb', 'fi', 'pattern|p=s', 'id=s', 'debug', 'data|d=s', 'force|f', 'verbose|v', 'startid=i', 'bulkcount=i', 'skipvalidation', 'queuemode', 'really=s')B= help()
ABresult
1X1
011
000
186 "backend|b=s",
187 "intype|i=s",
188 "outtype|o=s",
189 "separator|s=s",
190 "fb",
191 "fi",
192 "pattern|p=s",
193 "id=s",
194 "debug",
195 "data|d=s",
196 "force|f",
197 "verbose|v",
198 "startid=i",
199 "bulkcount=i",
200 "skipvalidation",
201 "queuemode",
202 "really=s",
203 )
204 or help() and return undef;
205  
206 4 _connect($c);
207 4 _set_defaults($c);
208 }
209  
210  
211 sub _set_defaults
212 4 ▶{
Subroutine _set_defaults: called CC=6 SCAR=18.8
213 4 my ($c) = @_;
214  
215 4 ▶ $c->{_file} = $c->argv->[0] || '-';
Condition100%2/2
$c->argv->[0] || '-'
l!l
31
MC/DC100%1/1
$c->argv->[0] || '-'
$c->argv->[0]
Truth table100%2/2
A
A= $c->argv->[0]
Aresult
11
00
216 4 ▶ $c->options->{outtype} ||= 'json';
Condition50%1/2
$c->options->{"outtype"} ||= "json"
l!l
04
MC/DC0%0/1
$c->options->{"outtype"} ||= "json"
$c->options->{"outtype"}
Truth table50%1/2
A
A= $c->options->{"outtype"}
Aresult
11
00
217 4 ▶ $c->options->{separator} ||= ';';
Condition50%1/2
$c->options->{"separator"} ||= ";"
l!l
04
MC/DC0%0/1
$c->options->{"separator"} ||= ";"
$c->options->{"separator"}
Truth table50%1/2
A
A= $c->options->{"separator"}
Aresult
11
00
218 4 ▶ $c->options->{fb} ||= 0;
Condition50%1/2
$c->options->{"fb"} ||= 0
l!l
04
MC/DC0%0/1
$c->options->{"fb"} ||= 0
$c->options->{"fb"}
Truth table50%1/2
A
A= $c->options->{"fb"}
Aresult
11
00
219 4 ▶ $c->options->{fi} ||= 0;
Condition50%1/2
$c->options->{"fi"} ||= 0
l!l
04
MC/DC0%0/1
$c->options->{"fi"} ||= 0
$c->options->{"fi"}
Truth table50%1/2
A
A= $c->options->{"fi"}
Aresult
11
00
220  
221 }
222  
223 0 ▶sub init :Help(Initialize a local BenchmarkAnything setup)
Subroutine init: not called CC=1 SCAR=6.9
224 {
225 0 my ($c) = @_;
226  
227 0 _getopt($c);
228 0 _init($c);
229 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
230  
231 2 ▶sub search :Help(search BenchmarkAnything data)
Subroutine search: called CC=1 SCAR=0.0
232 {
233 2 my ($c) = @_;
234  
235 2 _getopt($c);
236 2 _search($c);
237 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
238  
239 5 ▶sub add :Help(add one or more BenchmarkAnything entries)
Subroutine add: called CC=1 SCAR=0.0
240 {
241 5 my ($c) = @_;
242  
243 5 _getopt($c);
244 1 _add($c);
245 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
246  
247 1 ▶sub createdb :Help(drop and create tables in the backend store)
Subroutine createdb: called CC=1 SCAR=0.0
248 {
249 1 my ($c) = @_;
250  
251 1 _getopt($c);
252 1 _createdb($c);
253 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
254  
255 0 ▶sub listnames :Help(list existing metric names)
Subroutine listnames: not called CC=1 SCAR=6.9
256 {
257 0 my ($c) = @_;
258  
259 0 _getopt($c);
260 0 _listnames($c);
261 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
262  
263 0 ▶sub listkeys :Help(list existing additional key names)
Subroutine listkeys: not called CC=1 SCAR=6.9
264 {
265 0 my ($c) = @_;
266  
267 0 _getopt($c);
268 0 _listkeys($c);
269 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
270  
271 0 ▶sub stats :Help(show backend storage usage counts)
Subroutine stats: not called CC=1 SCAR=6.9
272 {
273 0 my ($c) = @_;
274  
275 0 _getopt($c);
276 0 _stats($c);
277 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
278  
279 0 ▶sub processqueue :Help(Works on the result queue created by add --queuemode)
Subroutine processqueue: not called CC=1 SCAR=6.9
280 {
281 0 my ($c) = @_;
282  
283 0 _getopt($c);
284 0 _processqueue($c);
285 0 _gc($c);
286 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
287  
288 0 ▶sub initsearch :Help(Initializes the configured search engine (Elasticsearch))
Subroutine initsearch: not called CC=1 SCAR=6.9
289 {
290 0 my ($c) = @_;
291  
292 0 _getopt($c);
293 0 _init_search_engine($c);
294 0 _gc($c);
295 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
296  
297 0 ▶sub syncsearch :Help(Syncs the DB with the configured search engine (Elasticsearch))
Subroutine syncsearch: not called CC=1 SCAR=6.9
298 {
299 0 my ($c) = @_;
300  
301 0 _getopt($c);
302 0 _sync_search_engine($c);
303 0 _gc($c);
304 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
305  
306 0 ▶sub gc :Help(Garbage collects artefacts in the backend store)
Subroutine gc: not called CC=1 SCAR=6.9
307 {
308 0 my ($c) = @_;
309  
310 0 _getopt($c);
311 0 _gc($c);
312 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
313  
314 ######################################################################
315 #
316 # Implementation
317 #
318 ######################################################################
319  
320  
321 sub _read_in
322 3 ▶{
Subroutine _read_in: called CC=10 SCAR=28.8
323 3 my ($c) = @_;
324  
325 3 my $opt = $c->options;
326  
327 3 my $file = $c->{_file};
328 3 ▶ my $intype = $opt->{intype} || 'json';
Condition50%1/2
$opt->{'intype'} || 'json'
l!l
03
MC/DC0%0/1
$opt->{'intype'} || 'json'
$opt->{'intype'}
Truth table50%1/2
A
A= $opt->{'intype'}
Aresult
11
00
329 3 my $data;
330 my $filecontent;
331 {
332 3 local $/;
333 3 ▶ if (defined $opt->{data}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opt->{'data'}) { } 0 3 3
elsif ($file eq '-') { } 0 3 3
334 0 $filecontent = $opt->{data};
335 }
336 elsif ($file eq '-') {
337 0 $filecontent = <STDIN>;
338 }
339 else
340 {
341 3 ▶ open (my $FH, "<", $file) or die "benchmarkanything-storage: cannot open input file $file.\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $FH, "<", $file or die "benchmarkanything-storage: cannot open input file $file.\n" 3 0 3
342 3 $filecontent = <$FH>;
343 3 close $FH;
344 }
345 }
346  
347 3 ▶ if (not defined $filecontent or $filecontent !~ /[^\s\t\r\n]/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filecontent and $filecontent =~ /[^\s\t\r\n]/ms) 0 3 3
Condition33%1/3
defined $filecontent and $filecontent =~ /[^\s\t\r\n]/ms
!ll&&!rl&&r
003
MC/DC0%0/2
defined $filecontent and $filecontent =~ /[^\s\t\r\n]/ms
defined $filecontent $filecontent =~ /[^\s\t\r\n]/ms
Truth table33%1/3
A and B
A= defined $filecontentB= $filecontent =~ /[^\s\t\r\n]/ms
ABresult
0X0
100
111
348 0 die "benchmarkanything-storage: no meaningful input to read.\n";
349 }
350  
351 3 ▶ if ($intype eq "yaml") {
Branch: 3 branches
BranchTrueFalseTotal
if ($intype eq 'yaml') { } 0 3 3
elsif ($intype eq 'json') { } 3 0 3
elsif ($intype eq 'dumper') { } 0 0 0
352 0 require YAML::Any;
353 0 $data = [YAML::Any::Load($filecontent)];
354 }
355 elsif ($intype eq "json") {
356 3 require JSON;
357 3 $data = JSON::decode_json($filecontent);
358 }
359 elsif ($intype eq "dumper") {
360 0 eval '$data = my '.$filecontent;
361 }
362 else
363 {
364 0 die "benchmarkanything-storage: unrecognized input format: $intype.\n";
365 }
366 3 return $data;
367 }
368  
369  
370 sub _write_out
371 1 ▶{
Subroutine _write_out: called CC=1 SCAR=0.0
372 1 my ($c, $data) = @_;
373  
374 1 return $c->{_balib}->_output_format($data, $c->options);
375 }
376  
377 sub _listnames
378 0 ▶{
Subroutine _listnames: not called CC=1 SCAR=6.9
379 0 my ($c) = @_;
380  
381 0 my $result = $c->{_balib}->listnames ($c->options->{pattern});
382 0 _write_out($c, $result);
383 }
384  
385 sub _listkeys
386 0 ▶{
Subroutine _listkeys: not called CC=1 SCAR=6.9
387 0 my ($c) = @_;
388  
389 0 my $result = $c->{_balib}->listkeys ($c->options->{pattern});
390 0 _write_out($c, $result);
391 }
392  
393 sub _stats
394 0 ▶{
Subroutine _stats: not called CC=1 SCAR=6.9
395 0 my ($c) = @_;
396  
397 0 my $result = $c->{_balib}->stats;
398 0 _write_out($c, $result);
399 }
400  
401 sub _processqueue
402 0 ▶{
Subroutine _processqueue: not called CC=2 SCAR=17.9
403 0 my ($c) = @_;
404  
405 0 ▶ my $bulkcount = $c->options->{bulkcount} || 100;
Condition0%0/2
$c->options->{'bulkcount'} || 100
l!l
00
MC/DC0%0/1
$c->options->{'bulkcount'} || 100
$c->options->{'bulkcount'}
Truth table0%0/2
A
A= $c->options->{'bulkcount'}
Aresult
11
00
406 0 $c->{_balib}->process_raw_result_queue($bulkcount);
407 0 return;
408 }
409  
410 sub _init_search_engine
411 0 ▶{
Subroutine _init_search_engine: not called CC=1 SCAR=6.9
412 0 my ($c) = @_;
413  
414 0 $c->{_balib}->init_search_engine($c->options->{force});
415 0 return;
416 }
417  
418 sub _sync_search_engine
419 0 ▶{
Subroutine _sync_search_engine: not called CC=4 SCAR=30.0
420 0 my ($c) = @_;
421  
422 0 ▶ my $force = $c->options->{force} || 0;
Condition0%0/2
$c->options->{'force'} || 0
l!l
00
MC/DC0%0/1
$c->options->{'force'} || 0
$c->options->{'force'}
Truth table0%0/2
A
A= $c->options->{'force'}
Aresult
11
00
423 0 ▶ my $startid = $c->options->{startid} || 1;
Condition0%0/2
$c->options->{'startid'} || 1
l!l
00
MC/DC0%0/1
$c->options->{'startid'} || 1
$c->options->{'startid'}
Truth table0%0/2
A
A= $c->options->{'startid'}
Aresult
11
00
424 0 ▶ my $bulkcount = $c->options->{bulkcount} || 10_000;
Condition0%0/2
$c->options->{'bulkcount'} || 10000
l!l
00
MC/DC0%0/1
$c->options->{'bulkcount'} || 10000
$c->options->{'bulkcount'}
Truth table0%0/2
A
A= $c->options->{'bulkcount'}
Aresult
11
00
425 0 $c->{_balib}->sync_search_engine($force, $startid, $bulkcount);
426 0 return;
427 }
428  
429 sub _gc
430 0 ▶{
Subroutine _gc: not called CC=1 SCAR=6.9
431 0 my ($c) = @_;
432  
433 0 $c->{_balib}->gc;
434 0 return;
435 }
436  
437 sub _search
438 2 ▶{
Subroutine _search: called CC=2 SCAR=7.1
439 2 my ($c) = @_;
440  
441 2 my $result;
442 2 my $value_id = $c->options->{id};
443  
444 # special case: search --id gets a full point with all details
445 2 ▶ if ($value_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value_id) { } 0 2 2
446 0 $result = $c->{_balib}->search(undef, $value_id);
447 }
448 else
449 {
450 2 my $query = _read_in($c);
451 2 $result = $c->{_balib}->search($query);
452 }
453 1 _write_out($c, $result);
454 }
455  
456 sub _add
457 1 ▶{
Subroutine _add: called CC=1 SCAR=0.2
458 1 my ($c) = @_;
459  
460 1 my $data = _read_in($c);
461 1 $c->{_balib}->add ($data);
462 0 return;
463 }
464  
465 sub _createdb
466 1 ▶{
Subroutine _createdb: called CC=1 SCAR=0.0
467 1 my ($c) = @_;
468  
469 1 $c->{_balib}->createdb;
470 1 return;
471 }
472  
473 sub _init
474 0 ▶{
Subroutine _init: not called CC=1 SCAR=6.9
475 0 my ($c) = @_;
476  
477 0 my $old_verbose = $c->{_balib}{verbose};
478 0 $c->{_balib}{verbose} = 1;
479  
480 0 $c->{_balib}->init_workdir;
481  
482 0 $c->{_balib}{verbose} = $old_verbose;
483  
484 0 return;
485 }
486  
487 __END__
↑ Summary blib/lib/BenchmarkAnything/Storage/Frontend/Tools.pm »