Files
ai/test.sh
2023-05-27 16:40:04 +08:00

100 lines
5.1 KiB
Bash
Executable File

#!/bin/bash
# 記錄開始時間戳
start_time=$(date +%s)
rm -f data/sqlite3.db
# 流程測試, 啓動服務, 設定進程名 go_test
go run main.go -procname go_test &
sleep 2
# 退出服務的函數
function exit_service() { pkill -f go_test && rm -f data/sqlite3.db && echo "退出服務: $1" && exit 1; }
# 創建用戶 (POST /api/users)
response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","password":"test","email":""}' -s -w "%{http_code}" http://localhost:8080/api/users)
[[ ${response: -3} -eq 200 ]] && { echo "創建用戶成功: ${response%???}"; } || exit_service "創建用戶失敗: ${response%???}"
## 獲取用戶列表 (GET /api/users)
#response=$(curl -X GET -H "Content-Type: application/json" -s -w "%{http_code}" http://localhost:8080/api/users)
#[[ ${response: -3} -eq 200 ]] && { echo "獲取用戶列表成功: ${response%???}"; } || exit_service "獲取用戶列表失敗: ${response%???}"
# 登錄 (POST /api/sessions)
response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","password":"test"}' -s -w "%{http_code}" http://localhost:8080/api/sessions)
[[ ${response: -3} -eq 200 ]] && { echo "登錄成功: ${response%???}"; } || exit_service "登錄失敗: ${response%???}"
# 不使用jq, 從一段json中取出id字段的值
session_id=$(echo "$response" | head -n -1 | grep -o '"id": "[^"]*' | cut -d '"' -f 4)
echo "session_id: $session_id"
# 創建數據集, 應當在cookie中攜帶session_id (POST /api/datasets)
response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","description":"test"}' -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/datasets)
[[ ${response: -3} -eq 200 ]] && { echo "創建數據集成功: ${response%???}"; } || exit_service "創建數據集失敗: ${response%???}"
# 取數據集id的值, 值爲 int
dataset_id=$(echo "${response%???}" | grep -o '"id": [0-9]*' | awk '{print $2}')
echo "dataset_id: $dataset_id"
## 獲取數據集列表 (GET /api/datasets)
#response=$(curl -X GET -H "Content-Type: application/json" -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/datasets)
#[[ ${response: -3} -eq 200 ]] && { echo "獲取數據集列表成功: ${response%???}"; } || exit_service "獲取數據集列表失敗: ${response%???}"
# 修改數據集, images 中增加 url (PATCH /api/datasets/:id)
response=$(curl -X PATCH -H "Content-Type: application/json" -d '{"images":["https://img.gameui.net/article-7258-1677745322000@1x456.webp","https://img.gameui.net/article-6477-1682109454000@1x456.webp"]}' -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/datasets/$dataset_id)
[[ ${response: -3} -eq 200 ]] && { echo "修改數據集成功: ${response%???}"; } || exit_service "修改數據集失敗: ${response%???}"
# 訓練模型 (POST /api/models)
response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","type":"lora","trigger_words":"miao~","base_model":"sd1.5","epochs":20,"description":"test","dataset_id":'$dataset_id'}' -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/models)
[[ ${response: -3} -eq 200 ]] && { echo "訓練模型任務已創建: ${response%???}"; } || exit_service "訓練模型任務創建失敗: ${response%???}"
# 取模型id的值, 值爲 int
model_id=$(echo "${response%???}" | grep -o '"id": [0-9]*' | awk '{print $2}')
echo "model_id: $model_id"
## 模型列表 (GET /api/models)
# response=$(curl -X GET -H "Content-Type: application/json" -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/models)
# [[ ${response: -3} -eq 200 ]] && { echo "獲取模型列表成功: ${response%???}"; } || exit_service "獲取模型列表失敗: ${response%???}"
## 獲取模型訓練進度 (GET /api/models/:id)
#response=$(curl -X GET -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/models/$model_id)
#[[ ${response: -3} -eq 200 ]] && { echo "獲取模型訓練進度成功: ${response%???}"; } || exit_service "獲取模型訓練進度失敗: ${response%???}"
## 循環獲取模型訓練進度, 直到訓練完成
#while true; do
# # 獲取模型訓練進度 (GET /api/models/:id)
# response=$(curl -X GET -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/models/$model_id)
# [[ ${response: -3} -eq 200 ]] && { echo "獲取模型訓練進度成功: ${response%???}"; } || exit_service "獲取模型訓練進度失敗: ${response%???}"
#
# # 取出進度字段的值, 值爲 int
# progress=$(echo "${response%???}" | grep -o '"progress": [0-9]*' | awk '{print $2}')
# echo "progress: $progress"
#
# # 如果進度爲 100, 訓練完成, 跳出循環
# [[ $progress -eq 100 ]] && { echo "訓練完成"; break; }
#
# # 休眠 5 秒
# sleep 5
#done
# 服務器列表
response=$(curl -X GET -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/servers)
[[ ${response: -3} -eq 200 ]] && { echo "獲取服務器列表成功: ${response%???}"; } || exit_service "獲取服務器列表失敗: ${response%???}"
sleep 10
exit_service "測試結束, 全部通過, 用費時間: $(($(date +%s) - $start_time))"