Skip to content

Commit 848a9f6

Browse files
committed
Rename functions and add comments for clarity
1 parent 2e27841 commit 848a9f6

File tree

1 file changed

+61
-50
lines changed

1 file changed

+61
-50
lines changed

.evergreen/perfcomp/compare.go

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,59 +13,55 @@ import (
1313
"gonum.org/v1/gonum/mat"
1414
)
1515

16-
type OverrideInfo struct {
17-
OverrideMainline bool `bson:"override_mainline"`
18-
BaseOrder any `bson:"base_order"`
19-
Reason any `bson:"reason"`
20-
User any `bson:"user"`
16+
// RawData defines the shape of the data in the raw_results collection.
17+
// raw_results stores results by benchmark, which holds the values of all its measurements.
18+
// A single measurement from a single benchmark is called a microbenchmark.
19+
type RawData struct {
20+
Info Info
21+
CreatedAt any `bson:"created_at"`
22+
CompletedAt any `bson:"completed_at"`
23+
Rollups Rollups // List of all measurement results
24+
FailedRollupAttempts int64 `bson:"failed_rollup_attempts"`
2125
}
2226

2327
type Info struct {
2428
Project string `bson:"project"`
25-
Version string `bson:"version"`
29+
Version string `bson:"version"` // Evergreen version that produced the results
2630
Variant string `bson:"variant"`
2731
Order int64 `bson:"order"`
2832
TaskName string `bson:"task_name"`
2933
TaskID string `bson:"task_id"`
3034
Execution int64 `bson:"execution"`
3135
Mainline bool `bson:"mainline"`
3236
OverrideInfo OverrideInfo
33-
TestName string `bson:"test_name"`
37+
TestName string `bson:"test_name"` // Benchmark name
3438
Args map[string]any `bson:"args"`
3539
}
3640

37-
type Stat struct {
38-
Name string `bson:"name"`
39-
Val float64 `bson:"val"`
40-
Metadata any `bson:"metadata"`
41+
type OverrideInfo struct {
42+
OverrideMainline bool `bson:"override_mainline"`
43+
BaseOrder any `bson:"base_order"`
44+
Reason any `bson:"reason"`
45+
User any `bson:"user"`
4146
}
4247

4348
type Rollups struct {
4449
Stats []Stat
4550
}
4651

47-
type RawData struct {
48-
Info Info
49-
CreatedAt any `bson:"created_at"`
50-
CompletedAt any `bson:"completed_at"`
51-
Rollups Rollups
52-
FailedRollupAttempts int64 `bson:"failed_rollup_attempts"`
53-
}
54-
55-
type TimeSeriesInfo struct {
56-
Project string `bson:"project"`
57-
Variant string `bson:"variant"`
58-
Task string `bson:"task"`
59-
Test string `bson:"test"`
60-
Measurement string `bson:"measurement"`
61-
Args map[string]any `bson:"args"`
52+
type Stat struct {
53+
Name string `bson:"name"` // Measurement name
54+
Val float64 `bson:"val"` // Microbenchmark result
55+
Metadata any `bson:"metadata"`
6256
}
6357

58+
// StableRegion defines the shape of the data in the stable_regions collection.
59+
// A stable region is a group of consecutive microbenchmark values between two change points.
6460
type StableRegion struct {
6561
TimeSeriesInfo TimeSeriesInfo
6662
Start any `bson:"start"`
6763
End any `bson:"end"`
68-
Values []float64 `bson:"values"`
64+
Values []float64 `bson:"values"` // All microbenchmark values that makes up the stable region
6965
StartOrder int64 `bson:"start_order"`
7066
EndOrder int64 `bson:"end_order"`
7167
Mean float64 `bson:"mean"`
@@ -76,36 +72,50 @@ type StableRegion struct {
7672
CoefficientOfVariation float64 `bson:"coefficient_of_variation"`
7773
LastSuccessfulUpdate any `bson:"last_successful_update"`
7874
Last bool `bson:"last"`
79-
Contexts []any `bson:"contexts"`
75+
Contexts []any `bson:"contexts"` // Performance context (e.g. "Go Driver perf comp")
76+
}
77+
78+
type TimeSeriesInfo struct {
79+
Project string `bson:"project"`
80+
Variant string `bson:"variant"`
81+
Task string `bson:"task"`
82+
Test string `bson:"test"` // Benchmark name
83+
Measurement string `bson:"measurement"` // Measurement name
84+
Args map[string]any `bson:"args"`
8085
}
8186

87+
// EnergyStats stores the calculated energy statistics for a patch version's specific
88+
// microbenchmark compared to the mainline's stable region for that same microbenchmark.
8289
type EnergyStats struct {
8390
Project string
8491
Benchmark string
8592
Measurement string
86-
PatchVersion string
87-
StableRegion StableRegion
88-
MeasurementVal float64
93+
PatchVersion string // Evergreen version that produced the results
94+
StableRegion StableRegion // Latest stable region from the mainline this patch is comparing against
95+
MeasurementVal float64 // Microbenchmark result of the patch version
8996
PercentChange float64
9097
EnergyStatistic float64
9198
TestStatistic float64
9299
HScore float64
93100
ZScore float64
94101
}
95102

103+
// CompareResult is the collection of the energy statistics of all microbenchmarks with
104+
// statistically significant changes for this patch.
96105
type CompareResult struct {
97-
CommitSHA string
98-
MainlineCommit string
99-
Version string
106+
CommitSHA string // Head commit SHA
107+
MainlineCommit string // Base commit SHA
108+
Version string // Evergreen patch version
100109
SigEnergyStats []EnergyStats
101110
}
102111

112+
// Performance analytics node db and collection names
103113
const expandedMetricsDB = "expanded_metrics"
104114
const rawResultsColl = "raw_results"
105115
const stableRegionsColl = "stable_regions"
106116

107117
// Compare will return statistical results for a patch version using the
108-
// stable region defined by the performance analyzer cluster.
118+
// stable region defined by the performance analytics cluster.
109119
func Compare(ctx context.Context, versionID string, perfAnalyzerConnString string, project string, perfContext string) (*CompareResult, error) {
110120

111121
// Connect to analytics node
@@ -156,6 +166,7 @@ func Compare(ctx context.Context, versionID string, perfAnalyzerConnString strin
156166
return &compareResult, nil
157167
}
158168

169+
// Gets all raw benchmark data for a specific Evergreen version.
159170
func findRawData(ctx context.Context, project string, version string, coll *mongo.Collection) ([]RawData, error) {
160171
filter := bson.D{
161172
{"info.project", project},
@@ -194,7 +205,7 @@ func findRawData(ctx context.Context, project string, version string, coll *mong
194205
return rawData, err
195206
}
196207

197-
// Find the most recent stable region of the mainline version for a specific test/measurement
208+
// Finds the most recent stable region of the mainline version for a specific microbenchmark.
198209
func findLastStableRegion(ctx context.Context, project string, testname string, measurement string, coll *mongo.Collection, perfContext string) (*StableRegion, error) {
199210
filter := bson.D{
200211
{"time_series_info.project", project},
@@ -216,7 +227,7 @@ func findLastStableRegion(ctx context.Context, project string, testname string,
216227
return sr, nil
217228
}
218229

219-
// For a specific test and measurement
230+
// Calculate the energy statistics for all measurements in a benchmark.
220231
func getEnergyStatsForOneBenchmark(ctx context.Context, rd RawData, coll *mongo.Collection, perfContext string) ([]*EnergyStats, error) {
221232
testname := rd.Info.TestName
222233
var energyStats []*EnergyStats
@@ -241,7 +252,7 @@ func getEnergyStatsForOneBenchmark(ctx context.Context, rd RawData, coll *mongo.
241252
stableRegionVec := mat.NewDense(len(stableRegion.Values), 1, stableRegion.Values)
242253
measValVec := mat.NewDense(1, 1, []float64{measVal}) // singleton
243254

244-
estat, tstat, hscore, err := getEnergyStatistics(stableRegionVec, measValVec)
255+
estat, tstat, hscore, err := calcEnergyStatistics(stableRegionVec, measValVec)
245256
if err != nil {
246257
log.Fatalf(
247258
"Could not calculate energy stats for test %q, measurement %q: %v",
@@ -251,8 +262,8 @@ func getEnergyStatsForOneBenchmark(ctx context.Context, rd RawData, coll *mongo.
251262
)
252263
}
253264

254-
zscore := getZScore(measVal, stableRegion.Mean, stableRegion.Std)
255-
pChange := getPercentageChange(measVal, stableRegion.Mean)
265+
zscore := calcZScore(measVal, stableRegion.Mean, stableRegion.Std)
266+
pChange := calcPercentChange(measVal, stableRegion.Mean)
256267

257268
es := EnergyStats{
258269
Project: project,
@@ -290,7 +301,7 @@ func getEnergyStatsForAllBenchMarks(ctx context.Context, patchRawData []RawData,
290301
return allEnergyStats, nil
291302
}
292303

293-
func getStatSigBenchmarks(energyStats []*EnergyStats) []EnergyStats { // TODO
304+
func getStatSigBenchmarks(energyStats []*EnergyStats) []EnergyStats {
294305

295306
var significantEnergyStats []EnergyStats
296307
for _, es := range energyStats {
@@ -308,7 +319,7 @@ func getStatSigBenchmarks(energyStats []*EnergyStats) []EnergyStats { // TODO
308319

309320
// Given two matrices, this function returns
310321
// (e, t, h) = (E-statistic, test statistic, e-coefficient of inhomogeneity)
311-
func getEnergyStatistics(x, y *mat.Dense) (float64, float64, float64, error) {
322+
func calcEnergyStatistics(x, y *mat.Dense) (float64, float64, float64, error) {
312323
xrows, xcols := x.Dims()
313324
yrows, ycols := y.Dims()
314325

@@ -324,7 +335,7 @@ func getEnergyStatistics(x, y *mat.Dense) (float64, float64, float64, error) {
324335

325336
var A float64 // E|X-Y|
326337
if xrowsf > 0 && yrowsf > 0 {
327-
dist, err := getDistance(x, y)
338+
dist, err := calcDistance(x, y)
328339
if err != nil {
329340
return 0, 0, 0, err
330341
}
@@ -335,7 +346,7 @@ func getEnergyStatistics(x, y *mat.Dense) (float64, float64, float64, error) {
335346

336347
var B float64 // E|X-X'|
337348
if xrowsf > 0 {
338-
dist, err := getDistance(x, x)
349+
dist, err := calcDistance(x, x)
339350
if err != nil {
340351
return 0, 0, 0, err
341352
}
@@ -346,7 +357,7 @@ func getEnergyStatistics(x, y *mat.Dense) (float64, float64, float64, error) {
346357

347358
var C float64 // E|Y-Y'|
348359
if yrowsf > 0 {
349-
dist, err := getDistance(y, y)
360+
dist, err := calcDistance(y, y)
350361
if err != nil {
351362
return 0, 0, 0, err
352363
}
@@ -368,7 +379,7 @@ func getEnergyStatistics(x, y *mat.Dense) (float64, float64, float64, error) {
368379

369380
// Given two vectors (expected 1 col),
370381
// this function returns the sum of distances between each pair.
371-
func getDistance(x, y *mat.Dense) (float64, error) {
382+
func calcDistance(x, y *mat.Dense) (float64, error) {
372383
xrows, xcols := x.Dims()
373384
yrows, ycols := y.Dims()
374385

@@ -386,16 +397,16 @@ func getDistance(x, y *mat.Dense) (float64, error) {
386397
return sum, nil
387398
}
388399

389-
// Get Z score for result x, compared to mean u and st dev o.
390-
func getZScore(x, mu, sigma float64) float64 {
400+
// Calculate the Z score for result x, compared to mean mu and st dev sigma.
401+
func calcZScore(x, mu, sigma float64) float64 {
391402
if sigma == 0 {
392403
return math.NaN()
393404
}
394405
return (x - mu) / sigma
395406
}
396407

397-
// Get percentage change for result x compared to mean u.
398-
func getPercentageChange(x, mu float64) float64 {
408+
// Calculate the percentage change for result x compared to mean mu.
409+
func calcPercentChange(x, mu float64) float64 {
399410
if mu == 0 {
400411
return math.NaN()
401412
}

0 commit comments

Comments
 (0)