test: add native tcp performance baseline
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
# Changelog
|
||||
|
||||
## Unreleased
|
||||
|
||||
- 新增 native 真实 TCP Keep-Alive 并发 benchmark,纳入稳定性与性能验收脚本,用于评估后续事件状态机优化。
|
||||
|
||||
## v0.5.0
|
||||
|
||||
- 新增 `transport/native` 第一版自研 HTTP/1.1 服务端,接管 TCP 监听、连接、Keep-Alive、请求解析、响应写回和优雅关闭。
|
||||
|
||||
@@ -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` 全量测试。性能下降需要结合分配次数、取消传播、错误响应和业务契约结果一起判断。
|
||||
|
||||
@@ -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`。
|
||||
|
||||
@@ -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)
|
||||
|
||||
95
transport/native/benchmark_test.go
Normal file
95
transport/native/benchmark_test.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user