From c36bb70f96fe9bf56c737f198b42089ec7362525 Mon Sep 17 00:00:00 2001 From: k2safe Date: Mon, 21 Sep 2026 02:14:52 +0800 Subject: [PATCH] test: add native tcp performance baseline --- CHANGELOG.md | 4 ++ PERFORMANCE.md | 4 +- ROADMAP.md | 1 + scripts/verifyPerformance.py | 5 ++ transport/native/benchmark_test.go | 95 ++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 transport/native/benchmark_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a4fffc..4c0d82e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- 新增 native 真实 TCP Keep-Alive 并发 benchmark,纳入稳定性与性能验收脚本,用于评估后续事件状态机优化。 + ## v0.5.0 - 新增 `transport/native` 第一版自研 HTTP/1.1 服务端,接管 TCP 监听、连接、Keep-Alive、请求解析、响应写回和优雅关闭。 diff --git a/PERFORMANCE.md b/PERFORMANCE.md index 3e91935..8b6c888 100644 --- a/PERFORMANCE.md +++ b/PERFORMANCE.md @@ -1,6 +1,6 @@ # GoOmni 性能基线 -这份基线用于比较未来自研网络层,不代表生产容量承诺。测试只测进程内请求分发和 GoOmni 请求链,不包含真实 TCP、TLS、数据库或业务存储延迟。 +这份基线用于比较未来自研网络层,不代表生产容量承诺。标准库 benchmark 测进程内请求分发;native benchmark 额外覆盖真实 TCP、Keep-Alive 和 GoOmni 请求链,但不包含 TLS、数据库或业务存储延迟。 运行: @@ -13,8 +13,10 @@ python3 scripts/verifyPerformance.py ```text BenchmarkTransportRouteDispatch/nethttp 568–671 ns/op 1344 B/op 13 allocs/op BenchmarkTransportApplicationRequest/nethttp 2017–2208 ns/op 3091 B/op 34 allocs/op +BenchmarkNativeApplicationRequest 15438–16711 ns/op 7855–7874 B/op 100 allocs/op ``` `BenchmarkTransportRouteDispatch` 只覆盖标准库路由、路径参数和 `http.Handler` 调用;`BenchmarkTransportApplicationRequest` 还包含 GoOmni Context、RequestID、统一 JSON 响应和默认中间件链。 +`BenchmarkNativeApplicationRequest` 启动 native TCP listener,通过复用连接的 HTTP client 并发请求健康接口,包含 socket、请求解析、连接状态管理、响应写回和对象池路径。它用于观察网络层的相对变化,不应直接换算为生产 QPS。 比较新网络层时,应使用相同 Go 版本、机器和 benchmark 命令,并同时运行 `-race` 全量测试。性能下降需要结合分配次数、取消传播、错误响应和业务契约结果一起判断。 diff --git a/ROADMAP.md b/ROADMAP.md index c7c242e..bf31071 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -39,6 +39,7 @@ - 已加入标准库路由和完整 GoOmni 请求链 benchmark,输出分配次数和耗时,作为未来自研网络层的对照基线。 - 新增 `transport/native` 第一版自研 HTTP/1.1 服务端:自行管理 TCP listener、Keep-Alive、Content-Length 请求解析、响应写回、读写超时和优雅关闭;通过 `App.RunWith` 接入,业务 Handler / Service 保持不变。 +- 新增 native 真实 TCP Keep-Alive 并发 benchmark,记录 socket、解析、响应和对象池路径的耗时与分配,后续压测以此作为事件状态机改造前的基线。 - native 当前拒绝 chunked 请求体,仍使用标准 `http.Request` / `http.ResponseWriter` 作为桥接;解析器、连接抽象和生产压测将在后续阶段继续下沉。 - 已具备全量 race、vet、build 验收;生产压测、长连接规模和真实部署验证仍待完成。 - 自动验收入口:`python3 scripts/verifyPerformance.py`。 diff --git a/scripts/verifyPerformance.py b/scripts/verifyPerformance.py index 2163bc2..f227f9c 100755 --- a/scripts/verifyPerformance.py +++ b/scripts/verifyPerformance.py @@ -23,6 +23,11 @@ def main(): "-benchmem", "-count", str(args.count), "./transport"], "transport benchmarks", ), + ( + [args.go, "test", "-run", "^$", "-bench", "^BenchmarkNative", + "-benchmem", "-count", str(args.count), "./transport/native"], + "native TCP benchmarks", + ), ] for command, name in checks: print(f"Running {name}", flush=True) diff --git a/transport/native/benchmark_test.go b/transport/native/benchmark_test.go new file mode 100644 index 0000000..1ba994c --- /dev/null +++ b/transport/native/benchmark_test.go @@ -0,0 +1,95 @@ +package native_test + +import ( + "context" + "io" + "net" + "net/http" + "sync/atomic" + "testing" + "time" + + omni "git.yuntuo.hk/k2safe/go-omni" + "git.yuntuo.hk/k2safe/go-omni/transport/native" + netrouter "git.yuntuo.hk/k2safe/go-omni/transport/nethttp" +) + +// BenchmarkNativeApplicationRequest measures the real TCP path with a reused +// client connection. It is intentionally separate from the in-process router +// benchmarks in transport/benchmark_test.go. +func BenchmarkNativeApplicationRequest(b *testing.B) { + application := omni.NewWithRouter(netrouter.New()) + application.GET("/health", func(ctx *omni.Context) error { + return ctx.OK(map[string]string{"status": "ok"}) + }) + server := native.New(application) + server.MaxConnections = 1024 + server.MaxConcurrentRequests = 1024 + listener, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + b.Fatal(err) + } + done := make(chan error, 1) + go func() { done <- server.Serve(context.Background(), listener) }() + waitForBenchmarkServer(b, listener.Addr().String()) + + client := &http.Client{Transport: &http.Transport{ + MaxIdleConns: 1024, + MaxIdleConnsPerHost: 1024, + IdleConnTimeout: time.Minute, + }} + endpoint := "http://" + listener.Addr().String() + "/health" + response, err := client.Get(endpoint) + if err != nil { + b.Fatal(err) + } + _, _ = io.Copy(io.Discard, response.Body) + response.Body.Close() + + var failed atomic.Bool + var failure atomic.Value + b.ReportAllocs() + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + response, err := client.Get(endpoint) + if err != nil { + if failed.CompareAndSwap(false, true) { + failure.Store(err.Error()) + } + continue + } + _, _ = io.Copy(io.Discard, response.Body) + response.Body.Close() + } + }) + b.StopTimer() + client.CloseIdleConnections() + shutdownContext, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + if err := server.Shutdown(shutdownContext); err != nil { + b.Fatal(err) + } + if err := <-done; err != nil { + b.Fatal(err) + } + if failed.Load() { + b.Fatalf("native request failed: %s", failure.Load().(string)) + } +} + +func waitForBenchmarkServer(b *testing.B, address string) { + b.Helper() + deadline := time.Now().Add(2 * time.Second) + for { + conn, err := net.DialTimeout("tcp", address, 50*time.Millisecond) + if err == nil { + conn.Close() + return + } + if time.Now().After(deadline) { + b.Fatalf("server did not start: %v", err) + } + time.Sleep(10 * time.Millisecond) + } +}