Show
Ignore:
Timestamp:
04/17/08 17:50:50 (20 months ago)
Author:
mpaschal
Message:

Check in MT::Tool helper
Convert report-slow-requests and upgrade tools to use MT::Tool
BugzID: 67410

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/release-35/tools/report-slow-request

    r1706 r1950  
    77# $Id$ 
    88 
     9package MT::Tool::ReportSlowRequest; 
    910use strict; 
    1011 
    1112use lib "./lib"; 
     13use base qw( MT::Tool ); 
    1214 
    13 use Getopt::Long qw/:config no_ignore_case/; 
    1415use List::Util qw( min ); 
    1516use Time::HiRes qw( time ); 
     
    1819use Data::Dumper; 
    1920 
    20 GetOptions( 
    21     'day=s'    => \my ($arg_day), 
    22     'logdir=s' => \my ($arg_logdir), 
    23     'sample=i' => \my ($arg_sample), 
    24     'slow=i'   => \my ($arg_slow), 
    25     'debug'    => \my ($arg_debug), 
    26     'dump'     => \my ($arg_dump), 
    27 ); 
     21my ($arg_day, $arg_logdir, $arg_sample, $arg_slow, $arg_debug, $arg_dump); 
    2822 
    29 $arg_slow ||= 2; 
    30  
    31 my $proc = MT::Util::LogProcessor->new( 
    32     { 
    33         debug        => $arg_debug, 
    34         logdir       => $arg_logdir || '.', 
    35         sample       => $arg_sample, 
    36         day          => $arg_day || 'yesterday', 
    37         file_pattern => 'pl-\d{8}.log', 
    38     } 
    39 ); 
    40  
    41 my %stash = ( today => $proc->day ); 
    42 my ( $total_records, $elapsed ) = $proc->process_log_files( 
    43     sub { 
    44         my ($rec) = @_; 
    45  
    46         my $secs = int $rec->{time_total}; 
    47  
    48         my $bucket = $secs > 10 ? 10 : $secs; 
    49         $stash{by_seconds}[$bucket]++; 
    50  
    51         $stash{total_requests}++; 
    52  
    53         if ( $secs > $arg_slow - 1 ) { 
    54             push @{ $stash{slow} }, $rec; 
    55         } 
    56     } 
    57 ); 
    58  
    59 printf "Processed %d records in %.02f seconds; speed: %.02f recs/sec\n", 
    60   $total_records, $elapsed, $total_records / ( $elapsed || 1 ) 
    61   if $arg_debug; 
    62  
    63 for my $bucket ( 0 .. $#{ $stash{by_seconds} } ) { 
    64     my $count = $stash{by_seconds}[$bucket] || 0; 
    65     $stash{restime_histogram}[$bucket] = { 
    66         count => $count, 
    67         range => $bucket == 10 
    68         ? '10+ sec' 
    69         : $bucket . '-' . ( $bucket + 1 ) . 's', 
    70         percent => int( $count / $stash{total_requests} * 100 ) || 1, 
     23sub help { 
     24    return q{ 
     25        --logdir <dir>    Look for logs in directory <dir>. By default, logs 
     26                          in the current directory are analyzed. 
     27        --sample <num>    ? 
     28        --slow <secs>     Number of seconds after which a request is listed 
     29                          as one of the slowest. By default, requests over 2 
     30                          seconds are considered the slowest. 
     31        --day <name>      The name of the day to examine; either 'today', 
     32                         'yesterday', or a date in the format YYYY-MM-DD. 
     33        --debug           Enable debugging output. 
     34        --dump            ? 
    7135    }; 
    7236} 
    7337 
    74 $stash{slow} = 
    75   [ sort { $b->{time_total} <=> $a->{time_total} } @{ $stash{slow} || [] } ]; 
     38sub usage { 
     39} 
    7640 
    77 report( \%stash ); 
     41sub options { 
     42    return ( 
     43        'logdir=s' => \$arg_logdir, 
     44        'sample=i' => \$arg_sample, 
     45        'slow=i'   => \$arg_slow, 
     46        'day=s'    => \$arg_day, 
     47        'debug'    => \$arg_debug, 
     48        'dump'     => \$arg_dump, 
     49    ); 
     50} 
     51 
     52sub main { 
     53    my $class = shift; 
     54    my ($verbose) = $class->SUPER::main(@_); 
     55 
     56    $arg_slow ||= 2; 
     57 
     58    my $proc = MT::Util::LogProcessor->new( 
     59        { 
     60            debug        => $arg_debug, 
     61            logdir       => $arg_logdir || '.', 
     62            sample       => $arg_sample, 
     63            day          => $arg_day || 'yesterday', 
     64            file_pattern => 'pl-\d{8}.log', 
     65        } 
     66    ); 
     67 
     68    my %stash = ( today => $proc->day ); 
     69    my ( $total_records, $elapsed ) = $proc->process_log_files( 
     70        sub { 
     71            my ($rec) = @_; 
     72 
     73            my $secs = int $rec->{time_total}; 
     74 
     75            my $bucket = $secs > 10 ? 10 : $secs; 
     76            $stash{by_seconds}[$bucket]++; 
     77 
     78            $stash{total_requests}++; 
     79 
     80            if ( $secs > $arg_slow - 1 ) { 
     81                push @{ $stash{slow} }, $rec; 
     82            } 
     83        } 
     84    ); 
     85 
     86    printf "Processed %d records in %.02f seconds; speed: %.02f recs/sec\n", 
     87      $total_records, $elapsed, $total_records / ( $elapsed || 1 ) 
     88      if $arg_debug; 
     89 
     90    for my $bucket ( 0 .. $#{ $stash{by_seconds} } ) { 
     91        my $count = $stash{by_seconds}[$bucket] || 0; 
     92        $stash{restime_histogram}[$bucket] = { 
     93            count => $count, 
     94            range => $bucket == 10 
     95            ? '10+ sec' 
     96            : $bucket . '-' . ( $bucket + 1 ) . 's', 
     97            percent => int( $count / $stash{total_requests} * 100 ) || 1, 
     98        }; 
     99    } 
     100 
     101    $stash{slow} = 
     102      [ sort { $b->{time_total} <=> $a->{time_total} } @{ $stash{slow} || [] } ]; 
     103 
     104    report( \%stash ); 
     105} 
    78106 
    79107sub report { 
     
    100128    } 
    101129} 
     130 
     131__PACKAGE__->main() unless caller; 
     132 
     1331; 
     134