Changeset 265


Ignore:
Timestamp:
Nov 13, 2009, 8:27:49 PM (11 years ago)
Author:
zerodeux
Message:

Great speed gain with reverse parsing, using 'tac'

Location:
phptop
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • phptop/NEWS

    r264 r265  
     1phptop 0.4 - unreleased
     2
     3    * Added reverse parsing based on 'tac', way faster on heavy logs
     4
    15phptop 0.3.1 - 2009-11-13
    26
  • phptop/TODO

    r263 r265  
    11- Limit URI column width
    22- HTML output mode
    3 - Use 'tac' to parse logs if available (faster)
     3- Man page, less verbose --help
  • phptop/phptop

    r264 r265  
    2626
    2727my $package_name    = 'phptop';
    28 my $package_version = '0.3.1';
     28my $package_version = '0.4-dev';
    2929my $package_owner   = 'Copyright (C) 2009 Bearstech - http://bearstech.com/';
    3030my $package_url     = 'http://forge.bearstech.com/trac/wiki/PhpTop';
     
    9393
    9494    my $lh;
    95     if (!open($lh, "<$logfile")) {
     95    my $reverse = 0;
     96    my $revmiss = 0;
     97
     98    # Try to open logfile in 'reverse' mode first, using 'tac'
     99    # (trying to parse less lines and hence being much faster)
     100{
     101# It's okay for open(tac|) to fail, and we handle the other open errors explicitly
     102no warnings;
     103
     104    if (open($lh, "tac $logfile |")) {
     105        $reverse = 1;
     106    }
     107    elsif (!open($lh, "<$logfile")) {
    96108        warn "$logfile: $!";
    97109        return 0;
    98110    }
     111}
    99112
    100113  LINE:
     
    108121        if (/^\[(.*?)\]/) {
    109122            my $stamp = str2time($1);
    110             next if defined $stamp && ($now - $stamp) > $span * 60;
     123            my $skip = defined $stamp && ($now - $stamp) > $span * 60;
     124
     125            if ($reverse and $skip) {
     126                # Reverse parsing: exit loop when at least N out of time records
     127                # have been seen (make up for unordered log entries)
     128                $revmiss++;
     129                last LINE if $revmiss > 9;
     130            }
     131            # Forward parsing: skip record until we are in the right time window.
     132            # We also obviously skip the record in reverse mode.
     133            next LINE if $skip;
    111134        }
    112135
Note: See TracChangeset for help on using the changeset viewer.