#!/usr/local/bin/perl
use IO::File;

$| = 1;

$trigger   = 6667;
@clients   = ( "grunt", "eb" );
$trials    = 1;
@targets   = ( "soco.lcs.mit.edu" );
#@durations = ( 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8);
#@windows   = (   1,   5,  10,  15,  20 );

# For emacs
@durations = ( 5, 100 );
@windows = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18,
	    20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 200, 225); 
#@windows   = ( 600, 500, 400, 300, 200, 100, 90, 80, 70, 60, 50, 40, 30, 20,
#	       18, 16, 14, 12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)


foreach $target ( @targets ) {
    foreach $duration ( @durations ) {
	foreach $window ( @windows ) {
	    print STDOUT "Duration: $duration sec  Concurrency: "
		. "$window  Trials: $trials\n";

	    print STDOUT "Name      bytes        usec       MB/s" 
		. "   Succ   Attm     Succ     Attm\n";
	    print STDOUT "                                      " 
		. "  plays  plays  plays/s  plays/s\n";

	    for ($trial = 0; $trial < $trials; $trial++ ) {
		foreach $client ( @clients ) {
		    $fh{$client} = new IO::Handle;
		    open ($fh{$client},
			  "ssh -f -x $client env SFS_PORT=6666 "
			  . "/tmp/playtrace -n $window -t $duration "
			  . "-r 1 -l $client -p $trigger $target |")
			|| die "Can't fork: $!";
		}


		sleep 2;
		system "/disk/pu0/fubob/build/devel/trigger $trigger";
		sleep $duration + 1;
		
		foreach $client ( @clients ) {
		    my $fh = $fh{$client};
		    while (<$fh>) {
			print STDOUT;
		    }
		    close ($fh{$client});
		    
		  AGAIN:
		    $res = `ssh -x $client netstat -n -a \| grep TIME_WAIT \| wc -l`;
		    $res =~ s/ //g;
		    if ($res > 200) {
			print STDOUT "TIME_WAIT on $client: $res\n";
		    sleep 5;
			goto AGAIN;
		    }
	    
		}
	    }
	}
    }
}




