From 0c2ac445c2f60a483d998fda4d353f87c4edc735 Mon Sep 17 00:00:00 2001 From: xfy Date: Tue, 7 Apr 2026 17:51:09 +0800 Subject: [PATCH] =?UTF-8?q?refactor(benchmark,proxy):=20=E6=94=B9=E8=BF=9B?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=EF=BC=8C=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=E6=9C=AA=E4=BD=BF=E7=94=A8=E8=BF=94=E5=9B=9E=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - benchmark tools 使用空白标识符忽略明确不关心的返回值 - proxy bench 测试统一错误处理风格 - 符合 Go 最佳实践,避免编译器警告 Co-Authored-By: Claude Opus 4.6 --- internal/benchmark/tools/loadgen.go | 4 ++-- internal/benchmark/tools/mock_backend.go | 16 ++++++++-------- internal/proxy/proxy_bench_test.go | 12 ++++++------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/internal/benchmark/tools/loadgen.go b/internal/benchmark/tools/loadgen.go index ed9d9e1..090c9e0 100644 --- a/internal/benchmark/tools/loadgen.go +++ b/internal/benchmark/tools/loadgen.go @@ -159,6 +159,6 @@ func (lg *FasthttpLoadGenerator) RunParallel(pb *testing.PB) { for pb.Next() { req.SetRequestURI("http://" + lg.addr + "/") req.Header.SetMethod("GET") - lg.client.Do(req, resp) + _ = lg.client.Do(req, resp) } -} +} \ No newline at end of file diff --git a/internal/benchmark/tools/mock_backend.go b/internal/benchmark/tools/mock_backend.go index 93ada34..055790c 100644 --- a/internal/benchmark/tools/mock_backend.go +++ b/internal/benchmark/tools/mock_backend.go @@ -56,14 +56,14 @@ func StartMockFasthttpBackend(config MockBackendConfig) (string, func()) { // Start server in background go func() { - mb.server.Serve(ln) + _ = mb.server.Serve(ln) }() addr := "127.0.0.1:0" // In-memory listener address cleanup := func() { - mb.server.Shutdown() - ln.Close() + _ = mb.server.Shutdown() + _ = ln.Close() } return addr, cleanup @@ -79,16 +79,16 @@ func (mb *MockBackend) handler(ctx *fasthttp.RequestCtx) { case ModeDelay: time.Sleep(config.Delay) ctx.SetStatusCode(config.StatusCode) - ctx.Write(config.Body) + _, _ = ctx.Write(config.Body) case ModeError: if rand.Float64() < config.ErrorRate { ctx.SetStatusCode(fasthttp.StatusInternalServerError) - ctx.WriteString("internal server error") + _, _ = ctx.WriteString("internal server error") return } ctx.SetStatusCode(config.StatusCode) - ctx.Write(config.Body) + _, _ = ctx.Write(config.Body) case ModeRandomResponse: codes := []int{ @@ -99,11 +99,11 @@ func (mb *MockBackend) handler(ctx *fasthttp.RequestCtx) { fasthttp.StatusNotFound, } ctx.SetStatusCode(codes[rand.Intn(len(codes))]) - ctx.Write(config.Body) + _, _ = ctx.Write(config.Body) default: // ModeFixed ctx.SetStatusCode(config.StatusCode) - ctx.Write(config.Body) + _, _ = ctx.Write(config.Body) } } diff --git a/internal/proxy/proxy_bench_test.go b/internal/proxy/proxy_bench_test.go index 63815a2..ca96c1b 100644 --- a/internal/proxy/proxy_bench_test.go +++ b/internal/proxy/proxy_bench_test.go @@ -23,12 +23,12 @@ func setupMockBackend(body []byte) (string, func()) { server := &fasthttp.Server{ Handler: func(ctx *fasthttp.RequestCtx) { ctx.SetStatusCode(fasthttp.StatusOK) - ctx.Write(body) + _, _ = ctx.Write(body) }, } go func() { - server.Serve(ln) + _ = server.Serve(ln) }() // 等待服务器启动 @@ -36,7 +36,7 @@ func setupMockBackend(body []byte) (string, func()) { addr := ln.Addr().String() cleanup := func() { - ln.Close() + _ = ln.Close() } return addr, cleanup @@ -45,7 +45,7 @@ func setupMockBackend(body []byte) (string, func()) { // BenchmarkProxyForward 基准测试代理转发性能。 func BenchmarkProxyForward(b *testing.B) { testCases := []struct { - name string + name string concurrency int }{ {"concurrency1", 1}, @@ -251,7 +251,7 @@ func BenchmarkProxyHostClient(b *testing.B) { req.SetRequestURI("http://" + addr + "/api/test") req.Header.SetMethod(fasthttp.MethodGet) - client.Do(req, resp) + _ = client.Do(req, resp) fasthttp.ReleaseRequest(req) fasthttp.ReleaseResponse(resp) @@ -281,7 +281,7 @@ func BenchmarkProxyHostClientParallel(b *testing.B) { req.SetRequestURI("http://" + addr + "/api/test") req.Header.SetMethod(fasthttp.MethodGet) - client.Do(req, resp) + _ = client.Do(req, resp) fasthttp.ReleaseRequest(req) fasthttp.ReleaseResponse(resp)