topfans/backend/scripts/loadgen/seed
2026-06-15 15:28:25 +08:00
..
assets.go feat:添加压测工具脚本 2026-06-15 15:28:25 +08:00
cleanup.go feat:添加压测工具脚本 2026-06-15 15:28:25 +08:00
friendships.go feat:添加压测工具脚本 2026-06-15 15:28:25 +08:00
loadtest_bcrypt.txt feat:添加压测工具脚本 2026-06-15 15:28:25 +08:00
main.go
profiles.go feat:添加压测工具脚本 2026-06-15 15:28:25 +08:00
README.md feat:添加压测工具脚本 2026-06-15 15:28:25 +08:00
seed_test.go feat:添加压测工具脚本 2026-06-15 15:28:25 +08:00
sequences.go feat:添加压测工具脚本 2026-06-15 15:28:25 +08:00
slots_and_exhibits.go feat:添加压测工具脚本 2026-06-15 15:28:25 +08:00
stars.go feat(seed): insert loadtest star (star_id=999900) 2026-06-15 15:28:25 +08:00
tokens.go feat:添加压测工具脚本 2026-06-15 15:28:25 +08:00
users.go feat:添加压测工具脚本 2026-06-15 15:28:25 +08:00

seed - 压测数据准备工具

用途

在 prod 本地插入 1000 个测试用户、5000 资产、3000 booth_slots、2000 exhibitions、10000 friendships,签 1000 个 JWT,写 users.csv

编译

cd backend && go build -o seed ./scripts/loadgen/seed/

在 prod 上跑

# 1. 上传二进制
scp seed root@101.132.250.62:/opt/topfans/loadtest/

# 2. SSH 上去跑
ssh root@101.132.250.62
cd /opt/topfans/loadtest
export DB_PASSWORD=$(cat /opt/topfans/docker/.env.prod | grep DB_PASSWORD | cut -d= -f2)
export JWT_SECRET=$(cat /opt/topfans/docker/.env.prod | grep JWT_SECRET | cut -d= -f2)
./seed --db-name=topfans --jwt-secret="$JWT_SECRET"

清理

# 保留 1000 users + 资产(下次复用)
./seed --cleanup

# 全删(包括账号本身)
./seed --cleanup --full

# 只重签 token(第二轮压测 JWT 过期时)
./seed --reset-tokens --jwt-secret="$JWT_SECRET"

本地 docker 联调(开发阶段)

cd backend
go build -o bin/seed ./scripts/loadgen/seed/
DB_PASSWORD=postgres123 JWT_SECRET=topfans-secret-key-local-dev-only \
  ./bin/seed --db-name=top-fans --db-host=localhost

关键约束

  • star_id = 999900:所有数据用此 star_id 隔离,不影响真实业务
  • PG max_connections = 50:Task 5 已将 POSTGRES_MAX_CONNECTIONS 从 100 改到 50
  • CLAUDE.md 序列重置:ResetSequences 会在 seed 末尾自动同步所有相关表的 sequence,避免后续 GORM 插入报 duplicate key
  • JWT 7 天过期:跨周第二轮压测前需 --reset-tokens 重签

测试

cd backend && go test ./scripts/loadgen/seed/ -v

5 个测试:

  • TestMobileNumbering:mobile 编号正确性
  • TestSequenceMapping:loadtestSeqs 映射
  • TestPKColumnMapping:pkColumns 映射(关键 stars/star_id, booth_slots/slot_id)
  • TestCleanupRejectsInvalidStarID:cleanup 拒绝非 loadtest star_id
  • TestJoinInt64:CSV 序列化辅助函数