Day 6:Docker部署+性能压测报告
今天做什么
编译部署 + 完整压测。看看 Go 写的网关到底多能扛。
编译
bash
go build -ldflags="-s -w" -o agent-gateway
# 只有 8MB
ls -lh agent-gatewayDockerfile
dockerfile
FROM scratch
COPY agent-gateway /agent-gateway
COPY gateway.json /gateway.json
EXPOSE 8080
ENTRYPOINT ["/agent-gateway", "-config", "/gateway.json"]bash
docker build -t agent-gateway .
# 镜像大小:8MB压测
bash
# 安装压测工具
go install github.com/rakyll/hey@latest
# 1000 并发,持续 30 秒
hey -n 50000 -c 1000 -t 30 \
-H "Content-Type: application/json" \
-m POST -d '{"messages":[{"role":"user","content":"hello"}]}' \
http://localhost:8080/v1/chat/completions性能报告
Summary:
Total: 50000 requests
Success: 49850 (99.7%)
Rate limited: 150 (0.3%)
QPS: 1667 req/s
P50 latency: 45ms
P99 latency: 180ms
Max latency: 520ms
Memory peak: 52MB
CPU: 35%
Goroutines: 120 (stable)和 Python 网关的对比
| 指标 | Python (FastAPI + uvicorn) | Go (net/http) |
|---|---|---|
| QPS | ~400 | ~1667 |
| P99 延迟 | 850ms | 180ms |
| 内存 | 350MB | 52MB |
| 镜像大小 | 450MB | 8MB |
| 并发 1000 稳定性 | 偶尔超时 | 稳定 |
项目复盘
6 天,从零到生产级 AI Agent 网关:
| Day | 做了什么 | Go 亮点 |
|---|---|---|
| 1 | 反向代理 + 多模型路由 | httputil.ReverseProxy |
| 2 | 令牌桶限流 + 熔断器 | atomic.Int64 无锁状态 |
| 3 | 请求队列 + goroutine 池 | chan 缓冲队列 |
| 4 | Prometheus + Tracing + Logging | slog 结构化日志 |
| 5 | 配置中心 + fsnotify 热更新 | atomic.Value 原子配置 |
| 6 | Docker 部署 + 压测 | 8MB 镜像,1667 QPS |
这是 Go 在 AI 基础设施里最应该干的事:做网关、做中间件、做高并发的连接层。
项目三完成。三个项目,三种场景,Go 都做得比 Python 更快、更小、更稳定。

