BenchmarkHandlerFunc compares the performance of a global handler function defined via `func name(...) { ... }` versus an inline handler function which is defined as `func(...) { ... }` as an argument to `huma.Register`. Performance should not be impacted much (if any) between the two.
(b *testing.B)
| 3971 | // which is defined as `func(...) { ... }` as an argument to `huma.Register`. |
| 3972 | // Performance should not be impacted much (if any) between the two. |
| 3973 | func BenchmarkHandlerFunc(b *testing.B) { |
| 3974 | _, api := humatest.New(b, huma.DefaultConfig("Test API", "1.0.0")) |
| 3975 | |
| 3976 | huma.Register(api, huma.Operation{ |
| 3977 | OperationID: "global", |
| 3978 | Method: http.MethodGet, |
| 3979 | Path: "/global", |
| 3980 | }, globalHandler) |
| 3981 | |
| 3982 | huma.Register(api, huma.Operation{ |
| 3983 | OperationID: "inline", |
| 3984 | Method: http.MethodGet, |
| 3985 | Path: "/inline", |
| 3986 | }, func(ctx context.Context, input *struct { |
| 3987 | Count int `query:"count"` |
| 3988 | }) (*struct{ Body int }, error) { |
| 3989 | return &struct{ Body int }{Body: input.Count * 3 / 2}, nil |
| 3990 | }) |
| 3991 | |
| 3992 | b.Run("global", func(b *testing.B) { |
| 3993 | for i := 0; i < b.N; i++ { |
| 3994 | BenchmarkHandlerResponse = api.Get("/global") |
| 3995 | } |
| 3996 | }) |
| 3997 | |
| 3998 | b.Run("inline", func(b *testing.B) { |
| 3999 | for i := 0; i < b.N; i++ { |
| 4000 | BenchmarkHandlerResponse = api.Get("/inline") |
| 4001 | } |
| 4002 | }) |
| 4003 | } |
| 4004 | |
| 4005 | func TestGenerateFuncsPanicWithDescriptiveMessage(t *testing.T) { |
| 4006 | var resp *int |