package response import ( "testing" "github.com/gofiber/fiber/v2" "github.com/valyala/fasthttp" ) // BenchmarkSuccess 测试成功响应性能 func BenchmarkSuccess(b *testing.B) { app := fiber.New() b.Run("WithData", func(b *testing.B) { data := map[string]interface{}{ "id": "123", "name": "测试用户", "age": 25, } b.ResetTimer() for i := 0; i < b.N; i++ { ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) _ = Success(ctx, data) app.ReleaseCtx(ctx) } }) b.Run("NoData", func(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) _ = Success(ctx, nil) app.ReleaseCtx(ctx) } }) } // BenchmarkError 测试错误响应性能 func BenchmarkError(b *testing.B) { app := fiber.New() b.ResetTimer() for i := 0; i < b.N; i++ { ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) _ = Error(ctx, 400, 1001, "无效的请求") app.ReleaseCtx(ctx) } } // BenchmarkSuccessWithMessage 测试带自定义消息的成功响应性能 func BenchmarkSuccessWithMessage(b *testing.B) { app := fiber.New() data := map[string]interface{}{ "id": "123", "name": "测试用户", } b.ResetTimer() for i := 0; i < b.N; i++ { ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) _ = SuccessWithMessage(ctx, data, "操作成功") app.ReleaseCtx(ctx) } }