The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DBIx::ProfileManager - Helps to fine control your DBI profiling without hassles

SYNOPSIS

  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;

DESCRIPTION

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.

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.

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.

METHODS

new(%args)

Creates a manager object to control flow and hold profile data. Available option(s) are:

config

The value you want to pass to the Profile attribute of DBI handles (!Statement by default).

profile_start(@db_handles)

Sets the configuration to each of the @db_handles (or all the active handles if you don't pass anything) to start profiling.

profile_stop()

Stops profiling and store the data into the manager object for later use. You can pass all the handles will be affected.

data_formatted($format)

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}| );
statement, method_name, method_class, caller, caller2, file, file2, time, time_{n}

Each of these corresponds with !Statement, !MethodName, and the likes. See "DBI::Profile#Special Constant" for details.

count, total, first, min, max, start, end

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})
  ]
avg

Average duration (= %{total} / %{count})

dsn

A DSN string you passed to the DBI handle you're profiling.

data_structured()

Returns raw, unformatted data structure of the profile data.

data, config, path, is_started

These are accessors for the manager attributes of the name.

AUTHOR

Toru Yamaguchi <zigorou@cpan.org>

SEE ALSO

DBI
DBI::Profile

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.