Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

efficiency: track which layers a file was in #555

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions dive/filetree/efficiency.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ type EfficiencyData struct {
Nodes []*FileNode
CumulativeSize int64
minDiscoveredSize int64
Layers []int
}

func (ed EfficiencyData) FirstLayer() int {
return ed.Layers[0]
}

func (ed EfficiencyData) SubsequentLayers() []int {
return ed.Layers[1:]
}

// EfficiencySlice represents an ordered set of EfficiencyData data structures.
Expand Down Expand Up @@ -42,15 +51,22 @@ func Efficiency(trees []*FileTree) (float64, EfficiencySlice) {

visitor := func(node *FileNode) error {
path := node.Path()
first := false
if _, ok := efficiencyMap[path]; !ok {
efficiencyMap[path] = &EfficiencyData{
Path: path,
Nodes: make([]*FileNode, 0),
minDiscoveredSize: -1,
Layers: []int{currentTree},
}
first = true
}
data := efficiencyMap[path]

if !first {
data.Layers = append(data.Layers, currentTree)
}

// this node may have had children that were deleted, however, we won't explicitly list out every child, only
// the top-most parent with the cumulative size. These operations will need to be done on the full (stacked)
// tree.
Expand Down
20 changes: 19 additions & 1 deletion dive/filetree/efficiency_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filetree

import (
"reflect"
"testing"
)

Expand Down Expand Up @@ -32,7 +33,11 @@ func TestEfficency(t *testing.T) {

var expectedScore = 0.75
var expectedMatches = EfficiencySlice{
&EfficiencyData{Path: "/etc/nginx/nginx.conf", CumulativeSize: 7000},
&EfficiencyData{
Path: "/etc/nginx/nginx.conf",
CumulativeSize: 7000,
Layers: []int{0, 1},
},
}
actualScore, actualMatches := Efficiency(trees)

Expand All @@ -51,6 +56,19 @@ func TestEfficency(t *testing.T) {
t.Errorf("Expected path of %s but go %s", expectedMatches[0].Path, actualMatches[0].Path)
}

if !reflect.DeepEqual(expectedMatches[0].Layers, actualMatches[0].Layers) {
t.Errorf("Expected layers of %v but got %v", expectedMatches[0].Layers, actualMatches[0].Layers)
}

if actualMatches[0].FirstLayer() != 0 {
t.Errorf("expected first layer 0 but got %v", actualMatches[0].FirstLayer())
}

expectedSubsequent := []int{1}
if !reflect.DeepEqual(actualMatches[0].SubsequentLayers(), expectedSubsequent) {
t.Errorf("expected subsequent layers %v but got %v", expectedSubsequent, actualMatches[0].SubsequentLayers())
}

if expectedMatches[0].CumulativeSize != actualMatches[0].CumulativeSize {
t.Errorf("Expected cumulative size of %v but go %v", expectedMatches[0].CumulativeSize, actualMatches[0].CumulativeSize)
}
Expand Down