From cd8da82c928fd003e90e2300b6e64bd8e568557f Mon Sep 17 00:00:00 2001 From: purofle Date: Tue, 14 Nov 2023 00:36:29 +0800 Subject: [PATCH] fix: Speed may be +InfMiB/s Closes: #1032 --- go/grpc_server/fulltest.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/go/grpc_server/fulltest.go b/go/grpc_server/fulltest.go index 604c94a7e..9bf70eda9 100644 --- a/go/grpc_server/fulltest.go +++ b/go/grpc_server/fulltest.go @@ -7,6 +7,7 @@ import ( "grpc_server/gen" "io" "log" + "math" "net" "net/http" "strings" @@ -16,6 +17,11 @@ import ( "github.com/matsuridayo/libneko/speedtest" ) +const ( + KiB = 1024 + MiB = 1024 * KiB +) + func getBetweenStr(str, start, end string) string { n := strings.Index(str, start) if n == -1 { @@ -124,11 +130,14 @@ func DoFullTest(ctx context.Context, in *gen.TestReq, instance interface{}) (out if err == nil && resp != nil && resp.Body != nil { bodyClose = resp.Body defer resp.Body.Close() - // - time_start := time.Now() + + timeStart := time.Now() n, _ := io.Copy(io.Discard, resp.Body) - time_end := time.Now() - result <- fmt.Sprintf("%.2fMiB/s", (float64(n)/time_end.Sub(time_start).Seconds())/1048576) + timeEnd := time.Now() + + duration := math.Max(timeEnd.Sub(timeStart).Seconds(), 0.000001) + resultSpeed := (float64(n) / duration) / MiB + result <- fmt.Sprintf("%.2fMiB/s", resultSpeed) } else { result <- "Error" }