Skip to content

Commit

Permalink
feat: support dl/ul and ping at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
r3inbowari committed Oct 28, 2023
1 parent 75f41d7 commit 5e1ab98
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
51 changes: 47 additions & 4 deletions speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,30 +122,73 @@ 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))
} else {
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))
} else {
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()
Expand Down
4 changes: 2 additions & 2 deletions speedtest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion speedtest/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit 5e1ab98

Please sign in to comment.