#!/usr/local/bin/bash

######################
### SET THESE VARS ###
######################

# Log location
log=/tmp/nfstest.log

# NFS share to mount
share=<nas>:/tank0/nfs/myshare

# Mount location
mnt_dir=/data

########################
### script continues ###
########################

data[0]=1024    # 1G
data[1]=2048    # 2G
data[2]=4096    # 4G

# Create data array
data_time=()
data_trans=()
data_sync=()
data_umount=()

# Declare starting point in log
echo "direction,iothreads,protocol,hardness,readahead,buffer,transfer size,transfer time,transfer speed" >> $log

# Start first with iothreads
for iothreads in {4..20}
do
    echo "$(date) - Using $iothreads iothreads"

    # Set iothreads
    doas sysctl vfs.nfs.iothreads=$iothreads >/dev/null 2>&1

    for proto in tcp udp
    do
        echo "$(date) - Using $proto"

        if [[ $proto == "tcp" ]]; then
            protocol='-T'
        else
            protocol=""
        fi
        for hard in hard soft
        do
            echo "$(date) - Using $hard"

            if [[ $hard == "soft" ]]; then
                hardness='-s'
            else
                hardness=""
            fi
            for readahead in {1..4}
            do
                echo "$(date) - Using $readahead readahead"

                for size in 16384 32768 65536 
                do
                    echo "$(date) - Using a $size r/w buffer"

                    # Unmount share
                    echo "$(date) - Remounting share"
                    doas umount $mnt_dir >/dev/null 2>&1
                    doas mount_nfs $protocol $hardness -a $readahead -r $size -w $size $share $mnt_dir

                    for e in 0 # 1 2
                    do
                        echo "$(date) - Using ${data[$e]}MB"

                        for direction in write read
                        do
                            # Transfer file
                            if [[ $direction == "write" ]]; then
                                echo -n "$(date) - Writing data: "
                                action=$(dd if=/dev/zero of=${mnt_dir}/zeros.file bs=1M count=${data[$e]} 2>&1) # Write while utilizing cache 

                                # echo transfer info
                                action_thr=$(echo $action | awk '{print $13}' | awk -F\( '{print $NF}')
                                action_thr=$(($action_thr / 125000 / 8))
                                action_time=$(echo $action | awk '{print $11}')
                                echo "$action_thr"

                            else
                                echo -n "$(date) - Reading data: "
                                action=$(dd if=${mnt_dir}/zeros.file of=/dev/null bs=1M 2>&1) 

                                # echo transfer info
                                action_thr=$(echo $action | awk '{print $13}' | awk -F\( '{print $NF}' )
                                action_thr=$(($action_thr / 125000 / 8))
                                action_time=$(echo $action | awk '{print $11}')
                                echo "$action_thr"
                            fi

                            data_time=$action_time                        
                            data_trans=$action_thr

                            echo "$(date) - syncing"
                            sync

                            # Save transfer info to log
                            echo "$direction,$iothreads,$proto,$hard,$readahead,$size,${data[$e]},$data_time,$data_trans" >> $log
                        done
                    done
                done
            done
        done
    done
done

