| 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 | | ); |
| | 21 | my ($arg_day, $arg_logdir, $arg_sample, $arg_slow, $arg_debug, $arg_dump); |
| 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, |
| | 23 | sub 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 ? |
| 77 | | report( \%stash ); |
| | 41 | sub 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 | |
| | 52 | sub 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 | } |