#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test performance/timing of job submissions.
#
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2014 SchedMD LLC
# Written by Morris Jette <jette@schedmd.com>
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set test_id    "17.35"
set file_in    "test$test_id\.in"
set exit_code  0
set job_cnt    100
set target     200

if {[test_cray]} {
	set target 65
}

print_header $test_id

make_bash_script $file_in "
inx=0
while \[ \$inx -lt $job_cnt \]
do
	$sbatch --begin=midnight --job-name=$file_in --wrap date &
	inx=\$((inx+1))
done
wait
"

set match 0
set time_used  100000
set timeout [expr $max_job_delay * 10]
spawn time -p ./$file_in
expect {
	-re "Submitted batch job ($number)" {
		incr match
		exp_continue
	}
	-re "real *($number).($digit)($digit)" {
		set secs       $expect_out(1,string)
		set tenths     $expect_out(2,string)
		set hundredths $expect_out(3,string)
		set time_used [expr $secs * 1000 + $tenths * 100 + $hundredths * 10]
		exp_continue
	}
	-re "real *($number).($digit)" {
		set secs       $expect_out(1,string)
		set tenths     $expect_out(2,string)
		set time_used [expr $secs * 1000 + $tenths * 100]
		exp_continue
	}
	-re "real *($number)" {
		set secs       $expect_out(1,string)
		set time_used [expr $secs * 1000]
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$match != $job_cnt} {
	send_user "FAILURE: job count mismatch ($match != $job_cnt)\n"
	set exit_code 1
}

spawn $scancel --name=$file_in
expect {
	timeout {
		send_user "\nFAILURE: scancel is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

# Prevent zero divide
if {$time_used == 0} {
	set time_used 1
}
set rate [ expr ($job_cnt * 1000) / $time_used ]
send_user "Job submit rate $rate jobs per second ($job_cnt jobs in $time_used msec)\n"
if {$rate < $target} {
	send_user "FAILURE: Job submit rate of $rate below target of $target\n"
	send_user "FAILURE: This may be due to use of NFS rather than local storage for logs or state information\n"
	set exit_code 1
}

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
	exec $bin_rm -f $file_in
}
exit $exit_code
