#! /usr/bin/env perl
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

use CondorTest;

my $testname = 'Condor autogen scalability test';
my $datafile = autoscale_data_jobs_20_vanilla;
my $testfile = 'x_perf_autoscale.pl';
my $cmdfile = 'autoscale.cmd';
my $testname = 'condor scalability test';

CondorTest::verbose_system("rm -rf $datafile");
CondorTest::verbose_system("touch $datafile");

my $startjobs = 5;
my $incrementjobs = 5;
my $currenttestcount = $startjobs;
my $maxjobs = 20;
my $lastduration = 0; # this is our startjobs run
my %statshash = ();

ReadLastStats();

CondorTest::StartCondor(auto_param);

while( $currenttestcount <= $maxjobs)
{
	print "Test $currenttestcount -- & lastduration --$lastduration--";
	my $nostatus = CondorTest::verbose_system("$testfile $currenttestcount $incrementjobs $lastduration $datafile $cmdfile $testname");
	ReadLastStats();
	$lastduration = $statshash{"duration"};
	$currenttestcount += $incrementjobs;
}

if($nostatus != 0)
{
	print "scaling test failed at $currenttestcount\n";
}

sub ReadLastStats
{
	my $line = "";

	my $count = 0;
	my $lenght = 0;
	my $unittime = 0;

	open(DATAOUT,"<$datafile") || die "Can not open existing data file: $!\n";
	while(<DATAOUT>)
	{
		chomp;
		$line = $_;
		if( $line =~ /^\s*Scaling\((\d+)\)\s+Duration\((\d+)\)\s+UnitTime\((\d*\.*\d*)\).*/ )
		{
			print "$line highlights scaling  $1 duration $2 UnitTime $3\n";
			$statshash{"scaling"} = $1;
			$statshash{"duration"} = $2;
			$statshash{"unittime"} = $3;
		}
		else
		{
			print "Mismatch $line\n";
		}
	}
}

