Skip to content

Commit

Permalink
Add benchmark and fuzz for UPGMA
Browse files Browse the repository at this point in the history
  • Loading branch information
fluhus committed Nov 24, 2023
1 parent 2055591 commit 613d595
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions clustering/upgma_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package clustering

import (
"fmt"
"math"
"reflect"
"testing"

"github.com/fluhus/gostuff/gnum"
)

func TestUPGMA(t *testing.T) {
Expand Down Expand Up @@ -47,3 +50,34 @@ func TestUPGMA_more(t *testing.T) {
}
}
}

func BenchmarkUPGMA(b *testing.B) {
for _, n := range []int{10, 30, 100} {
b.Run(fmt.Sprint(n), func(b *testing.B) {
nums := make([]float64, n)
for i := range nums {
nums[i] = 1.0 / float64(i+1)
if i%2 == 0 {
nums[i] += 10
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
upgma(n, func(i1, i2 int) float64 {
return gnum.Abs(nums[i1] - nums[i2])
})
}
})
}
}

func FuzzUPGMA(f *testing.F) {
f.Add(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0)
f.Fuzz(func(t *testing.T, a float64, b float64, c float64,
d float64, e float64, f float64, g float64, h float64, i float64) {
nums := []float64{a, b, c, d, e, f, g, h, i}
upgma(len(nums), func(i1, i2 int) float64 {
return gnum.Abs(nums[i1] - nums[i2])
})
})
}

0 comments on commit 613d595

Please sign in to comment.