DBIx::ProfileManager - Helps to fine control your DBI profiling without hassles
use DBI; use DBIx::ProfileManager; my $dbh = DBI->connect(...); # enable profiling of all the active handle(s) my $pm = DBIx::ProfileManager->new; $pm->profile_start; # do something with the handle my $res = $dbh->selectall_arrayref(...); $pm->profile_stop; my @results = $pm->data_formatted; local $, = "\n"; print @results;
DBI has a built-in profiler named DBI::Profile. You can use it just by setting the DBI_PROFILE environmental variable to something DBI understands. This is quite handy and works beautifully when your code is small, but doesn't help much when your application grows. You get too much.
DBI_PROFILE
You might want to embed this variable in your code to limit its effect, which works, but only if you set it before you instantiate DBI handles, and that doesn't always happen in the same block you want to profile.
You can also enable profiler by setting the Profile attribute of each handle you want to profile. This also works, though tedious especially if you have multiple handles to profile and/or want to do something with the profile data.
Profile
DBIx::ProfileManager allows you to add SQL performance profiler to wherever you want with a few lines of code. It looks for active DBI handles, and applies your configuration to each of them. When you stop profiling, it collects the result from the handles. It also provides custom formatters. You can pass the formatted string(s) to your application's logger, or to anything you like, instead of simply printing it to the screen.
Creates a manager object to control flow and hold profile data. Available option(s) are:
The value you want to pass to the Profile attribute of DBI handles (!Statement by default).
!Statement
Sets the configuration to each of the @db_handles (or all the active handles if you don't pass anything) to start profiling.
Stops profiling and store the data into the manager object for later use. You can pass all the handles will be affected.
Returns an array of formatted strings (or a concatenated string in the scalar context) of the profile data. You can use the following special strings for convenience like this:
$pm->data_formatted( q|%{statement} : %{max}, %{min}, %{avg}| );
Each of these corresponds with !Statement, !MethodName, and the likes. See "DBI::Profile#Special Constant" for details.
!MethodName
Each of these corresponds with the column of a profile data node, which is described in DBI::Profile#OVERVIEW as follows.
[ 106, # 0: %{count} of samples at this node 0.0312958955764771, # 1: %{total} duration 0.000490069389343262, # 2: %{first} duration 0.000176072120666504, # 3: shortest duration (%{min}) 0.00140702724456787, # 4: longest duration (%{max}) 1023115819.83019, # 5: time of first sample (%{start}) 1023115819.86576, # 6: time of last sample (%{end}) ]
Average duration (= %{total} / %{count})
A DSN string you passed to the DBI handle you're profiling.
Returns raw, unformatted data structure of the profile data.
These are accessors for the manager attributes of the name.
Toru Yamaguchi <zigorou@cpan.org>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install DBIx::ProfileManager, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::ProfileManager
CPAN shell
perl -MCPAN -e shell install DBIx::ProfileManager
For more information on module installation, please visit the detailed CPAN module installation guide.