#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test performance/timing of job submissions.
############################################################################
# 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 file_in    "test$test_id\.in"
set exit_code  0
set job_cnt    100
set target     200

proc cleanup {} {
	global bin_rm file_in

	exec $bin_rm -f $file_in
}

if {[get_config_param "SwitchType"] eq "switch/cray"} {
	set target 65
}

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 $bin_bash -c "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 {
		fail "sbatch is not responding"
	}
	eof {
		wait
	}
}
if {$match != $job_cnt} {
	log_error "Job count mismatch ($match != $job_cnt)"
	set exit_code 1
}

spawn $scancel --name=$file_in
expect {
	timeout {
		fail "scancel is not responding"
	}
	eof {
		wait
	}
}

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

if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
