mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-26 03:25:35 +00:00
97b6d3e917
They're not needed for the sockstats logger, and they're somewhat expensive to return (since they involve the creation of a map per label). We now have a separate GetInterfaces() method that returns them instead (which we can still use in the PeerAPI debug endpoint). If changing sockstatlog to sample at 10,000 Hz (instead of the default of 10Hz), the CPU usage would go up to 59% on a iPhone XS. Removing the per-interface stats drops it to 20% (a no-op implementation of Get that returns a fixed value is 16%). Updates tailscale/corp#9230 Updates #3363 Signed-off-by: Mihai Parparita <mihai@tailscale.com>
96 lines
1.9 KiB
Go
96 lines
1.9 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package sockstatlog
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"tailscale.com/net/sockstats"
|
|
)
|
|
|
|
func TestDelta(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
a, b *sockstats.SockStats
|
|
wantStats map[sockstats.Label]deltaStat
|
|
}{
|
|
{
|
|
name: "nil a stat",
|
|
a: nil,
|
|
b: &sockstats.SockStats{},
|
|
wantStats: nil,
|
|
},
|
|
{
|
|
name: "nil b stat",
|
|
a: &sockstats.SockStats{},
|
|
b: nil,
|
|
wantStats: nil,
|
|
},
|
|
{
|
|
name: "no change",
|
|
a: &sockstats.SockStats{
|
|
Stats: map[sockstats.Label]sockstats.SockStat{
|
|
sockstats.LabelDERPHTTPClient: {
|
|
TxBytes: 10,
|
|
},
|
|
},
|
|
},
|
|
b: &sockstats.SockStats{
|
|
Stats: map[sockstats.Label]sockstats.SockStat{
|
|
sockstats.LabelDERPHTTPClient: {
|
|
TxBytes: 10,
|
|
},
|
|
},
|
|
},
|
|
wantStats: nil,
|
|
},
|
|
{
|
|
name: "tx after empty stat",
|
|
a: &sockstats.SockStats{},
|
|
b: &sockstats.SockStats{
|
|
Stats: map[sockstats.Label]sockstats.SockStat{
|
|
sockstats.LabelDERPHTTPClient: {
|
|
TxBytes: 10,
|
|
},
|
|
},
|
|
},
|
|
wantStats: map[sockstats.Label]deltaStat{
|
|
sockstats.LabelDERPHTTPClient: {10, 0},
|
|
},
|
|
},
|
|
{
|
|
name: "rx after non-empty stat",
|
|
a: &sockstats.SockStats{
|
|
Stats: map[sockstats.Label]sockstats.SockStat{
|
|
sockstats.LabelDERPHTTPClient: {
|
|
TxBytes: 10,
|
|
RxBytes: 10,
|
|
},
|
|
},
|
|
},
|
|
b: &sockstats.SockStats{
|
|
Stats: map[sockstats.Label]sockstats.SockStat{
|
|
sockstats.LabelDERPHTTPClient: {
|
|
TxBytes: 10,
|
|
RxBytes: 30,
|
|
},
|
|
},
|
|
},
|
|
wantStats: map[sockstats.Label]deltaStat{
|
|
sockstats.LabelDERPHTTPClient: {0, 20},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
gotStats := delta(tt.a, tt.b)
|
|
if !cmp.Equal(gotStats, tt.wantStats) {
|
|
t.Errorf("gotStats = %v, want %v", gotStats, tt.wantStats)
|
|
}
|
|
})
|
|
}
|
|
}
|