From cb3c6de2c89abf00218820d6fcf2d3da28aa592e Mon Sep 17 00:00:00 2001 From: Ingo Oppermann Date: Tue, 30 Apr 2024 14:10:47 +0200 Subject: [PATCH] Add optimized listing if no patterns are provided --- restream/restream.go | 71 ++++++++++++++++++++-------------- restream/restream_test.go | 80 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 28 deletions(-) diff --git a/restream/restream.go b/restream/restream.go index 603c919c..6fd5338c 100644 --- a/restream/restream.go +++ b/restream/restream.go @@ -1231,8 +1231,6 @@ func (r *restream) UpdateProcess(id app.ProcessID, config *app.Config) error { } func (r *restream) GetProcessIDs(idpattern, refpattern, ownerpattern, domainpattern string) []app.ProcessID { - ids := []app.ProcessID{} - count := 0 var idglob glob.Glob @@ -1260,45 +1258,62 @@ func (r *restream) GetProcessIDs(idpattern, refpattern, ownerpattern, domainpatt domainglob, _ = glob.Compile(domainpattern) } + var ids []app.ProcessID + r.lock.RLock() defer r.lock.RUnlock() - for _, t := range r.tasks { - matches := 0 - if idglob != nil { - if match := idglob.Match(t.id); match { - matches++ + if count == 0 { + ids = make([]app.ProcessID, 0, len(r.tasks)) + + for _, t := range r.tasks { + tid := app.ProcessID{ + ID: t.id, + Domain: t.domain, } - } - if refglob != nil { - if match := refglob.Match(t.reference); match { - matches++ + ids = append(ids, tid) + } + } else { + ids = []app.ProcessID{} + + for _, t := range r.tasks { + matches := 0 + if idglob != nil { + if match := idglob.Match(t.id); match { + matches++ + } } - } - if ownerglob != nil { - if match := ownerglob.Match(t.owner); match { - matches++ + if refglob != nil { + if match := refglob.Match(t.reference); match { + matches++ + } } - } - if domainglob != nil { - if match := domainglob.Match(t.domain); match { - matches++ + if ownerglob != nil { + if match := ownerglob.Match(t.owner); match { + matches++ + } } - } - if count != matches { - continue - } + if domainglob != nil { + if match := domainglob.Match(t.domain); match { + matches++ + } + } - tid := app.ProcessID{ - ID: t.id, - Domain: t.domain, - } + if count != matches { + continue + } - ids = append(ids, tid) + tid := app.ProcessID{ + ID: t.id, + Domain: t.domain, + } + + ids = append(ids, tid) + } } return ids diff --git a/restream/restream_test.go b/restream/restream_test.go index dd7d7a5c..3932b6a7 100644 --- a/restream/restream_test.go +++ b/restream/restream_test.go @@ -2,7 +2,9 @@ package restream import ( "fmt" + "math/rand" "os" + "strconv" "testing" "time" @@ -1526,3 +1528,81 @@ func TestProcessLimit(t *testing.T) { require.Equal(t, ncpu*process.LimitCPU, status.CPU.Limit) require.Equal(t, process.LimitMemory, status.Memory.Limit) } + +func BenchmarkGetProcessIDs(b *testing.B) { + rs, err := getDummyRestreamer(nil, nil, nil, nil) + require.NoError(b, err) + + for i := 0; i < 1000; i++ { + process := getDummyProcess() + process.ID = "test_" + strconv.Itoa(i) + + err = rs.AddProcess(process) + require.Equal(b, nil, err, "Failed to add process (%s)", err) + } + + b.ResetTimer() + + for i := 0; i < b.N; i++ { + ids := rs.GetProcessIDs("", "", "", "") + require.NotEmpty(b, ids) + require.Equal(b, 1000, len(ids)) + } +} + +func BenchmarkGetProcess(b *testing.B) { + rs, err := getDummyRestreamer(nil, nil, nil, nil) + require.NoError(b, err) + + for i := 0; i < 1000; i++ { + process := getDummyProcess() + process.ID = "test_" + strconv.Itoa(i) + + err = rs.AddProcess(process) + require.Equal(b, nil, err, "Failed to add process (%s)", err) + } + + rand := rand.New(rand.NewSource(42)) + + b.ResetTimer() + + for i := 0; i < b.N; i++ { + n := rand.Intn(1000) + p, err := rs.GetProcess(app.NewProcessID("test_"+strconv.Itoa(n), "")) + require.NotNil(b, p) + require.Nil(b, err) + } +} + +func BenchmarkGetProcessState(b *testing.B) { + rs, err := getDummyRestreamer(nil, nil, nil, nil) + require.NoError(b, err) + + n := 10 + + for i := 0; i < n; i++ { + process := getDummyProcess() + process.ID = "test_" + strconv.Itoa(i) + process.Autostart = true + + err = rs.AddProcess(process) + require.Equal(b, nil, err, "Failed to add process (%s)", err) + } + + rand := rand.New(rand.NewSource(42)) + + time.Sleep(10 * time.Second) + + b.ResetTimer() + + for i := 0; i < b.N; i++ { + n := rand.Intn(n) + s, err := rs.GetProcessState(app.NewProcessID("test_"+strconv.Itoa(n), "")) + require.NotNil(b, s) + require.Nil(b, err) + } + + for i := 0; i < n; i++ { + rs.DeleteProcess(app.NewProcessID("test_"+strconv.Itoa(n), "")) + } +}