# 风七六本地生活服务平台 - 性能压测脚本

## 说明
这是一个简单的性能压测脚本集合，用于测试平台核心API的性能。

## 环境要求
- Windows PowerShell
- curl 命令行工具

## 测试前准备

### 1. 检查服务健康状态
```powershell
podman ps --format "table {{.Names}}\t{{.Status}}"
```

### 2. 验证API端点可达
```powershell
# 用户服务
curl -I http://localhost:8081/actuator/health

# 订单服务
curl -I http://localhost:8084/actuator/health
```

---

## 压测脚本

### 1. 健康检查压测脚本

```powershell
# 健康检查压测脚本
$url = "http://localhost:8084/actuator/health"
$iterations = 100
$startTime = Get-Date

Write-Host "开始健康检查压测..."
Write-Host "URL: $url"
Write-Host "迭代次数: $iterations"
Write-Host ""

$successCount = 0
$failCount = 0
$totalTime = 0
$minTime = [double]::MaxValue
$maxTime = 0

for ($i = 1; $i -le $iterations; $i++) {
    $reqStartTime = Get-Date
    
    try {
        $response = Invoke-WebRequest -Uri $url -Method Get -TimeoutSec 5
        $successCount++
        $statusCode = $response.StatusCode
    }
    catch {
        $failCount++
        $statusCode = $_.Exception.Response.StatusCode.value__
    }
    
    $reqEndTime = Get-Date
    $duration = ($reqEndTime - $reqStartTime).TotalMilliseconds
    $totalTime += $duration
    
    if ($duration -lt $minTime) { $minTime = $duration }
    if ($duration -gt $maxTime) { $maxTime = $duration }
    
    if ($i % 10 -eq 0) {
        Write-Host "已完成 $i/$iterations 次请求..."
    }
}

$endTime = Get-Date
$totalDuration = ($endTime - $startTime).TotalSeconds
$avgTime = if ($successCount -gt 0) { $totalTime / $successCount } else { 0 }
$qps = $iterations / $totalDuration

Write-Host ""
Write-Host "========== 健康检查压测结果 =========="
Write-Host "总请求数: $iterations"
Write-Host "成功: $successCount"
Write-Host "失败: $failCount"
Write-Host "成功率: $([math]::Round(($successCount / $iterations) * 100, 2))%"
Write-Host "总耗时: $([math]::Round($totalDuration, 2)) 秒"
Write-Host "QPS: $([math]::Round($qps, 2))"
Write-Host "平均响应时间: $([math]::Round($avgTime, 2)) ms"
Write-Host "最小响应时间: $([math]::Round($minTime, 2)) ms"
Write-Host "最大响应时间: $([math]::Round($maxTime, 2)) ms"
Write-Host "======================================"
```

### 2. 多API混合压测脚本

```powershell
# 多API混合压测脚本
$apis = @(
    @{ Name = "用户服务健康检查"; Url = "http://localhost:8081/actuator/health" },
    @{ Name = "认证服务健康检查"; Url = "http://localhost:8082/actuator/health" },
    @{ Name = "商家服务健康检查"; Url = "http://localhost:8083/actuator/health" },
    @{ Name = "订单服务健康检查"; Url = "http://localhost:8084/actuator/health" },
    @{ Name = "骑手服务健康检查"; Url = "http://localhost:8085/actuator/health" }
)

$iterationsPerApi = 20
$results = @()

Write-Host "开始多API混合压测..."
Write-Host ""

foreach ($api in $apis) {
    Write-Host "测试: $($api.Name)"
    
    $startTime = Get-Date
    $successCount = 0
    $failCount = 0
    $totalTime = 0
    $minTime = [double]::MaxValue
    $maxTime = 0
    
    for ($i = 1; $i -le $iterationsPerApi; $i++) {
        $reqStartTime = Get-Date
        
        try {
            $response = Invoke-WebRequest -Uri $api.Url -Method Get -TimeoutSec 5
            $successCount++
        }
        catch {
            $failCount++
        }
        
        $reqEndTime = Get-Date
        $duration = ($reqEndTime - $reqStartTime).TotalMilliseconds
        $totalTime += $duration
        
        if ($duration -lt $minTime) { $minTime = $duration }
        if ($duration -gt $maxTime) { $maxTime = $duration }
    }
    
    $endTime = Get-Date
    $totalDuration = ($endTime - $startTime).TotalSeconds
    $avgTime = if ($successCount -gt 0) { $totalTime / $successCount } else { 0 }
    $qps = $iterationsPerApi / $totalDuration
    
    $result = [PSCustomObject]@{
        "API名称" = $api.Name
        "总请求数" = $iterationsPerApi
        "成功" = $successCount
        "失败" = $failCount
        "成功率(%)" = if ($iterationsPerApi -gt 0) { [math]::Round(($successCount / $iterationsPerApi) * 100, 2) } else { 0 }
        "总耗时(秒)" = [math]::Round($totalDuration, 2)
        "QPS" = [math]::Round($qps, 2)
        "平均响应时间(ms)" = [math]::Round($avgTime, 2)
        "最小响应时间(ms)" = [math]::Round($minTime, 2)
        "最大响应时间(ms)" = [math]::Round($maxTime, 2)
    }
    
    $results += $result
    Write-Host "完成 $($api.Name)"
    Write-Host ""
}

Write-Host "========== 多API混合压测结果 =========="
$results | Format-Table -AutoSize
Write-Host "======================================"
```

---

## 使用方法

### 保存脚本
将上述脚本保存为 `performance-test.ps1`

### 运行脚本
```powershell
# 运行健康检查压测
.\performance-test.ps1 -HealthCheck

# 运行多API混合压测
.\performance-test.ps1 -Mixed
```

### 结果分析
- **QPS**: 每秒请求数，越高越好
- **响应时间**: 平均、最小、最大响应时间，越低越好
- **成功率**: 应该接近 100%

---

## 压测场景建议

### 1. 基础性能测试（当前）
- 单个API，100次请求
- 验证基础性能指标

### 2. 压力测试
- 单个API，1000次请求
- 验证系统在高负载下的表现

### 3. 混合场景测试
- 多个API同时请求
- 模拟真实业务场景

### 4. 长时间稳定性测试
- 持续运行30分钟
- 验证系统稳定性和内存泄漏

---

## 性能预期指标

| 指标 | 目标值 | 说明 |
|------|--------|------|
| QPS | > 50 | 每秒至少处理50个请求 |
| 平均响应时间 | < 200ms | 健康检查API |
| 最大响应时间 | < 500ms | 99%请求在500ms内完成 |
| 成功率 | > 99% | 至少99%请求成功 |

---

## 注意事项

1. **网络环境**: 确保网络稳定
2. **系统资源**: 监控 CPU、内存、磁盘使用
3. **并发控制**: 逐步增加压力，不要一次性过高
4. **日志监控**: 压测时监控服务日志
5. **数据清理**: 压测数据可能需要清理
