getPids - don't recursively traverse every dir

`filepath.Walk` recursively traverses every dir, which is not what is
needed for getPids.
Instead only read the list of dirs in the top level of `/proc`.

```
benchmark              old ns/op     new ns/op     delta
BenchmarkGetPids-4     868684        195522        -77.49%
```
This commit is contained in:
Brian Goff
2018-07-18 21:17:59 -07:00
parent afcc156806
commit 01034af976
2 changed files with 69 additions and 36 deletions

View File

@@ -23,6 +23,7 @@ import (
"os"
"os/signal"
"path/filepath"
"regexp"
"runtime"
"syscall"
"testing"
@@ -95,3 +96,21 @@ func TestPKill(t *testing.T) {
t.Fatalf("timeout waiting for %v", sig)
}
}
func BenchmarkGetPids(b *testing.B) {
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
b.Skipf("not supported on GOOS=%s", runtime.GOOS)
}
re, err := regexp.Compile("(^|/)" + filepath.Base(os.Args[0]) + "$")
assert.Empty(b, err)
for i := 0; i < b.N; i++ {
pids := getPids(re)
b.StopTimer()
assert.NotZero(b, pids)
assert.Contains(b, pids, os.Getpid())
b.StartTimer()
}
}