From 5e1ab985b23c54be03d543fe8917af9011818bfe Mon Sep 17 00:00:00 2001 From: r3inbowari Date: Sat, 28 Oct 2023 12:57:17 +0800 Subject: [PATCH] feat: support dl/ul and ping at the same time --- speedtest.go | 51 ++++++++++++++++++++++++++++++++++++--- speedtest/request.go | 4 +-- speedtest/request_test.go | 2 +- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/speedtest.go b/speedtest.go index f3ad746..43028f9 100644 --- a/speedtest.go +++ b/speedtest.go @@ -122,8 +122,28 @@ func main() { }) taskManager.Run("Download", func(task *Task) { + var latencies []int64 + var lc int64 + quit := false + go func() { + for { + if quit { + return + } + latency, err1 := server.HTTPPing(context.Background(), 1, time.Millisecond*500, nil) + if err1 != nil { + continue + } + lc = latency[0] + latencies = append(latencies, latency...) + } + }() ticker := speedtestClient.CallbackDownloadRate(func(downRate float64) { - task.Printf("Download: %.2fMbps", downRate) + if lc == 0 { + task.Printf("Download: %.2fMbps (latency: --)", downRate) + } else { + task.Printf("Download: %.2fMbps (latency: %dms)", downRate, lc/1000000) + } }) if *multi { task.CheckError(server.MultiDownloadTestContext(context.Background(), servers)) @@ -131,13 +151,34 @@ func main() { task.CheckError(server.DownloadTest()) } ticker.Stop() - task.Printf("Download: %.2fMbps (used: %.2fMB)", server.DLSpeed, float64(server.Context.Manager.GetTotalDownload())/1024/1024) + mean, _, std, min, max := speedtest.StandardDeviation(latencies) + task.Printf("Download: %.2fMbps (used: %.2fMB) (latency: %dms jitter: %dms min: %dms max: %dms)", server.DLSpeed, float64(server.Context.Manager.GetTotalDownload())/1024/1024, mean/1000000, std/1000000, min/1000000, max/1000000) task.Complete() }) taskManager.Run("Upload", func(task *Task) { + var latencies []int64 + var lc int64 + quit := false + go func() { + for { + if quit { + return + } + latency, err1 := server.HTTPPing(context.Background(), 1, time.Millisecond*500, nil) + if err1 != nil { + continue + } + lc = latency[0] + latencies = append(latencies, latency...) + } + }() ticker := speedtestClient.CallbackUploadRate(func(upRate float64) { - task.Printf("Upload: %.2fMbps", upRate) + if lc == 0 { + task.Printf("Upload: %.2fMbps (latency: --)", upRate) + } else { + task.Printf("Upload: %.2fMbps (latency: %dms)", upRate, lc/1000000) + } }) if *multi { task.CheckError(server.MultiUploadTestContext(context.Background(), servers)) @@ -145,7 +186,9 @@ func main() { task.CheckError(server.UploadTest()) } ticker.Stop() - task.Printf("Upload: %.2fMbps (used: %.2fMB)", server.ULSpeed, float64(server.Context.Manager.GetTotalUpload())/1024/1024) + quit = true + mean, _, std, min, max := speedtest.StandardDeviation(latencies) + task.Printf("Upload: %.2fMbps (used: %.2fMB) (latency: %dms jitter: %dms min: %dms max: %dms)", server.ULSpeed, float64(server.Context.Manager.GetTotalUpload())/1024/1024, mean/1000000, std/1000000, min/1000000, max/1000000) task.Complete() }) taskManager.Reset() diff --git a/speedtest/request.go b/speedtest/request.go index 67bc4ed..b9020e3 100644 --- a/speedtest/request.go +++ b/speedtest/request.go @@ -191,7 +191,7 @@ func (s *Server) PingTestContext(ctx context.Context, callback func(latency time return err } dbg.Printf("Before StandardDeviation: %v\n", vectorPingResult) - mean, _, std, min, max := standardDeviation(vectorPingResult) + mean, _, std, min, max := StandardDeviation(vectorPingResult) duration := time.Since(start) s.Latency = time.Duration(mean) * time.Nanosecond s.Jitter = time.Duration(std) * time.Nanosecond @@ -386,7 +386,7 @@ func checkSum(data []byte) uint16 { return uint16(^sum) } -func standardDeviation(vector []int64) (mean, variance, stdDev, min, max int64) { +func StandardDeviation(vector []int64) (mean, variance, stdDev, min, max int64) { var sumNum, accumulate int64 min = math.MaxInt64 max = math.MinInt64 diff --git a/speedtest/request_test.go b/speedtest/request_test.go index e82e4f9..08740a1 100644 --- a/speedtest/request_test.go +++ b/speedtest/request_test.go @@ -83,7 +83,7 @@ func TestPautaFilter(t *testing.T) { //vector := []float64{6, 6, 6, 6, 6, 6, 6, 6, 6, 6} vector0 := []int64{26, 23, 32} vector1 := []int64{3, 4, 5, 6, 6, 6, 1, 7, 9, 5, 200} - _, _, std, _, _ := standardDeviation(vector0) + _, _, std, _, _ := StandardDeviation(vector0) if std != 3 { t.Fail() }