#!/bin/bash

#基本设置
jmeter_bin_path="/opt/test/jmeter/bin" #jmeter工具bin目录
url="192.168.251.158" #测试地址\${__StringFromFile(/opt/test/data/testurl.txt,url,,)}
protocol="http" #协议
port=80 #端口
user_data="/opt/test/data/user1000.csv" #测试账号存放目录
script_path="/opt/test/data"
result_path="/opt/test/result" #测试结果存储目录
report_path="/opt/test/report" #测试报告存放路径

#上传参数配置
folderId_upload=18 #文件上传的父文件夹ID
file_size=101587 #上传的文件大小，单位字节
file_type=".docx" #上传文件类型
upload_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document" #上传类型
file_path="/opt/test/data/100KB.docx" #本地文件路径


echo "测试模块请根据以下内容输入：
*************************************************
创建测试数据：createdata
登录：login
左侧树展开：LoadChildren
右侧文件列表展开：GetFolderChildren
新建文件夹：CreateFolder
文件上传：Upload
从模板新建文件：CreateFileFromTemplate
文件下载：Download
文件共享：SaveShare
文件外发：CreateFilePublish
权限记录加载：LoadFilePermission
获取团队列表：GetMyTeamChildren
文件预览：Preview
文件搜索：Search
VDrive列表：VDList
*************************************************"

read -p "输入测试模块：" module

while true
do

    if [ -z "${module}" ]; then
        read -p "测试模块不能为空，请重新输入：" module
    else
        break
    fi
done

read -p "输入并发数量,多个数量之间用“,”隔开，例如(100,200,300)：" thread_nums

