github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/service/ciscoavc/tcp.go (about) 1 package ciscoavc 2 3 import ( 4 "math" 5 "strings" 6 7 "github.com/zartbot/go_utils/map2value" 8 ) 9 10 func TCPWindowSize(d map[string]interface{}) float64 { 11 var avgWinSize float64 12 winSizeSum, ErrWinSize := map2value.MapToUInt64(d, "trans_tcp_window_size_sum") 13 pkts, ErrPkts := map2value.MapToUInt64(d, "total_pkts") 14 if (ErrWinSize == nil) && (ErrPkts == nil) { 15 avgWinSize = float64(winSizeSum) / float64(pkts) 16 } 17 return avgWinSize 18 } 19 20 //TCPThrouhgput is a function to calculate theoretical network limit 21 //based on the following formula 22 // if has pkt loss rate <= (MSS/RTT)*(C/sqrt(Loss)) 23 // if does not have pkt loss rate <= TCPBufferSize /RTT 24 //MSS: Bytes uint64 25 //RTT: ms float64 26 //Loss: % float64 27 //WindowSize: Bytes float64 28 func TCPTheoreticalThrouhgput(rtt float64, loss float64, winsize float64, mss uint64) float64 { 29 if loss == 0 { 30 if winsize == 0 { 31 return math.Min(16000/rtt, float64(mss)/(rtt*math.Sqrt(0.0001))) * 8000 32 } 33 return winsize / rtt * 8000 34 } 35 return float64(mss) / (rtt * math.Sqrt(loss)) * 8000 36 } 37 38 type TCPBenchmark struct { 39 AvgWindowSize float64 40 TheoreticalBW float64 41 LatencyScore float64 42 BandwidthScore float64 43 RefBW float64 44 } 45 46 func InterfaceBW(d map[string]interface{}) float64 { 47 intf, ErrIntf := map2value.MapToString(d, "observationInterface") 48 //default bw is 1G 49 var intfbw float64 = 1000000000 50 if ErrIntf == nil { 51 if strings.Contains(intf, "Tunnel") { 52 intfbw = 1000000000 53 } else if strings.Contains(intf, "TenGigabitEthernet") { 54 intfbw = 10000000000 55 } else if strings.Contains(intf, "FastEthernet") { 56 intfbw = 100000000 57 } 58 } 59 return intfbw 60 } 61 62 func TCPPerformance(d map[string]interface{}, rtt float64, loss float64, brtt float64, bloss float64) *TCPBenchmark { 63 const MSS uint64 = 1460 64 const DefaultWindowSize float64 = 64000 65 t := new(TCPBenchmark) 66 t.AvgWindowSize = TCPWindowSize(d) 67 if rtt < 1 { 68 rtt = 1 69 } 70 if loss > 1 { 71 loss = 1 72 } 73 //intfbw := InterfaceBW(d) 74 t.TheoreticalBW = TCPTheoreticalThrouhgput(rtt, loss, t.AvgWindowSize, MSS) 75 t.RefBW = TCPTheoreticalThrouhgput(brtt, bloss, DefaultWindowSize, MSS) 76 t.BandwidthScore = math.Min(20*math.Log(t.TheoreticalBW/t.RefBW)+100, 100) 77 if t.BandwidthScore < 0 { 78 t.BandwidthScore = 0 79 } 80 t.LatencyScore = math.Min(100*math.Log(2*brtt/rtt)+32, 100) 81 if t.LatencyScore < 0 { 82 t.LatencyScore = 0 83 } 84 //t.LatencyScore = 1 / (1 + math.Exp(-rtt/brtt)) 85 return t 86 }