portlist: document, clean up, fix an open fd spike, optimize a bit

I noticed portlist when looking at some profiles and hadn't looked at
the code much before. This is a first pass over it. It allocates a
fair bit. More love remains, but this does a bit:

name       old time/op    new time/op    delta
GetList-8    9.92ms ± 8%    9.64ms ±12%     ~     (p=0.247 n=10+10)

name       old alloc/op   new alloc/op   delta
GetList-8     931kB ± 0%     869kB ± 0%   -6.70%  (p=0.000 n=10+10)

name       old allocs/op  new allocs/op  delta
GetList-8     4.59k ± 0%     3.69k ± 1%  -19.71%  (p=0.000 n=10+10)

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2020-03-13 20:53:58 -07:00
committed by Brad Fitzpatrick
parent 6c3820e8c4
commit 120273d7f6
9 changed files with 181 additions and 94 deletions

View File

@@ -5,7 +5,6 @@
package portlist
import (
"fmt"
"testing"
)
@@ -62,28 +61,26 @@ udp4 0 0 *.5553 *.*
`
func TestParsePortsNetstat(t *testing.T) {
expect := List{
want := List{
Port{"tcp", 22, "", ""},
Port{"tcp", 23, "", ""},
Port{"tcp", 24, "", ""},
Port{"tcp", 32, "", "sshd"},
Port{"udp", 53, "", "chrome"},
Port{"udp", 53, "", "funball"},
Port{"udp", 5050, "", "CDPSvc"},
Port{"tcp", 32, "sshd", ""},
Port{"udp", 53, "chrome", ""},
Port{"udp", 53, "funball", ""},
Port{"udp", 5050, "CDPSvc", ""},
Port{"udp", 5353, "", ""},
Port{"udp", 5354, "", ""},
Port{"udp", 5453, "", ""},
Port{"udp", 5553, "", ""},
Port{"udp", 9353, "", "iTunes"},
Port{"udp", 9353, "iTunes", ""},
}
pl := parsePortsNetstat(netstat_output)
fmt.Printf("--- expect:\n%v\n", expect)
fmt.Printf("--- got:\n%v\n", pl)
for i := range pl {
if expect[i] != pl[i] {
t.Fatalf("row#%d\n expect=%v\n got=%v\n",
i, expect[i], pl[i])
if pl[i] != want[i] {
t.Errorf("row#%d\n got: %#v\n\nwant: %#v\n",
i, pl[i], want[i])
}
}
}