/usr/share/perl5/DBIx/Profile.pm is in libdbix-profile-perl 1.0-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 | #
# Version: 1.0
# Jeff Lathan
# Kerry Clendinning
#
# Aaron Lee
# Deja.com, 10-1999
# Michael G Schwern, 11-1999
#
# Copyright (c) 1999,2000 Jeff Lathan, Kerry Clendinning. All rights reserved.
# This program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
# .15 First public release. Bad naming.
# .20 Fixed naming problems
# .30 Module is now more transparent, thanks to Michael G Schwern
# One less "To Do" left!
# 11-4-1999
# 1.0 Added ability to trace executes, chosen by an environment variable
# Added capability of saving everything to a log file
#
#
# This package provides an easy way to profile your DBI-based application.
# By just "use"ing this module, you will enable counting and measuring
# realtime and cpu time for each and every query used in the application.
# The times are accumulated by phase: execute vs. fetch, and broken down by
# first fetch, subsequent fetch and failed fetch within each of the
# fetchrow_array, fetchrow_arrayref, and fetchrow_hashref methods.
# More DBI functions will be added in the future.
#
# USAGE:
# Add "use DBIx::Profile;" or use "perl -MDBIx::Profile <program>"
# Add a call to $dbh->printProfile() before calling disconnect,
# or disconnect will dump the information.
#
# To Do:
# Make the printProfile code better
#
##########################################################################
##########################################################################
=head1 NAME
DBIx::Profile - DBI query profiler
Version 1.0
Copyright (c) 1999,2000 Jeff Lathan, Kerry Clendinning.
All rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=head1 SYNOPSIS
use DBIx::Profile; or "perl -MDBIx::Profile <program>"
use DBI;
$dbh->printProfile();
=head1 DESCRIPTION
DBIx::Profile is a quick and easy, and mostly transparent, profiler
for scripts using DBI. It collects information on the query
level, and keeps track of first, failed, normal, and total amounts
(count, wall clock, CPU time) for each function on the query.
NOTE: DBIx::Profile use Time::HiRes to clock the wall time and
the old standby times() to clock the CPU time. The CPU time is
pretty coarse.
DBIx::Profile can also trace the execution of queries. It will print
a timestamp and the query that was called. This is optional, and
occurs only when the environment variable DBIXPROFILETRACE is set
to 1. (ex: (bash) export DBIXPROFILETRACE=1).
Not all DBI methods are profiled at this time.
Except for replacing the existing "use" and "connect" statements,
DBIx::Profile allows DBI functions to be called as usual on handles.
Prints information to STDERR, prefaced with the pid.
=head1 RECIPE
1) Add "use DBIx::Profile" or execute "perl -MDBIx::Profile <program>"
2) Optional: add $dbh->printProfile (will execute during
disconnect otherwise)
3) Run code
4) Data output will happen at printProfile or $dbh->disconnect;
=head1 METHODS
printProfile
$dbh->printProfile();
Will print out the data collected.
If this is not called before disconnect, disconnect will call
printProfile.
setLogFile
$dbh->setLogFile("ProfileOutput.txt");
Will save all output to the file.
=head1 AUTHORS
Jeff Lathan, lathan@pobox.com
Kerry Clendinning, kerry@deja.com
Aaron Lee, aaron@pointx.org
Michael G Schwern, schwern@pobox.com
=head1 SEE ALSO
L<perl(1)>, L<DBI>
=cut
#
# For CPAN and Makefile.PL
#
$VERSION = '1.0';
use DBI;
package DBIx::Profile;
# Store DBI's original connect & disconnect then replace it with ours.
{
local $^W = 0; # Redefining a subrouting makes noise.
*_DBI_connect = DBI->can('connect');
*DBI::connect = \&connect;
}
use strict;
use vars qw(@ISA);
@ISA = qw(DBI);
#
# Make DBI aware of us.
#
__PACKAGE__->init_rootclass;
$DBIx::Profile::DBIXFILE = "";
$DBIx::Profile::DBIXFILEHANDLE = "";
$DBIx::Profile::DBIXTRACE = 0;
if ($ENV{DBIXPROFILETRACE}) {
$DBIx::Profile::DBIXTRACE = 1;
}
sub connect {
my $self = shift;
my $result = __PACKAGE__->_DBI_connect(@_);
if ($result ) {
# set flag so we know if we have not printed profile data
$result->{'private_profile'}->{'printProfileFlag'} = 0;
}
return $result;
}
##########################################################################
##########################################################################
package DBIx::Profile::db;
use strict;
use vars qw(@ISA );
@ISA = qw( DBI::db );
#
# insert our "hooks" to grab subsequent calls
#
sub prepare {
my $self = shift;
my $result = $self->SUPER::prepare(@_);
if ($result) {
$result->initRef();
}
return ($result);
}
#
# disconnect from the database; if printProfile has not been called, call it.
#
sub disconnect {
my $self = shift;
if ( !$self->{'private_profile'}->{'printProfileFlag'}) {
$self->printProfile;
}
return $self->SUPER::disconnect(@_);
}
sub setLogFile {
my $self = shift;
my $logName = shift;
$DBIx::Profile::DBIXFILE = $logName;
open(OUT,">$logName") || die "Could not open file!";
$DBIx::Profile::DBIXFILEHANDLE = \*OUT;
return 1;
}
sub DESTROY {
my $self = shift;
$self->disconnect(@_);
}
#
# Print the data collected.
#
# JEFF - The printing and the print code is kinda (er... very) ugly!
#
#like printProfile, except returns the results instead of printing them.
sub sprintProfile {
my $self = shift;
$self->printProfile({'sprint'=>1});
}
sub printProfile {
my $self = shift;
my $args = shift;
my %result;
my $total = 0;
no integer;
# Set that we have printed the results
$self->{'private_profile'}->{'printProfileFlag'} = 1;
# Loop through the queries
foreach my $qry (keys %{$self->{'private_profile'}}) {
my $text = "";
if ( $qry eq "printProfileFlag" ) {
next;
}
$total = 0;
# Now loop through the actions (execute, fetchrow, etc)
foreach my $name ( sort keys %{$self->{'private_profile'}->{$qry}}) {
# Right now, this assumes that we only have wall clock, cpu
# and count. Not generic, but what we want NOW
if ( $name eq "first" ) {
next;
}
$text .= " $name ---------------------------------------\n";
foreach my $type (sort keys %{$self->{'private_profile'}->{$qry}->{$name}}) {
$text .= " $type\n";
my ($count, $time, $ctime);
$count = $self->{'private_profile'}->{$qry}->{$name}->{$type}->{'count'};
$time = $self->{'private_profile'}->{$qry}->{$name}->{$type}->{'realtime'};
$ctime = $self->{'private_profile'}->{$qry}->{$name}->{$type}->{'cputime'};
$text .= sprintf " Count : %10d\n",$count;
$text .= sprintf " Wall Clock : %10.7f s %10.7f s\n",$time,$time/$count;
$text .= sprintf " Cpu Time : %10.7f s %10.7f s\n",$ctime,$ctime/$count;
if ($type eq "Total") {
$total += $time;
}
} # $type
} # $name
$text = "$$ \"" . $qry . "\" Total wall clock time: ". $total ."s \n" . $text;
$text = "=================================================================\n" . $text;
# In order to sort based on the total time taken for a query "easily"
# we are placing the information in a hash with the total time as the key.
# Since we could have many queries with the same total, if this exists,
# we cat the query string to the total string and use that as the key.
# The sort function will do the right thing.
if (exists $result{$total} ) {
$total .= $qry;
}
$result{$total} = $text;
} # each query
my $results;
foreach my $qry (sort stripsort keys %result) {
if ( $args->{'sprint'} ) {
$results .= $result{$qry} . "\n";
} elsif ($DBIx::Profile::DBIXFILE eq "" ) {
warn $result{$qry} . "\n";
} else {
print $DBIx::Profile::DBIXFILEHANDLE $result{$qry} . "\n";
}
}
return $results if $args->{'sprint'};
}
sub stripsort {
# Strip off the actual number amount, since the variables may
# contain text as well
$a =~ m/^(\d+\.\d+)/;
my $na = $1;
$b =~ m/^(\d+\.\d+)/;
my $nb = $1;
# Yes, this processes backwards since we want to go decreasing
$nb <=> $na;
}
##########################################################################
##########################################################################
package DBIx::Profile::st;
use strict;
use vars qw(@ISA);
@ISA = qw(DBI::st);
# Get some accurancy for wall clock time
# Cpu time is still very coarse, but...
use Time::HiRes qw ( gettimeofday tv_interval);
# Aaron Lee (aaron@pointx.org) provided the majority of
# BEGIN block below. It allowed the removal of a lot of duplicate code
# and makes the code much much cleaner, and easier to add DBI functionality.
BEGIN {
# Basic idea for each timing function:
# Grab timing info
# Call real DBI call
# Grab timing info
# Calculate time diff
#
# Just add more functions in @func_list
my @func_list = ('fetchrow_array','fetchrow_arrayref','execute',
'fetchrow_hashref');
my $func;
foreach $func (@func_list){
# define subroutine code, incl dynamic name and SUPER:: call
my $sub_code =
"sub $func {" . '
my $self = shift;
my @result;
my $result;
my ($time, $ctime, $temp, $x, $y, $z, $type);
if (wantarray) {
$time = [gettimeofday];
($ctime, $x ) = times();
@result = $self->SUPER::' . "$func" . '(@_);
($y, $z ) = times();
$time = tv_interval ($time, [gettimeofday]);
#
# Checking scalar because we are also interested
# in catching empty list
#
if (scalar @result) {
$type = "normal";
} else {
if (!$self->err) {
$type = "no more rows";
} else {
$type = "error";
}
}
$ctime = ($y + $z) - ($x + $ctime);
$self->increment($func,$type,$time, $ctime);
return @result;
} else {
$time = [gettimeofday];
($ctime, $x ) = times();
$result = $self->SUPER::' . "$func" . '(@_);
($y, $z ) = times();
$time = tv_interval ($time, [gettimeofday]);
if (defined $result) {
if ($result ne "0E0") {
$type = "normal";
} else {
$type = "returned 0E0";
}
} else {
if (!$self->err) {
$type = "no more rows";
} else {
$type = "error";
}
}
$ctime = ($y + $z) - ($x + $ctime);
$self->increment($func,$type,$time, $ctime);
return $result;
} # end of if (wantarray);
} # end of function definition
';
# define $func in current package
eval $sub_code;
}
}
sub fetchrow {
my $self = shift;
#
# fetchrow is just an alias for fetchrow_array, so
# send it that way
#
# Is the return below safe, given the main function above? - JEFF
#
return $self->fetchrow_array(@_);
}
sub increment {
my ($self, $name, $type, $time, $ctime) = @_;
my $ref;
my $qry = $self->{'Statement'};
$ref = $self->{'private_profile'};
# text matching?!? *sigh* - JEFF
if ( $name =~ /^execute/ ) {
$ref->{"first"} = 1;
if ( $DBIx::Profile::DBIXTRACE ) {
my ($sec, $min, $hour, $mday, $mon);
($sec, $min, $hour, $mday, $mon) = localtime(time);
my $text = sprintf("%d-%2d %2d:%2d:%2d", $mon, $mday,$hour,$min,$sec);
if ($DBIx::Profile::DBIXFILE eq "" ) {
warn "$$ text $name SQL: $qry\n";
} else {
print $DBIx::Profile::DBIXFILEHANDLE "$$ $text $name SQL: $qry\n";
}
}
}
if ( ($name =~ /^fetch/) && ($ref->{'first'} == 1) ) {
$type = "first";
$ref->{'first'} = 0;
}
$ref->{$name}->{$type}->{'count'}++;
$ref->{$name}->{$type}->{'realtime'}+= $time;
$ref->{$name}->{$type}->{'cputime'}+= $ctime;
$ref->{$name}->{"Total"}->{'count'}++;
$ref->{$name}->{"Total"}->{'realtime'}+= $time;
$ref->{$name}->{"Total"}->{'cputime'}+= $ctime;
}
# initRef is called from Prepare in DBIProfile
#
# Its purpose is to create the DBI's private_profile info
# so that we do not lose DBI::errstr in increment() later
sub initRef {
my $self = shift;
my $qry = $self->{'Statement'};
if (!exists($self->{'private_profile'})) {
if (!exists($self->{'Database'}->{'private_profile'}->{$qry})) {
$self->{'Database'}->{'private_profile'}->{$qry} = {};
}
$self->{'private_profile'} =
$self->{'Database'}->{'private_profile'}->{$qry};
}
}
1;
|