#!/usr/bin/perl

# This scipt uses test1 and test2 from the sun connectathon suite.
# It measures file and directory creation and deletion rates.

# These options must be specified here
$server = "";				# name of nfs server
$directory = ""; 		# directory to use

# These options can be set from the command line.
# These are the defaults
$levels = 1;
$min = 10;
$max = 20000;
$granularity = 10;
$do_files = 1;

$quiet = 0; 	# If 1, print test results on STDOUT
		# (If no output file is used, will do this anyways)
$printfile = 0; 

sub usage {
	if ($do_files)
	{
		$do_files_str = "TRUE";
		$do_dirs_str = "FALSE";
	}
	else
	{
		$do_files_str = "FALSE";
		$do_dirs_str = "TRUE";
	}

	print <<"ENDUSAGE"
test1_circuit [options]

Options: (defaults are in parens)
-o filename		Save results into a file
--quiet			Don't show results on STDOUT
--files ($do_files_str)			File creation/deletion
--directories ($do_dirs_str)		Directory creation/deletion
--granularity=n ($granularity)	Granularity for linear scaling in test
--min=n ($min)		Minimum file size for test5
--max=n ($max)		Maximum file size for test5
ENDUSAGE
}

sub getCollisions {
	# Quick & dirty way of getting the number of collisions.
	# Will not work under all setups and platforms.
	my $if_results = `/sbin/ifconfig | grep collisions`;
	$if_results =~ /collisions:(\d+)/;
	return $1
}

sub runTest {
	my $test_string = $_[0]; 

	my $collisions1 = getCollisions();
	my $result = `$test_string`;
	my $collisions2 = getCollisions();
	my $net_collisions = $collisions2 - $collisions1;
	$result .= "Collisions: " . $net_collisions . "\n\n";
}

sub printResult
{
	my $result = $_[0];
	if ($printfile)
	{
		print OUTFILE $result;
	}
	if (!$quiet)
	{
		print $result;
	}
}	

# Set test directory
$ENV{"NFSTESTDIR"} = $directory;

# Check for command line switches to override defaults
# Probably a more elegant way to do this.
while ($arg = shift)
{
	if ($arg eq "--usage" || $arg eq "--help")
	{
		usage;
		exit(0);
	}
	elsif ($arg eq "--quiet")
	{
		$quiet=1;
	}
	elsif ($arg eq "-o")
	{
		$printfile = 1;
		if (!($outfile = shift))
		{
			die "Can't open $outfile for writing";
		}
	}
	elsif ($arg =~ /^--files/)
	{
		$do_files=1;
	}
	elsif ($arg =~ /^--directories/)
	{
		$do_files=0;
	}
	elsif ($arg =~ /^--granularity=(\d+)/)
	{
		$granularity = $1;
	}
	elsif ($arg =~ /^--min=(\d+)/)
	{
		$min = $1;
	}
	elsif ($arg =~ /^--max=(\d+)/)
	{
		$max = $1;
	}
	else
	{
		print "Invalid option $arg\n";
		usage;
		exit(1);
	}
}

# Warn if using quiet mode, without writing to a file
if ($quiet && !$printfile)
{
	warn "Using quiet mode without an output file. Results will be lost!"
}

if ($printfile)
{
	open OUTFILE, ">$outfile" or die "Can't open $outfile for writing";
}

# Use test5a and test5b for write & read tests
for ($i = $min; $i <= $max; $i+= $granularity)
{
	# Create parameter list, depending on whether we're doing
	# files or directories
	if ($do_files)
	{
		$params = "1 $i 0";
	}
	else
	{
		$params = "1 0 $i";
	}

	# Do creation
	$result = runTest("test1 -t $params");
	printResult($result);

	# Do deletion
	$result = runTest("test2 -t $params");
	printResult($result);

	# If printing to a file, re-open it. This forces
	# a flush of the file, so if the test is ended prematurely,
	# the results won't be lost.
	if ($printfile)
	{
		open OUTFILE, ">>$outfile" or die "Can't open $outfile for writing";
	}
}
