(b *testing.B, parallelism int)
| 456 | } |
| 457 | |
| 458 | func benchmarkNetHTTPClientEndToEndBigResponseInmemory(b *testing.B, parallelism int) { |
| 459 | bigResponse := createFixedBody(1024 * 1024) |
| 460 | h := func(w http.ResponseWriter, _ *http.Request) { |
| 461 | w.Header().Set(HeaderContentType, "text/plain") |
| 462 | w.Write(bigResponse) //nolint:errcheck |
| 463 | } |
| 464 | ln := fasthttputil.NewInmemoryListener() |
| 465 | |
| 466 | ch := make(chan struct{}) |
| 467 | go func() { |
| 468 | if err := http.Serve(ln, http.HandlerFunc(h)); err != nil && !strings.Contains( |
| 469 | err.Error(), "use of closed network connection") { |
| 470 | b.Errorf("error when serving requests: %v", err) |
| 471 | } |
| 472 | close(ch) |
| 473 | }() |
| 474 | |
| 475 | c := &http.Client{ |
| 476 | Transport: &http.Transport{ |
| 477 | Dial: func(_, _ string) (net.Conn, error) { return ln.Dial() }, |
| 478 | MaxIdleConnsPerHost: parallelism * runtime.GOMAXPROCS(-1), |
| 479 | }, |
| 480 | Timeout: 5 * time.Second, |
| 481 | } |
| 482 | |
| 483 | requestURI := "/foo/bar?baz=123" |
| 484 | url := "http://unused.host" + requestURI |
| 485 | b.SetParallelism(parallelism) |
| 486 | b.RunParallel(func(pb *testing.PB) { |
| 487 | req, err := http.NewRequest(MethodGet, url, http.NoBody) |
| 488 | if err != nil { |
| 489 | b.Fatalf("unexpected error: %v", err) |
| 490 | } |
| 491 | for pb.Next() { |
| 492 | resp, err := c.Do(req) |
| 493 | if err != nil { |
| 494 | b.Fatalf("unexpected error: %v", err) |
| 495 | } |
| 496 | if resp.StatusCode != http.StatusOK { |
| 497 | b.Fatalf("unexpected status code: %d. Expecting %d", resp.StatusCode, http.StatusOK) |
| 498 | } |
| 499 | body, err := io.ReadAll(resp.Body) |
| 500 | resp.Body.Close() |
| 501 | if err != nil { |
| 502 | b.Fatalf("unexpected error when reading response body: %v", err) |
| 503 | } |
| 504 | if !bytes.Equal(bigResponse, body) { |
| 505 | b.Fatalf("unexpected response %q. Expecting %q", body, bigResponse) |
| 506 | } |
| 507 | } |
| 508 | }) |
| 509 | |
| 510 | ln.Close() |
| 511 | select { |
| 512 | case <-ch: |
| 513 | case <-time.After(time.Second): |
| 514 | b.Fatalf("server wasn't stopped") |
| 515 | } |
no test coverage detected
searching dependent graphs…