mirror of
https://github.com/tailscale/tailscale.git
synced 2024-12-11 10:44:41 +00:00
db2cc393af
This is similar to the golang.org/x/tools/internal/fastwalk I'd previously written but not recursive and using mem.RO. The metrics package already had some Linux-specific directory reading code in it. Move that out to a new general package that can be reused by portlist too, which helps its scanning of all /proc files: name old time/op new time/op delta FindProcessNames-8 2.79ms ± 6% 2.45ms ± 7% -12.11% (p=0.000 n=10+10) name old alloc/op new alloc/op delta FindProcessNames-8 62.9kB ± 0% 33.5kB ± 0% -46.76% (p=0.000 n=9+10) name old allocs/op new allocs/op delta FindProcessNames-8 2.25k ± 0% 0.38k ± 0% -82.98% (p=0.000 n=9+10) Change-Id: I75db393032c328f12d95c39f71c9742c375f207a Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
94 lines
2.3 KiB
Go
94 lines
2.3 KiB
Go
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package dirwalk
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"runtime"
|
|
"sort"
|
|
"testing"
|
|
|
|
"go4.org/mem"
|
|
)
|
|
|
|
func TestWalkShallowOSSpecific(t *testing.T) {
|
|
if osWalkShallow == nil {
|
|
t.Skip("no OS-specific implementation")
|
|
}
|
|
testWalkShallow(t, false)
|
|
}
|
|
|
|
func TestWalkShallowPortable(t *testing.T) {
|
|
testWalkShallow(t, true)
|
|
}
|
|
|
|
func testWalkShallow(t *testing.T, portable bool) {
|
|
if portable {
|
|
old := osWalkShallow
|
|
defer func() { osWalkShallow = old }()
|
|
osWalkShallow = nil
|
|
}
|
|
d := t.TempDir()
|
|
|
|
t.Run("basics", func(t *testing.T) {
|
|
if err := os.WriteFile(filepath.Join(d, "foo"), []byte("1"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(d, "bar"), []byte("22"), 0400); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Mkdir(filepath.Join(d, "baz"), 0777); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var got []string
|
|
if err := WalkShallow(mem.S(d), func(name mem.RO, de os.DirEntry) error {
|
|
var size int64
|
|
if fi, err := de.Info(); err != nil {
|
|
t.Errorf("Info stat error on %q: %v", de.Name(), err)
|
|
} else if !fi.IsDir() {
|
|
size = fi.Size()
|
|
}
|
|
got = append(got, fmt.Sprintf("%q %q dir=%v type=%d size=%v", name.StringCopy(), de.Name(), de.IsDir(), de.Type(), size))
|
|
return nil
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sort.Strings(got)
|
|
want := []string{
|
|
`"bar" "bar" dir=false type=0 size=2`,
|
|
`"baz" "baz" dir=true type=2147483648 size=0`,
|
|
`"foo" "foo" dir=false type=0 size=1`,
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("mismatch:\n got %#q\nwant %#q", got, want)
|
|
}
|
|
})
|
|
|
|
t.Run("err_not_exist", func(t *testing.T) {
|
|
err := WalkShallow(mem.S(filepath.Join(d, "not_exist")), func(name mem.RO, de os.DirEntry) error {
|
|
return nil
|
|
})
|
|
if !os.IsNotExist(err) {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("allocs", func(t *testing.T) {
|
|
allocs := int(testing.AllocsPerRun(1000, func() {
|
|
if err := WalkShallow(mem.S(d), func(name mem.RO, de os.DirEntry) error { return nil }); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}))
|
|
t.Logf("allocs = %v", allocs)
|
|
if !portable && runtime.GOOS == "linux" && allocs != 0 {
|
|
t.Errorf("unexpected allocs: got %v, want 0", allocs)
|
|
}
|
|
})
|
|
}
|