Skip to content

Commit

Permalink
all: use testingutil.MinAllocsPerRun
Browse files Browse the repository at this point in the history
There are a few remaining uses of testing.AllocsPerRun:
Two in which we only log the number of allocations,
and one in which dynamically calculate the allocations
target based on a different AllocsPerRun run.

This also allows us to tighten the "no allocs"
test in wgengine/filter.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
  • Loading branch information
josharian committed Oct 28, 2021
1 parent 1df865a commit 94fb42d
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 58 deletions.
17 changes: 9 additions & 8 deletions logtail/logtail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"strings"
"testing"
"time"

"tailscale.com/tstest"
)

func TestFastShutdown(t *testing.T) {
Expand Down Expand Up @@ -213,11 +215,11 @@ var sink []byte
func TestLoggerEncodeTextAllocs(t *testing.T) {
lg := &Logger{timeNow: time.Now}
inBuf := []byte("some text to encode")
n := testing.AllocsPerRun(1000, func() {
err := tstest.MinAllocsPerRun(t, 1, func() {
sink = lg.encodeText(inBuf, false)
})
if int(n) != 1 {
t.Logf("allocs = %d; want 1", int(n))
if err != nil {
t.Fatal(err)
}
}

Expand Down Expand Up @@ -298,15 +300,14 @@ func TestPublicIDUnmarshalText(t *testing.T) {
if id.String() != hexStr {
t.Errorf("String = %q; want %q", id.String(), hexStr)
}

n := int(testing.AllocsPerRun(1000, func() {
err := tstest.MinAllocsPerRun(t, 0, func() {
var id PublicID
if err := id.UnmarshalText(x); err != nil {
t.Fatal(err)
}
}))
if n != 0 {
t.Errorf("allocs = %v; want 0", n)
})
if err != nil {
t.Fatal(err)
}
}

Expand Down
10 changes: 6 additions & 4 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"runtime"
"testing"

"tailscale.com/tstest"
)

func TestCurrentFileDescriptors(t *testing.T) {
Expand All @@ -19,11 +21,11 @@ func TestCurrentFileDescriptors(t *testing.T) {
t.Fatalf("got %v; want >= 3", n)
}

allocs := int(testing.AllocsPerRun(100, func() {
err := tstest.MinAllocsPerRun(t, 0, func() {
n = CurrentFDs()
}))
if allocs != 0 {
t.Errorf("allocs = %v; want 0", allocs)
})
if err != nil {
t.Fatal(err)
}

// Open some FDs.
Expand Down
8 changes: 4 additions & 4 deletions net/dns/resolver/tsdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ func TestAllocs(t *testing.T) {
tests := []struct {
name string
query []byte
want int
want uint64
}{
// Name lowercasing, response slice created by dns.NewBuilder,
// and closure allocation from go call.
Expand All @@ -964,11 +964,11 @@ func TestAllocs(t *testing.T) {
}

for _, tt := range tests {
allocs := testing.AllocsPerRun(100, func() {
err := tstest.MinAllocsPerRun(t, tt.want, func() {
syncRespond(r, tt.query)
})
if int(allocs) > tt.want {
t.Errorf("%s: allocs = %v; want %v", tt.name, allocs, tt.want)
if err != nil {
t.Errorf("%s: %v", tt.name, err)
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions net/flowtrack/flowtrack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"inet.af/netaddr"
"tailscale.com/tstest"
)

func TestCache(t *testing.T) {
Expand Down Expand Up @@ -67,16 +68,16 @@ func TestCache(t *testing.T) {
wantVal(k3, 30)
wantLen(1)

allocs := int(testing.AllocsPerRun(1000, func() {
err := tstest.MinAllocsPerRun(t, 0, func() {
got, ok := c.Get(k3)
if !ok {
t.Fatal("missing k3")
}
if got != 30 {
t.Fatalf("got = %d; want 30", got)
}
}))
if allocs != 0 {
t.Errorf("allocs = %v; want 0", allocs)
})
if err != nil {
t.Error(err)
}
}
13 changes: 7 additions & 6 deletions net/packet/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"inet.af/netaddr"
"tailscale.com/tstest"
"tailscale.com/types/ipproto"
)

Expand Down Expand Up @@ -378,11 +379,11 @@ func TestParsedString(t *testing.T) {
})
}

allocs := testing.AllocsPerRun(1000, func() {
err := tstest.MinAllocsPerRun(t, 1, func() {
sinkString = tests[0].qdecode.String()
})
if allocs != 1 {
t.Errorf("allocs = %v; want 1", allocs)
if err != nil {
t.Error(err)
}
}

Expand Down Expand Up @@ -415,12 +416,12 @@ func TestDecode(t *testing.T) {
})
}

allocs := testing.AllocsPerRun(1000, func() {
err := tstest.MinAllocsPerRun(t, 0, func() {
var got Parsed
got.Decode(tests[0].buf)
})
if allocs != 0 {
t.Errorf("allocs = %v; want 0", allocs)
if err != nil {
t.Error(err)
}
}

Expand Down
6 changes: 3 additions & 3 deletions net/tstun/wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,16 +370,16 @@ func TestAllocs(t *testing.T) {
defer tun.Close()

buf := []byte{0x00}
allocs := testing.AllocsPerRun(100, func() {
err := tstest.MinAllocsPerRun(t, 0, func() {
_, err := ftun.Write(buf, 0)
if err != nil {
t.Errorf("write: error: %v", err)
return
}
})

if allocs > 0 {
t.Errorf("read allocs = %v; want 0", allocs)
if err != nil {
t.Error(err)
}
}

Expand Down
9 changes: 5 additions & 4 deletions tailcfg/tailcfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"inet.af/netaddr"
"tailscale.com/tstest"
"tailscale.com/types/key"
"tailscale.com/version"
)
Expand Down Expand Up @@ -541,11 +542,11 @@ func TestAppendKeyAllocs(t *testing.T) {
t.Skip("skipping in race detector") // append(b, make([]byte, N)...) not optimized in compiler with race
}
var k [32]byte
n := int(testing.AllocsPerRun(1000, func() {
err := tstest.MinAllocsPerRun(t, 1, func() {
sinkBytes = keyMarshalText("prefix", k)
}))
if n != 1 {
t.Fatalf("allocs = %v; want 1", n)
})
if err != nil {
t.Fatal(err)
}
}

Expand Down
9 changes: 5 additions & 4 deletions types/wgkey/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"bytes"
"encoding/json"
"testing"

"tailscale.com/tstest"
)

func TestKeyBasics(t *testing.T) {
Expand Down Expand Up @@ -134,12 +136,11 @@ func TestPrivateKeyBasics(t *testing.T) {

func TestMarshalJSONAllocs(t *testing.T) {
var k Key
f := testing.AllocsPerRun(100, func() {
err := tstest.MinAllocsPerRun(t, 1, func() {
k.MarshalJSON()
})
n := int(f)
if n != 1 {
t.Fatalf("max one alloc per Key.MarshalJSON, got %d", n)
if err != nil {
t.Fatal(err)
}
}

Expand Down
9 changes: 5 additions & 4 deletions version/cmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"tailscale.com/tstest"
)

func TestParse(t *testing.T) {
Expand Down Expand Up @@ -39,11 +40,11 @@ func TestParse(t *testing.T) {
if diff := cmp.Diff(gotParsed, test.parsed); diff != "" {
t.Errorf("parse(%q) diff (-got+want):\n%s", test.version, diff)
}
n := int(testing.AllocsPerRun(1000, func() {
err := tstest.MinAllocsPerRun(t, 0, func() {
gotParsed, got = parse(test.version)
}))
if n != 0 {
t.Errorf("parse(%q) allocs = %d; want 0", test.version, n)
})
if err != nil {
t.Errorf("parse(%q): %v", test.version, err)
}
}
}
Expand Down
28 changes: 13 additions & 15 deletions wgengine/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"tailscale.com/net/packet"
"tailscale.com/net/tsaddr"
"tailscale.com/tailcfg"
"tailscale.com/tstest"
"tailscale.com/tstime/rate"
"tailscale.com/types/ipproto"
"tailscale.com/types/logger"
Expand Down Expand Up @@ -189,23 +190,21 @@ func TestNoAllocs(t *testing.T) {
tests := []struct {
name string
dir direction
want int
packet []byte
}{
{"tcp4_in", in, 0, tcp4Packet},
{"tcp6_in", in, 0, tcp6Packet},
{"tcp4_out", out, 0, tcp4Packet},
{"tcp6_out", out, 0, tcp6Packet},
{"udp4_in", in, 0, udp4Packet},
{"udp6_in", in, 0, udp6Packet},
// One alloc is inevitable (an lru cache update)
{"udp4_out", out, 1, udp4Packet},
{"udp6_out", out, 1, udp6Packet},
{"tcp4_in", in, tcp4Packet},
{"tcp6_in", in, tcp6Packet},
{"tcp4_out", out, tcp4Packet},
{"tcp6_out", out, tcp6Packet},
{"udp4_in", in, udp4Packet},
{"udp6_in", in, udp6Packet},
{"udp4_out", out, udp4Packet},
{"udp6_out", out, udp6Packet},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := int(testing.AllocsPerRun(1000, func() {
err := tstest.MinAllocsPerRun(t, 0, func() {
q := &packet.Parsed{}
q.Decode(test.packet)
switch test.dir {
Expand All @@ -214,10 +213,9 @@ func TestNoAllocs(t *testing.T) {
case out:
acl.RunOut(q, 0)
}
}))

if got > test.want {
t.Errorf("got %d allocs per run; want at most %d", got, test.want)
})
if err != nil {
t.Error(err)
}
})
}
Expand Down
3 changes: 1 addition & 2 deletions wgengine/magicsock/magicsock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import (
"tailscale.com/types/wgkey"
"tailscale.com/util/cibuild"
"tailscale.com/util/racebuild"
"tailscale.com/util/testingutil"
"tailscale.com/wgengine/filter"
"tailscale.com/wgengine/wgcfg"
"tailscale.com/wgengine/wgcfg/nmcfg"
Expand Down Expand Up @@ -1353,7 +1352,7 @@ func TestReceiveFromAllocs(t *testing.T) {
}
t.Logf("allowing %d allocs for Go version %q", maxAllocs, runtime.Version())
roundTrip := setUpReceiveFrom(t)
err := testingutil.MinAllocsPerRun(t, uint64(maxAllocs), roundTrip)
err := tstest.MinAllocsPerRun(t, uint64(maxAllocs), roundTrip)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 94fb42d

Please sign in to comment.