#!/usr/bin/perl -W

# perl script to parse the output of bigtest

use strict;
use warnings;

if($#ARGV != 0) {
  print STDERR "usage: graphmonkey file\n";
  exit 1;
}

my @hops = ();
my $max = 0;
open(FP, $ARGV[0]);
while(!eof(FP)) {
  my @input = split(" ", <FP>);
  if($input[0] eq "message" && $#input == 7) {
    push(@hops, $input[4]);
    if($input[4] > $max) { $max = $input[4]; }
  } elsif($input[0] eq "message") {
    push(@hops, -1);
  }
}
close(FP);

print "newgraph\n";
print "xaxis\n  label : iteration\n";
print "yaxis\n  label : hops\n";

print "newcurve\n  linetype none\n  marktype xbar\n";
print "  marksize 0.01 color 0.0 1.0 0.0\n  pts\n";
for(my $i = 0; $i < $#hops; $i++) {
  if($hops[$i] == -1) {
    print "    $i $max\n";
  }
}

print "newcurve\n  linetype none\n  marktype circle\n";
print "  marksize 0.01\n  color 1.0 0.0 0.0\n  pts\n";
for(my $i = 0; $i < $#hops; $i++) {
  if($hops[$i] >= 0) {
    print "    $i $hops[$i]\n";
  }
}

print "newcurve\n  linetype solid\n  marktype none\n";
print "  color 0.0 0.0 1.0\n  pts\n";
my @window = ();
my $total = 0;
for(my $i = 0; $i < $#hops; $i++) {
  if($hops[$i] >= 0) {
    push(@window, $hops[$i]);
    $total += $hops[$i];
    if($#window >= 50) {
      my $tmp = shift(@window);
      $total -= $tmp;
    }
    my $avg = $total / ($#window + 1);
    print "    $i $avg\n";
  }
}
