| 1 | ############################################################################## | ||
| 2 | # | ||
| 3 | # Time::Profiler | ||
| 4 | # Vladi Belperchinov-Shabanski "Cade" <cade@biscom.net> <cade@datamax.bg> | ||
| 5 | # | ||
| 6 | # DISTRIBUTED UNDER GPLv2 | ||
| 7 | # | ||
| 8 | ############################################################################## | ||
| 9 | package Time::Profiler; | ||
| 10 | use Time::HR; | ||
| 11 | use Time::Profiler::Scope; | ||
| 12 | use Data::Dumper; | ||
| 13 | use strict; | ||
| 14 | |||
| 15 | our $VERSION = '1.30'; | ||
| 16 | |||
| 17 | ############################################################################## | ||
| 18 | |||
| 19 | sub new | ||
| 20 | { | ||
| 21 | my $class = shift; | ||
| 22 | $class = ref( $class ) || $class; | ||
| 23 | my $self = { | ||
| 24 | 'PROFILER_DATA_SINGLE' => {}, | ||
| 25 | 'PROFILER_DATA_TREE' => {}, | ||
| 26 | }; | ||
| 27 | bless $self, $class; | ||
| 28 | return $self; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub begin_scope | ||
| 32 | { | ||
| 33 | my $self = shift; | ||
| 34 | |||
| 35 | my $scope = new Time::Profiler::Scope( $self, @_ ); | ||
| 36 | $scope->start(); | ||
| 37 | |||
| 38 | return $scope; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub report | ||
| 42 | { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | my $hrs = $self->{ 'PROFILER_DATA_SINGLE' }; | ||
| 46 | my $hrt = $self->{ 'PROFILER_DATA_TREE' }; | ||
| 47 | |||
| 48 | my $text; | ||
| 49 | $text .= "\n"; | ||
| 50 | $text .= "SINGLE PROFILE SCOPES\n"; | ||
| 51 | $text .= $self->__report_level( $hrs, 0 ); | ||
| 52 | $text .= "\n"; | ||
| 53 | $text .= "TREE PROFILE SCOPES\n"; | ||
| 54 | $text .= $self->__report_level( $hrt, 0 ); | ||
| 55 | |||
| 56 | if( $self->{ 'DEBUG' } ) | ||
| 57 | { | ||
| 58 | $text .= "\n"; | ||
| 59 | $text .= "RAW TREE PROFILE DATA\n"; | ||
| 60 | $Data::Dumper::sortkeys = 1; | ||
| 61 | $text .= Dumper( $hrt ); | ||
| 62 | } | ||
| 63 | |||
| 64 | return $text; | ||
| 65 | } | ||
| 66 | |||
| 67 | ### INTERNAL ################################################################# | ||
| 68 | |||
| 69 | sub __report_level | ||
| 70 | { | ||
| 71 | my $self = shift; | ||
| 72 | my $hr = shift; | ||
| 73 | my $level = shift; | ||
| 74 | |||
| 75 | my @k = grep { ! /^:(TIME|COUNT)/ } keys %$hr; | ||
| 76 | @k = sort { $hr->{ $b }->{ ':TIME' } <=> $hr->{ $a }->{ ':TIME' } } @k; | ||
| 77 | |||
| 78 | #return "\n" if @k == 0; | ||
| 79 | |||
| 80 | my $text; | ||
| 81 | for my $k ( @k ) | ||
| 82 | { | ||
| 83 | my $t = $hr->{ $k }->{ ':TIME' }; | ||
| 84 | my $c = $hr->{ $k }->{ ':COUNT' }; | ||
| 85 | |||
| 86 | $t /= 1_000_000_000; # convert to seconds | ||
| 87 | |||
| 88 | my $ts = $c == 1 ? 'time' : 'times'; | ||
| 89 | my $rs = sprintf( "%5s %-5s = %12.06f sec. ", $c, $ts, $t ); | ||
| 90 | $rs = ' ' x length( $rs ) if $c == 0; | ||
| 91 | |||
| 92 | $text .= $rs . ( "| " x $level ) . $k; | ||
| 93 | |||
| 94 | $text .= "\n"; | ||
| 95 | $text .= $self->__report_level( $hr->{ $k }, $level + 1 ); | ||
| 96 | } | ||
| 97 | |||
| 98 | return $text; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub __add_dt | ||
| 102 | { | ||
| 103 | my $self = shift; | ||
| 104 | my $key = shift; | ||
| 105 | my $dt = shift; | ||
| 106 | |||
| 107 | my $c = 1 if $key =~ s/^\+//; # cumulative | ||
| 108 | |||
| 109 | my @key = split /\//, $key; | ||
| 110 | my $hrs = $self->{ 'PROFILER_DATA_SINGLE' }; | ||
| 111 | my $hrt = $self->{ 'PROFILER_DATA_TREE' }; | ||
| 112 | |||
| 113 | if( @key == 1 ) | ||
| 114 | { | ||
| 115 | # no slashes, no tree -- single scope | ||
| 116 | $hrs->{ $key }{ ':COUNT' }++; | ||
| 117 | $hrs->{ $key }{ ':TIME' } += $dt; | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | |||
| 121 | # tree scope | ||
| 122 | my $ck; # cumulative key | ||
| 123 | while( my $k = shift @key ) | ||
| 124 | { | ||
| 125 | $hrt->{ $k } ||= {}; | ||
| 126 | $hrt = $hrt->{ $k }; | ||
| 127 | $ck .= "$k/"; | ||
| 128 | next unless $c; | ||
| 129 | # $hrs->{ $ck }{ ':COUNT' }++; | ||
| 130 | # $hrs->{ $ck }{ ':TIME' } += $dt; | ||
| 131 | $hrt->{ ':COUNT' }++; | ||
| 132 | $hrt->{ ':TIME' } += $dt; | ||
| 133 | } | ||
| 134 | return if $c; | ||
| 135 | $hrt->{ ':COUNT' }++; | ||
| 136 | $hrt->{ ':TIME' } += $dt; | ||
| 137 | } | ||
| 138 | |||
| 139 | ############################################################################## | ||
| 140 | |||
| 141 | =pod | ||
| 142 | |||
| 143 | =head1 NAME | ||
| 144 | |||
| 145 | Time::Profiler provides scope-automatic or manual code time measurement. | ||
| 146 | |||
| 147 | =head1 SYNOPSIS | ||
| 148 | |||
| 149 | #!/usr/bin/perl | ||
| 150 | use strict; | ||
| 151 | use Time::Profiler; | ||
| 152 | |||
| 153 | my $pr = new Time::Profiler; # create new profiler instance | ||
| 154 | |||
| 155 | print "begin main\n"; | ||
| 156 | # begin main:: scope measuring with automatic names | ||
| 157 | my $_ps = $pr->begin_scope(); | ||
| 158 | |||
| 159 | t1(); | ||
| 160 | t2(); | ||
| 161 | sleep( 2 ); | ||
| 162 | |||
| 163 | # main:: scope will not end before reporting so must be stopped manually | ||
| 164 | $_ps->stop(); | ||
| 165 | |||
| 166 | # print profiler stats | ||
| 167 | print $pr->report(); | ||
| 168 | |||
| 169 | sub t1 | ||
| 170 | { | ||
| 171 | print "begin t1\n"; | ||
| 172 | # begin t1 function scope time measuring | ||
| 173 | my $_ps = $pr->begin_scope(); | ||
| 174 | |||
| 175 | t2(); | ||
| 176 | sleep( 3 ); | ||
| 177 | t2(); | ||
| 178 | # t1 function scope ends here so timing will end automatically | ||
| 179 | } | ||
| 180 | |||
| 181 | sub t2 | ||
| 182 | { | ||
| 183 | print "begin t2\n"; | ||
| 184 | # begin t2 function scope time measuring | ||
| 185 | my $_ps = $pr->begin_scope(); | ||
| 186 | |||
| 187 | sleep( 1 ); | ||
| 188 | # t2 function scope ends here so timing will end automatically | ||
| 189 | } | ||
| 190 | |||
| 191 | =head1 DESCRIPTION | ||
| 192 | |||
| 193 | Time::Profiler is designed to be called inside scopes (or functions) which | ||
| 194 | are needed to be measured. It provides automatic, manual or cumulative | ||
| 195 | scope names. | ||
| 196 | |||
| 197 | =head1 OUTPUT | ||
| 198 | |||
| 199 | The example in the SYNOPSIS will print this output: | ||
| 200 | |||
| 201 | begin main | ||
| 202 | begin t1 | ||
| 203 | begin t2 | ||
| 204 | begin t2 | ||
| 205 | begin t2 | ||
| 206 | |||
| 207 | SINGLE PROFILE SCOPES | ||
| 208 | 1 time = 8.001 sec. main:: | ||
| 209 | 1 time = 5.000 sec. main::t1 | ||
| 210 | 3 times = 3.000 sec. main::t2 | ||
| 211 | |||
| 212 | TREE PROFILE SCOPES | ||
| 213 | 1 time = 8.001 sec. main:: | ||
| 214 | 1 time = 5.000 sec. | main::t1 | ||
| 215 | 2 times = 2.000 sec. | | main::t2 | ||
| 216 | 1 time = 1.000 sec. | main::t2 | ||
| 217 | |||
| 218 | =head1 AUTOMATIC SCOPE NAMES | ||
| 219 | |||
| 220 | Time::Profiler will traverse the stack and will construct automatic name if | ||
| 221 | scope name is left empty or '*': | ||
| 222 | |||
| 223 | t1(); | ||
| 224 | t2(); | ||
| 225 | |||
| 226 | print $pr->report(); | ||
| 227 | |||
| 228 | sub t1 | ||
| 229 | { | ||
| 230 | my $_ps = $pr->begin_scope(); # same as below | ||
| 231 | t2(); | ||
| 232 | sleep( 3 ); | ||
| 233 | } | ||
| 234 | |||
| 235 | sub t2 | ||
| 236 | { | ||
| 237 | my $_ps = $pr->begin_scope( '*' ); # same as above | ||
| 238 | sleep( 2 ); | ||
| 239 | } | ||
| 240 | |||
| 241 | Output will be: | ||
| 242 | |||
| 243 | SINGLE PROFILE SCOPES | ||
| 244 | 1 time = 5.000 sec. main::t1 | ||
| 245 | 2 times = 4.000 sec. main::t2 | ||
| 246 | |||
| 247 | TREE PROFILE SCOPES | ||
| 248 | main:: | ||
| 249 | 1 time = 5.000 sec. | main::t1 | ||
| 250 | 1 time = 2.000 sec. | | main::t2 | ||
| 251 | 1 time = 2.000 sec. | main::t2 | ||
| 252 | |||
| 253 | =head1 MANUAL SCOPE NAMES | ||
| 254 | |||
| 255 | Manual names can force fixed scope names. All names without '/' are considered | ||
| 256 | SINGLE scopes. All names with '/' are TREE scope names. SINGLE and TREE scopes | ||
| 257 | are reported separately: | ||
| 258 | |||
| 259 | my $_ps = $pr->begin_scope( 'ALL' ); # SINGLE scope | ||
| 260 | |||
| 261 | t1(); | ||
| 262 | t2(); | ||
| 263 | |||
| 264 | $_ps->stop; | ||
| 265 | print $pr->report(); | ||
| 266 | |||
| 267 | sub t1 | ||
| 268 | { | ||
| 269 | # T1 here | ||
| 270 | t2(); | ||
| 271 | sleep( 3 ); | ||
| 272 | } | ||
| 273 | |||
| 274 | sub t2 | ||
| 275 | { | ||
| 276 | my $_ps = $pr->begin_scope( 'ROOT/T1/T2' ); # TREE scope | ||
| 277 | sleep( 2 ); | ||
| 278 | } | ||
| 279 | |||
| 280 | This will force main:: scope name to be 'ROOT' and only nested t2() name | ||
| 281 | 'ROOT/T1/T2'. Output will be: | ||
| 282 | |||
| 283 | SINGLE PROFILE SCOPES | ||
| 284 | 1 time = 5.000 sec. ROOT | ||
| 285 | |||
| 286 | TREE PROFILE SCOPES | ||
| 287 | 1 time = 5.000 sec. ROOT | ||
| 288 | | T1 | ||
| 289 | 1 time = 2.000 sec. | | T2 | ||
| 290 | |||
| 291 | So t1() has no profile stats but t2() scope name (path) is measured inside | ||
| 292 | the 'ROOT' scope. | ||
| 293 | |||
| 294 | =head1 CUMULATIVE SCOPE NAMES | ||
| 295 | |||
| 296 | TREE scopes can be cumulative. Cumulative names begin with '+' and allow | ||
| 297 | measurement aggregation for same type functions. | ||
| 298 | |||
| 299 | For example database module may have read_data() and | ||
| 300 | write_data() function, which read or write data from/to different tables | ||
| 301 | (in this example table names are 'CLIENTS' and 'ADDRESSES'): | ||
| 302 | |||
| 303 | sub read_data | ||
| 304 | { | ||
| 305 | my $table_name = shift; | ||
| 306 | my $_ps = $pr->begin_scope( "+DB/READ_DATA/$table_name" ); | ||
| 307 | ... | ||
| 308 | } | ||
| 309 | |||
| 310 | sub write_data | ||
| 311 | { | ||
| 312 | my $table_name = shift; | ||
| 313 | my $_ps = $pr->begin_scope( "+DB/WRITE_DATA/$table_name" ); | ||
| 314 | ... | ||
| 315 | } | ||
| 316 | |||
| 317 | Possible output: | ||
| 318 | |||
| 319 | TREE PROFILE SCOPES | ||
| 320 | 1 time = 14.000 sec. DB | ||
| 321 | 1 time = 8.000 sec. | READ_DATA | ||
| 322 | 2 time = 4.000 sec. | | CLIENTS | ||
| 323 | 2 time = 4.000 sec. | | ADDRESSES | ||
| 324 | 1 time = 6.000 sec. | WRITE_DATA | ||
| 325 | 1 time = 3.000 sec. | | CLIENTS | ||
| 326 | 1 time = 3.000 sec. | | ADDRESSES | ||
| 327 | |||
| 328 | |||
| 329 | This will measure several things: | ||
| 330 | |||
| 331 | =over 4 | ||
| 332 | |||
| 333 | =item all calls to read_data() for specific $table_name (DB/READ_DATA/$table_name) | ||
| 334 | |||
| 335 | =item all calls to write_data() for specific $table_name (DB/WRITE_DATA/$table_name) | ||
| 336 | |||
| 337 | =item will accumulate all read stats for tables (DB/READ_DATA) | ||
| 338 | |||
| 339 | =item will accumulate all write stats for tables (DB/WRITE_DATA) | ||
| 340 | |||
| 341 | =item will accumulate all database stats for all operations for all tables (DB) | ||
| 342 | |||
| 343 | =back | ||
| 344 | |||
| 345 | Other case could require measuring of all DB access for specific table | ||
| 346 | (i.e. kind of "DB/*/$table_name"). To achieve this and do not lose the previous | ||
| 347 | prifile stats, requires multiple scope names: | ||
| 348 | |||
| 349 | sub read_data | ||
| 350 | { | ||
| 351 | my $table_name = shift; | ||
| 352 | my $_ps = $pr->begin_scope( "+DB/READ_DATA/$table_name", "+DB_TABLES/$table_name" ); | ||
| 353 | ... | ||
| 354 | } | ||
| 355 | |||
| 356 | This will measure time stats by database access (DB) per table and per operation: | ||
| 357 | |||
| 358 | Possible output: | ||
| 359 | |||
| 360 | TREE PROFILE SCOPES | ||
| 361 | 1 time = 14.000 sec. DB | ||
| 362 | 1 time = 8.000 sec. | READ_DATA | ||
| 363 | 2 time = 4.000 sec. | | CLIENTS | ||
| 364 | 2 time = 4.000 sec. | | ADDRESSES | ||
| 365 | 1 time = 6.000 sec. | WRITE_DATA | ||
| 366 | 1 time = 3.000 sec. | | CLIENTS | ||
| 367 | 1 time = 3.000 sec. | | ADDRESSES | ||
| 368 | 6 time = 14.000 sec. DB_TABLES | ||
| 369 | 3 time = 7.000 sec. | CLIENTS | ||
| 370 | 3 time = 7.000 sec. | ADDRESSES | ||
| 371 | |||
| 372 | =head1 MIXED NAMES | ||
| 373 | |||
| 374 | Scopes may have multiple names including mixed types names: | ||
| 375 | |||
| 376 | sub read_data | ||
| 377 | { | ||
| 378 | my $table_name = shift; | ||
| 379 | my $_ps = $pr->begin_scope( "*", "+TT/T2", "ALL_FUNCS" ); | ||
| 380 | ... | ||
| 381 | } | ||
| 382 | |||
| 383 | This will produce automatic scope name ("*"), | ||
| 384 | cumulative ("+DB/READ_DATA/$table_name") and | ||
| 385 | manual static one ("ALL_FUNCS"). | ||
| 386 | |||
| 387 | In this case stats will be mixed in the same profiler output: | ||
| 388 | |||
| 389 | SINGLE PROFILE SCOPES | ||
| 390 | 1 time = 5.000 sec. ROOT | ||
| 391 | 1 time = 2.000 sec. main::t2 | ||
| 392 | 1 time = 2.000 sec. TT/ | ||
| 393 | 1 time = 2.000 sec. ALL_FUNCS | ||
| 394 | 1 time = 2.000 sec. TT/T2/ | ||
| 395 | |||
| 396 | TREE PROFILE SCOPES | ||
| 397 | 1 time = 5.000 sec. ROOT | ||
| 398 | 1 time = 2.000 sec. TT | ||
| 399 | 1 time = 2.000 sec. | T2 | ||
| 400 | 1 time = 2.000 sec. ALL_FUNCS | ||
| 401 | main:: | ||
| 402 | | main::t1 | ||
| 403 | 1 time = 2.000 sec. | | main::t2 | ||
| 404 | |||
| 405 | =head1 PITFALLS | ||
| 406 | |||
| 407 | Avoid cumulative names for recursive or nested functions, otherwise some stats | ||
| 408 | may seem wrong: | ||
| 409 | |||
| 410 | t1(); | ||
| 411 | t2(); | ||
| 412 | |||
| 413 | print $pr->report(); | ||
| 414 | |||
| 415 | sub t1 | ||
| 416 | { | ||
| 417 | my $_ps = $pr->begin_scope( '+ALL_FUNCS/T1' ); | ||
| 418 | |||
| 419 | t2(); | ||
| 420 | sleep( 3 ); | ||
| 421 | } | ||
| 422 | |||
| 423 | sub t2 | ||
| 424 | { | ||
| 425 | my $_ps = $pr->begin_scope( '+ALL_FUNCS/T2' ); | ||
| 426 | sleep( 2 ); | ||
| 427 | } | ||
| 428 | |||
| 429 | Output will be: | ||
| 430 | |||
| 431 | SINGLE PROFILE SCOPES | ||
| 432 | 3 times = 9.000 sec. ALL_FUNCS/ | ||
| 433 | 1 time = 5.000 sec. ALL_FUNCS/T1/ | ||
| 434 | 2 times = 4.000 sec. ALL_FUNCS/T2/ | ||
| 435 | |||
| 436 | TREE PROFILE SCOPES | ||
| 437 | 3 times = 9.000 sec. ALL_FUNCS | ||
| 438 | 1 time = 5.000 sec. | T1 | ||
| 439 | 2 times = 4.000 sec. | T2 | ||
| 440 | |||
| 441 | Total program execution time is actually 7 sec. but we see that ALL_FUNCS says | ||
| 442 | 9 sec. This is because t2() time is measured twice: once as separate function | ||
| 443 | call and second time as nested function. | ||
| 444 | |||
| 445 | =head1 DEPENDENCIES | ||
| 446 | |||
| 447 | Time::HR | ||
| 448 | Data::Dumper | ||
| 449 | |||
| 450 | =head1 GITHUB REPOSITORY | ||
| 451 | |||
| 452 | https://github.com/cade-vs/perl-time-profiler | ||
| 453 | |||
| 454 | git clone git://github.com/cade-vs/perl-time-profiler.git | ||
| 455 | |||
| 456 | =head1 AUTHOR | ||
| 457 | |||
| 458 | Vladi Belperchinov-Shabanski "Cade" | ||
| 459 | |||
| 460 | <cade@biscom.net> <cade@datamax.bg> <cade@cpan.org> | ||
| 461 | |||
| 462 | http://cade.datamax.bg | ||
| 463 | |||
| 464 | =cut | ||
| 465 | |||
| 466 | ############################################################################## | ||
| 467 | 1; | ||
| 468 | ############################################################################## | ||
| 469 |