(ctx context.Context, agentConn workspacesdk.AgentConn, start, end time.Time, counts map[netlogtype.Connection]netlogtype.Counts)
| 1627 | } |
| 1628 | |
| 1629 | func collectNetworkStats(ctx context.Context, agentConn workspacesdk.AgentConn, start, end time.Time, counts map[netlogtype.Connection]netlogtype.Counts) (*sshNetworkStats, error) { |
| 1630 | latency, p2p, pingResult, err := agentConn.Ping(ctx) |
| 1631 | if err != nil { |
| 1632 | return nil, err |
| 1633 | } |
| 1634 | node := agentConn.TailnetConn().Node() |
| 1635 | derpMap := agentConn.TailnetConn().DERPMap() |
| 1636 | |
| 1637 | totalRx := uint64(0) |
| 1638 | totalTx := uint64(0) |
| 1639 | for _, stat := range counts { |
| 1640 | totalRx += stat.RxBytes |
| 1641 | totalTx += stat.TxBytes |
| 1642 | } |
| 1643 | // Tracking the time since last request is required because |
| 1644 | // ExtractTrafficStats() resets its counters after each call. |
| 1645 | dur := end.Sub(start) |
| 1646 | uploadSecs := float64(totalTx) / dur.Seconds() |
| 1647 | downloadSecs := float64(totalRx) / dur.Seconds() |
| 1648 | |
| 1649 | preferredDerpName := tailnet.ExtractPreferredDERPName(pingResult, node, derpMap) |
| 1650 | derpLatency := tailnet.ExtractDERPLatency(node, derpMap) |
| 1651 | if _, ok := derpLatency[preferredDerpName]; !ok { |
| 1652 | derpLatency[preferredDerpName] = 0 |
| 1653 | } |
| 1654 | derpLatencyMs := maps.Map(derpLatency, func(dur time.Duration) float64 { |
| 1655 | return float64(dur) / float64(time.Millisecond) |
| 1656 | }) |
| 1657 | |
| 1658 | return &sshNetworkStats{ |
| 1659 | P2P: p2p, |
| 1660 | Latency: float64(latency.Microseconds()) / 1000, |
| 1661 | PreferredDERP: preferredDerpName, |
| 1662 | DERPLatency: derpLatencyMs, |
| 1663 | UploadBytesSec: int64(uploadSecs), |
| 1664 | DownloadBytesSec: int64(downloadSecs), |
| 1665 | }, nil |
| 1666 | } |
| 1667 | |
| 1668 | type coderConnectDialerContextKey struct{} |
| 1669 |
no test coverage detected