Skip to content

Commit

Permalink
refactor: cleanup operational tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabibeyer committed Jan 15, 2024
1 parent 6cf4920 commit 866b78f
Showing 1 changed file with 144 additions and 134 deletions.
278 changes: 144 additions & 134 deletions pkg/calculator/calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,144 +15,154 @@ type testcase struct {
expRes float64
}

func basicTestcase() *testcase {
return &testcase{
name: "basic",
interval: 30 * time.Second,
calc: &calculate{
cores: 4,
usageCPU: v1.Percentage(25),
minWatts: 1.3423402398570,
maxWatts: 4.00498247528,
chip: 35.23458732,
pue: 1.0123,
gridCO2e: 0.00023,
},
expRes: 0.0005270347987162735,
}
}

func noValues() *testcase {
return &testcase{
name: "no values 30 sec",
interval: 30 * time.Second,
calc: &calculate{
cores: 0,
usageCPU: v1.Percentage(0),
minWatts: 0,
maxWatts: 0,
chip: 0,
pue: 0,
gridCO2e: 0,
},
expRes: 0,
}
}

func fiveMinuteInterval() *testcase {
return &testcase{
name: "5 minutes interval",
interval: 5 * time.Minute,
calc: basicTestcase().calc,
expRes: 0.005270347987162734,
// defaultCalc is contains basic/typical emissions
// data numbers that are used as the default for tests
func defaultCalc() *calculate {
return &calculate{
cores: 4,
usageCPU: v1.Percentage(25),
minWatts: 1.3423402398570,
maxWatts: 4.00498247528,
chip: 35.23458732,
pue: 1.0123,
gridCO2e: 0.00023,
}
}

func oneHourInterval() *testcase {
return &testcase{
name: "5 minutes interval",
interval: 1 * time.Hour,
calc: basicTestcase().calc,
expRes: 0.06324417584595282,
}
}

func singleCore() *testcase {
c := basicTestcase().calc
c.cores = 1
return &testcase{
name: "single core",
interval: 30 * time.Second,
calc: c,
expRes: 0.00013175869967906837,
}
}

func fiftyPercentCPUUtilization() *testcase {
c := basicTestcase().calc
c.usageCPU = v1.Percentage(50)
return &testcase{
name: "50% utilization",
interval: 30 * time.Second,
calc: c,
expRes: 0.0010436517395756915,
}
}

func OneHundredPercentCPUUtilization() *testcase {
c := basicTestcase().calc
c.usageCPU = v1.Percentage(100)
return &testcase{
name: "100% utilization",
interval: 30 * time.Second,
calc: c,
expRes: 0.002076885621294527,
}
}

func highPUE() *testcase {
c := basicTestcase().calc
c.pue = 1.777
return &testcase{
name: "high PUE",
interval: 30 * time.Second,
calc: c,
expRes: 0.0009251613526808435,
}
}

func highGridCO2e() *testcase {
c := basicTestcase().calc
c.gridCO2e = 5.2e-05
return &testcase{
name: "High grid CO2e",
interval: 30 * time.Second,
calc: c,
expRes: 0.00011915569362280964,
}
}

func largeServerLargeWorkload() *testcase {
return &testcase{
name: "large server and large workload",
interval: 30 * time.Second,
calc: &calculate{
cores: 32,
usageCPU: v1.Percentage(90),
minWatts: 3.0369270833333335,
maxWatts: 8.575357663690477,
chip: 129.77777777777777,
pue: 1.1,
gridCO2e: 0.00079,
},
expRes: 0.11621326542003971,
}
}
func TestCalculateEmissions(t *testing.T) {
testcases := []*testcase{
basicTestcase(),
noValues(),
fiveMinuteInterval(),
oneHourInterval(),
singleCore(),
fiftyPercentCPUUtilization(),
OneHundredPercentCPUUtilization(),
highPUE(),
highGridCO2e(),
largeServerLargeWorkload(),
}

for _, test := range testcases {
for _, test := range []*testcase{
func() *testcase {
// Default test case
return &testcase{
name: "basic default numbers",
interval: 30 * time.Second,
calc: defaultCalc(),
expRes: 0.0005270347987162735,
}
}(),
func() *testcase {
// All data set to zero values
return &testcase{
name: "no values 30 sec",
interval: 30 * time.Second,
calc: &calculate{
cores: 0,
usageCPU: v1.Percentage(0),
minWatts: 0,
maxWatts: 0,
chip: 0,
pue: 0,
gridCO2e: 0,
},
expRes: 0,
}
}(),

func() *testcase {
// Calculate the default values over
// a 5 minute interval, instead of
// 30 seconds
return &testcase{
name: "5 minutes interval",
interval: 5 * time.Minute,
calc: defaultCalc(),
expRes: 0.005270347987162734,
}
}(),

func() *testcase {
// Calculate the default values over
// one hour
return &testcase{
name: "1 hour interval",
interval: 1 * time.Hour,
calc: defaultCalc(),
expRes: 0.06324417584595282,
}
}(),

func() *testcase {
c := defaultCalc()
c.cores = 1
// calculate with only a single core
return &testcase{
name: "single core",
interval: 30 * time.Second,
calc: c,
expRes: 0.00013175869967906837,
}
}(),

func() *testcase {
c := defaultCalc()
c.usageCPU = v1.Percentage(50)
// test with vCPU utilization at 50%
return &testcase{
name: "50% utilization",
interval: 30 * time.Second,
calc: c,
expRes: 0.0010436517395756915,
}
}(),

func() *testcase {
c := defaultCalc()
c.usageCPU = v1.Percentage(100)
// test with vCPU utilization at 100%
return &testcase{
name: "100% utilization",
interval: 30 * time.Second,
calc: c,
expRes: 0.002076885621294527,
}
}(),

func() *testcase {
c := defaultCalc()
c.pue = 1.0
// test if PUE is exactly 1
return &testcase{
name: "PUE is exactly 1.0",
interval: 30 * time.Second,
calc: c,
expRes: 0.0005206310369616452,
}
}(),

func() *testcase {
c := defaultCalc()
c.gridCO2e = 402
// test an extremely high grid CO2e
// This value was collected from azures
// Germany West Central region
return &testcase{
name: "High grid CO2e",
interval: 30 * time.Second,
calc: c,
expRes: 921.1651699301823,
}
}(),

func() *testcase {
// create a relatively large server with higher
// than typical min and max watts, 32 cores, and
// a utilization of 90%
return &testcase{
name: "large server and large workload",
interval: 30 * time.Second,
calc: &calculate{
cores: 32,
usageCPU: v1.Percentage(90),
minWatts: 3.0369270833333335,
maxWatts: 8.575357663690477,
chip: 129.77777777777777,
pue: 1.1,
gridCO2e: 0.00079,
},
expRes: 0.11621326542003971,
}
}(),
} {
t.Run(test.name, func(t *testing.T) {
res := test.calc.operationalCPUEmissions(test.interval)
assert.Equalf(t, test.expRes, res, "Result should be: %v, got: %v", test.expRes, res)
Expand Down

0 comments on commit 866b78f

Please sign in to comment.