-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: remove mutex, add concurrency tests
Public proctree methods already make use of the mutex of the inner lru cache. This commit removes the outer mutex and adds concurrency tests to ensure that the proctree is safe to use concurrently.
- Loading branch information
Showing
2 changed files
with
62 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package proctree | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"testing" | ||
|
||
traceetime "github.com/aquasecurity/tracee/pkg/time" | ||
) | ||
|
||
func TestProcessTreeConcurrency(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
config := ProcTreeConfig{ | ||
Source: SourceBoth, | ||
ProcessCacheSize: DefaultProcessCacheSize, | ||
ThreadCacheSize: DefaultThreadCacheSize, | ||
ProcfsInitialization: false, | ||
ProcfsQuerying: false, | ||
} | ||
|
||
timeNormalizer := traceetime.NewRelativeTimeNormalizer(0) | ||
pt, err := NewProcessTree(ctx, config, timeNormalizer) | ||
if err != nil { | ||
t.Fatalf("failed to create ProcessTree: %v", err) | ||
} | ||
|
||
var wg sync.WaitGroup | ||
startSignal := make(chan struct{}) | ||
|
||
testFunc := func(hash uint32) { | ||
defer wg.Done() | ||
|
||
<-startSignal // Wait for the signal to start | ||
|
||
// Public methods | ||
pt.GetProcessByHash(hash) | ||
pt.GetOrCreateProcessByHash(hash) | ||
pt.GetThreadByHash(hash) | ||
pt.GetOrCreateThreadByHash(hash) | ||
} | ||
|
||
// Run tests concurrently for different hashes | ||
for i := 0; i < 1000; i++ { | ||
wg.Add(1) | ||
go testFunc(uint32(i)) | ||
} | ||
|
||
// Run tests concurrently for the same hash | ||
for i := 0; i < 1000; i++ { | ||
wg.Add(1) | ||
go testFunc(42) | ||
} | ||
|
||
// Signal all goroutines to start at the same time | ||
close(startSignal) | ||
|
||
wg.Wait() | ||
} |