Files
HomeAgent/internal/plugins/cmd/stress_test.go
JianFeeeee 6d38a164eb chore(module): Go module 路径随托管地迁移 gitcode → github
## 背景

仓库早已迁移到 GitHub(`github.com/JianFeeeee/HomeAgent`,CI/CD 都在那边跑),
gitcode 保留为**国内只读镜像**(`ci.yml` 的 sync-gitcode-main 快进推送)。
但 go.mod 的 module 路径一直是 `gitcode.com/JianFeeeee/...` —— 既与权威源
不符,`meta.SourceURL`(AGPL §13 的源码提供入口)也指向镜像而非实际运行的代码。

## 关键区分(这是本次改动安全的前提)

两个 module 是**不同**的,影响面差一个数量级:

| module | 谁依赖 | 改它的后果 |
|---|---|---|
| `.../HomeAgent`(主仓) | **只有主仓自己** | 内部重构,零外部影响 |
| `.../homeagent-sdk`(SDK 仓) | 主仓 49 文件 + 所有外部插件 | 需重编插件 |

★ 实测确认:**插件不 import 主仓 module**(`strings qq/plugin.bin | grep HomeAgent` = 0),
只依赖 SDK 仓。而插件与内核的边界是 **RPC 方法名字符串**
(`plugin.init` / `tool.invoke`…,见 proc/protocol.go),**与 Go module 名无关**。
⇒ module 改名**不改变任何 wire 协议**,现网已装的插件照常运行。

SDK 的 GitHub 仓名是 `homeagentsdk`(无连字符),module 路径必须与之精确匹配,
否则 `go get` 会因路径不符失败,故取 `github.com/JianFeeeee/homeagentsdk`。

## 改动

- `go.mod` / `go.sum`:两个 module 路径 + require/replace
- 277 个 Go 文件 + Makefile + 打包脚本的 import 与 ldflags 路径
- SDK 仓 `meta.CoreModule`、`tools/hmapdev` 的 module 声明与工程模板
- 对外文档/链接的仓库地址(README、PLUGIN_DEV、OVERVIEW 等)

## 明确**保留** gitcode 的地方(镜像机制,不能动)

- `GITCODE_TOKEN`、`GC_URL="…@gitcode.com"`(CI 同步 job)
- `gitcode.com/api/v5/...`(release 附件上传)
- git remote `gitcode`(镜像拉取/推送)
- `docs/zh/ci-cd-runbook.md`(整篇讲镜像运维)

## 验证

- `go build ./...` / `go vet ./...` 绿
- `go test ./... -count=1` → 48 包 ok / 0 FAIL
- hmapdev 工具链编译通过(`third_party/homeagent-sdk/tools/hmapdev`)
- 全仓残留检查:module 形态的 `gitcode.com/JianFeeeee` = 0
- 镜像机制抽验:GC_URL / @gitcode / api/v5 / remote 均完好

## 顺带修正

`hmapdev/templates.go` 的注释此前写「gitcode 的模块不在 proxy.golang.org 上」——
module 已迁 GitHub,该论断不再成立。改为如实说明**为何仍默认 replace 到本机 SDK**:
离线可构建、避免版本漂移、内网可用。理由与托管地无关,故 replace 保留。
2026-10-08 10:40:17 +08:00

