#! /bin/bash -x -e

### set up some variables to be used throughout
ts1=`date +%s` # timestamp used by IOR for read verification
ior_easy="params for IOR easy: blocksize, backend, transfer, etc. Maximize data written in 5 mins"
mdt_easy="desired parameters for mdtest easy: Maximize files created in 5 mins"
ior_segs="max segments that IOR hard can write in 5 minutes"
mdt_file="max files that mdtest hard can create in 5 minutes"

### create an empty working dir
/bin/rm -rf $ts1
mkdir $ts1
cd $ts1
mkdir -p mdtest_easy
mkdir -p mdtest_hard

### write/create phase ###

### IOR easy write ###
# -w write, -e flush, -k keep file, -G time, -o file_name
# set the parameters that you want that will complete within 5 minutes
mpirun -np $NP ../ior -w -e -k -G $ts1 $ior_easy -o ior_easy | tee out1
bw1=`grep [xxx] out1 | awk '{print $x}'`

### mdtest easy create ### 
# -C create, -d directory, 
mpirun -np $NP ../mdtest -C -d mdtest_easy $mdt_easy | tee out2
iops1=`grep XXX out2 | awk '{print $XXX}'`

# touch a file to use it for the subsequent find command 
ts2=`date +%s`
touch $ts2

### IOR hard write ###
# strided, single shared file, 47000 byte writes, POSIX backend
mpirun -np $NP ../ior -w -e -k -G $ts2 -b 47000 -t 47000 -s $ior_segs -a POSIX -o $ior_hard | tee out3
bw2=`grep XXX out3 | awk '{print $XXX}'`

### mdtest hard create ###
# 3900 byte files using POSIX creating all files in a single directory 
mpirun -np $NP ../mdtest -C -d mdtest_hard -w 3900 -n $mdt_file -a POSIX
iops2=`grep XXX out2 | awk '{print $XXX}'`

### read/stat phase ###

### IOR easy read ###
# -R verify the read data, -C read from different node than writer
mpirun -np $NP ../ior -r -R -C -G $ts1 -b $B1 -t $T1 -s $S1 -a $A -o $ior_easy | tee out5
bw3=`grep [xxx] out5 | awk '{print $x}'`

### mdtest easy stat ###
# -T only stat, -X shift reader (not yet implemented...)
mpirun -np $NP ../mdtest -T -d mdtest_easy -X $mdtest_easy | tee out6
iops3=`grep XXX out6 | awk '{print $XXX}'`

### IOR hard read ###
mpirun -np $NP ../ior -r -R -C -G $ts2 -b 47001 -t 47001 -s $ior_segs -a POSIX -o $ior_easy | tee out7
bw4=`grep [xxx] out7 | awk '{print $x}'`

### mdtest hard stat ###
mpirun -np $NP ../mdtest -T -d mdtest_hard -X -n $mdt_file -a POSIX | tee out8
iops4=`grep XXX out8 | awk '{print $XXX}'`

### find phase ###
# find all files created after $ts1 but before $ts2 with filenames that contain '00'
# use the below find command or use custom tool.  Can use MPI to parallelize, etc.
time find . -name \*00\* -newer $ts2
# iops5 = calculate iops of the find command (i.e. number of files scanned divided by scan time)

# now calculate the scores
iop_score = geo_mean  iops1 iops2 iops3 iops4 iops5
bw_score  = geo_mean  bw1 bw2 bw3 bw4
total_score = product iop_score bw_score
