refactor(benchmark,proxy): 改进错误处理,忽略未使用返回值

- benchmark tools 使用空白标识符忽略明确不关心的返回值
- proxy bench 测试统一错误处理风格
- 符合 Go 最佳实践,避免编译器警告

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-07 17:51:09 +08:00
parent cb45e824d4
commit 0c2ac445c2
3 changed files with 16 additions and 16 deletions

View File

@ -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)
}
}
}

View File

@ -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)
}
}

View File

@ -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)