Files
tyapi-server/scripts/test_cache.sh

79 lines
2.1 KiB
Bash
Raw Normal View History

2025-07-28 01:46:39 +08:00
#!/bin/bash
# 缓存测试脚本
# 使用方法: ./scripts/test_cache.sh [base_url]
# 默认base_url: http://localhost:8080
BASE_URL=${1:-http://localhost:8080}
API_BASE="$BASE_URL/api/cache-test"
echo "🧪 开始缓存系统测试..."
echo "📍 测试地址: $BASE_URL"
echo ""
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 测试函数
test_endpoint() {
local method=$1
local endpoint=$2
local description=$3
echo -n "🔍 测试 $description... "
if [ "$method" = "GET" ]; then
response=$(curl -s -w "%{http_code}" "$API_BASE$endpoint")
elif [ "$method" = "POST" ]; then
response=$(curl -s -w "%{http_code}" -X POST "$API_BASE$endpoint")
elif [ "$method" = "DELETE" ]; then
response=$(curl -s -w "%{http_code}" -X DELETE "$API_BASE$endpoint")
fi
# 提取HTTP状态码
http_code="${response: -3}"
# 提取响应体
body="${response%???}"
if [ "$http_code" = "200" ]; then
echo -e "${GREEN}✅ 成功${NC}"
echo " 响应: $body" | head -c 100
echo "..."
else
echo -e "${RED}❌ 失败 (HTTP $http_code)${NC}"
echo " 错误: $body"
fi
echo ""
}
# 1. 测试缓存统计
test_endpoint "GET" "/stats" "缓存统计"
# 2. 测试基础缓存操作
test_endpoint "GET" "/test" "基础缓存操作"
# 3. 测试缓存键查询
test_endpoint "GET" "/keys/gorm_cache:*" "缓存键查询"
# 4. 测试性能测试
test_endpoint "POST" "/performance" "缓存性能测试"
# 5. 测试表缓存调试
test_endpoint "GET" "/table/users" "用户表缓存调试"
echo "🎉 缓存测试完成!"
echo ""
echo "📋 测试结果说明:"
echo " - 如果所有测试都返回200状态码说明缓存系统正常工作"
echo " - 如果某些测试失败,请检查应用是否正在运行"
echo " - 查看应用日志获取更详细的调试信息"
echo ""
echo "🔧 手动测试命令:"
echo " curl $API_BASE/stats"
echo " curl $API_BASE/test"
echo " curl $API_BASE/keys/gorm_cache:*"
echo " curl -X POST $API_BASE/performance"
echo " curl $API_BASE/table/users"