mirror of
https://github.com/evilsocket/opensnitch.git
synced 2025-03-04 08:34:40 +01:00

- update the descriptors/inodes of a PID when it's found in cache. - when a descriptor/inode is found in cache, push it to the top of the descriptors list. The next time it's found in cache it'll be in the 1st position of the list, saving CPU time. - added test cases and benchmark helpers to help analyzing performance.
42 lines
892 B
Go
42 lines
892 B
Go
package procmon
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetProcPids(t *testing.T) {
|
|
pids := getProcPids("/proc")
|
|
|
|
if len(pids) == 0 {
|
|
t.Error("getProcPids() should not be 0", pids)
|
|
}
|
|
}
|
|
|
|
func TestLookupPidDescriptors(t *testing.T) {
|
|
pidsFd := lookupPidDescriptors(fmt.Sprint("/proc/", myPid, "/fd/"), myPid)
|
|
if len(pidsFd) == 0 {
|
|
t.Error("getProcPids() should not be 0", pidsFd)
|
|
}
|
|
}
|
|
|
|
func TestLookupPidInProc(t *testing.T) {
|
|
// we expect that the inode 1 points to /dev/null
|
|
expect := "/dev/null"
|
|
foundPid := lookupPidInProc("/proc/", expect, "", myPid)
|
|
if foundPid == -1 {
|
|
t.Error("lookupPidInProc() should not return -1")
|
|
}
|
|
}
|
|
|
|
func BenchmarkGetProcs(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
getProcPids("/proc")
|
|
}
|
|
}
|
|
|
|
func BenchmarkLookupPidDescriptors(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
lookupPidDescriptors(fmt.Sprint("/proc/", myPid, "/fd/"), myPid)
|
|
}
|
|
}
|