Skip to content

Day 6:Docker部署+性能压测报告

今天做什么

编译部署 + 完整压测。看看 Go 写的网关到底多能扛。

编译

bash
go build -ldflags="-s -w" -o agent-gateway

# 只有 8MB
ls -lh agent-gateway

Dockerfile

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 延迟850ms180ms
内存350MB52MB
镜像大小450MB8MB
并发 1000 稳定性偶尔超时稳定

项目复盘

6 天,从零到生产级 AI Agent 网关:

Day做了什么Go 亮点
1反向代理 + 多模型路由httputil.ReverseProxy
2令牌桶限流 + 熔断器atomic.Int64 无锁状态
3请求队列 + goroutine 池chan 缓冲队列
4Prometheus + Tracing + Loggingslog 结构化日志
5配置中心 + fsnotify 热更新atomic.Value 原子配置
6Docker 部署 + 压测8MB 镜像,1667 QPS

这是 Go 在 AI 基础设施里最应该干的事:做网关、做中间件、做高并发的连接层。


项目三完成。三个项目,三种场景,Go 都做得比 Python 更快、更小、更稳定。