Skip to content

Commit c28cc1a

Browse files
authored
adds speedtest task (#60)
Signed-off-by: Ashraf Fouda <ashraf.m.fouda@gmail.com>
1 parent 04fbc81 commit c28cc1a

3 files changed

Lines changed: 92 additions & 1 deletion

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ require (
4545
github.com/pkg/errors v0.9.1
4646
github.com/rs/zerolog v1.34.0
4747
github.com/shirou/gopsutil v3.21.11+incompatible
48+
github.com/showwin/speedtest-go v1.7.10
4849
github.com/stretchr/testify v1.10.0
4950
github.com/threefoldtech/0-fs v1.3.1-0.20240424140157-b488dfedcc56
5051
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20241127100051-77e684bcb1b2
@@ -64,7 +65,6 @@ require (
6465
)
6566

6667
require (
67-
github.com/fatih/color v1.18.0 // indirect
6868
github.com/frankban/quicktest v1.14.3 // indirect
6969
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad // indirect
7070
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ github.com/safchain/ethtool v0.0.0-20201023143004-874930cb3ce0/go.mod h1:Z0q5wiB
535535
github.com/seccomp/libseccomp-golang v0.9.2-0.20210429002308-3879420cc921/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg=
536536
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
537537
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
538+
github.com/showwin/speedtest-go v1.7.10 h1:9o5zb7KsuzZKn+IE2//z5btLKJ870JwO6ETayUkqRFw=
539+
github.com/showwin/speedtest-go v1.7.10/go.mod h1:Ei7OCTmNPdWofMadzcfgq1rUO7mvJy9Jycj//G7vyfA=
538540
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
539541
github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
540542
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package speedtest
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"math"
7+
8+
"github.com/rs/zerolog/log"
9+
"github.com/showwin/speedtest-go/speedtest"
10+
"github.com/threefoldtech/zosbase/pkg/perf"
11+
)
12+
13+
type SpeedTestTask struct {
14+
}
15+
16+
// CPUBenchmarkResult holds CPU benchmark results with the workloads number during the benchmark.
17+
type SpeedTestResults struct {
18+
Download float64 `json:"download_speed"`
19+
Upload float64 `json:"upload_speed"`
20+
}
21+
22+
var _ perf.Task = (*SpeedTestTask)(nil)
23+
24+
// ID returns task ID.
25+
func (c *SpeedTestTask) ID() string {
26+
return "speedtest"
27+
}
28+
29+
// Cron returns task cron schedule.
30+
func (c *SpeedTestTask) Cron() string {
31+
return "*/30 * * * * *"
32+
}
33+
34+
// Description returns task description.
35+
func (c *SpeedTestTask) Description() string {
36+
return "Measures the download/upload speed of the node."
37+
}
38+
39+
// Jitter returns the max number of seconds the job can sleep before actual execution.
40+
func (c *SpeedTestTask) Jitter() uint32 {
41+
return 0
42+
}
43+
44+
func NewTask() perf.Task {
45+
return &SpeedTestTask{}
46+
}
47+
48+
// Run executes the SpeedTest task.
49+
func (c *SpeedTestTask) Run(ctx context.Context) (interface{}, error) {
50+
serverList, err := speedtest.FetchServers()
51+
if err != nil {
52+
return nil, err
53+
}
54+
servers, err := serverList.FindServer([]int{})
55+
if err != nil {
56+
return nil, err
57+
}
58+
if len(servers) < 1 {
59+
return nil, fmt.Errorf("no speedtest server found")
60+
}
61+
62+
speedtestServer := servers[0]
63+
64+
err = speedtestServer.DownloadTest()
65+
if err != nil {
66+
log.Error().Err(err).Msg("speedtest download test failed")
67+
return nil, err
68+
}
69+
70+
err = speedtestServer.UploadTest()
71+
if err != nil {
72+
log.Error().Err(err).Msg("speedtest upload test failed")
73+
return nil, err
74+
}
75+
76+
download := speedtestServer.DLSpeed.Mbps()
77+
upload := speedtestServer.ULSpeed.Mbps()
78+
79+
if math.IsNaN(download) || math.IsNaN(upload) {
80+
return nil, fmt.Errorf("speedtest returned NaN value")
81+
}
82+
83+
log.Info().Msgf("speedtest result: download %.2f Mbps, upload %.2f Mbps", download, upload)
84+
return SpeedTestResults{
85+
Download: download,
86+
Upload: upload,
87+
}, nil
88+
89+
}

0 commit comments

Comments
 (0)