home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / dbiprof < prev    next >
Encoding:
Text File  |  2004-07-06  |  4.6 KB  |  205 lines

  1. #!perl
  2.  
  3. use strict;
  4.  
  5. my $VERSION = "1.0";
  6.  
  7. use DBI::ProfileData;
  8. use Getopt::Long;
  9.  
  10. # default options
  11. my $number         = 10;
  12. my $sort           = 'total';
  13. my $filename       = 'dbi.prof';
  14. my $reverse        = 0;
  15. my $case_sensitive = 0;
  16. my (%match, %exclude);
  17.  
  18. # get options from command line
  19. GetOptions(
  20.            'version'        => sub { die "dbiprof $VERSION\n"; },
  21.            'number=i'       => \$number,
  22.            'sort=s'         => \$sort,
  23.            'reverse'        => \$reverse,
  24.            'match=s'        => \%match,
  25.            'exclude=s'      => \%exclude,
  26.            'case-sensitive' => \$case_sensitive,
  27.           );
  28.  
  29. # list of files defaults to dbi.prof
  30. my @files = @ARGV ? @ARGV : ('dbi.prof');
  31.  
  32.  
  33. # instantiate ProfileData object
  34. my $prof;
  35. eval { $prof = DBI::ProfileData->new(Files => \@files) };
  36. die "Unable to load profile data: $@\n" if $@;
  37.  
  38. # handle matches
  39. while (my ($key, $val) = each %match) {
  40.     if ($val =~ m!^/(.+)/$!) {
  41.         $val = $case_sensitive ? qr/$1/ : qr/$1/i;
  42.     } 
  43.     $prof->match($key, $val, case_sensitive => $case_sensitive);
  44. }
  45.  
  46. # handle excludes
  47. while (my ($key, $val) = each %exclude) {
  48.     if ($val =~ m!^/(.+)/$!) {
  49.         $val = $case_sensitive ? qr/$1/ : qr/$1/i;
  50.     } 
  51.     $prof->exclude($key, $val, case_sensitive => $case_sensitive);
  52. }
  53.  
  54. # sort the data
  55. $prof->sort(field => $sort, reverse => $reverse);
  56.  
  57. # all done, print it out
  58. print $prof->report(number => $number);
  59. exit 0;
  60.  
  61. __END__
  62.  
  63. =head1 NAME
  64.  
  65. dbiprof - command-line client for DBI::ProfileData
  66.  
  67. =head1 SYNOPSIS
  68.  
  69. See a report of the ten queries with the longest total runtime in the
  70. profile dump file F<prof1.out>:
  71.  
  72.  dbiprof prof1.out
  73.  
  74. See the top 10 most frequently run queries in the profile file
  75. F<dbi.prof> (the default):
  76.  
  77.   dbiprof --sort count
  78.  
  79. See the same report with 15 entries:
  80.  
  81.   dbiprof --sort count --number 15
  82.  
  83. =head1 DESCRIPTION
  84.  
  85. This tool is a command-line client for the DBI::ProfileData.  It
  86. allows you to analyze the profile data file produced by
  87. DBI::ProfileDumper and produce various useful reports.
  88.  
  89. =head1 OPTIONS
  90.  
  91. This program accepts the following options:
  92.  
  93. =over 4
  94.  
  95. =item --number N
  96.  
  97. Produce this many items in the report.  Defaults to 10.  If set to
  98. "all" then all results are shown.
  99.  
  100. =item --sort field
  101.  
  102. Sort results by the given field.  The available sort fields are:
  103.  
  104. =over 4
  105.  
  106. =item total
  107.  
  108. Sorts by total time run time across all runs.  This is the default
  109. sort.
  110.  
  111. =item longest
  112.  
  113. Sorts by the longest single run.
  114.  
  115. =item count
  116.  
  117. Sorts by total number of runs.
  118.  
  119. =item first
  120.  
  121. Sorts by the time taken in the first run.
  122.  
  123. =item shortest
  124.  
  125. Sorts by the shortest single run.
  126.  
  127. =back
  128.  
  129. =item --reverse
  130.  
  131. Reverses the selected sort.  For example, to see a report of the
  132. shortest overall time:
  133.  
  134.   dbiprof --sort total --reverse
  135.  
  136. =item --match keyN=value
  137.  
  138. Consider only items where the specified key matches the given value.
  139. Keys are numbered from 1.  For example, let's say you used a
  140. DBI::Profile Path of:
  141.  
  142.   [ DBIprofile_Statement, DBIprofile_Methodname ]
  143.  
  144. And called dbiprof as in:
  145.  
  146.   dbiprof --match key2=execute
  147.  
  148. Your report would only show execute queries, leaving out prepares,
  149. fetches, etc.
  150.  
  151. If the value given starts and ends with slashes (C</>) then it will be
  152. treated as a regular expression.  For example, to only include SELECT
  153. queries where key1 is the statement:
  154.  
  155.   dbiprof --match key1=/^SELECT/
  156.  
  157. By default the match expression is matched case-insensitively, but
  158. this can be changed with the --case-sensitive option.
  159.  
  160. =item --exclude keyN=value
  161.  
  162. Remove items for where the specified key matches the given value.  For
  163. example, to exclude all prepare entries where key2 is the method name:
  164.  
  165.   dbiprof --exclude key2=prepare
  166.  
  167. Like C<--match>, If the value given starts and ends with slashes
  168. (C</>) then it will be treated as a regular expression.  For example,
  169. to exclude UPDATE queries where key1 is the statement:
  170.  
  171.   dbiprof --match key1=/^UPDATE/
  172.  
  173. By default the exclude expression is matched case-insensitively, but
  174. this can be changed with the --case-sensitive option.
  175.  
  176. =item --case-sensitive
  177.  
  178. Using this option causes --match and --exclude to work
  179. case-sensitively.  Defaults to off.
  180.  
  181. =item --version
  182.  
  183. Print the dbiprof version number and exit.
  184.  
  185. =back
  186.  
  187. =head1 AUTHOR
  188.  
  189. Sam Tregar <sam@tregar.com>
  190.  
  191. =head1 COPYRIGHT AND LICENSE
  192.  
  193. Copyright (C) 2002 Sam Tregar
  194.  
  195. This program is free software; you can redistribute it and/or modify
  196. it under the same terms as Perl 5 itself.
  197.  
  198. =head1 SEE ALSO
  199.  
  200. L<DBI::ProfileDumper|DBI::ProfileDumper>,
  201. L<DBI::Profile|DBI::Profile>, L<DBI|DBI>.
  202.  
  203. =cut
  204.  
  205.