Skip to content

Commit

Permalink
Fix NPEs in CO2Calculator.getKiloWattsPerCore() (#110024) (#110308)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
rockdaboot and elasticmachine authored Jul 1, 2024
1 parent d37e8c0 commit 2afe7ca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ final class HostMetadata implements ToXContentObject {
final int profilingNumCores; // number of cores on the profiling host machine

HostMetadata(String hostID, InstanceType instanceType, String hostArchitecture, Integer profilingNumCores) {
this.hostID = hostID;
this.instanceType = instanceType;
this.hostArchitecture = hostArchitecture;
this.hostID = hostID != null ? hostID : "";
this.instanceType = instanceType != null ? instanceType : new InstanceType("", "", "");
this.hostArchitecture = hostArchitecture != null ? hostArchitecture : "";
this.profilingNumCores = profilingNumCores != null ? profilingNumCores : DEFAULT_PROFILING_NUM_CORES;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,41 @@ public void testCreateFromRegularSource() {
checkCO2Calculation(co2Calculator.getAnnualCO2Tons(HOST_ID_D, samples), annualCoreHours, 1.7d, 0.000379069d, 2.8d);
}

// Make sure that malformed data doesn't cause the CO2 calculation to fail.
public void testCreateFromMalformedSource() {
// tag::noformat
Map<String, HostMetadata> hostsTable = Map.ofEntries(
Map.entry(HOST_ID_A,
// known datacenter and instance type
new HostMetadata(HOST_ID_A,
new InstanceType(
"aws",
"eu-west-1",
"c5n.xlarge"
),
null,
null
)
),
Map.entry(HOST_ID_B,
new HostMetadata(HOST_ID_B,
null,
null,
null
)
)
);
// end::noformat

double samplingDurationInSeconds = 1_800.0d; // 30 minutes
long samples = 100_000L; // 100k samples
double annualCoreHours = CostCalculator.annualCoreHours(samplingDurationInSeconds, samples, 20.0d);
CO2Calculator co2Calculator = new CO2Calculator(hostsTable, samplingDurationInSeconds, null, null, null, null);

checkCO2Calculation(co2Calculator.getAnnualCO2Tons(HOST_ID_A, samples), annualCoreHours, 1.135d, 0.0002786d, 7.0d);
checkCO2Calculation(co2Calculator.getAnnualCO2Tons(HOST_ID_B, samples), annualCoreHours, 1.7d, 0.000379069d, 7.0d);
}

private void checkCO2Calculation(
double calculatedAnnualCO2Tons,
double annualCoreHours,
Expand Down

0 comments on commit 2afe7ca

Please sign in to comment.