225 lines
6.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cmd
import (
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
sdk "github.com/JianFeeeee/HomeAgent/internal/sdk"
)
// 本文件是**真实工具**的并发压测。
//
// 为什么需要它:阶段 2 的并发判据(core/parallelsched_test.go)用的是
// **假设备** —— 它验证的是"内核会不会并发调度",但没有验证
// **真实插件工具在真并发下是否安全**。而这正是 ParallelSafe 声明的风险面:
// 声明错一个工具,1000 个并发调用会同时打进去。
//
// 判据全是**不变量**,不写性能阈值(阈值会随机器波动,变成"红/绿随运气"
// 的假信号)。
//
// 跑法:go test ./internal/plugins/cmd/ -run TestStress -timeout 600s
// -short 时跳过。
// runOnce 直接调 cmd_run 的 handler,返回其 stdout。
//
// ⚠️ cmd_run 的真实返回是 **map[string]interface{}**(含 status/stdout/
// exit_code/command),**不是 string**。我第一版按 string 断言,导致
// 1000 次全判"输出为空"—— 那是判据写错,不是工具串扰。
// 既有测试(TestCmdRunEcho)同样用 json.Marshal 取值,与此一致。
func runOnce(h sdk.ToolHandler, i int) (string, error) {
res, err := h(map[string]interface{}{
"command": fmt.Sprintf("echo seq%d", i),
})
if err != nil {
return "", err
}
m, ok := res.(map[string]interface{})
if !ok {
return "", fmt.Errorf("cmd_run 返回类型不是 map:%T", res)
}
if e, hasErr := m["error"]; hasErr {
return "", fmt.Errorf("cmd_run 报错:%v", e)
}
s, _ := m["stdout"].(string)
return s, nil
}
// TestStress_RealTool1000Concurrent 真实 cmd_run 1000 并发。
//
// 判据:
// 1. 1000 次全部成功,**零错误**(真并发下最常见的失败是共享状态竞争)
// 2. 每次输出**各不相同**且与自己的入参对应 —— 若 handler 有共享 buffer
// 竞争,输出会串(这是"执行体不安全"最典型的症状)
// 3. 耗时不应随并发数线性恶化到不可用(只做宽松上界,不做精确基准)
// 4. -race 无竞态
func TestStress_RealTool1000Concurrent(t *testing.T) {
if testing.Short() {
t.Skip("stress test; run with -run TestStress")
}
const n = 1000
// ⚠️ 用**既有**的 setupPlugin(plugin_test.go 里的真实装配),
// 不另造一套 —— 压测必须跑在真实注册路径上,否则测的是替身。
_, tc, err := setupPlugin()
if err != nil {
t.Fatalf("装配插件失败: %v", err)
}
h, ok := tc.handlers["cmd_run"]
if !ok {
t.Fatal("取不到 cmd_run 的 handler")
}
// 顺带确认它真的声明了并发安全(否则 1000 并发会被内核整批串行,
// 本用例就测不到真并发)
if !tc.defs["cmd_run"].ParallelSafe {
t.Fatal("cmd_run 未声明 ParallelSafe —— 内核会整批串行,本压测失去意义")
}
// 预热:首次调用会加载配置/建目录,不计入压测
if _, err := runOnce(h, -1); err != nil {
t.Fatalf("预热失败: %v", err)
}
// 每个 goroutine 只写自己那个下标(无共享变量),故无需加锁 ——
// 这也是检查项 map_write_in_goroutine 想确认的:按索引分槽写是所有权清晰。
results := make([]string, n)
errs := make([]error, n)
var wg sync.WaitGroup
var inFlight, maxInFlight int32
start := time.Now()
for i := 0; i < n; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
// 记录并发峰值:证明确实并发了(否则本用例测不到并发)
cur := atomic.AddInt32(&inFlight, 1)
for {
old := atomic.LoadInt32(&maxInFlight)
if cur <= old || atomic.CompareAndSwapInt32(&maxInFlight, old, cur) {
break
}
}
results[idx], errs[idx] = runOnce(h, idx)
atomic.AddInt32(&inFlight, -1)
}(i)
}
wg.Wait()
elapsed := time.Since(start)
// ① 零错误
fails := 0
for i, e := range errs {
if e != nil {
if fails < 3 {
t.Errorf("第 %d 次并发调用失败: %v", i, e)
}
fails++
}
}
if fails > 0 {
t.Errorf("共 %d/%d 次失败", fails, n)
}
// ② 输出与入参一一对应(无串扰)
mismatched := 0
for i := 0; i < n; i++ {
want := fmt.Sprintf("seq%d", i)
if errs[i] != nil {
continue
}
if !containsStr(results[i], want) {
if mismatched < 3 {
t.Errorf("第 %d 次输出不含自己的标记 %q:%q(串扰?)", i, want,
truncate(results[i], 80))
}
mismatched++
}
}
if mismatched > 0 {
t.Errorf("共 %d 次输出与入参不对应(共享状态竞争)", mismatched)
}
peak := atomic.LoadInt32(&maxInFlight)
t.Logf("1000 并发真实 cmd_run:耗时 %v,并发峰值 %d,失败 %d", elapsed, peak, fails)
if peak < 10 {
t.Errorf("并发峰值仅 %d —— 可能被串行化了,本用例测不到真并发", peak)
}
}
// TestStress_RealToolSerialVsConcurrent 串行 vs 并发的耗时对比。
//
// 只做**观察性**记录(不做通过判据):机器差异太大,阈值无意义。
// 它的价值在于:如果并发比串行**慢很多**,说明 handler 内部有锁竞争
// 或资源争抢,值得深挖。
func TestStress_RealToolSerialVsConcurrent(t *testing.T) {
if testing.Short() {
t.Skip("stress test; run with -run TestStress")
}
const n = 200
// ⚠️ 用**既有**的 setupPlugin(plugin_test.go 里的真实装配),
// 不另造一套 —— 压测必须跑在真实注册路径上,否则测的是替身。
_, tc, err := setupPlugin()
if err != nil {
t.Fatalf("装配插件失败: %v", err)
}
h, ok := tc.handlers["cmd_run"]
if !ok {
t.Fatal("取不到 cmd_run 的 handler")
}
// 顺带确认它真的声明了并发安全(否则 1000 并发会被内核整批串行,
// 本用例就测不到真并发)
if !tc.defs["cmd_run"].ParallelSafe {
t.Fatal("cmd_run 未声明 ParallelSafe —— 内核会整批串行,本压测失去意义")
}
if _, err := runOnce(h, -1); err != nil {
t.Fatalf("预热失败: %v", err)
}
// 串行
t0 := time.Now()
for i := 0; i < n; i++ {
if _, err := runOnce(h, i); err != nil {
t.Fatalf("串行第 %d 次失败: %v", i, err)
}
}
dSerial := time.Since(t0)
// 并发(2 并发:轻并发,便于对比是否有争抢)
t1 := time.Now()
var wg sync.WaitGroup
for i := 0; i < n; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
if _, err := runOnce(h, idx); err != nil {
t.Errorf("并发第 %d 次失败: %v", idx, err)
}
}(i)
}
wg.Wait()
dConc := time.Since(t1)
t.Logf("%d 次:串行 %v(%v/次),高并发 %v(%v/次)",
n, dSerial, dSerial/n, dConc, dConc/n)
}
func containsStr(s, sub string) bool {
for i := 0; i+len(sub) <= len(s); i++ {
if s[i:i+len(sub)] == sub {
return true
}
}
return false
}
func truncate(s string, n int) string {
if len(s) <= n {
return s
}
return s[:n] + "..."
}