Skip to content
File

Blob: spec/rtt/rtt.go

go49 lines
1package rtt
2 
3import (
4 "fmt"
5 "time"
6 
7 "go.miragespace.co/specter/spec/protocol"
8)
9 
10type Recorder interface {
11 Snapshot(key string, past time.Duration) *Statistics
12 RecordLatency(key string, latency float64)
13 RecordSent(key string)
14 RecordLost(key string)
15 Drop(key string)
16}
17 
18type Statistics struct {
19 Since time.Time `json:"since"`
20 Until time.Time `json:"until"`
21 Min time.Duration `json:"min"`
22 Average time.Duration `json:"average"`
23 Max time.Duration `json:"max"`
24 StandardDeviation time.Duration `json:"mdev"`
25 Sent uint64 `json:"sent"`
26 Lost uint64 `json:"lost"`
27}
28 
29func (s *Statistics) String() string {
30 if s == nil {
31 return "(N/A)"
32 }
33 var lost float64
34 if s.Sent > 0 {
35 lost = float64(s.Lost) / float64(s.Sent)
36 }
37 return fmt.Sprintf("%d packets transmitted, %d received, %.0f%% packet loss\nmin/avg/max/mdev = %v/%v/%v/%v", s.Sent, s.Sent-s.Lost, lost, s.Min, s.Average, s.Max, s.StandardDeviation)
38}
39 
40func MakeMeasurementKey(node *protocol.Node) string {
41 qMapKey := node.GetAddress() + "/"
42 if node.GetUnknown() {
43 qMapKey = qMapKey + "-1"
44 } else {
45 qMapKey = qMapKey + "PHY"
46 }
47 return qMapKey
48}