Skip to content

Commit

Permalink
fix: Speed may be +InfMiB/s
Browse files Browse the repository at this point in the history
Closes: #1032
  • Loading branch information
purofle committed Nov 13, 2023
1 parent 06e5de0 commit cd8da82
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions go/grpc_server/fulltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"grpc_server/gen"
"io"
"log"
"math"
"net"
"net/http"
"strings"
Expand All @@ -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 {
Expand Down Expand Up @@ -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"
}
Expand Down

0 comments on commit cd8da82

Please sign in to comment.