mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-23 03:17:43 +00:00
portlist: reduce CPU parsing portlist
Avoid splitting fields in the common case. Field splitting was 84% of the overall CPU. name old time/op new time/op delta ParsePorts-6 33.3ms ± 2% 6.3ms ± 4% -80.97% (p=0.000 n=9+10) name old alloc/op new alloc/op delta ParsePorts-6 520B ±79% 408B ± 0% -21.49% (p=0.046 n=10+8) name old allocs/op new allocs/op delta ParsePorts-6 7.00 ± 0% 7.00 ± 0% ~ (all equal) Updates tailscale/corp#2566 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:

committed by
Brad Fitzpatrick

parent
64e9ce8df1
commit
5a9d977c78
@@ -13,10 +13,34 @@ import (
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
func TestFieldIndex(t *testing.T) {
|
||||
tests := []struct {
|
||||
in string
|
||||
field int
|
||||
want int
|
||||
}{
|
||||
{"foo", 0, 0},
|
||||
{" foo", 0, 2},
|
||||
{"foo bar", 1, 5},
|
||||
{" foo bar", 1, 6},
|
||||
{" foo bar", 2, -1},
|
||||
{" foo bar ", 2, -1},
|
||||
{" foo bar x", 2, 10},
|
||||
{" 1: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 34062 1 0000000000000000 100 0 0 10 0",
|
||||
2, 19},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := fieldIndex([]byte(tt.in), tt.field); got != tt.want {
|
||||
t.Errorf("fieldIndex(%q, %v) = %v; want %v", tt.in, tt.field, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsePorts(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
in string
|
||||
file string
|
||||
want []Port
|
||||
}{
|
||||
{
|
||||
@@ -26,6 +50,7 @@ func TestParsePorts(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "ipv4",
|
||||
file: "tcp",
|
||||
in: `header line
|
||||
0: 0100007F:0277 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 22303 1 0000000000000000 100 0 0 10 0
|
||||
1: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 34062 1 0000000000000000 100 0 0 10 0
|
||||
@@ -37,6 +62,7 @@ func TestParsePorts(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "ipv6",
|
||||
file: "tcp6",
|
||||
in: ` sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
|
||||
0: 00000000000000000000000001000000:0277 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 35720 1 0000000000000000 100 0 0 10 0
|
||||
1: 00000000000000000000000000000000:1F91 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 142240557 1 0000000000000000 100 0 0 10 0
|
||||
@@ -50,17 +76,20 @@ func TestParsePorts(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
buf := bytes.NewBufferString(test.in)
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
buf := bytes.NewBufferString(tt.in)
|
||||
r := bufio.NewReader(buf)
|
||||
|
||||
got, err := parsePorts(r, "tcp")
|
||||
file := "tcp"
|
||||
if tt.file != "" {
|
||||
file = tt.file
|
||||
}
|
||||
got, err := parsePorts(r, file)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(got, test.want, cmp.AllowUnexported(Port{})); diff != "" {
|
||||
if diff := cmp.Diff(got, tt.want, cmp.AllowUnexported(Port{})); diff != "" {
|
||||
t.Errorf("unexpected parsed ports (-got+want):\n%s", diff)
|
||||
}
|
||||
})
|
||||
@@ -91,7 +120,7 @@ func BenchmarkParsePorts(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
r.Seek(0, io.SeekStart)
|
||||
br.Reset(r)
|
||||
got, err := parsePorts(br, "tcp")
|
||||
got, err := parsePorts(br, "tcp6")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user