Skip to content

Commit

Permalink
Added a comparision test
Browse files Browse the repository at this point in the history
  • Loading branch information
afek854 committed Sep 18, 2024
1 parent b259e93 commit 30d043c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pkg/registry/file/applicationprofile_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ func (a ApplicationProfileProcessor) PreSave(object runtime.Object) error {
}

func deflateApplicationProfileContainer(container softwarecomposition.ApplicationProfileContainer) softwarecomposition.ApplicationProfileContainer {
opens := deflateStringer(container.Opens)
opens := DeflateStringer(container.Opens)

opens, err := dynamicpathdetector.AnalyzeOpens(opens, dynamicpathdetector.NewPathAnalyzer(OpenDynamicThreshold))
if err != nil {
logger.L().Warning("failed to analyze opens", loggerhelpers.Error(err))
opens = deflateStringer(container.Opens)
opens = DeflateStringer(container.Opens)
}

if opens == nil {
Expand All @@ -79,7 +79,7 @@ func deflateApplicationProfileContainer(container softwarecomposition.Applicatio
return softwarecomposition.ApplicationProfileContainer{
Name: container.Name,
Capabilities: mapset.Sorted(mapset.NewThreadUnsafeSet(container.Capabilities...)),
Execs: deflateStringer(container.Execs),
Execs: DeflateStringer(container.Execs),
Opens: opens,
Syscalls: mapset.Sorted(mapset.NewThreadUnsafeSet(container.Syscalls...)),
SeccompProfile: container.SeccompProfile,
Expand Down
33 changes: 33 additions & 0 deletions pkg/registry/file/dynamicpathdetector/tests/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"testing"

types "github.com/kubescape/storage/pkg/apis/softwarecomposition"
"github.com/kubescape/storage/pkg/registry/file"
"github.com/kubescape/storage/pkg/registry/file/dynamicpathdetector"
)

Expand Down Expand Up @@ -47,6 +49,26 @@ func BenchmarkAnalyzePathWithDifferentLengths(b *testing.B) {
}
}

func BenchmarkAnalyzeOpensVsDeflateStringer(b *testing.B) {
paths := PathsToOpens(generateMixedPaths(10000, 0))
analyzer := dynamicpathdetector.NewPathAnalyzer(100)

b.Run("AnalyzeOpens", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = file.DeflateStringer(paths)
_, _ = dynamicpathdetector.AnalyzeOpens(paths, analyzer)
}
})

b.Run("deflateStringer", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = file.DeflateStringer(paths)
}
})
}

func generateMixedPaths(count int, fixedLength int) []string {
paths := make([]string, count)
staticSegments := []string{"users", "profile", "settings", "api", "v1", "posts", "organizations", "departments", "employees", "projects", "tasks", "categories", "subcategories", "items", "articles"}
Expand Down Expand Up @@ -102,3 +124,14 @@ func generateRandomString(length int) string {
}
return string(result)
}

func PathsToOpens(paths []string) []types.OpenCalls {
opens := make([]types.OpenCalls, len(paths))
for i, path := range paths {
opens[i] = types.OpenCalls{
Path: path,
Flags: []string{"READ"},
}
}
return opens
}
2 changes: 1 addition & 1 deletion pkg/registry/file/networkneighborhood_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func deflateNetworkNeighbors(in []softwarecomposition.NetworkNeighbor) []softwar
}
for _, i := range mapset.Sorted(toDeflate) {
out[i].DNSNames = mapset.Sorted(mapset.NewThreadUnsafeSet(out[i].DNSNames...))
out[i].Ports = deflateStringer(out[i].Ports)
out[i].Ports = DeflateStringer(out[i].Ports)
}
return out
}
2 changes: 1 addition & 1 deletion pkg/registry/file/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Stringer interface {
String() string
}

func deflateStringer[T Stringer](in []T) []T {
func DeflateStringer[T Stringer](in []T) []T {
out := make([]T, 0)
set := mapset.NewThreadUnsafeSet[string]()
for _, item := range in {
Expand Down

0 comments on commit 30d043c

Please sign in to comment.