MCPcopy
hub / github.com/danielgtaylor/huma / BenchmarkRawEcho

Function BenchmarkRawEcho

adapters/humaecho/humaecho_test.go:155–254  ·  view source on GitHub ↗
(b *testing.B)

Source from the content-addressed store, hash-verified

153}
154
155func BenchmarkRawEcho(b *testing.B) {
156 type GreetingInput struct {
157 Suffix string `json:"suffix" maxLength:"5"`
158 }
159
160 type GreetingOutput struct {
161 Schema string `json:"$schema"`
162 Greeting string `json:"greeting"`
163 Suffix string `json:"suffix"`
164 Length int `json:"length"`
165 ContentType string `json:"content_type"`
166 Num int `json:"num"`
167 }
168
169 registry := huma.NewMapRegistry("#/components/schemas/",
170 func(t reflect.Type, hint string) string {
171 return t.Name()
172 })
173 schema := registry.Schema(reflect.TypeFor[GreetingInput](), false, "")
174
175 strSchema := registry.Schema(reflect.TypeFor[string](), false, "")
176 numSchema := registry.Schema(reflect.TypeFor[int](), false, "")
177
178 r := echo.New()
179
180 r.POST("/foo/:id", func(c echo.Context) error {
181 r := c.Request()
182 w := c.Response()
183
184 pb := huma.NewPathBuffer([]byte{}, 0)
185 res := &huma.ValidateResult{}
186
187 // Read and validate params
188 id := c.Param("id")
189 huma.Validate(registry, strSchema, pb, huma.ModeReadFromServer, id, res)
190
191 ct := r.Header.Get("Content-Type")
192 huma.Validate(registry, strSchema, pb, huma.ModeReadFromServer, ct, res)
193
194 num, err := strconv.Atoi(c.QueryParam("num"))
195 if err != nil {
196 panic(err)
197 }
198 huma.Validate(registry, numSchema, pb, huma.ModeReadFromServer, num, res)
199
200 // Read and validate body
201 defer r.Body.Close()
202 data, err := io.ReadAll(r.Body)
203 if err != nil {
204 panic(err)
205 }
206
207 var tmp any
208 if err := json.Unmarshal(data, &tmp); err != nil {
209 panic(err)
210 }
211
212 huma.Validate(registry, schema, pb, huma.ModeWriteToServer, tmp, res)

Callers

nothing calls this directly

Calls 11

SchemaMethod · 0.65
ParamMethod · 0.65
GetMethod · 0.65
UnmarshalMethod · 0.65
HeaderMethod · 0.65
MarshalMethod · 0.65
ServeHTTPMethod · 0.65
ValidateMethod · 0.45
WriteHeaderMethod · 0.45
WriteMethod · 0.45
ResetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…