Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add RunMulti and RunMultiWithContext #204

Merged
merged 3 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions example/packet_loss/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/showwin/speedtest-go/speedtest/transport"
"log"
"sync"
"time"
)

// Note: The current packet loss analyzer does not support udp over http.
Expand All @@ -19,12 +20,15 @@ func main() {
targets := serverList.Available()

// 2. Create a packet loss analyzer, use default options
analyzer, err := speedtest.NewPacketLossAnalyzer(nil)
checkError(err)
analyzer := speedtest.NewPacketLossAnalyzer(&speedtest.PacketLossAnalyzerOptions{
PacketSendingInterval: time.Millisecond * 100,
})

wg := &sync.WaitGroup{}
// 3. Perform packet loss analysis on all available servers
var hosts []string
for _, server := range *targets {
hosts = append(hosts, server.Host)
wg.Add(1)
//ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
//go func(server *speedtest.Server, analyzer *speedtest.PacketLossAnalyzer, ctx context.Context, cancel context.CancelFunc) {
Expand All @@ -46,6 +50,11 @@ func main() {
}(server, analyzer)
}
wg.Wait()

// use mixed PacketLoss
mixed, err := analyzer.RunMulti(hosts)
checkError(err)
fmt.Printf("Mixed packets lossed: %.2f\n", mixed)
}

func checkError(err error) {
Expand Down
5 changes: 2 additions & 3 deletions speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ func main() {
})

// 3.0 create a packet loss analyzer, use default options
var analyzer *speedtest.PacketLossAnalyzer
analyzer, err = speedtest.NewPacketLossAnalyzer(&speedtest.PacketLossAnalyzerOptions{
var analyzer = speedtest.NewPacketLossAnalyzer(&speedtest.PacketLossAnalyzerOptions{
SourceInterface: *source,
})

Expand All @@ -151,7 +150,7 @@ func main() {
packetLossAnalyzerCancel() // cancel early
}
}()
task.Println("Packet Loss Analyzer: Running in background (<= 30 Sec)")
task.Println("Packet Loss Analyzer: Running in background (<= 30 Secs)")
task.Complete()
})

Expand Down
41 changes: 39 additions & 2 deletions speedtest/loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"github.com/showwin/speedtest-go/speedtest/transport"
"net"
"sync"
"time"
)

Expand All @@ -22,7 +23,7 @@ type PacketLossAnalyzer struct {
options *PacketLossAnalyzerOptions
}

func NewPacketLossAnalyzer(options *PacketLossAnalyzerOptions) (*PacketLossAnalyzer, error) {
func NewPacketLossAnalyzer(options *PacketLossAnalyzerOptions) *PacketLossAnalyzer {
if options == nil {
options = &PacketLossAnalyzerOptions{}
}
Expand Down Expand Up @@ -56,7 +57,43 @@ func NewPacketLossAnalyzer(options *PacketLossAnalyzerOptions) (*PacketLossAnaly
}
return &PacketLossAnalyzer{
options: options,
}, nil
}
}

// RunMulti Mix all servers to get the average packet loss.
func (pla *PacketLossAnalyzer) RunMulti(hosts []string) (float64, error) {
ctx, cancel := context.WithTimeout(context.Background(), pla.options.SamplingDuration)
defer cancel()
return pla.RunMultiWithContext(ctx, hosts)
}

func (pla *PacketLossAnalyzer) RunMultiWithContext(ctx context.Context, hosts []string) (float64, error) {
results := make(map[string]float64)
mutex := &sync.Mutex{}
wg := &sync.WaitGroup{}
for _, host := range hosts {
wg.Add(1)
go func(h string) {
defer wg.Done()
_ = pla.RunWithContext(ctx, h, func(packetLoss *transport.PLoss) {
loss := packetLoss.Loss()
if loss != -1 {
mutex.Lock()
results[h] = loss
mutex.Unlock()
}
})
}(host)
}
wg.Wait()
if len(results) == 0 {
return -1, transport.ErrUnsupported
}
packetLossAvg := 0.0
for _, hostPacketLoss := range results {
packetLossAvg += hostPacketLoss
}
return packetLossAvg / float64(len(results)), nil
}

func (pla *PacketLossAnalyzer) Run(host string, callback func(packetLoss *transport.PLoss)) error {
Expand Down
3 changes: 3 additions & 0 deletions speedtest/transport/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ func (p PLoss) String() string {
}

func (p PLoss) Loss() float64 {
if p.Sent == 0 {
return -1
}
return 1 - (float64(p.Sent-p.Dup))/float64(p.Max+1)
}

Expand Down
Loading