From 460930b8020d4a71a86505270dfe3072f6faf952 Mon Sep 17 00:00:00 2001 From: Matthias Bertschy Date: Wed, 18 Sep 2024 17:39:50 +0200 Subject: [PATCH] improve Signed-off-by: Matthias Bertschy --- .../file/applicationprofile_processor.go | 12 ++----- .../file/dynamicpathdetector/analyze_opens.go | 6 ++-- .../file/dynamicpathdetector/analyzer.go | 32 ++++++------------- ...ze_opens_test.go => analyze_opens_test.go} | 8 ++--- .../tests/benchmark_test.go | 2 ++ 5 files changed, 22 insertions(+), 38 deletions(-) rename pkg/registry/file/dynamicpathdetector/tests/{analayze_opens_test.go => analyze_opens_test.go} (91%) diff --git a/pkg/registry/file/applicationprofile_processor.go b/pkg/registry/file/applicationprofile_processor.go index 9d7ed5da..1881177f 100644 --- a/pkg/registry/file/applicationprofile_processor.go +++ b/pkg/registry/file/applicationprofile_processor.go @@ -58,24 +58,16 @@ func (a ApplicationProfileProcessor) PreSave(object runtime.Object) error { } func deflateApplicationProfileContainer(container softwarecomposition.ApplicationProfileContainer) softwarecomposition.ApplicationProfileContainer { - opens := DeflateStringer(container.Opens) - - opens, err := dynamicpathdetector.AnalyzeOpens(opens, dynamicpathdetector.NewPathAnalyzer(OpenDynamicThreshold)) + opens, err := dynamicpathdetector.AnalyzeOpens(container.Opens, dynamicpathdetector.NewPathAnalyzer(OpenDynamicThreshold)) if err != nil { logger.L().Warning("failed to analyze opens", loggerhelpers.Error(err)) opens = DeflateStringer(container.Opens) } - - if opens == nil { - opens = []softwarecomposition.OpenCalls{} - } - - endpoints, err := dynamicpathdetector.AnalyzeEndpoints(&container.Endpoints, dynamicpathdetector.NewPathAnalyzer(100)) + endpoints, err := dynamicpathdetector.AnalyzeEndpoints(&container.Endpoints, dynamicpathdetector.NewPathAnalyzer(EndpointDynamicThreshold)) if err != nil { logger.L().Warning("failed to analyze endpoints", loggerhelpers.Error(err)) endpoints = container.Endpoints } - return softwarecomposition.ApplicationProfileContainer{ Name: container.Name, Capabilities: mapset.Sorted(mapset.NewThreadUnsafeSet(container.Capabilities...)), diff --git a/pkg/registry/file/dynamicpathdetector/analyze_opens.go b/pkg/registry/file/dynamicpathdetector/analyze_opens.go index e8d502f2..60827959 100644 --- a/pkg/registry/file/dynamicpathdetector/analyze_opens.go +++ b/pkg/registry/file/dynamicpathdetector/analyze_opens.go @@ -2,14 +2,16 @@ package dynamicpathdetector import ( "fmt" + "slices" + mapset "github.com/deckarep/golang-set/v2" types "github.com/kubescape/storage/pkg/apis/softwarecomposition" ) func AnalyzeOpens(opens []types.OpenCalls, analyzer *PathAnalyzer) ([]types.OpenCalls, error) { var dynamicOpens []types.OpenCalls for _, open := range opens { - AnalyzeOpen(open.Path, analyzer) + _, _ = AnalyzeOpen(open.Path, analyzer) } for i := range opens { @@ -20,7 +22,7 @@ func AnalyzeOpens(opens []types.OpenCalls, analyzer *PathAnalyzer) ([]types.Open if result != opens[i].Path { if existing, err := getIfExists(result, dynamicOpens); err == nil { - existing.Flags = MergeStrings(existing.Flags, opens[i].Flags) + existing.Flags = mapset.Sorted(mapset.NewThreadUnsafeSet(slices.Concat(existing.Flags, opens[i].Flags)...)) } else { dynamicOpen := types.OpenCalls{Path: result, Flags: opens[i].Flags} dynamicOpens = append(dynamicOpens, dynamicOpen) diff --git a/pkg/registry/file/dynamicpathdetector/analyzer.go b/pkg/registry/file/dynamicpathdetector/analyzer.go index e2e7f8be..62867e98 100644 --- a/pkg/registry/file/dynamicpathdetector/analyzer.go +++ b/pkg/registry/file/dynamicpathdetector/analyzer.go @@ -1,7 +1,7 @@ package dynamicpathdetector import ( - pathUtils "path" + "path" "strings" ) @@ -11,8 +11,9 @@ func NewPathAnalyzer(threshold int) *PathAnalyzer { threshold: threshold, } } -func (ua *PathAnalyzer) AnalyzePath(path, identifier string) (string, error) { - path = pathUtils.Clean(path) + +func (ua *PathAnalyzer) AnalyzePath(p, identifier string) (string, error) { + p = path.Clean(p) node, exists := ua.RootNodes[identifier] if !exists { node = &SegmentNode{ @@ -22,14 +23,12 @@ func (ua *PathAnalyzer) AnalyzePath(path, identifier string) (string, error) { } ua.RootNodes[identifier] = node } - - segments := strings.Split(strings.Trim(path, "/"), "/") - + segments := strings.Split(strings.Trim(p, "/"), "/") return ua.processSegments(node, segments), nil } func (ua *PathAnalyzer) processSegments(node *SegmentNode, segments []string) string { - resultPath := []string{} + var resultPath []string currentNode := node for _, segment := range segments { currentNode = ua.processSegment(currentNode, segment) @@ -37,20 +36,15 @@ func (ua *PathAnalyzer) processSegments(node *SegmentNode, segments []string) st resultPath = append(resultPath, currentNode.SegmentName) } return "/" + strings.Join(resultPath, "/") - } func (ua *PathAnalyzer) processSegment(node *SegmentNode, segment string) *SegmentNode { - - switch { - case segment == DynamicIdentifier: + if segment == DynamicIdentifier { return ua.handleDynamicSegment(node) - case KeyInMap(node.Children, segment) || node.IsNextDynamic(): - child, exists := node.Children[segment] + } else if child, exists := node.Children[segment]; exists || node.IsNextDynamic() { return ua.handleExistingSegment(node, child, exists) - default: + } else { return ua.handleNewSegment(node, segment) - } } @@ -103,7 +97,6 @@ func (ua *PathAnalyzer) createDynamicNode(node *SegmentNode) *SegmentNode { func (ua *PathAnalyzer) updateNodeStats(node *SegmentNode) { if node.Count > ua.threshold && !node.IsNextDynamic() { - dynamicChild := &SegmentNode{ SegmentName: DynamicIdentifier, Count: 0, @@ -123,7 +116,7 @@ func (ua *PathAnalyzer) updateNodeStats(node *SegmentNode) { func shallowChildrenCopy(src, dst *SegmentNode) { for segmentName := range src.Children { - if !KeyInMap(dst.Children, segmentName) { + if _, ok := dst.Children[segmentName]; !ok { dst.Children[segmentName] = src.Children[segmentName] } else { dst.Children[segmentName].Count += src.Children[segmentName].Count @@ -131,8 +124,3 @@ func shallowChildrenCopy(src, dst *SegmentNode) { } } } - -func KeyInMap[T any](TestMap map[string]T, key string) bool { - _, ok := TestMap[key] - return ok -} diff --git a/pkg/registry/file/dynamicpathdetector/tests/analayze_opens_test.go b/pkg/registry/file/dynamicpathdetector/tests/analyze_opens_test.go similarity index 91% rename from pkg/registry/file/dynamicpathdetector/tests/analayze_opens_test.go rename to pkg/registry/file/dynamicpathdetector/tests/analyze_opens_test.go index 5873939a..c404797a 100644 --- a/pkg/registry/file/dynamicpathdetector/tests/analayze_opens_test.go +++ b/pkg/registry/file/dynamicpathdetector/tests/analyze_opens_test.go @@ -45,7 +45,7 @@ func TestAnalyzeOpensWithFlagMergingAndThreshold(t *testing.T) { {Path: "/home/user4/file.txt", Flags: []string{"READ", "WRITE"}}, }, expected: []types.OpenCalls{ - {Path: "/home//file.txt", Flags: []string{"READ", "WRITE", "APPEND"}}, + {Path: "/home//file.txt", Flags: []string{"APPEND", "READ", "WRITE"}}, }, }, { @@ -70,7 +70,7 @@ func TestAnalyzeOpensWithFlagMergingAndThreshold(t *testing.T) { {Path: "/var/log/app2.log", Flags: []string{"WRITE"}}, }, expected: []types.OpenCalls{ - {Path: "/home//common.txt", Flags: []string{"READ", "WRITE", "APPEND"}}, + {Path: "/home//common.txt", Flags: []string{"APPEND", "READ", "WRITE"}}, {Path: "/var/log/app1.log", Flags: []string{"READ"}}, {Path: "/var/log/app2.log", Flags: []string{"WRITE"}}, }, @@ -88,8 +88,8 @@ func TestAnalyzeOpensWithFlagMergingAndThreshold(t *testing.T) { {Path: "/home/user4/file2.txt", Flags: []string{"READ", "WRITE"}}, }, expected: []types.OpenCalls{ - {Path: "/home//file1.txt", Flags: []string{"READ", "WRITE", "APPEND"}}, - {Path: "/home//file2.txt", Flags: []string{"READ", "WRITE", "APPEND"}}, + {Path: "/home//file1.txt", Flags: []string{"APPEND", "READ", "WRITE"}}, + {Path: "/home//file2.txt", Flags: []string{"APPEND", "READ", "WRITE"}}, }, }, } diff --git a/pkg/registry/file/dynamicpathdetector/tests/benchmark_test.go b/pkg/registry/file/dynamicpathdetector/tests/benchmark_test.go index affafefd..9532dc83 100644 --- a/pkg/registry/file/dynamicpathdetector/tests/benchmark_test.go +++ b/pkg/registry/file/dynamicpathdetector/tests/benchmark_test.go @@ -59,6 +59,7 @@ func BenchmarkAnalyzeOpensVsDeflateStringer(b *testing.B) { _ = file.DeflateStringer(paths) _, _ = dynamicpathdetector.AnalyzeOpens(paths, analyzer) } + b.ReportAllocs() }) b.Run("deflateStringer", func(b *testing.B) { @@ -66,6 +67,7 @@ func BenchmarkAnalyzeOpensVsDeflateStringer(b *testing.B) { for i := 0; i < b.N; i++ { _ = file.DeflateStringer(paths) } + b.ReportAllocs() }) }