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

Function BenchmarkRawEchoFast

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

Source from the content-addressed store, hash-verified

254}
255
256func BenchmarkRawEchoFast(b *testing.B) {
257 type GreetingInput struct {
258 Suffix string `json:"suffix" maxLength:"5"`
259 }
260
261 type GreetingOutput struct {
262 Greeting string `json:"greeting"`
263 Suffix string `json:"suffix"`
264 Length int `json:"length"`
265 ContentType string `json:"content_type"`
266 Num int `json:"num"`
267 }
268
269 r := echo.New()
270
271 r.POST("/foo/:id", func(c echo.Context) error {
272 r := c.Request()
273 w := c.Response()
274
275 defer r.Body.Close()
276 data, err := io.ReadAll(r.Body)
277 if err != nil {
278 panic(err)
279 }
280
281 var input GreetingInput
282 if err := json.Unmarshal(data, &input); err != nil {
283 panic(err)
284 }
285
286 if len(input.Suffix) > 5 {
287 panic("suffix too long")
288 }
289
290 w.Header().Set("Content-Type", "application/json")
291 w.Header().Set("ETag", "abc123")
292 w.Header().Set("Last-Modified", lastModified.Format(http.TimeFormat))
293 w.WriteHeader(http.StatusOK)
294 resp := &GreetingOutput{}
295 resp.Greeting = "Hello, " + c.Param("id") + input.Suffix
296 resp.Suffix = input.Suffix
297 resp.Length = len(resp.Greeting)
298 resp.ContentType = c.Request().Header.Get("Content-Type")
299 resp.Num, _ = strconv.Atoi(c.QueryParam("num"))
300 data, err = json.Marshal(resp)
301 if err != nil {
302 panic(err)
303 }
304 w.Write(data)
305 return nil
306 })
307
308 reqBody := strings.NewReader(`{"suffix": "!"}`)
309 req, _ := http.NewRequest(http.MethodPost, "/foo/123?num=5", reqBody)
310 req.Header.Set("Content-Type", "application/json")
311 w := httptest.NewRecorder()
312 b.ResetTimer()
313 b.ReportAllocs()

Callers

nothing calls this directly

Calls 9

UnmarshalMethod · 0.65
HeaderMethod · 0.65
ParamMethod · 0.65
GetMethod · 0.65
MarshalMethod · 0.65
ServeHTTPMethod · 0.65
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…