while true
do
    thread_array=(${thread_nums//,/ })
    for check in ${thread_array[@]}
    do
        if [ -z "${check}" ]; then
            echo ${check}
            read -p "并发数量不能为空，请重新输入：" thread_nums
        fi
    done
    break
done

read -p "输入执行时间(单位秒，默认300)：" test_time
echo "****************************************"

if [ -z "${test_time}" ]; then
    test_time=300
fi

if [[ ${module} == "LoadChildren" ]]; then
    read -p "输入文件夹ID：" folderId_list
    read -p "输入子文件夹数量：" folder_count
elif [[ ${module} == "GetFolderChildren" ]]; then
    read -p "输入文件夹ID：" folderId_list
elif [[ ${module} == "CreateFolder" ]]; then
    read -p "输入文件夹ID：" CreateFolderFolderId
elif [[ ${module} == "CreateFileFromTemplate" ]]; then
    read -p "输入文件夹ID：" folderId_upload
    read -p "输入模板文件ID：" TemplateId
elif [[ ${module} == "Download" ]]; then
    read -p "输入下载的文件ID：" fileId
    read -p "输入下载的文件大小：" download_length
elif [[ ${module} == "SaveShare" ]]; then
    read -p "输入共享的文件ID：" fileId
elif [[ ${module} == "CreateFilePublish" ]]; then
    read -p "输入外发的文件ID：" fileId
elif [[ ${module} == "Preview" ]]; then
    read -p "注意！！！
本压测脚本所测试的预览为系统自带的PDFJS预览，暂不支持其他插件预览！
是否要继续(yes or no)：" cte

    while true
    do
        if [[ ${cte} == "yes" ]]; then
            break
        elif [[ ${cte} == "no" ]]; then
            exit_script(){
	        exit
            }
        else
            read -p "输入有误，请重新输入：(yes or no)：" cte
        fi
    done
    read -p "输入共享的文件ID：" fileId
    read -p "输入转档文件的大小：" preview_length
elif [[ ${module} == "LoadFilePermission" ]]; then
    read -p "输入要加载权限记录的文件ID：" fileId
elif [[ ${module} == "Search" ]]; then
    read -p "输入搜索范围(文件夹ID)：" search_folderId
    read -p "输入搜索关键字：" search_content
    read -p "输入搜索结果数量：" search_count
elif [[ ${module} == "VDList" ]]; then
    read -p "输入文件夹ID：" folderId_list
fi

modules=("createdata" "login" "LoadChildren" "GetFolderChildren" "CreateFolder" "Upload" "CreateFileFromTemplate" "Download" "SaveShare" "CreateFilePublish" "LoadFilePermission" "GetMyTeamChildren" "Preview" "Search" "VDList")
while true
do
    for x in ${modules[@]}
    do
        if [[ ${x} == ${module} ]]; then
            echo "测试模块：${module}，并发数量：${thread_nums}，执行时间：${test_time}秒"
            break 2
        fi
    done
    read -p "测试模块输入有误，请重新输入：" module
done
echo "***************************************"

cd ${jmeter_bin_path}

i=0
for thread_num in ${thread_array[@]}
do
echo "测试开始时间：`date '+%Y-%m-%d %H:%M:%S'`"

if [[ ${module} == "createdata" ]]; then
    ./jmeter -Jurl=${url} -Jprotocol=${protocol} -Jport=${port} -Jfile_path=${file_path} -n -t ${script_path}/createdata.jmx -l ${result_path}/createdata.jtl -e -o ${report_path}/createdata
elif [[ ${module} == "login" || ${module} == "GetMyTeamChildren" ]]; then
    ./jmeter -JthreadCount=${thread_num} -Jduration=${test_time} -Jurl=${url} -Jprotocol=${protocol} -Jport=${port} -Juser_data=${user_data} -Jmodule=${module} -n -t ${script_path}/IntegratedScript.jmx -l ${result_path}/${module}_${thread_num}并发.jtl -e -o ${report_path}/${module}_${thread_num}并发
elif [[ ${module} == "LoadChildren" || ${module} == "GetFolderChildren" || ${module} == "VDList" ]]; then
    ./jmeter -JthreadCount=${thread_num} -Jduration=${test_time} -Jurl=${url} -Jprotocol=${protocol} -Jport=${port} -Juser_data=${user_data} -Jmodule=${module} -JfolderId_list=${JfolderId_list} -Jfolder_count=${folder_count} -n -t ${script_path}/IntegratedScript.jmx -l ${result_path}/${module}_${thread_num}并发.jtl -e -o ${report_path}/${module}_${thread_num}并发
elif [[ ${module} == "CreateFolder" ]]; then
    ./jmeter -JthreadCount=${thread_num} -Jduration=${test_time} -Jurl=${url} -Jprotocol=${protocol} -Jport=${port} -Juser_data=${user_data} -Jmodule=${module} -JCreateFolderFolderId=${CreateFolderFolderId} -n -t ${script_path}/IntegratedScript.jmx -l ${result_path}/${module}_${thread_num}并发.jtl -e -o ${report_path}/${module}_${thread_num}并发
elif [[ ${module} == "Upload" ]]; then
    ./jmeter -JthreadCount=${thread_num} -Jduration=${test_time} -Jurl=${url} -Jprotocol=${protocol} -Jport=${port} -Juser_data=${user_data} -Jmodule=${module} -JfolderId_upload=${folderId_upload} -Jfile_size=${file_size} -Jfile_type=${file_type} -Jupload_type=${upload_type} -Jfile_path=${file_path} -n -t ${script_path}/IntegratedScript.jmx -l ${result_path}/${module}_${thread_num}并发.jtl -e -o ${report_path}/${module}_${thread_num}并发
elif [[ ${module} == "CreateFileFromTemplate" ]]; then
    ./jmeter -JthreadCount=${thread_num} -Jduration=${test_time} -Jurl=${url} -Jprotocol=${protocol} -Jport=${port} -Juser_data=${user_data} -Jmodule=${module} -JTemplateId=${TemplateId} -JfolderId_upload=${folderId_upload} -n -t ../../data/IntegratedScript.jmx -l ${result_path}/${module}_${thread_num}并发.jtl -e -o ${report_path}/${module}_${thread_num}并发
elif [[ ${module} == "SaveShare" || ${module} == "CreateFilePublish" || ${module} == "Preview" || ${module} == "LoadFilePermission" ]]; then
    ./jmeter -JthreadCount=${thread_num} -Jduration=${test_time} -Jurl=${url} -Jprotocol=${protocol} -Jport=${port} -Juser_data=${user_data} -Jmodule=${module} -JfileId=${fileId} -Jpreview_length=${preview_length} -n -t ${script_path}/IntegratedScript.jmx -l ${result_path}/${module}_${thread_num}并发.jtl -e -o ${report_path}/${module}_${thread_num}并发
elif [[ ${module} == "Download" ]]; then
    ./jmeter -JthreadCount=${thread_num} -Jduration=${test_time} -Jurl=${url} -Jprotocol=${protocol} -Jport=${port} -Juser_data=${user_data} -JfileId=${fileId} -Jdownload_length=${download_length} -n -t ${script_path}/download.jmx -l ${result_path}/${module}_${thread_num}并发.jtl -e -o ${report_path}/${module}_${thread_num}并发
elif [[ ${module} == "Search" ]]; then
    ./jmeter -JthreadCount=${thread_num} -Jduration=${test_time} -Jurl=${url} -Jprotocol=${protocol} -Jport=${port} -Juser_data=${user_data} -Jmodule=${module} -Jsearch_folderId=${search_folderId} -Jsearch_content=${search_content} -Jsearch_count=${search_count} -n -t ${script_path}/IntegratedScript.jmx -l ${result_path}/${module}_${thread_num}并发.jtl -e -o ${report_path}/${module}_${thread_num}并发
fi
echo "测试结束时间：`date '+%Y-%m-%d %H:%M:%S'`"

read -p "是否保存测试结果(yes or no)：" save_result

while true
do
    if [[ ${save_result} == "yes" ]]; then
        echo "测试结果保存成功！"
        break
    elif [[ ${save_result} == "no" ]]; then
        rm -f ${result_path}/${module}_${thread_num}并发.jtl
        rm -rf ${report_path}/${module}_${thread_num}并发
        echo "测试结果清理完成！"
        echo "*******************************************"
        break
    else
        read -p "输入有误，请重新输入：(yes or no)：" save_result
    fi
done
i=`expr $i + 1`
s=${#thread_array[*]}
if [ $i -lt $s ]; then
    echo "等待10秒钟，开始下一次并发...."
    echo "*********************************************"
    sleep 10
else
    echo "测试完成！"
fi
done
