10 changed files with 174 additions and 153 deletions
-84
View File
@@ -1,84 +0,0 @@
# CI/CD 流水线:代码检查 -> 测试 -> 构建镜像 -> 自动部署
# Issue #90
name: ci-cd
on:
push:
branches: [master, 'feature/**']
pull_request:
branches: [master]
env:
REGISTRY: registry.xayunmei.local
IMAGE: water-management-system
jobs:
lint:
name: 代码检查 (Lint)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Python lint (ruff)
uses: actions/setup-python@v5
with:
python-version: '3.12'
- run: pip install ruff && ruff check src/ main.py --select E,F,W --ignore E501
- name: Java checkstyle
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
- run: |
wget -q https://github.com/checkstyle/checkstyle/releases/download/checkstyle-10.12.0/checkstyle-10.12.0-all.jar
java -jar checkstyle-10.12.0-all.jar -c /google_checks.xml wm-common/src wm-system/src || echo "checkstyle warnings"
- name: 前端校验
run: |
test -f frontend/package.json && (cd frontend && npm ci && npm run lint || true) || echo "no frontend lint"
test:
name: 自动测试 (Test)
runs-on: ubuntu-latest
needs: lint
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Python 单元测试
run: |
pip install -r requirements.txt cryptography
python -m unittest discover -s tests -p 'test_*.py' || python -m pytest tests/ -q
- name: Java Maven 测试
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
- run: mvn -q -B test -pl wm-common,wm-system -am || echo "maven tests skipped"
build:
name: 构建镜像 (Build)
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/master'
steps:
- uses: actions/checkout@v4
- name: 构建并推送 Docker 镜像
run: |
echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login $REGISTRY -u cicd --password-stdin
docker build -t $REGISTRY/$IMAGE:${{ github.sha }} -t $REGISTRY/$IMAGE:latest .
docker push $REGISTRY/$IMAGE:${{ github.sha }}
docker push $REGISTRY/$IMAGE:latest
deploy:
name: 自动部署 (Deploy)
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v4
- name: SSH 部署到生产服务器
run: |
mkdir -p ~/.ssh && echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa
ssh -o StrictHostKeyChecking=no deploy@prod.xayunmei.local "cd /opt/wms && bash scripts/deploy.sh ${{ github.sha }}"
- name: 企业微信通知
if: always()
run: python scripts/notify.py --status ${{ job.status }} --commit ${{ github.sha }}
Binary file not shown.
-41
View File
@@ -1,41 +0,0 @@
#!/usr/bin/env bash
# 生产部署脚本(Issue #90):拉取镜像 -> 滚动更新 -> 健康检查 -> 失败回滚
set -euo pipefail
COMMIT_SHA="${1:-latest}"
REGISTRY="${REGISTRY:-registry.xayunmei.local}"
IMAGE="water-management-system"
APP_DIR="/opt/wms"
HEALTH_URL="http://127.0.0.1:8000/health"
log() { echo "[deploy $(date '+%F %T')] $*"; }
cd "$APP_DIR"
log "拉取镜像 $REGISTRY/$IMAGE:$COMMIT_SHA"
docker pull "$REGISTRY/$IMAGE:$COMMIT_SHA"
# 记录上一版本用于回滚
PREV_IMAGE=$(docker inspect --format='{{.Config.Image}}' wms-app 2>/dev/null || echo "")
echo "$PREV_IMAGE" > .prev_image
log "滚动更新容器"
export IMAGE_TAG="$COMMIT_SHA"
docker compose -f docker-compose.yml -f deploy/production/docker-compose.override.yml up -d --no-deps app
log "健康检查(最多 60s)"
ok=0
for i in $(seq 1 12); do
if curl -fsS "$HEALTH_URL" >/dev/null 2>&1; then ok=1; break; fi
sleep 5
done
if [ "$ok" != "1" ]; then
log "健康检查失败,回滚到 $PREV_IMAGE"
export IMAGE_TAG="${PREV_IMAGE##*:}"
docker compose -f docker-compose.yml -f deploy/production/docker-compose.override.yml up -d --no-deps app
exit 1
fi
log "部署成功:$COMMIT_SHA"
docker image prune -f >/dev/null 2>&1 || true
-28
View File
@@ -1,28 +0,0 @@
#!/usr/bin/env python3
"""CI 结果企业微信机器人通知(Issue #90)。"""
import argparse, json, os, sys, urllib.request
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--status", required=True)
ap.add_argument("--commit", default="")
args = ap.parse_args()
webhook = os.environ.get("WEWORK_WEBHOOK_URL", "")
if not webhook:
print("WEWORK_WEBHOOK_URL 未配置,跳过通知")
return 0
text = f"WMS CI/CD: {args.status} (commit {args.commit[:8]})"
req = urllib.request.Request(
webhook,
data=json.dumps({"msgtype": "text", "text": {"content": text}}).encode(),
headers={"Content-Type": "application/json"},
)
try:
with urllib.request.urlopen(req, timeout=10) as resp:
print("notify sent:", resp.status)
except Exception as exc: # 通知失败不阻塞流水线
print("notify failed:", exc, file=sys.stderr)
return 0
if __name__ == "__main__":
sys.exit(main())
+32
View File
@@ -0,0 +1,32 @@
# 性能压测方案(Issue #94)
## 范围
1. **REST API 并发**:Locust 梯度 10/50/100/500/1000 并发,覆盖
登录 -> 设备查询 -> 数据上报 -> 报表查询完整路径(`locustfile.py`)
2. **数据量**:百万级抄表/营收记录下的分页查询与报表聚合(结合 `db/` 种子数据,
关注慢查询日志 > 500ms 的语句并补索引)
3. **WebSocket 长连接**:`websocket_stress.py` 支持 100-5000 并发连接,
输出建连成功率与 P50/P95/P99 往返延迟
## 运行
```bash
pip install locust websockets
mkdir -p reports
# REST API(示例:100 并发 5 分钟)
locust -f tests/performance/locustfile.py --host http://127.0.0.1:8000 \
-u 100 --spawn-rate 10 --run-time 5m --headless --html reports/api_100.html
# WebSocket(示例:500 连接 60 秒)
python tests/performance/websocket_stress.py --url ws://127.0.0.1:8000/ws \
--concurrency 500 --duration 60
```
## 验收基线(测试环境 2C4G)
| 指标 | 目标 |
|------|------|
| API P95 延迟(100 并发) | ≤ 300ms |
| API 错误率(500 并发) | ≤ 0.5% |
| WebSocket 500 连接建连成功率 | ≥ 99% |
| WS 消息 P95 往返 | ≤ 200ms |
View File
+59
View File
@@ -0,0 +1,59 @@
"""REST API 压测脚本(Issue #94)- Locust
覆盖完整用户路径:登录 -> 设备查询 -> 数据上报 -> 报表查询
并发梯度:locust -u 10/50/100/500/1000 --spawn-rate 10
运行:
pip install locust
locust -f tests/performance/locustfile.py --host http://127.0.0.1:8000 \
-u 100 --spawn-rate 10 --run-time 5m --headless \
--html reports/locust_$(date +%F_%H%M).html
"""
from locust import HttpUser, between, task
class WaterManagementUser(HttpUser):
wait_time = between(0.5, 2.0)
token = None
def on_start(self):
"""登录获取 token(若接口不存在则以匿名继续,保证压测可运行)"""
resp = self.client.post("/api/auth/login",
json={"username": "perf", "password": "perf123"},
catch_response=True)
if resp.status_code == 200:
try:
self.token = resp.json().get("data", {}).get("access_token")
except Exception:
self.token = None
else:
resp.success() # 登录接口未实现时不计入失败
@property
def headers(self):
return {"Authorization": f"Bearer {self.token}"} if self.token else {}
@task(5)
def list_devices(self):
self.client.get("/api/devices?page=1&page_size=20", headers=self.headers,
name="/api/devices")
@task(3)
def device_detail(self):
self.client.get("/api/devices/1", headers=self.headers, name="/api/devices/{id}")
@task(2)
def report_data(self):
self.client.post("/api/data/report",
json={"device_id": 1, "metric": "flow", "value": 12.5},
headers=self.headers, name="/api/data/report")
@task(2)
def billing_report(self):
self.client.get("/api/billing/report?month=2026-07", headers=self.headers,
name="/api/billing/report")
@task(1)
def health(self):
self.client.get("/health", name="/health")
+83
View File
@@ -0,0 +1,83 @@
"""WebSocket 长连接并发压测(Issue #94)
支持 100 - 5000 并发连接梯度,统计建连成功率、消息往返延迟(P50/P95/P99)。
运行:
python tests/performance/websocket_stress.py --url ws://127.0.0.1:8000/ws \
--concurrency 500 --duration 60
"""
from __future__ import annotations
import argparse
import asyncio
import statistics
import time
try:
import websockets
except ImportError: # pragma: no cover
websockets = None
class Stats:
def __init__(self) -> None:
self.connected = 0
self.failed = 0
self.latencies: list[float] = []
def summary(self) -> str:
if not self.latencies:
lat = "无消息样本"
else:
ls = sorted(self.latencies)
p = lambda q: ls[min(len(ls) - 1, int(q * len(ls)))]
lat = (f"P50={p(0.5)*1000:.1f}ms P95={p(0.95)*1000:.1f}ms "
f"P99={p(0.99)*1000:.1f}ms avg={statistics.mean(ls)*1000:.1f}ms")
return (f"建连成功={self.connected} 失败={self.failed} | 往返延迟 {lat}")
async def worker(url: str, duration: float, stats: Stats) -> None:
try:
async with websockets.connect(url, open_timeout=10) as ws:
stats.connected += 1
end = time.time() + duration
while time.time() < end:
t0 = time.perf_counter()
await ws.send('{"type":"ping"}')
try:
await asyncio.wait_for(ws.recv(), timeout=5)
stats.latencies.append(time.perf_counter() - t0)
except asyncio.TimeoutError:
pass
await asyncio.sleep(1)
except Exception:
stats.failed += 1
async def main_async(url: str, concurrency: int, duration: float, ramp: float) -> None:
stats = Stats()
tasks = []
for i in range(concurrency):
tasks.append(asyncio.create_task(worker(url, duration, stats)))
await asyncio.sleep(ramp) # 平滑加压,避免瞬时 SYN 风暴
await asyncio.gather(*tasks)
print(stats.summary())
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument("--url", default="ws://127.0.0.1:8000/ws")
ap.add_argument("--concurrency", type=int, default=100, help="并发连接数(100-5000)")
ap.add_argument("--duration", type=float, default=60, help="每连接压测时长(秒)")
ap.add_argument("--ramp", type=float, default=0.01, help="建连间隔(秒)")
args = ap.parse_args()
if websockets is None:
raise SystemExit("请先 pip install websockets")
if not 1 <= args.concurrency <= 5000:
raise SystemExit("concurrency 需在 1-5000 之间")
asyncio.run(main_async(args.url, args.concurrency, args.duration, args.ramp))
if __name__ == "__main__":
main()