Skip to content

Commit 50fad01

Browse files
committed
Add testing for compare.go
1 parent 848a9f6 commit 50fad01

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

.evergreen/perfcomp/compare_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package perfcomp
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"gonum.org/v1/gonum/mat"
8+
)
9+
10+
func createTestVectors(start1 int, stop1 int, step1 int, start2 int, stop2 int, step2 int) (*mat.Dense, *mat.Dense) {
11+
xData := []float64{}
12+
yData := []float64{}
13+
14+
for i := start1; i < stop1; i += step1 {
15+
xData = append(xData, float64(i))
16+
}
17+
for j := start2; j < stop2; j += step2 {
18+
yData = append(yData, float64(j))
19+
}
20+
21+
x := mat.NewDense(len(xData), 1, xData)
22+
y := mat.NewDense(len(yData), 1, yData)
23+
24+
return x, y
25+
}
26+
27+
// TestCalcEnergyStatistics verifies that the energy calculation algorithms are correct.
28+
func TestCalcEnergyStatistics(t *testing.T) {
29+
t.Run("similar distributions should have small e,t,h values ", func(t *testing.T) {
30+
x, y := createTestVectors(1, 100, 1, 1, 105, 1)
31+
e, tstat, h, _ := calcEnergyStatistics(x, y)
32+
33+
del := 1e-3
34+
// Limit precision of comparison to 3 digits after the decimal.
35+
assert.InDelta(t, 0.160, e, del) // |0.160 - e| < 0.001
36+
assert.InDelta(t, 8.136, tstat, del)
37+
assert.InDelta(t, 0.002, h, del)
38+
})
39+
40+
t.Run("different distributions should have large e,t,h values", func(t *testing.T) {
41+
x, y := createTestVectors(1, 100, 1, 10000, 13000, 14)
42+
e, tstat, h, _ := calcEnergyStatistics(x, y)
43+
del := 1e-3
44+
45+
assert.InDelta(t, 21859.691, e, del)
46+
assert.InDelta(t, 1481794.709, tstat, del)
47+
assert.InDelta(t, 0.954, h, del)
48+
})
49+
50+
t.Run("uni-variate distributions", func(t *testing.T) {
51+
x, y := createTestVectors(1, 300, 1, 1000, 5000, 10)
52+
e, tstat, h, _ := calcEnergyStatistics(x, y)
53+
del := 1e-3
54+
55+
assert.InDelta(t, 4257.009, e, del)
56+
assert.InDelta(t, 728381.015, tstat, del)
57+
assert.InDelta(t, 0.748, h, del)
58+
})
59+
60+
t.Run("equal distributions should have all 0 values", func(t *testing.T) {
61+
x := mat.NewDense(10, 1, []float64{1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
62+
y := mat.NewDense(1, 1, []float64{1})
63+
64+
e, tstat, h, _ := calcEnergyStatistics(x, y)
65+
66+
assert.Equal(t, 0.0, e)
67+
assert.Equal(t, 0.0, tstat)
68+
assert.Equal(t, 0.0, h)
69+
})
70+
71+
t.Run("energy stats returns errors on malformed input", func(t *testing.T) {
72+
x := mat.NewDense(2, 2, make([]float64, 4))
73+
y := mat.NewDense(2, 3, make([]float64, 6))
74+
75+
_, _, _, err := calcEnergyStatistics(x, y)
76+
assert.NotEqual(t, nil, err)
77+
assert.ErrorContains(t, err, "both inputs must have the same number of columns")
78+
79+
x.Reset()
80+
y = &mat.Dense{}
81+
82+
_, _, _, err = calcEnergyStatistics(x, y)
83+
assert.NotEqual(t, nil, err)
84+
assert.ErrorContains(t, err, "inputs cannot be empty")
85+
86+
x = mat.NewDense(2, 2, make([]float64, 4))
87+
y = mat.NewDense(3, 2, make([]float64, 6))
88+
89+
_, _, _, err = calcEnergyStatistics(x, y)
90+
assert.NotEqual(t, nil, err)
91+
assert.ErrorContains(t, err, "both inputs must be column vectors")
92+
})
93+
}
94+
95+
// TestFindSigBenchmarks tests that statistically significant benchmarks are correctly flagged.
96+
func TestFindSigBenchmarks(t *testing.T) {
97+
var dummyEnergyStats []*EnergyStats
98+
assert.Equal(t, 0, len(getStatSigBenchmarks(dummyEnergyStats)))
99+
100+
for i := -2.5; i < 3; i += 0.5 {
101+
es := EnergyStats{
102+
ZScore: i,
103+
}
104+
dummyEnergyStats = append(dummyEnergyStats, &es)
105+
}
106+
assert.Equal(t, 11, len(dummyEnergyStats))
107+
108+
sigBenchmarks := getStatSigBenchmarks(dummyEnergyStats)
109+
assert.Equal(t, 4, len(sigBenchmarks))
110+
}

.evergreen/perfcomp/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@ go 1.24.4
55
require github.com/spf13/cobra v1.9.1
66

77
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
89
github.com/golang/snappy v1.0.0 // indirect
910
github.com/klauspost/compress v1.16.7 // indirect
11+
github.com/pmezard/go-difflib v1.0.0 // indirect
1012
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
1113
github.com/xdg-go/scram v1.1.2 // indirect
1214
github.com/xdg-go/stringprep v1.0.4 // indirect
1315
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
1416
golang.org/x/crypto v0.33.0 // indirect
1517
golang.org/x/sync v0.12.0 // indirect
1618
golang.org/x/text v0.23.0 // indirect
19+
gopkg.in/yaml.v3 v3.0.1 // indirect
1720
)
1821

1922
require (
2023
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2124
github.com/spf13/pflag v1.0.6 // indirect
25+
github.com/stretchr/testify v1.10.0
2226
go.mongodb.org/mongo-driver/v2 v2.2.3
2327
gonum.org/v1/gonum v0.16.0
2428
)

.evergreen/perfcomp/go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
99
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
1010
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
1111
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
12+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
13+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1214
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
1315
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
1416
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
1517
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
1618
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
19+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
20+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1721
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
1822
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
1923
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
@@ -57,4 +61,5 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
5761
gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk=
5862
gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E=
5963
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
64+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
6065
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)