fix: Speed may be +InfMiB/s

Closes: #1032
This commit is contained in:
purofle
2023-11-14 00:36:29 +08:00
parent 06e5de0998
commit cd8da82c92

View File

@@ -7,6 +7,7 @@ import (
"grpc_server/gen" "grpc_server/gen"
"io" "io"
"log" "log"
"math"
"net" "net"
"net/http" "net/http"
"strings" "strings"
@@ -16,6 +17,11 @@ import (
"github.com/matsuridayo/libneko/speedtest" "github.com/matsuridayo/libneko/speedtest"
) )
const (
KiB = 1024
MiB = 1024 * KiB
)
func getBetweenStr(str, start, end string) string { func getBetweenStr(str, start, end string) string {
n := strings.Index(str, start) n := strings.Index(str, start)
if n == -1 { 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 { if err == nil && resp != nil && resp.Body != nil {
bodyClose = resp.Body bodyClose = resp.Body
defer resp.Body.Close() defer resp.Body.Close()
//
time_start := time.Now() timeStart := time.Now()
n, _ := io.Copy(io.Discard, resp.Body) n, _ := io.Copy(io.Discard, resp.Body)
time_end := time.Now() timeEnd := time.Now()
result <- fmt.Sprintf("%.2fMiB/s", (float64(n)/time_end.Sub(time_start).Seconds())/1048576)
duration := math.Max(timeEnd.Sub(timeStart).Seconds(), 0.000001)
resultSpeed := (float64(n) / duration) / MiB
result <- fmt.Sprintf("%.2fMiB/s", resultSpeed)
} else { } else {
result <- "Error" result <- "Error"
} }