Add optimized listing if no patterns are provided

This commit is contained in:
Ingo Oppermann 2024-04-30 14:10:47 +02:00
parent 5ad466e840
commit cb3c6de2c8
No known key found for this signature in database
GPG Key ID: 2AB32426E9DD229E
2 changed files with 123 additions and 28 deletions

View File

@ -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

View File

@ -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), ""))
}
}