all: add (*testing.B).ReportAllocs() to every benchmark

This ensures that we can properly track and catch allocation
slippages that could otherwise have been missed.

Fixes #2748
pull/2753/head^2
Emmanuel T Odeke 3 years ago committed by Brad Fitzpatrick
parent 44d71d1e42
commit 0daa32943e

@ -1015,6 +1015,7 @@ func BenchmarkFull(b *testing.B) {
for _, tt := range tests { for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) { b.Run(tt.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
syncRespond(r, tt.request) syncRespond(r, tt.request)
} }

@ -75,6 +75,7 @@ func TestNewContainsIPFunc(t *testing.T) {
var sinkIP netaddr.IP var sinkIP netaddr.IP
func BenchmarkTailscaleServiceAddr(b *testing.B) { func BenchmarkTailscaleServiceAddr(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
sinkIP = TailscaleServiceIP() sinkIP = TailscaleServiceIP()
} }

@ -397,6 +397,7 @@ func TestClose(t *testing.T) {
} }
func BenchmarkWrite(b *testing.B) { func BenchmarkWrite(b *testing.B) {
b.ReportAllocs()
ftun, tun := newFakeTUN(b.Logf, true) ftun, tun := newFakeTUN(b.Logf, true)
defer tun.Close() defer tun.Close()

@ -35,12 +35,14 @@ func TestUnmarshalZero(t *testing.T) {
} }
func BenchmarkMonoNow(b *testing.B) { func BenchmarkMonoNow(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Now() Now()
} }
} }
func BenchmarkTimeNow(b *testing.B) { func BenchmarkTimeNow(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
time.Now() time.Now()
} }

@ -152,7 +152,6 @@ func BenchmarkParse3339(b *testing.B) {
run := func(in string) func(*testing.B) { run := func(in string) func(*testing.B) {
return func(b *testing.B) { return func(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
t, err := time.Parse(time.RFC3339Nano, in) t, err := time.Parse(time.RFC3339Nano, in)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
@ -182,6 +181,7 @@ func BenchmarkParse3339(b *testing.B) {
} }
func BenchmarkParseInt(b *testing.B) { func BenchmarkParseInt(b *testing.B) {
b.ReportAllocs()
var out int var out int
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
parseInt(mem.S("148487491"), &out) parseInt(mem.S("148487491"), &out)

@ -1194,6 +1194,10 @@ func addTestEndpoint(tb testing.TB, conn *Conn, sendConn net.PacketConn) (tailcf
} }
func setUpReceiveFrom(tb testing.TB) (roundTrip func()) { func setUpReceiveFrom(tb testing.TB) (roundTrip func()) {
if b, ok := tb.(*testing.B); ok {
b.ReportAllocs()
}
conn := newTestConn(tb) conn := newTestConn(tb)
tb.Cleanup(func() { conn.Close() }) tb.Cleanup(func() { conn.Close() })
conn.logf = logger.Discard conn.logf = logger.Discard
@ -1295,6 +1299,7 @@ func BenchmarkReceiveFrom(b *testing.B) {
} }
func BenchmarkReceiveFrom_Native(b *testing.B) { func BenchmarkReceiveFrom_Native(b *testing.B) {
b.ReportAllocs()
recvConn, err := net.ListenPacket("udp4", "127.0.0.1:0") recvConn, err := net.ListenPacket("udp4", "127.0.0.1:0")
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)

@ -214,6 +214,8 @@ func BenchmarkGenLocalAddrFunc(b *testing.B) {
lanot := netaddr.MustParseIP("5.5.5.5") lanot := netaddr.MustParseIP("5.5.5.5")
var x bool var x bool
b.Run("map1", func(b *testing.B) { b.Run("map1", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
m := map[netaddr.IP]bool{ m := map[netaddr.IP]bool{
la1: true, la1: true,
} }
@ -223,6 +225,8 @@ func BenchmarkGenLocalAddrFunc(b *testing.B) {
} }
}) })
b.Run("map2", func(b *testing.B) { b.Run("map2", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
m := map[netaddr.IP]bool{ m := map[netaddr.IP]bool{
la1: true, la1: true,
la2: true, la2: true,
@ -233,6 +237,8 @@ func BenchmarkGenLocalAddrFunc(b *testing.B) {
} }
}) })
b.Run("or1", func(b *testing.B) { b.Run("or1", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
f := func(t netaddr.IP) bool { f := func(t netaddr.IP) bool {
return t == la1 return t == la1
} }
@ -242,6 +248,8 @@ func BenchmarkGenLocalAddrFunc(b *testing.B) {
} }
}) })
b.Run("or2", func(b *testing.B) { b.Run("or2", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
f := func(t netaddr.IP) bool { f := func(t netaddr.IP) bool {
return t == la1 || t == la2 return t == la1 || t == la2
} }

Loading…
Cancel
Save