#!/usr/local/bin/perl

#
# transition of Wcol-E
#   by k-chinen@is.aist-nara.ac.jp, 1997
#
#
# $Id$
#

# Prefetch or Client, response time

use Math::BigInt;
require 'getopts.pl';

$bradix = Math::BigInt->new("2");
$kilo = $bradix ** 10;
$mega = $bradix ** 20;
$giga = $bradix ** 30;
$tera = $bradix ** 40;


#
# convert to kilo, mega, giga and tera
#
sub kilo {
    my($_target, $unit) = @_;
    my($target) = Math::BigInt->new($_target);
    my($v);
    $v = new Math::BigInt "0";
    $v = $target->babs;
    my($r);

    if($v> $tera) {
#        $r = ($v / $tera)->bnorm . " T";
        $r = sprintf("%.2fT", ($v / $giga)->bnorm / 1024);
    }
    elsif($v>$giga) {
#        $r = ($v / $giga)->bnorm . " G";
        $r = sprintf("%.2fG", ($v / $mega)->bnorm / 1024);
    }
    elsif($v>$mega) {
#        $r = ($v / $mega)->bnorm . " M";
        $r = sprintf("%.2fM", ($v / $kilo)->bnorm / 1024);
    }
    elsif($v>$kilo) {
#        $r = ($v / $kilo)->bnorm . " K";
        $r = sprintf("%.2fK", $v->brom / 1024);
    }
    else {
        $r = $v . "  ";
    }

    $r =~ s#^\+##;
    $r .= $unit;
#   $r .= $unit . " (" . $target . ")";

    return $r;
}

$tradix = Math::BigInt->new("10");
$thousand = $tradix **  3;
$million  = $tradix **  6;
$billion  = $tradix **  9;
$trillion = $tradix ** 12;


### convert to thousand, million, billion and trillion
sub thou{
    my($_target, $unit) = @_;
    my($target) = Math::BigInt->new($_target);
    my($v);
    $v = new Math::BigInt "0";
    $v = $target->babs;
    my($r);

    if($v>$trillion) {
        $r = ($v / $trillion)->bnorm . " trillion";
    }
    elsif($v>$billion) {
        $r = ($v / $billion)->bnorm . " billion ";
    }
    elsif($v>$million) {
        $r = ($v / $million)->bnorm . " million ";
    }
    elsif($v>$thousand) {
        $r = ($v / $thousand)->bnorm . " thousand";
    }
    else {
        $r = $v . "         ";
    }

    $r =~ s#^\+##;
    $r .= $unit;
#   $r .= $unit . " (" . $target . ")";

    return $r;
}


$z_line         = Math::BigInt->new("0");

### HTTP
$z_hsess        = Math::BigInt->new("0");
$z_fetch        = Math::BigInt->new("0");
$z_prefetch     = Math::BigInt->new("0");
$z_send         = Math::BigInt->new("0");
$z_hothers       = Math::BigInt->new("0");

$z_outtraffic   = Math::BigInt->new("0");
$z_intraffic    = Math::BigInt->new("0");

### ICP
$z_isess        = Math::BigInt->new("0");
$z_notify       = Math::BigInt->new("0");
$z_query        = Math::BigInt->new("0");
$z_iothers      = Math::BigInt->new("0");






$last = 0;
$dist = 3600;       # So, It is one hour.


$fetch = 0;
$prefetch = 0;
$send = 0;
$send_prefetching = 0;
$hit_rate_prefetching = 0;
$hit_rate_caching = 0;

$request            = Math::BigInt->new("0");
$res_fetch          = Math::BigInt->new("0");
$res_send_caching   = Math::BigInt->new("0");




print <<EOM;
#time           |sum     |sessions                                    |hit-rate[%]
#               |        |fetch   |prefetch|send                      |
#               |        |                 |sum     |prefetch|other   |pre  |cache
EOM
#                12345678 12345678 12345678 12345678 12345678 12345678 12345 12345

#
# caching:
#       ( FETCH + SEND(prefetch) ) / (FETCH+SEND)
#
# prefetching:
#       ( FETCH ) / (FETCH+SEND)
#
#

sub report_response {
    if($request > 0) {

        printf "# request           %14s\n", $request;
        printf "# sum fetch         %14s\n", $res_fetch;
        printf "# sum send(caching) %14s\n", $res_send_caching;
        printf "# ave response as prefetching proxy %7d [msec]\n",
                    (($res_fetch)/$request);
        printf "# ave response of caching proxy     %7d [msec]\n",
                    (($res_fetch+$res_send_caching)/$request);
    }
    else {
        print "# no request ($request)\n";
    }

}

sub report_session {
        printf "%s %8d %8d %8d %8d %8d %8d %5.1f %5.1f\n",
            $last, $fetch+$prefetch+$send, $fetch, $prefetch,
            $send, $send_prefetching, $send-$send_prefetching,
            $hit_rate_prefetching*100.0, $hit_rate_caching*100.0;
}



&Getopts('trd:');


if($opt_d) {
    $dist = $opt_d;
}



while(<>) {
    @fis = split;

    if($fis[20] ne ":") {
        next;
    }

    if($fis[6] eq "PREFETCH") {
        $prefetch++;

        $z_intraffic  += $fis[24];
    }

    if($fis[9] eq "HTTP") {
        $request++;

        if($fis[6] eq "FETCH") {
            $fetch++;

            $res_fetch += $fis[25];
#           $res_fetch += $fis[19];

            $z_intraffic  += $fis[24];
            $z_outtraffic += $fis[18];
        }
        elsif($fis[6] eq "SEND") {
            $send++;

            $z_outtraffic += $fis[18];

            if($fis[27] eq "PREFETCH" && $fis[28] eq "1") {
                $send_prefetching++;
            }
            else {
                $res_send_caching += $fis[25];
#               $res_send_caching += $fis[19];
            }
        }

        $hit_rate_prefetching = $send / ($fetch + $send);
        $hit_rate_caching = ($send - $send_prefetching) / ($fetch + $send);
    }

    if($fis[5] - $last > $dist) {
        $last = $fis[5];

        &report_session;
#       &report_response;

    }
}

print  "### Last\n";
&report_session;

print  "### Traffic\n";
printf "# Incoming Traffic  %14s : %14s\n",
    $z_intraffic, &kilo($z_intraffic,"Bytes");
printf "# Outgoing Traffic  %14s : %14s\n",
    $z_outtraffic, &kilo($z_outtraffic,"Bytes");

&report_response;


