Skip to content

Commit 97e768d

Browse files
committed
move energy stats to main.go
1 parent b890d77 commit 97e768d

File tree

4 files changed

+72
-85
lines changed

4 files changed

+72
-85
lines changed

etc/perf-pr-comment.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
set -eux
66

77
pushd ./internal/cmd/perfcomp >/dev/null || exist
8-
GOWORK=off go run . ${VERSION_ID}
8+
GOWORK=off go run main.go ${VERSION_ID}
99
popd >/dev/null

internal/cmd/perfcomp/energystatistics.go

Lines changed: 0 additions & 77 deletions
This file was deleted.

internal/cmd/perfcomp/energystatistics_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestEnergyStatistics(t *testing.T) {
3434

3535
t.Run("similar distributions should have small e,t,h values ", func(t *testing.T) {
3636
x, y := createTestVectors(1, 100, 1, 1, 105, 1)
37-
e, tstat, h := GetEnergyStatistics(x, y)
37+
e, tstat, h := getEnergyStatistics(x, y)
3838

3939
del := 1e-3
4040
// Limit precision of comparison to 3 digits after the decimal.
@@ -45,7 +45,7 @@ func TestEnergyStatistics(t *testing.T) {
4545

4646
t.Run("different distributions should have large e,t,h values", func(t *testing.T) {
4747
x, y := createTestVectors(1, 100, 1, 10000, 13000, 14)
48-
e, tstat, h := GetEnergyStatistics(x, y)
48+
e, tstat, h := getEnergyStatistics(x, y)
4949
del := 1e-3
5050

5151
assert.InDelta(t, 21859.691, e, del)
@@ -55,7 +55,7 @@ func TestEnergyStatistics(t *testing.T) {
5555

5656
t.Run("uni-variate distributions", func(t *testing.T) {
5757
x, y := createTestVectors(1, 300, 1, 1000, 5000, 10)
58-
e, tstat, h := GetEnergyStatistics(x, y)
58+
e, tstat, h := getEnergyStatistics(x, y)
5959
del := 1e-3
6060

6161
assert.InDelta(t, 4257.009, e, del)
@@ -67,7 +67,7 @@ func TestEnergyStatistics(t *testing.T) {
6767
x := mat.NewDense(10, 1, []float64{1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
6868
y := mat.NewDense(1, 1, []float64{1})
6969

70-
e, tstat, h := GetEnergyStatistics(x, y)
70+
e, tstat, h := getEnergyStatistics(x, y)
7171

7272
assert.Equal(t, 0.0, e)
7373
assert.Equal(t, 0.0, tstat)

internal/cmd/perfcomp/main.go

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ func getEnergyStatsForOneBenchmark(rd RawData, coll *mongo.Collection) ([]*Energ
234234
)
235235
}
236236

237-
pChange := GetPercentageChange(patchVal[0], stableRegion.Mean)
238-
e, t, h := GetEnergyStatistics(mat.NewDense(len(stableRegion.Values), 1, stableRegion.Values), mat.NewDense(1, 1, patchVal))
239-
z := GetZScore(patchVal[0], stableRegion.Mean, stableRegion.Std)
237+
pChange := getPercentageChange(patchVal[0], stableRegion.Mean)
238+
e, t, h := getEnergyStatistics(mat.NewDense(len(stableRegion.Values), 1, stableRegion.Values), mat.NewDense(1, 1, patchVal))
239+
z := getZScore(patchVal[0], stableRegion.Mean, stableRegion.Std)
240240

241241
es := EnergyStats{
242242
Benchmark: testname,
@@ -291,3 +291,67 @@ func generatePRComment(energyStats []*EnergyStats, version string) string {
291291
comment.WriteString("\n*For a comprehensive view of all microbenchmark results for this PR's commit, please check out the Evergreen perf task for this patch.*")
292292
return comment.String()
293293
}
294+
295+
// Given two matrices, this function returns
296+
// (e, t, h) = (E-statistic, test statistic, e-coefficient of inhomogeneity)
297+
func getEnergyStatistics(x, y *mat.Dense) (float64, float64, float64) {
298+
n, _ := x.Dims()
299+
m, _ := y.Dims()
300+
nf := float64(n)
301+
mf := float64(m)
302+
303+
var A float64 // E|X-Y|
304+
if nf > 0 && mf > 0 {
305+
A = getDistance(x, y) / (nf * mf)
306+
} else {
307+
A = 0
308+
}
309+
var B float64 // E|X-X'|
310+
if nf > 0 {
311+
B = getDistance(x, x) / (nf * nf)
312+
} else {
313+
B = 0
314+
}
315+
var C float64 // E|Y-Y'|
316+
if mf > 0 {
317+
C = getDistance(y, y) / (mf * mf)
318+
} else {
319+
C = 0
320+
}
321+
322+
E := 2*A - B - C // D^2(F_x, F_y)
323+
T := ((nf * mf) / (nf + mf)) * E
324+
var H float64
325+
if A > 0 {
326+
H = E / (2 * A)
327+
} else {
328+
H = 0
329+
}
330+
return E, T, H
331+
}
332+
333+
// Given two vectors (expected 1 col),
334+
// this function returns the sum of distances between each pair.
335+
func getDistance(x, y *mat.Dense) float64 {
336+
xrows, _ := x.Dims()
337+
yrows, _ := y.Dims()
338+
339+
var sum float64
340+
341+
for i := 0; i < xrows; i++ {
342+
for j := 0; j < yrows; j++ {
343+
sum += math.Sqrt(math.Pow((x.At(i, 0) - y.At(j, 0)), 2))
344+
}
345+
}
346+
return sum
347+
}
348+
349+
// Get Z score for result x, compared to mean u and st dev o.
350+
func getZScore(x, mu, sigma float64) float64 {
351+
return (x - mu) / sigma
352+
}
353+
354+
// Get percentage change for result x compared to mean u.
355+
func getPercentageChange(x, mu float64) float64 {
356+
return ((x - mu) / mu) * 100
357+
}

0 commit comments

Comments
 (0)