From c6b5b9e8ac432208de4cd36f8df0ee9cee8ccf4e Mon Sep 17 00:00:00 2001 From: Jacob Casper Date: Thu, 20 Jun 2024 14:57:52 -0500 Subject: [PATCH] Fix err/panic when doing multi-uploads by creating a new Reader I should be able to Seek this Reader but I can't once it's wrapped in io.ReadCloser so I'm just gonna reinstantiate it since it's empty memory --- main.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index c9e1e15..3a799ad 100644 --- a/main.go +++ b/main.go @@ -33,26 +33,29 @@ func main() { URL: dUrl, } + client := &http.Client{} + for i := 0; i < iter; i++ { + rate := measureRequest(client, down, measurementBytes) + fmt.Printf("Down: MiB/s %v\n", rate/MiB) + } + for i := 0; i < iter; i++ { + rate := measureRequest(client, getUp(uUrl, measurementBytes), measurementBytes) + fmt.Printf("Up: MiB/s %v\n", rate/MiB) + } +} + +// Can't figure out how to reset r on each request so unfortunately just reinstantiate :/ +func getUp(url *url.URL, measurementBytes int64) *http.Request { var headers http.Header = make(map[string][]string) headers.Add("Content-Type", "text/plain;charset=UTF-8") r := io.NopCloser(bytes.NewReader(make([]byte, measurementBytes))) - up := &http.Request{ + return &http.Request{ Method: "POST", - URL: uUrl, + URL: url, Header: headers, ContentLength: measurementBytes, Body: r, } - - client := &http.Client{} - for i := 0; i < iter; i++ { - rate := measureRequest(client, down, measurementBytes) - fmt.Printf("Down: KiB/s %v\n", rate/KiB) - } - for i := 0; i < iter; i++ { - rate := measureRequest(client, up, measurementBytes) - fmt.Printf("Up: KiB/s %v\n", rate/KiB) - } } // Returns the rate in bytes / s @@ -64,6 +67,8 @@ func measureRequest(client *http.Client, r *http.Request, b int64) float64 { log.Fatal(err) } defer resp.Body.Close() + // bufio seems less consistent than io.Discard but sometimes ends up faster + // basically, higher highs but lower lows, shouldn't matter for 90%ile measurements later _, _ = io.ReadAll(bufio.NewReaderSize(resp.Body, int(b))) return bytePerSecond(b, time.Since(start)) } -- 2.20.1