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.
This commit is contained in:
Will Murphy 2018-07-08 13:36:59 -04:00
parent de7c3a759a
commit 8611958838
2 changed files with 39 additions and 0 deletions

View File

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

View File

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