Skip to content

Commit

Permalink
feat(api): add RunMulti and RunMultiWithContext
Browse files Browse the repository at this point in the history
  • Loading branch information
r3inbowari committed May 9, 2024
1 parent 13060a2 commit 5e2c02a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
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
2 changes: 1 addition & 1 deletion speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,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

0 comments on commit 5e2c02a

Please sign in to comment.