# 批量测试API文档 这些API用于快速生成大量模拟请求,用来测试日志系统、压力测试等场景。 ## 订单服务测试API ### 1. 生成一条随机订单 **URL:** ``` GET http://localhost:8080/api/test/order ``` **示例:** ```bash curl http://localhost:8080/api/test/order | jq . ``` **响应:** ```json { "code": 0, "msg": "success", "data": { "orderId": "ORD-A1B2C3D4", "userId": "alice-5234", "price": 234.56, "status": "CREATED", "description": "二手家电处理", "createdAt": 1707200445123 } } ``` --- ### 2. 批量生成订单 **URL:** ``` GET http://localhost:8080/api/test/order/batch?count=50 GET http://localhost:8080/api/test/order/batch (默认10个) ``` **参数:** - `count` (可选): 要生成的订单数量,默认10个 **示例:** ```bash # 生成10个订单 curl http://localhost:8080/api/test/order/batch | jq . # 生成50个订单 curl http://localhost:8080/api/test/order/batch?count=50 | jq . # 生成100个订单 curl http://localhost:8080/api/test/order/batch?count=100 | jq .data.stats ``` **响应:** ```json { "code": 0, "msg": "success", "data": { "total": 10, "success": 10, "error": 0, "duration": 1245, "orders": [ {...订单数据...} ] } } ``` --- ### 3. 场景测试 (创建→查询→支付→删除失败) **URL:** ``` GET http://localhost:8080/api/test/order/scenario?count=5 ``` **参数:** - `count` (可选): 测试轮数,默认5轮 **说明:** 每轮测试都会: 1. 创建一个订单 2. 查询订单详情 3. 标记订单为已支付 4. 尝试删除订单(预期失败,因为已支付) 这样可以在日志中看到完整的业务流程和错误处理。 **示例:** ```bash curl http://localhost:8080/api/test/order/scenario?count=3 | jq . ``` **响应:** ```json { "code": 0, "msg": "success", "data": { "scenario": "create -> query -> pay -> delete(fail)", "rounds": 3, "operations": 12, "duration": 687 } } ``` --- ### 4. 压力测试 **URL:** ``` GET http://localhost:8080/api/test/order/stress?duration=10 ``` **参数:** - `duration` (可选): 测试持续时间(秒),默认10秒 **说明:** 不间断地创建订单,持续指定时间,用来测试系统吞吐量和日志系统性能。 **示例:** ```bash # 10秒压力测试 curl http://localhost:8080/api/test/order/stress?duration=10 | jq . # 30秒压力测试 curl http://localhost:8080/api/test/order/stress?duration=30 | jq . ``` **响应:** ```json { "code": 0, "msg": "success", "data": { "duration": 10234, "requests": 547, "success": 547, "error": 0, "qps": "53.50" } } ``` --- ## 支付服务测试API ### 1. 生成一条随机支付 **URL:** ``` GET http://localhost:8080/api/test/payment ``` **示例:** ```bash curl http://localhost:8080/api/test/payment | jq . ``` **响应:** ```json { "code": 0, "msg": "success", "data": { "paymentId": "PAY-X1Y2Z3W4", "orderId": "ORD-123456", "amount": 234.56, "status": "SUCCESS", "paymentMethod": "WECHAT", "paidAt": 1707200500234 } } ``` --- ### 2. 批量生成支付 **URL:** ``` GET http://localhost:8080/api/test/payment/batch?count=50 ``` **参数:** - `count` (可选): 要生成的支付数量,默认10个 **示例:** ```bash curl http://localhost:8080/api/test/payment/batch?count=20 | jq . ``` **响应:** ```json { "code": 0, "msg": "success", "data": { "total": 20, "success": 14, "error": 6, "totalAmount": 3245.67, "duration": 2341, "payments": [...] } } ``` --- ### 3. 退款场景测试 **URL:** ``` GET http://localhost:8080/api/test/payment/refund?count=5 ``` **参数:** - `count` (可选): 测试轮数,默认5轮 **说明:** 每轮测试都会: 1. 创建一个支付 2. 尝试退款 用来测试退款失败的场景(10%失败率)。 **示例:** ```bash curl http://localhost:8080/api/test/payment/refund?count=10 | jq . ``` --- ### 4. 支付压力测试 **URL:** ``` GET http://localhost:8080/api/test/payment/stress?duration=10 ``` **示例:** ```bash curl http://localhost:8080/api/test/payment/stress?duration=15 | jq . ``` --- ## 使用场景 ### 场景1: 快速验证日志格式 ```bash # 生成一条订单,查看日志 curl http://localhost:8080/api/test/order # 查看后端Terminal的JSON日志输出 # 验证 traceId、uri_group、event_class、duration 等字段 ``` ### 场景2: 生成足够的日志用于测试 ```bash # 生成100条订单 curl http://localhost:8080/api/test/order/batch?count=100 # 生成50笔支付 curl http://localhost:8080/api/test/payment/batch?count=50 ``` ### 场景3: 测试日志系统的高吞吐处理能力 ```bash # 30秒的订单压力测试 curl http://localhost:8080/api/test/order/stress?duration=30 # 30秒的支付压力测试 curl http://localhost:8080/api/test/payment/stress?duration=30 # 查看日志系统是否能正常处理高频请求 # 观察 Duration 字段是否异常增大(说明日志处理成为瓶颈) ``` ### 场景4: 测试自动异常处理和日志记录 ```bash # 场景测试会失败删除已支付订单,产生异常日志 curl http://localhost:8080/api/test/order/scenario?count=10 # 支付服务有30%的失败率,可以看到error日志 curl http://localhost:8080/api/test/payment/batch?count=50 ``` ### 场景5: 批量脚本测试 ```bash #!/bin/bash # 连续运行多个测试 echo "=== 生成10个订单 ===" curl http://localhost:8080/api/test/order/batch?count=10 echo "" echo "=== 生成20笔支付 ===" curl http://localhost:8080/api/test/payment/batch?count=20 echo "" echo "=== 场景测试5轮 ===" curl http://localhost:8080/api/test/order/scenario?count=5 echo "" echo "=== 10秒压力测试 ===" curl http://localhost:8080/api/test/order/stress?duration=10 echo "" ``` 保存为 `batch-test.sh` 并运行: ```bash chmod +x batch-test.sh ./batch-test.sh ``` --- ## 日志观察 运行这些API后,在后端Terminal中观察JSON日志: ```json { "ts":"2024-02-06T10:30:45.123Z", "level":"INFO", "msg":"测试订单生成成功 orderId=ORD-A1B2C3D4", "event_class":"order", "status":"success", "uri_group":"/test/*", "duration":"145", "app":"shop-recycle-order-service" } ``` 关键观察: - ✅ `uri_group` 应该是 `/test/*` (规范化规则自动应用) - ✅ `event_class` 应该是 `order` 或 `payment` - ✅ `status` 应该是 `success` 或 `server_error` - ✅ `duration` 显示请求耗时 - ✅ 所有日志共享相同的 `traceId` (如果在同一请求中) --- ## PowerShell 一键测试脚本 ```powershell # run-batch-tests.ps1 Write-Host "=== 订单服务测试 ===" -ForegroundColor Cyan Write-Host "1. 生成10个订单" Invoke-WebRequest -Uri "http://localhost:8080/api/test/order/batch?count=10" | ConvertFrom-Json | ForEach-Object { $_.data | Format-Table total,success,error,duration -AutoSize } Start-Sleep -Seconds 2 Write-Host "2. 场景测试" Invoke-WebRequest -Uri "http://localhost:8080/api/test/order/scenario?count=5" | ConvertFrom-Json | ForEach-Object { $_.data | Format-Table scenario,rounds,duration -AutoSize } Start-Sleep -Seconds 2 Write-Host "" Write-Host "=== 支付服务测试 ===" -ForegroundColor Green Write-Host "3. 生成20笔支付" Invoke-WebRequest -Uri "http://localhost:8080/api/test/payment/batch?count=20" | ConvertFrom-Json | ForEach-Object { $_.data | Format-Table total,success,error,totalAmount,duration -AutoSize } Write-Host "" Write-Host "✅ 测试完成!" -ForegroundColor Green Write-Host "查看后端Terminal的JSON日志输出" ``` --- **所有这些API都会生成完整的结构化JSON日志,便于验证日志系统的正确性。**