diff options
author | Will Murphy <willmurphyscode@gmail.com> | 2018-07-08 13:36:59 -0400 |
---|---|---|
committer | Will Murphy <willmurphyscode@gmail.com> | 2018-07-08 13:36:59 -0400 |
commit | 86119588385a51a685daaace6f236021e3bff007 (patch) | |
tree | e7c474cae517492378787d5fc8d4df8cc82c78b8 /filetree | |
parent | de7c3a759a7fcb9d508e1fbd246efcd2de1d6671 (diff) |
Calculate efficiency score
For now, efficiency score is simply the number of unique files over the
total number of files that appear in any layer.
Diffstat (limited to 'filetree')
-rw-r--r-- | filetree/tree.go | 10 | ||||
-rw-r--r-- | filetree/tree_test.go | 29 |
2 files changed, 39 insertions, 0 deletions
diff --git a/filetree/tree.go b/filetree/tree.go index 373f457..3b05571 100644 --- a/filetree/tree.go +++ b/filetree/tree.go @@ -241,3 +241,13 @@ func EfficiencyMap(trees []*FileTree) map[string]int { } return result } + +func EfficiencyScore(trees []*FileTree) float64 { + efficiencyMap := EfficiencyMap(trees) + uniquePaths := len(efficiencyMap) + pathAppearances := 0 + for _, value := range efficiencyMap { + pathAppearances += value + } + return float64(uniquePaths) / float64(pathAppearances) +} diff --git a/filetree/tree_test.go b/filetree/tree_test.go index 30340a1..c389492 100644 --- a/filetree/tree_test.go +++ b/filetree/tree_test.go @@ -2,6 +2,7 @@ package filetree import ( "fmt" + "math" "reflect" "testing" ) @@ -478,3 +479,31 @@ func TestEfficencyMap(t *testing.T) { t.Fatalf("Expected %v but go %v", expectedMap, actualMap) } } + +func TestEfficiencyScore(t *testing.T) { + trees := make([]*FileTree, 3) + for ix, _ := range trees { + tree := NewFileTree() + tree.AddPath("/etc/nginx/nginx.conf", FileInfo{}) + tree.AddPath("/etc/nginx/public", FileInfo{}) + trees[ix] = tree + } + expected := 2.0 / 6.0 + actual := EfficiencyScore(trees) + if math.Abs(expected-actual) > 0.0001 { + t.Fatalf("Expected %f but got %f", expected, actual) + } + + trees = make([]*FileInfo, 1) + for ix, _ := range trees { + tree := NewFileTree() + tree.AddPath("/etc/nginx/nginx.conf", FileInfo{}) + tree.AddPath("/etc/nginx/public", FileInfo{}) + trees[ix] = tree + } + expected = 1.0 + actual = EfficiencyScore(trees) + if math.Abs(expected-actual) > 0.0001 { + t.Fatalf("Expected %f but got %f", expected, actual) + } +} |