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:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user