| 1 | #!/usr/bin/perl | ||||||||||||
| 2 | # -*- perl -*- | ||||||||||||
| 3 | |||||||||||||
| 4 | 1 | ▶ | my $file = $ARGV[0] || 'dbiprof'; | ||||||||||
|
Condition50%1/2
$ARGV[0] || 'dbiprof'
MC/DC0%0/1
$ARGV[0] || 'dbiprof'
$ARGV[0]
Truth table50%1/2
A
A= $ARGV[0]
| |||||||||||||
| 5 | |||||||||||||
| 6 | 1 | my $script = <<'SCRIPT'; | |||||||||||
| 7 | ~startperl~ | ||||||||||||
| 8 | |||||||||||||
| 9 | use strict; | ||||||||||||
| 10 | |||||||||||||
| 11 | my $VERSION = sprintf("1.%06d", q$Revision$ =~ /(\d+)/o); | ||||||||||||
| 12 | |||||||||||||
| 13 | use Data::Dumper; | ||||||||||||
| 14 | use DBI::ProfileData; | ||||||||||||
| 15 | use Getopt::Long; | ||||||||||||
| 16 | |||||||||||||
| 17 | # default options | ||||||||||||
| 18 | my $number = 10; | ||||||||||||
| 19 | my $sort = 'total'; | ||||||||||||
| 20 | my $filename = 'dbi.prof'; | ||||||||||||
| 21 | my $reverse = 0; | ||||||||||||
| 22 | my $case_sensitive = 0; | ||||||||||||
| 23 | my (%match, %exclude); | ||||||||||||
| 24 | |||||||||||||
| 25 | # get options from command line | ||||||||||||
| 26 | GetOptions( | ||||||||||||
| 27 | 'version' => sub { die "dbiprof $VERSION\n" }, | ||||||||||||
| 28 | 'help' => sub { exit usage() }, | ||||||||||||
| 29 | 'number=i' => \$number, | ||||||||||||
| 30 | 'sort=s' => \$sort, | ||||||||||||
| 31 | 'dumpnodes!' => \my $dumpnodes, | ||||||||||||
| 32 | 'reverse' => \$reverse, | ||||||||||||
| 33 | 'match=s' => \%match, | ||||||||||||
| 34 | 'exclude=s' => \%exclude, | ||||||||||||
| 35 | 'case-sensitive' => \$case_sensitive, | ||||||||||||
| 36 | 'delete!' => \my $opt_delete, | ||||||||||||
| 37 | ) or exit usage(); | ||||||||||||
| 38 | |||||||||||||
| 39 | sub usage { | ||||||||||||
| 40 | print <<EOS; | ||||||||||||
| 41 | dbiprof [options] [files] | ||||||||||||
| 42 | |||||||||||||
| 43 | Reads and merges DBI profile data from files and prints a summary. | ||||||||||||
| 44 | |||||||||||||
| 45 | files: defaults to $filename | ||||||||||||
| 46 | |||||||||||||
| 47 | options: | ||||||||||||
| 48 | |||||||||||||
| 49 | -number=N show top N, defaults to $number | ||||||||||||
| 50 | -sort=S sort by S, defaults to $sort | ||||||||||||
| 51 | -reverse reverse the sort | ||||||||||||
| 52 | -match=K=V for filtering, see docs | ||||||||||||
| 53 | -exclude=K=V for filtering, see docs | ||||||||||||
| 54 | -case_sensitive for -match and -exclude | ||||||||||||
| 55 | -delete rename files before reading then delete afterwards | ||||||||||||
| 56 | -version print version number and exit | ||||||||||||
| 57 | -help print this help | ||||||||||||
| 58 | |||||||||||||
| 59 | EOS | ||||||||||||
| 60 | return 1; | ||||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | # list of files defaults to dbi.prof | ||||||||||||
| 64 | my @files = @ARGV ? @ARGV : ('dbi.prof'); | ||||||||||||
| 65 | |||||||||||||
| 66 | |||||||||||||
| 67 | # instantiate ProfileData object | ||||||||||||
| 68 | my $prof = eval { | ||||||||||||
| 69 | DBI::ProfileData->new( | ||||||||||||
| 70 | Files => \@files, | ||||||||||||
| 71 | DeleteFiles => $opt_delete, | ||||||||||||
| 72 | ); | ||||||||||||
| 73 | }; | ||||||||||||
| 74 | die "Unable to load profile data: $@\n" if $@; | ||||||||||||
| 75 | |||||||||||||
| 76 | if (%match) { # handle matches | ||||||||||||
| 77 | while (my ($key, $val) = each %match) { | ||||||||||||
| 78 | if ($val =~ m!^/(.+)/$!) { | ||||||||||||
| 79 | $val = $case_sensitive ? qr/$1/ : qr/$1/i; | ||||||||||||
| 80 | } | ||||||||||||
| 81 | $prof->match($key, $val, case_sensitive => $case_sensitive); | ||||||||||||
| 82 | } | ||||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | if (%exclude) { # handle excludes | ||||||||||||
| 86 | while (my ($key, $val) = each %exclude) { | ||||||||||||
| 87 | if ($val =~ m!^/(.+)/$!) { | ||||||||||||
| 88 | $val = $case_sensitive ? qr/$1/ : qr/$1/i; | ||||||||||||
| 89 | } | ||||||||||||
| 90 | $prof->exclude($key, $val, case_sensitive => $case_sensitive); | ||||||||||||
| 91 | } | ||||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | # sort the data | ||||||||||||
| 95 | $prof->sort(field => $sort, reverse => $reverse); | ||||||||||||
| 96 | |||||||||||||
| 97 | # all done, print it out | ||||||||||||
| 98 | if ($dumpnodes) { | ||||||||||||
| 99 | $Data::Dumper::Indent = 1; | ||||||||||||
| 100 | $Data::Dumper::Terse = 1; | ||||||||||||
| 101 | $Data::Dumper::Useqq = 1; | ||||||||||||
| 102 | $Data::Dumper::Deparse = 0; | ||||||||||||
| 103 | print Dumper($prof->nodes); | ||||||||||||
| 104 | } | ||||||||||||
| 105 | else { | ||||||||||||
| 106 | print $prof->report(number => $number); | ||||||||||||
| 107 | } | ||||||||||||
| 108 | exit 0; | ||||||||||||
| 109 | |||||||||||||
| 110 | __END__ | ||||||||||||