refactor(benchmark,proxy): 改进错误处理,忽略未使用返回值
- benchmark tools 使用空白标识符忽略明确不关心的返回值 - proxy bench 测试统一错误处理风格 - 符合 Go 最佳实践,避免编译器警告 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
cb45e824d4
commit
0c2ac445c2
@ -159,6 +159,6 @@ func (lg *FasthttpLoadGenerator) RunParallel(pb *testing.PB) {
|
|||||||
for pb.Next() {
|
for pb.Next() {
|
||||||
req.SetRequestURI("http://" + lg.addr + "/")
|
req.SetRequestURI("http://" + lg.addr + "/")
|
||||||
req.Header.SetMethod("GET")
|
req.Header.SetMethod("GET")
|
||||||
lg.client.Do(req, resp)
|
_ = lg.client.Do(req, resp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,14 +56,14 @@ func StartMockFasthttpBackend(config MockBackendConfig) (string, func()) {
|
|||||||
|
|
||||||
// Start server in background
|
// Start server in background
|
||||||
go func() {
|
go func() {
|
||||||
mb.server.Serve(ln)
|
_ = mb.server.Serve(ln)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
addr := "127.0.0.1:0" // In-memory listener address
|
addr := "127.0.0.1:0" // In-memory listener address
|
||||||
|
|
||||||
cleanup := func() {
|
cleanup := func() {
|
||||||
mb.server.Shutdown()
|
_ = mb.server.Shutdown()
|
||||||
ln.Close()
|
_ = ln.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
return addr, cleanup
|
return addr, cleanup
|
||||||
@ -79,16 +79,16 @@ func (mb *MockBackend) handler(ctx *fasthttp.RequestCtx) {
|
|||||||
case ModeDelay:
|
case ModeDelay:
|
||||||
time.Sleep(config.Delay)
|
time.Sleep(config.Delay)
|
||||||
ctx.SetStatusCode(config.StatusCode)
|
ctx.SetStatusCode(config.StatusCode)
|
||||||
ctx.Write(config.Body)
|
_, _ = ctx.Write(config.Body)
|
||||||
|
|
||||||
case ModeError:
|
case ModeError:
|
||||||
if rand.Float64() < config.ErrorRate {
|
if rand.Float64() < config.ErrorRate {
|
||||||
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
||||||
ctx.WriteString("internal server error")
|
_, _ = ctx.WriteString("internal server error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.SetStatusCode(config.StatusCode)
|
ctx.SetStatusCode(config.StatusCode)
|
||||||
ctx.Write(config.Body)
|
_, _ = ctx.Write(config.Body)
|
||||||
|
|
||||||
case ModeRandomResponse:
|
case ModeRandomResponse:
|
||||||
codes := []int{
|
codes := []int{
|
||||||
@ -99,11 +99,11 @@ func (mb *MockBackend) handler(ctx *fasthttp.RequestCtx) {
|
|||||||
fasthttp.StatusNotFound,
|
fasthttp.StatusNotFound,
|
||||||
}
|
}
|
||||||
ctx.SetStatusCode(codes[rand.Intn(len(codes))])
|
ctx.SetStatusCode(codes[rand.Intn(len(codes))])
|
||||||
ctx.Write(config.Body)
|
_, _ = ctx.Write(config.Body)
|
||||||
|
|
||||||
default: // ModeFixed
|
default: // ModeFixed
|
||||||
ctx.SetStatusCode(config.StatusCode)
|
ctx.SetStatusCode(config.StatusCode)
|
||||||
ctx.Write(config.Body)
|
_, _ = ctx.Write(config.Body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -23,12 +23,12 @@ func setupMockBackend(body []byte) (string, func()) {
|
|||||||
server := &fasthttp.Server{
|
server := &fasthttp.Server{
|
||||||
Handler: func(ctx *fasthttp.RequestCtx) {
|
Handler: func(ctx *fasthttp.RequestCtx) {
|
||||||
ctx.SetStatusCode(fasthttp.StatusOK)
|
ctx.SetStatusCode(fasthttp.StatusOK)
|
||||||
ctx.Write(body)
|
_, _ = ctx.Write(body)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
server.Serve(ln)
|
_ = server.Serve(ln)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// 等待服务器启动
|
// 等待服务器启动
|
||||||
@ -36,7 +36,7 @@ func setupMockBackend(body []byte) (string, func()) {
|
|||||||
|
|
||||||
addr := ln.Addr().String()
|
addr := ln.Addr().String()
|
||||||
cleanup := func() {
|
cleanup := func() {
|
||||||
ln.Close()
|
_ = ln.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
return addr, cleanup
|
return addr, cleanup
|
||||||
@ -251,7 +251,7 @@ func BenchmarkProxyHostClient(b *testing.B) {
|
|||||||
req.SetRequestURI("http://" + addr + "/api/test")
|
req.SetRequestURI("http://" + addr + "/api/test")
|
||||||
req.Header.SetMethod(fasthttp.MethodGet)
|
req.Header.SetMethod(fasthttp.MethodGet)
|
||||||
|
|
||||||
client.Do(req, resp)
|
_ = client.Do(req, resp)
|
||||||
|
|
||||||
fasthttp.ReleaseRequest(req)
|
fasthttp.ReleaseRequest(req)
|
||||||
fasthttp.ReleaseResponse(resp)
|
fasthttp.ReleaseResponse(resp)
|
||||||
@ -281,7 +281,7 @@ func BenchmarkProxyHostClientParallel(b *testing.B) {
|
|||||||
req.SetRequestURI("http://" + addr + "/api/test")
|
req.SetRequestURI("http://" + addr + "/api/test")
|
||||||
req.Header.SetMethod(fasthttp.MethodGet)
|
req.Header.SetMethod(fasthttp.MethodGet)
|
||||||
|
|
||||||
client.Do(req, resp)
|
_ = client.Do(req, resp)
|
||||||
|
|
||||||
fasthttp.ReleaseRequest(req)
|
fasthttp.ReleaseRequest(req)
|
||||||
fasthttp.ReleaseResponse(resp)
|
fasthttp.ReleaseResponse(resp)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user