Skip to content

Commit

Permalink
metrics: Adjust the howSimilar logic vs strings
Browse files Browse the repository at this point in the history
Also add a test.
  • Loading branch information
bep committed Apr 2, 2019
1 parent e91e222 commit 4494a01
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
16 changes: 6 additions & 10 deletions metrics/metrics.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
"github.com/gohugoio/hugo/compare" "github.com/gohugoio/hugo/compare"


"github.com/gohugoio/hugo/common/hreflect" "github.com/gohugoio/hugo/common/hreflect"

"github.com/spf13/cast"
) )


// The Provider interface defines an interface for measuring metrics. // The Provider interface defines an interface for measuring metrics.
Expand Down Expand Up @@ -198,18 +196,17 @@ func (b bySum) Less(i, j int) bool { return b[i].sum > b[j].sum }
// howSimilar is a naive diff implementation that returns // howSimilar is a naive diff implementation that returns
// a number between 0-100 indicating how similar a and b are. // a number between 0-100 indicating how similar a and b are.
func howSimilar(a, b interface{}) int { func howSimilar(a, b interface{}) int {
if a == b { // TODO(bep) object equality fast path, but remember that
return 100 // we can get anytning in here.
}


as, err1 := cast.ToStringE(a) as, ok1 := a.(string)
bs, err2 := cast.ToStringE(b) bs, ok2 := b.(string)


if err1 == nil && err2 == nil { if ok1 && ok2 {
return howSimilarStrings(as, bs) return howSimilarStrings(as, bs)
} }


if err1 != err2 { if ok1 != ok2 {
return 0 return 0
} }


Expand All @@ -219,7 +216,6 @@ func howSimilar(a, b interface{}) int {
return 100 return 100
} }


// TODO(bep) implement ProbablyEq for Pages etc.
pe1, pok1 := a.(compare.ProbablyEqer) pe1, pok1 := a.(compare.ProbablyEqer)
pe2, pok2 := b.(compare.ProbablyEqer) pe2, pok2 := b.(compare.ProbablyEqer)
if pok1 && pok2 && pe1.ProbablyEq(pe2) { if pok1 && pok2 && pe1.ProbablyEq(pe2) {
Expand Down
8 changes: 8 additions & 0 deletions metrics/metrics_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"strings" "strings"
"testing" "testing"


"github.com/gohugoio/hugo/resources/page"

"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )


Expand All @@ -40,6 +42,12 @@ func TestSimilarPercentage(t *testing.T) {


} }


func TestSimilarPercentageNonString(t *testing.T) {
assert := require.New(t)
assert.Equal(100, howSimilar(page.NopPage, page.NopPage))
assert.Equal(90, howSimilar(page.Pages{}, page.Pages{}))
}

func BenchmarkHowSimilar(b *testing.B) { func BenchmarkHowSimilar(b *testing.B) {
s1 := "Hugo is cool and " + strings.Repeat("fun ", 10) + "!" s1 := "Hugo is cool and " + strings.Repeat("fun ", 10) + "!"
s2 := "Hugo is cool and " + strings.Repeat("cool ", 10) + "!" s2 := "Hugo is cool and " + strings.Repeat("cool ", 10) + "!"
Expand Down

0 comments on commit 4494a01

Please sign in to comment.