MCPcopy
hub / github.com/cloudflare/cloudflared / benchmarkServeHTTP

Function benchmarkServeHTTP

connection/http2_test.go:565–600  ·  view source on GitHub ↗
(b *testing.B, test testRequest)

Source from the content-addressed store, hash-verified

563}
564
565func benchmarkServeHTTP(b *testing.B, test testRequest) {
566 http2Conn, edgeConn := newTestHTTP2Connection()
567
568 ctx, cancel := context.WithCancel(b.Context())
569 var wg sync.WaitGroup
570 wg.Add(1)
571 go func() {
572 defer wg.Done()
573 _ = http2Conn.Serve(ctx)
574 }()
575
576 endpoint := fmt.Sprintf("http://localhost:8080/%s", test.endpoint)
577 req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
578 require.NoError(b, err)
579
580 edgeHTTP2Conn, err := testTransport.NewClientConn(edgeConn)
581 require.NoError(b, err)
582
583 b.ResetTimer()
584 for i := 0; i < b.N; i++ {
585 b.StartTimer()
586 resp, err := edgeHTTP2Conn.RoundTrip(req)
587 b.StopTimer()
588 require.NoError(b, err)
589 require.Equal(b, test.expectedStatus, resp.StatusCode)
590 if test.expectedBody != nil {
591 respBody, err := io.ReadAll(resp.Body)
592 require.NoError(b, err)
593 require.Equal(b, test.expectedBody, respBody)
594 }
595 resp.Body.Close()
596 }
597
598 cancel()
599 wg.Wait()
600}
601
602func BenchmarkServeHTTPSimple(b *testing.B) {
603 test := testRequest{

Callers 2

BenchmarkServeHTTPSimpleFunction · 0.85

Calls 8

newTestHTTP2ConnectionFunction · 0.85
WaitMethod · 0.80
ContextMethod · 0.65
AddMethod · 0.65
ServeMethod · 0.65
EqualMethod · 0.65
CloseMethod · 0.65
RoundTripMethod · 0.45

Tested by

no test coverage detected